-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameFunction.java
252 lines (225 loc) · 7.94 KB
/
GameFunction.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
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
package TicTacToe;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
public class GameFunction extends JComponent {
public static final int FIELD_EMPTY = 0;
public static final int FIELD_X = 10;
public static final int FIELD_O = 200;
public static int gameMode = 1;
public static int gameOver; // 1 - stop game
public static int[] bestMove = new int[2];
int[][] fields;
public static boolean isXTurn = true; // true - X, false - O
public GameFunction() {
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
fields = new int[3][3]; // game zone
newGame();
}
void newGame() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
fields[i][j] = FIELD_EMPTY;
gameOver = 0;
repaint();
}
}
isXTurn = true;
}
int checkWin() {
int diag = 0;
int diag2 = 0;
for (int i = 0; i < 3; i++) {
diag += fields[i][i];
diag2 += fields[i][2 - i];
}
if (diag == FIELD_O * 3 || diag == FIELD_X * 3) return diag;
if (diag2 == FIELD_O * 3 || diag2 == FIELD_X * 3) return diag2;
int check_i, check_j;
boolean hasEmpty = false;
for (int i = 0; i < 3; i++) {
check_i = 0;
check_j = 0;
for (int j = 0; j < 3; j++) {
if (fields[i][j] == 0) {
hasEmpty = true;
}
check_i += fields[i][j];
check_j += fields[j][i];
}
if (check_i == FIELD_O * 3 || check_i == FIELD_X * 3) {
return check_i;
}
if (check_j == FIELD_O * 3 || check_j == FIELD_X * 3) {
return check_j;
}
}
if (hasEmpty) {
return 0; // game not over
} else {
return -1; // Tie
}
}
public boolean isBoardFull(int[][] board) {
// Ïðîõîäèì ïî âñåì ÿ÷åéêàì äîñêè
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
// Åñëè íàõîäèì ïóñòóþ ÿ÷åéêó, çíà÷èò äîñêà íå çàïîëíåíà
if (board[i][j] == FIELD_EMPTY) {
return false;
}
}
}
// Åñëè ïóñòûõ ÿ÷ååê íå íàøëè, çíà÷èò äîñêà çàïîëíåíà
return true;
}
@Override
protected void processMouseEvent(MouseEvent mouseEvent) {
super.processMouseEvent(mouseEvent);
if (mouseEvent.getButton() == MouseEvent.BUTTON1) {
int x = mouseEvent.getX();
int y = mouseEvent.getY();
// get positions on click
int i1 = (int) ((float) x / getWidth() * 3);
int j1 = (int) ((float) y / getHeight() * 3);
int res = checkWin();
if (res != 0) {
if (res == FIELD_O * 3) {
JOptionPane.showMessageDialog(this, "The zeros have won!", "Victory!", JOptionPane.INFORMATION_MESSAGE);
gameOver = 1;
} else if (res == FIELD_X * 3) {
JOptionPane.showMessageDialog(this, "The crosses have won!", "Victory!", JOptionPane.INFORMATION_MESSAGE);
gameOver = 1;
} else if (isBoardFull(fields)){
JOptionPane.showMessageDialog(this, "draw!", "Draw!", JOptionPane.INFORMATION_MESSAGE);
gameOver = 1;
}
}
if (gameOver != 1) {
if (gameMode == 1) {
if (fields[i1][j1] == FIELD_EMPTY) {
fields[i1][j1] = isXTurn ? FIELD_X : FIELD_O;
isXTurn = !isXTurn;
repaint();
}
} else if (gameMode == 2) {
if (isXTurn) {
if (fields[i1][j1] == FIELD_EMPTY) {
fields[i1][j1] = FIELD_X;
isXTurn = false;
repaint();
}
}
if (!isXTurn) {
makeMove();
fields[bestMove[0]][bestMove[1]] = FIELD_O;
isXTurn = true;
repaint();
}
}
}
}
}
int minimax(int[][] fields, int depth, boolean isMaximizing) {
int result = checkWin();
if (result == FIELD_X * 3) {
return -1; // X wins, return a negative score
} else if (result == FIELD_O * 3) {
return 1; // O wins, return a positive score
} else if (isBoardFull(fields)) {
return 0; // Draw, return 0 score
}
if (isMaximizing) {
int bestScore = Integer.MIN_VALUE;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (fields[i][j] == FIELD_EMPTY) {
fields[i][j] = FIELD_O;
int score = minimax(fields, depth + 1, false);
fields[i][j] = FIELD_EMPTY;
bestScore = Math.max(score, bestScore);
}
}
}
return bestScore;
} else {
int bestScore = Integer.MAX_VALUE;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (fields[i][j] == FIELD_EMPTY) {
fields[i][j] = FIELD_X;
int score = minimax(fields, depth + 1, true);
fields[i][j] = FIELD_EMPTY;
bestScore = Math.min(score, bestScore);
}
}
}
return bestScore;
}
}
void makeMove() {
int bestScore = Integer.MIN_VALUE;
// Find the best move for O using minimax algorithm
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (fields[i][j] == FIELD_EMPTY) {
fields[i][j] = FIELD_O;
int score = minimax(fields, 6, false);
fields[i][j] = FIELD_EMPTY;
if (score > bestScore) {
bestScore = score;
bestMove[0] = i;
bestMove[1] = j;
}
}
}
}
}
void drawGrid(Graphics graphics) {
int w = getWidth();
int h = getHeight();
int dw = w / 3;
int dh = h / 3;
graphics.setColor(Color.BLACK);
for (int i = 1; i < 3; i++) {
graphics.drawLine(0,dh * i, w, dh * i);
graphics.drawLine(dw * i, 0, dw * i, h);
}
}
void drawX(int i, int j, Graphics graphics) {
graphics.setColor(Color.RED);
int dw = getWidth() / 3;
int dh = getWidth() / 3;
int x = i * dw;
int y = j * dh;
graphics.drawLine(x, y, x + dw, y + dh);
graphics.drawLine(x, y + dh,x + dw, y);
}
void drawO(int i, int j, Graphics graphics) {
graphics.setColor(Color.BLUE);
int dw = getWidth() / 3;
int dh = getHeight() / 3;
int x = i * dw;
int y = j * dh;
graphics.drawOval(x + 5 * dw / 100,y,dw * 9 / 10,dh);
}
void drawXO(Graphics graphics) {
if (gameOver != 1) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (fields[i][j] == FIELD_X) {
drawX(i, j, graphics);
} else if (fields[i][j] == FIELD_O) {
drawO(i, j, graphics);
}
}
}
}
}
@Override
protected void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
drawGrid(graphics);
drawXO(graphics);
}
}