-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic tac toe ai minimax algo
366 lines (348 loc) · 12.5 KB
/
tic tac toe ai minimax algo
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package tictactoe;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static char turn='X';
public static char player='X';
public static char ai='O';
public static int checkTable(char table[][]) {
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
if(j==0 && i==0) {
if((table[i][j]==table[i+1][j+1]&&table[i][j]==table[i+2][j+2])||(table[i][j]==table[i+1][j]&&table[i][j]==table[i+2][j])) {
if(table[i][j]=='X') {
return 10;
}else if(table[i][j]=='O') {
return -10;
}
}
}
if(j==0 &&i==2) {
if(table[i][j]==table[i-1][j+1]&&table[i][j]==table[i-2][j+2]) {
if(table[i][j]=='X') {
return 10;
}else if(table[i][j]=='O') {
return -10;
}
}
}
if(j==0) {
if(table[i][j]==table[i][j+1]&&table[i][j]==table[i][j+1]&&table[i][j]==table[i][j+2]) {
if(table[i][j]=='X') {
return 10;
}else if(table[i][j]=='O') {
return -10;
}
}
}
if(i==0&&j==1) {
if(table[i][j]==table[i+1][j]&&table[i][j]==table[i+2][j]) {
if(table[i][j]=='X') {
return 10;
}else if(table[i][j]=='O') {
return -10;
}
}
}
if(i==0&&j==2) {
if(table[i][j]==table[i+1][j]&&table[i][j]==table[i+2][j]) {
if(table[i][j]=='X') {
return 10;
}else if(table[i][j]=='O') {
return -10;
}
}
}
}
}
return 0;
}
public static void printTable(char table[][]) {
System.out.println("---------");
for(int i=0;i<3;i++) {
System.out.print("|");
for(int j=0;j<3;j++) {
if(table[i][j]!='_') {
System.out.print(" "+table[i][j]);
}else {
System.out.print(" "+" ");
}
}
System.out.print(" |"+"\n");
}
System.out.println("---------");
if(checkTable(table)==10) {
System.out.println("X wins");
}else if(checkTable(table)==-10) {
System.out.println("O wins");
}else if(checkTable(table)==0&&isOver(table)) {
System.out.println("Draw");
}
}
public static void main(String[] args) throws InterruptedException {
Scanner in=new Scanner(System.in);
char[][] table = new char[3][3];
resetTable(table);
String input ;
String cInput [];
while(true){
System.out.println("Input command: ");
input=in.nextLine();
cInput = input.split(" ");
if(cInput[0].equals("exit"))break;
if(cInput.length!=3||!cInput[0].equals("start")||!(cInput[1].equals("easy")||cInput[1].equals("user")||cInput[1].equals("medium")||cInput[1].equals("hard"))||!(cInput[2].equals("easy")||cInput[2].equals("user")||cInput[2].equals("medium")||cInput[2].equals("hard"))){
System.out.println("Bad parameters!");
continue;
}
printTable(table);
//This is where the game starts and we check wich player it is thats playing.Its the heart of the code.
while(!isOver(table)) {
if(turn=='X'){
if(cInput[1].equals("easy")){
pcMoveEasy(table);
}else if(cInput[1].equals("medium")){
pcMoveMedium(table);
}else if(cInput[1].equals("user")){
playerMove(table);
}else if(cInput[1].equals("hard")){
pcMoveHard(table);
}
if(checkTable(table)==10||checkTable(table)==-10){
printTable(table);
break;
}
printTable(table);
}else{
if(cInput[2].equals("easy")){
pcMoveEasy(table);
}else if(cInput[2].equals("medium")){
pcMoveMedium(table);
}else if(cInput[2].equals("user")){
playerMove(table);
}else if (cInput[2].equals("hard")) {
pcMoveHard(table);
}
if(checkTable(table)==10||checkTable(table)==-10){
printTable(table);
break;
}
printTable(table);
}
}
resetTable(table);
}
}
public static boolean isOver(char table[][]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (table[i][j] == '_') {
return false;
}
}
}
return true;
}
public static void resetTable(char table[][]){
turn='X';
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
table[i][j]='_';
}
}
}
public static void pcMoveEasy(char table[][]) throws InterruptedException {
System.out.println("Making move level \"easy\"");
Random lvlEasy = new Random();
int eCordI,eCordJ;
while(true){
eCordI = lvlEasy.nextInt(3);
eCordJ = lvlEasy.nextInt(3);
if(table[eCordI][eCordJ]!='_'){
continue;
}
table[eCordI][eCordJ]=turn;
Thread.sleep(300);
switchTurn();
break;
}
}
public static int checkTurn(char turn){
if(turn=='X'){
return 10;
}else{
return -10;
}
}
public static boolean pcMoveMedium(char table[][]) throws InterruptedException {
System.out.println("Making move level \"medium\"");
Random lvlEasy = new Random();
char tempTable[][] = table;
int eCordI,eCordJ;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(tempTable[i][j]=='_'){
//Checking win
tempTable[i][j]=turn;
if(checkTable(tempTable)==checkTurn(turn)){
table[i][j]=turn;
switchTurn();
return true;
}
tempTable[i][j]='_';
//Checking win of opponent
switchTurn();
tempTable[i][j]=turn;
if(checkTable(tempTable)==checkTurn(turn)){
switchTurn();
table[i][j]=turn;
switchTurn();
return false;
}
switchTurn();
tempTable[i][j]='_';
}
}
}
while(true){
eCordI= lvlEasy.nextInt(3);
eCordJ= lvlEasy.nextInt(3);
if(table[eCordI][eCordJ]!='_'){
continue;
}
Thread.sleep(300);
table[eCordI][eCordJ]=turn;
switchTurn();
break;
}
return false;
}
static class Move{
int row,col;
}
static Move findBestMove(char board[][])
{
int bestVal = (turn=='X')?-1000:1000;
Move bestMove = new Move();
bestMove.row = -1;
bestMove.col = -1;
// Traverse all cells, evaluate minimax function
// for all empty cells. And return the cell
// with optimal value.
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
// Check if cell is empty
if (board[i][j] == '_')
{
// Make the move
board[i][j] = (turn=='X')?player:ai;
// compute evaluation function for this
// move.
int moveVal = minimax(board, (turn=='X')?false:true);
// Undo the move
board[i][j] = '_';
// If the value of the current move is
// more than the best value, then update
// best/
if ((turn=='X')?moveVal > bestVal:moveVal<bestVal)
{
bestMove.row = i;
bestMove.col = j;
bestVal = moveVal;
}
}
}
}
return bestMove;
}
public static void pcMoveHard(char table[][]) throws InterruptedException {
System.out.println("Making move level \"hard\"");
Move bestMove = findBestMove(table);
table[bestMove.row][bestMove.col] = turn;
Thread.sleep(300);
switchTurn();
}
public static void switchTurn(){
switch(turn) {
case 'X':turn = 'O'; break;
case 'O':turn = 'X'; break;
}
}
public static int score(String res){
if(res.equals("X")){
return 10;
}else if(res.equals("O")){
return -10;
}else{
return 0;
}
}
public static int minimax(char board[][],boolean isMax) {
int result = checkTable(board);
if(result==10||result==-10){
return result;
}
if(isOver(board)){
return 0;
}
if (isMax) {
int bestScore = -9999;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
// Is the spot available?
if (board[i][j] == '_') {
board[i][j] = player;
bestScore = Math.max(minimax(board, !isMax), bestScore);
board[i][j] = '_';
}
}
}
return bestScore;
} else {
int bestScore = 9999;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
// Is the spot available?
if (board[i][j] == '_') {
board[i][j] = ai;
bestScore = Math.min(minimax(board, !isMax), bestScore);
board[i][j] = '_';
}
}
}
return bestScore;
}
}
public static void playerMove(char table[][]){
Scanner in = new Scanner(System.in);
String input;
String cordI,cordJ;
while(true){
try {
System.out.println("Enter the cords: ");
input = in.nextLine();
cordJ=input.split(" ")[0];
cordI=input.split(" ")[1];
if(Character.isLetter(cordI.charAt(0))||Character.isLetter(cordJ.charAt(0))){
System.out.println("You should enter numbers!");
continue;
}
if(Integer.parseInt(cordJ)>3||Integer.parseInt(cordI)>3){
System.out.println("Coordinates should be from 1 to 3!");
continue;
}
if(table[Math.abs(Integer.parseInt(cordI)-3)][Integer.parseInt(cordJ)-1]!='_'){
System.out.println("This cell is occupied! Choose another one!");
}else {
table[Math.abs(Integer.parseInt(cordI)-3)][Integer.parseInt(cordJ)-1]=turn;
switchTurn();
break;
}
}catch(Exception e) {
System.out.println("Wrong input!");
}
//in.nextLine();
}
}
}