-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.java
More file actions
74 lines (63 loc) · 1.96 KB
/
TicTacToe.java
File metadata and controls
74 lines (63 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.util.Random;
/**
* TicTacToe
* UC7 generates random slot values until a valid move is found,
* then places the computer symbol on the board.
*/
public class TicTacToe {
static char[][] board = {
{'-', '-', '-'},
{'-', '-', '-'},
{'-', '-', '-'}
};
static char computerSymbol = 'O';
/**
* Entry point of the program. Triggers the computer move.
*/
public static void main(String[] args) {
computerMove();
}
/**
* Generates random slot values until a valid move is found,
* then places the computer symbol on the board.
*/
static void computerMove() {
Random random = new Random();
int slot, row, col;
// Keep generating a random slot until a valid one is found
do {
slot = random.nextInt(9) + 1; // generates 1 to 9
row = getRowFromSlot(slot);
col = getColFromSlot(slot);
} while (!isValidMove(row, col));
// Place the computer's symbol on the validated cell
board[row][col] = computerSymbol;
System.out.println("Computer chose slot: " + slot);
System.out.println("Placed at -> Row: " + row + ", Col: " + col);
}
/**
* Converts slot number into row index using zero-based indexing.
* Input : Slot number (1-9)
* Output : Row index (0-2)
*/
static int getRowFromSlot(int slot) {
return (slot - 1) / 3;
}
/**
* Converts slot number into column index using modulo operation.
* Input : Slot number (1-9)
* Output : Column index (0-2)
*/
static int getColFromSlot(int slot) {
return (slot - 1) % 3;
}
/**
* Updates the board by placing the given symbol at
* the specified row and column.
* Input : Row, Column, Symbol
* Hint : Assume the move is already validated.
*/
static void placeMove(int row, int col, char symbol) {
board[row][col] = symbol;
}
}