-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToeWithAI
196 lines (165 loc) · 6.08 KB
/
TicTacToeWithAI
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import java.util.Scanner;
public class TicTacToeWithAI {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to Tic Tac Toe!");
char[][] board = initializeBoard();
printBoard(board);
char humanPlayer = 'X';
char aiPlayer = 'O';
while (true) {
// Human player's move
getHumanMove(board, humanPlayer);
printBoard(board);
// Check if human player wins
if (isWinner(board, humanPlayer)) {
System.out.println("Congratulations! You win!");
break;
}
// Check if it's a draw
if (isBoardFull(board)) {
System.out.println("It's a draw!");
break;
}
// AI player's move
System.out.println("AI's turn:");
makeAIMove(board, aiPlayer);
printBoard(board);
// Check if AI player wins
if (isWinner(board, aiPlayer)) {
System.out.println("AI wins! Better luck next time.");
break;
}
// Check if it's a draw
if (isBoardFull(board)) {
System.out.println("It's a draw!");
break;
}
}
scanner.close();
}
private static char[][] initializeBoard() {
char[][] board = {
{' ', ' ', ' '},
{' ', ' ', ' '},
{' ', ' ', ' '}
};
return board;
}
private static void printBoard(char[][] board) {
System.out.println("Current Tic Tac Toe Board:");
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
System.out.print("| " + board[i][j] + " ");
}
System.out.println("|");
}
for (int i = 0; i < board[0].length * 4 + 1; i++) {
System.out.print("-");
}
System.out.println();
}
private static void getHumanMove(char[][] board, char player) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Enter row (1-3) and column (1-3) for your move: ");
int row = scanner.nextInt() - 1;
int col = scanner.nextInt() - 1;
if (isValidMove(board, row, col)) {
board[row][col] = player;
break;
} else {
System.out.println("Invalid move. Try again.");
}
}
}
private static void makeAIMove(char[][] board, char player) {
int[] bestMove = minimax(board, player);
board[bestMove[0]][bestMove[1]] = player;
}
private static int[] minimax(char[][] board, char player) {
int[] bestMove = new int[]{-1, -1};
int bestScore = (player == 'O') ? Integer.MIN_VALUE : Integer.MAX_VALUE;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == ' ') {
board[i][j] = player;
int score = minimaxHelper(board, 0, false);
board[i][j] = ' ';
if ((player == 'O' && score > bestScore) || (player == 'X' && score < bestScore)) {
bestScore = score;
bestMove[0] = i;
bestMove[1] = j;
}
}
}
}
return bestMove;
}
private static int minimaxHelper(char[][] board, int depth, boolean isMaximizing) {
char humanPlayer = 'X';
char aiPlayer = 'O';
if (isWinner(board, humanPlayer)) {
return -1;
} else if (isWinner(board, aiPlayer)) {
return 1;
} else if (isBoardFull(board)) {
return 0;
}
if (isMaximizing) {
int bestScore = Integer.MIN_VALUE;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == ' ') {
board[i][j] = aiPlayer;
int score = minimaxHelper(board, depth + 1, false);
board[i][j] = ' ';
bestScore = Math.max(score, bestScore);
}
}
}
return bestScore;
} else {
int bestScore = Integer.MAX_VALUE;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == ' ') {
board[i][j] = humanPlayer;
int score = minimaxHelper(board, depth + 1, true);
board[i][j] = ' ';
bestScore = Math.min(score, bestScore);
}
}
}
return bestScore;
}
}
private static boolean isValidMove(char[][] board, int row, int col) {
return row >= 0 && row < board.length && col >= 0 && col < board[0].length && board[row][col] == ' ';
}
private static boolean isBoardFull(char[][] board) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == ' ') {
return false;
}
}
}
return true;
}
private static boolean isWinner(char[][] board, char player) {
// Check rows and columns
for (int i = 0; i < board.length; i++) {
if ((board[i][0] == player && board[i][1] == player && board[i][2] == player) ||
(board[0][i] == player && board[1][i] == player && board[2][i] == player)) {
return true;
}
}
// Check diagonals
if ((board[0][0] == player && board[1][1] == player && board[2][2] == player) ||
(board[0][2] == player && board[1][1] == player && board[2][0] == player)) {
return true;
}
return false;
}
}