-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameai.java
More file actions
128 lines (114 loc) · 3.61 KB
/
Copy pathgameai.java
File metadata and controls
128 lines (114 loc) · 3.61 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
import java.util.Scanner;
public class gameai {
public static void drawBoard(int[] matrix) {
System.out.println("Current State of Board:\n");
for (int i = 0; i < 9; i++) {
if (i > 0 && i % 3 == 0) {
System.out.println("\n");
}
if (matrix[i] == 0) {
System.out.print("- ");
} else if (matrix[i] == 1) {
System.out.print("O ");
} else if (matrix[i] == -1) {
System.out.print("X ");
}
}
System.out.println("\n");
}
public static void userTurn(int[] matrix) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Enter X's position from [1...9]: ");
int pos = scanner.nextInt();
if (pos >= 1 && pos <= 9 && matrix[pos - 1] == 0) {
matrix[pos - 1] = -1; // Player's move marked with -1
break;
} else {
System.out.println("Illegal Move! Try again.");
}
}
}
public static int minimax(int[] matrix, int player) {
int winner = analyzeBoard(matrix);
if (winner != 0) {
return winner * player;
}
int pos = -1;
int value = -2;
for (int i = 0; i < 9; i++) {
if (matrix[i] == 0) {
matrix[i] = player;
int score = -minimax(matrix, -player);
if (score > value) {
value = score;
pos = i;
}
matrix[i] = 0;
}
}
if (pos == -1) {
return 0;
}
return value;
}
public static void computerTurn(int[] matrix) {
int pos = -1;
int value = -2;
for (int i = 0; i < 9; i++) {
if (matrix[i] == 0) {
matrix[i] = 1;
int score = -minimax(matrix, -1);
matrix[i] = 0;
if (score > value) {
value = score;
pos = i;
}
}
}
matrix[pos] = 1;
}
public static int analyzeBoard(int[] matrix) {
int[][] cb = {
{ 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 },
{ 0, 3, 6 }, { 1, 4, 7 }, { 2, 5, 8 },
{ 0, 4, 8 }, { 2, 4, 6 }
};
for (int i = 0; i < 8; i++) {
if (matrix[cb[i][0]] != 0 &&
matrix[cb[i][0]] == matrix[cb[i][1]] &&
matrix[cb[i][0]] == matrix[cb[i][2]]) {
return matrix[cb[i][2]];
}
}
return 0;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] matrix = new int[9];
System.out.println("Computer : O Vs. You : X");
System.out.print("Enter to play 1(st) or 2(nd): ");
int player = scanner.nextInt();
for (int i = 0; i < 9; i++) {
if (analyzeBoard(matrix) != 0) {
break;
}
if ((i + player) % 2 == 0) {
computerTurn(matrix);
} else {
drawBoard(matrix);
userTurn(matrix);
}
}
int winner = analyzeBoard(matrix);
drawBoard(matrix);
if (winner == 0) {
System.out.println("Draw!!!");
} else if (winner == -1) {
System.out.println("X Wins!!! You Win!!!");
} else if (winner == 1) {
System.out.println("X Loses!!! O Wins!!!");
}
scanner.close();
}
}