-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.java
More file actions
157 lines (147 loc) · 4.68 KB
/
TicTacToe.java
File metadata and controls
157 lines (147 loc) · 4.68 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Pierce Gray
// Computer Science II
// 4/27/2024
// Personal Programming Exercise
// An implementation of the game tic-tac-toe with a computer opponent that uses the minimax algorithm to determine the best move.
public class TicTacToe {
public static final int EMPTY = 0;
public static final int X = 1;
public static final int O = 2;
public static final int UNKNOWN = -1;
public static final int LOSS = 0;
public static final int DRAW = 1;
public static final int WIN = 2;
public static void main(String[] args) {
System.out.println("Welcome to Tic Tac Toe.");
int[] board = new int[9];
printBoard(board);
while (true) {
System.out.print("Enter your move (1 - 9): ");
int move = Integer.parseInt(System.console().readLine());
if (board[move - 1] == EMPTY) {
board[move - 1] = X;
}
else {
System.out.println("Invalid move");
continue;
}
checkEnding(board);
// Computer move
board = move(board);
printBoard(board);
checkEnding(board);
}
}
public static int[] move(int[] board) {
int[] newBoard = board.clone();
int bestMove = -1;
int bestValue = -1;
for (int i = 0; i < 9; i++) {
if (board[i] == EMPTY) {
newBoard[i] = O;
int value = minimax(newBoard, false);
if (value > bestValue) {
bestMove = i;
bestValue = value;
}
newBoard[i] = EMPTY;
}
}
newBoard[bestMove] = O;
return newBoard;
}
public static void checkEnding(int[] board) {
int value = leaf(board);
if (value == WIN) {
System.out.println("\nComputer Wins:");
printBoard(board);
System.exit(0);
}
else if (value == LOSS) {
System.out.println("\nPlayer Wins:");
printBoard(board);
System.exit(0);
}
else if (value == DRAW) {
System.out.println("\nDraw:");
printBoard(board);
System.exit(0);
}
}
public static int minimax(int[] board, boolean isMaximizing) {
// Check for win, loss, or draw
int value = leaf(board);
if (value != UNKNOWN) {
return value;
}
if (isMaximizing) {
int max = -100;
for (int i = 0; i < 9; i++) {
if (board[i] == EMPTY) {
int[] newBoard = board.clone();
newBoard[i] = O;
int result = minimax(newBoard, false);
max = Math.max(max, result);
}
}
return max;
}
else {
int min = 100;
for (int i = 0; i < 9; i++) {
if (board[i] == EMPTY) {
int[] newBoard = board.clone();
newBoard[i] = X;
int result = minimax(newBoard, true);
min = Math.min(min, result);
}
}
return min;
}
}
public static int leaf(int[] board) {
// Check horizontal
for (int i = 0; i < 9; i += 3) {
if (board[i] != EMPTY && board[i] == board[i + 1] && board[i] == board[i + 2]) {
return board[i] == O ? WIN : LOSS;
}
}
// Check vertical
for (int i = 0; i < 3; i++) {
if (board[i] != EMPTY && board[i] == board[i + 3] && board[i] == board[i + 6]) {
return board[i] == O ? WIN : LOSS;
}
}
// Check diagonal
if (board[0] != EMPTY && board[0] == board[4] && board[0] == board[8]) {
return board[0] == O ? WIN : LOSS;
}
if (board[2] != EMPTY && board[2] == board[4] && board[2] == board[6]) {
return board[2] == O ? WIN : LOSS;
}
// Check for draw
for (int i = 0; i < 9; i++) {
if (board[i] == EMPTY) {
return UNKNOWN;
}
}
return DRAW;
}
public static void printBoard(int[] board) {
System.out.println();
for (int i = 0; i < 9; i++) {
if (board[i] == EMPTY) {
System.out.print(" " + (i + 1) + " ");
}
else if (board[i] == X) {
System.out.print(" X ");
}
else {
System.out.print(" O ");
}
if (i % 3 == 2) {
System.out.println("\n");
}
}
}
}