-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoMoku.java
302 lines (235 loc) · 8.59 KB
/
GoMoku.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
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
package com.example.gomoku;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class GoMoku extends Application {
GoMokuBoard board;
private static Button newGameButton;
private static Button resignButton;
private static Label message;
public void start(Stage stage) {
message = new Label("Click board to start.");
message.setStyle("-fx-font-size: 11pt; -fx-text-fill: lightgray");
newGameButton = new Button("New Game");
newGameButton.setOnAction(e -> board.doNewGame());
resignButton = new Button("Resign");
resignButton.setOnAction(e -> board.doResign());
board = new GoMokuBoard();
board.doNewGame();
board.setOnMouseClicked(e -> board.mousePressed(e));
board.relocate(30,30);
newGameButton.relocate(370,120);
resignButton.relocate(370,200);
message.relocate(30,380);
resignButton.setManaged(false);
resignButton.resize(100,30);
newGameButton.setManaged(false);
newGameButton.resize(100,30);
Pane root = new Pane();
root.setPrefWidth(500);
root.setPrefHeight(450);
//Add the child nodes to the Pane and set up the rest of the GUI
root.getChildren().addAll(board, newGameButton, resignButton, message);
root.setStyle("-fx-background-color: darkgreen; -fx-border-color: darkred; -fx-border-width: 3px");
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setResizable(false);
stage.setTitle("Go Moku");
stage.show();
}
//-------------------------------------Nested Classes--------------------------------------------------
public static class GoMokuData {
static final int WHITE = 1, BLACK = 2;
private final int[][] board;
private int winRow, winCol, winRow2, winCol2;
public static int arrayCount; //board[r][c] is the contents of row r, column c.
GoMokuData() {
board = new int[13][13]; //create an array to store five values of winData.
}
int pieceAt(int row, int col) {
return board[row][col];
}
void setData(int row, int col, int color) {
board[row][col] = color;
}
public int getStartRow() {
return winRow;
}
public int getStartCol() {
return winCol;
}
public int getEndRow() {
return winRow2;
}
public int getEndCol() {
return winCol2;
}
private int cP(int currentPlayer, int row, int col, int dirX, int dirY) {
int ct = 1;
int r, c;
r = row + dirX;
c = col + dirY;
while(r >= 0 && r < 13 && c >= 0 && c < 13 && board[r][c] == currentPlayer) {
ct++;
r += dirX;
c += dirY;
}
winRow = r - dirX;
winCol = c - dirY;
r = row - dirX;
c = col - dirY;
while(r >= 0 && r < 13 && c >= 0 && c < 13 && board[r][c] == currentPlayer) {
ct++;
r -= dirX;
c -= dirY;
}
winRow2 = r + dirX;
winCol2 = c + dirY;
return ct;
}
public boolean checkGame(int cp, int r, int c) {
if(cP(cp,r,c,0,1) >= 5)
return true;
else if(cP(cp,r,c,1,0) >=5)
return true;
else if(cP(cp,r,c,1,1) >= 5)
return true;
else if(cP(cp,r,c,-1,1) >= 5)
return true;
return false;
}
public boolean checkStatus(int currentPlayer, int row, int col) {
boolean value = checkGame(currentPlayer,row, col);
for (int[] ints : board) {
for (int anInt : ints) {
if (anInt != 0)
arrayCount++;
}
}
return arrayCount == (13 * 13) || value;
}
}
public static class GoMokuBoard extends Canvas {
GoMokuData board;
static boolean gameInProgress, newGame;
public int currentPlayer;
int selectedRow, selectedCol;
static final int EMPTY = 0, WHITE = 1, BLACK = 2;
boolean value;
GoMokuBoard() {
super(312,312);
}
public void doNewGame() {
board = new GoMokuData();
newGame = true;
currentPlayer = WHITE;
gameInProgress = true;
drawBoard();
message.setText("White plays first.");
}
public void doResign() {
gameInProgress = false;
gameStatus(0);
}
public void drawBoard() {
GraphicsContext g = getGraphicsContext2D();
g.setFont(Font.font(18));
g.setStroke(Color.BLACK);
g.setLineWidth(2);
value = board.checkStatus(currentPlayer,selectedRow,selectedCol);
//Draw the board..
for(int row = 0; row < 13; row++) {
for(int col = 0; col < 13; col++) {
g.setFill(Color.SANDYBROWN);
g.fillRect(row*24, col*24, 24, 24);
g.strokeRect(row*24, col*24, 24, 24);
if(board.pieceAt(row,col) == GoMokuData.WHITE) {
g.setFill(Color.WHITE);
g.strokeOval(row*24+3,col*24+3,18,18);
g.fillOval(row*24+3,col*24+3,18,18);
}
else if(board.pieceAt(row,col) == GoMokuData.BLACK) {
g.setFill(Color.BLACK);
g.strokeOval(row*24+3,col*24+3,18,18);
g.fillOval(row*24+3,col*24+3,18,18);
}
}
}
if(value) {
gameInProgress = false;
if(GoMokuData.arrayCount == (13 * 13))
currentPlayer = 0;
else {
g.setLineWidth(1);
g.setStroke(Color.GREEN);
g.strokeLine(board.getStartRow()*24+12,board.getStartCol()*24+12, board.getEndRow()*24+12, board.getEndCol()*24+12);
}
}
else {
if(currentPlayer == WHITE) {
if(newGame) {
currentPlayer = WHITE;
newGame = false;
}
else {
currentPlayer = BLACK;
}
}
else
currentPlayer = WHITE;
}
gameStatus(currentPlayer);
}
public void mousePressed(MouseEvent evt) {
if(!gameInProgress)
return;
double x = evt.getX();
double y = evt.getY();
int xx = (int)((x - 2) / 24);
int yy = (int)((y - 2) / 24);
selectedRow = Math.abs(xx);
selectedCol = Math.abs(yy);
if(board.pieceAt(selectedRow,selectedCol) != EMPTY)
return;
board.setData(selectedRow,selectedCol,currentPlayer);
drawBoard();
}
public void gameStatus(int currentPlayer) {
if(!gameInProgress) {
if(currentPlayer == EMPTY) {
message.setText("Start New Game.");
}
else if(currentPlayer == WHITE) {
newGameButton.setDisable(false);
message.setText("Game Ended. WHITE Wins.");
}
else {
message.setText("Game ended. BLACK Wins");
}
newGameButton.setDisable(false);
resignButton.setDisable(true);
}
else {
if(currentPlayer == WHITE) {
message.setText("White's turn.");
}
else if(currentPlayer == BLACK) {
message.setText("Black's turn.");
}
resignButton.setDisable(false);
newGameButton.setDisable(true);
}
}
}
public static void main(String[] args) {
launch();
}
}