-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAIPlayer.java
194 lines (168 loc) · 6.62 KB
/
AIPlayer.java
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
import java.util.Scanner;
public class AIPlayer {
private static final int COMPUTER = 1;
private static final int Player = 2;
private static final int SIDE = 3; // Length of the board
private static final char AI = 'O';
private static final char P1 = 'X';
private static void showBoard(char[][] board) {
System.out.printf("\t\t\t %c | %c | %c \n", board[0][0], board[0][1], board[0][2]);
System.out.println("\t\t\t_____________");
System.out.printf("\t\t\t %c | %c | %c \n", board[1][0], board[1][1], board[1][2]);
System.out.println("\t\t\t_____________");
System.out.printf("\t\t\t %c | %c | %c \n\n", board[2][0], board[2][1], board[2][2]);
}
private static void showInstructions() {
System.out.println("\t\t\t 1 | 2 | 3 ");
System.out.println("\t\t\t____________\n");
System.out.println("\t\t\t 4 | 5 | 6 ");
System.out.println("\t\t\t____________\n");
System.out.println("\t\t\t 7 | 8 | 9 \n");
}
private static void initialise(char[][] board) {
for (int i = 0; i < SIDE; i++) {
for (int j = 0; j < SIDE; j++) {
board[i][j] = ' ';
}
}
}
private static void declareWinner(int whoseTurn) {
if (whoseTurn == COMPUTER)
System.out.println("COMPUTER has won");
else
System.out.println("Player has won");
}
private static boolean rowCrossed(char[][] board) {
for (int i = 0; i < SIDE; i++) {
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
return true;
}
return false;
}
private static boolean columnCrossed(char[][] board) {
for (int i = 0; i < SIDE; i++) {
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
return true;
}
return false;
}
private static boolean diagonalCrossed(char[][] board) {
return (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') ||
(board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ');
}
private static boolean gameOver(char[][] board) {
return rowCrossed(board) || columnCrossed(board) || diagonalCrossed(board);
}
private static int minimax(char[][] board, int depth, boolean isAI) {
int score = 0;
int bestScore;
if (gameOver(board)) {
return isAI ? -1 : 1;
} else {
if (depth < 9) {
if (isAI) {
bestScore = -999;
for (int i = 0; i < SIDE; i++) {
for (int j = 0; j < SIDE; j++) {
if (board[i][j] == ' ') {
board[i][j] = AI;
score = minimax(board, depth + 1, false);
board[i][j] = ' ';
if (score > bestScore) {
bestScore = score;
}
}
}
}
return bestScore;
} else {
bestScore = 999;
for (int i = 0; i < SIDE; i++) {
for (int j = 0; j < SIDE; j++) {
if (board[i][j] == ' ') {
board[i][j] = P1;
score = minimax(board, depth + 1, true);
board[i][j] = ' ';
if (score < bestScore) {
bestScore = score;
}
}
}
}
return bestScore;
}
} else {
return 0;
}
}
}
private static int bestMove(char[][] board, int moveIndex) {
int x = -1, y = -1;
int score;
int bestScore = -999;
for (int i = 0; i < SIDE; i++) {
for (int j = 0; j < SIDE; j++) {
if (board[i][j] == ' ') {
board[i][j] = AI;
score = minimax(board, moveIndex + 1, false);
board[i][j] = ' ';
if (score > bestScore) {
bestScore = score;
x = i;
y = j;
}
}
}
}
return x * 3 + y;
}
private static void playTicTacToe() {
char[][] board = new char[SIDE][SIDE];
int moveIndex = 0, x, y;
initialise(board);
showInstructions();
while (!gameOver(board) && moveIndex != SIDE * SIDE) {
int n;
if (moveIndex % 2 == 0) {
System.out.print("You can insert in the following positions : ");
for (int i = 0; i < SIDE; i++)
for (int j = 0; j < SIDE; j++)
if (board[i][j] == ' ')
System.out.print((i * 3 + j) + 1 + " ");
System.out.print("\nEnter the position = ");
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
n--;
x = n / SIDE;
y = n % SIDE;
if (board[x][y] == ' ' && n < 9 && n >= 0) {
board[x][y] = P1;
System.out.printf("Player has put a %c in cell %d\n\n", P1, n + 1);
showBoard(board);
moveIndex++;
} else if (board[x][y] != ' ' && n < 9 && n >= 0) {
System.out.println("\nPosition is occupied, select any one place from the available places\n\n");
} else if (n < 0 || n > 8) {
System.out.println("Invalid position\n");
}
} else {
n = bestMove(board, moveIndex);
x = n / SIDE;
y = n % SIDE;
board[x][y] = AI;
System.out.printf("COMPUTER has put a %c in cell %d\n\n", AI, n + 1);
showBoard(board);
moveIndex++;
}
}
if (!gameOver(board) && moveIndex == SIDE * SIDE)
System.out.println("It's a draw\n");
else {
declareWinner(moveIndex % 2 == 0 ? Player : COMPUTER);
}
}
public static void main(String[] args) {
System.out.println("\t\t\t Tic-Tac-Toe");
playTicTacToe();
}
}