-
Notifications
You must be signed in to change notification settings - Fork 1
/
Gomoku.java
302 lines (243 loc) · 8.31 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
/*
* Gomoku.java
*
* Name: Javier Chavez
* Email: [email protected]
* Sept. 4 2013
* CS 251 Lab 2
*
*/
import java.util.Random;
import cs251.lab2.GomokuGUI;
import cs251.lab2.GomokuModel;
//TODO allow for Draw game
public class Gomoku implements GomokuModel{
private StringBuilder sb;
private Square [][] moves;
private Player winner = null;
private Player[] players;
private Player current;
private int playerNum;
public Gomoku() {
//create new array with empty values
this("computer");
}
public Gomoku(String s) {
//create new array with empty values
if (s.contains("computer")) {
players = new Player[]{
new Player(Square.CROSS, true),
new Player(Square.RING, false) };
System.out.print("com");
} else if (s.contains("person")) {
players = new Player[]{
new Player(Square.CROSS, false),
new Player(Square.RING, false) };
}
initMovesArray();
playerNum = new Random().nextInt(19283)%2==0?1:0;
System.out.print(playerNum);
current = players[playerNum];
if (current.isComputer()) {
compterTurn();
altPlayer();
}
}
/**
* main entry point of the game
* javac -cp "lab2.jar:." Gomoku.java
* @param args can be computer, x or just x
*/
public static void main(String[] args) {
//getting out of main to call non static methods
if (args.length == 0) {
// System.out.print(args.length);
new GomokuGUI ( new Gomoku() );
} else {
new GomokuGUI ( new Gomoku(args[0]) );
}
}
@Override
public Outcome doClick(int row, int col) {
//System.out.print(current.getSymbol());
// Make sure the square is not filled
if (moves[row][col] == Square.EMPTY) {
moves[row][col] = current.getSymbol();
// System.out.println(row+ ", " + col);
createBoardString();
}else{
// not recreating the string only logging it
System.out.println("filled");
}
// Check for win
if (isWin()) {
// currentPlayer.toChar();
winner = current;
return winner.getOutcome();
} else{
altPlayer();
}
if (current.isComputer()) {
compterTurn();
if (isWin()) {
winner = current;
return winner.getOutcome();
}else{
//alternate players
altPlayer();
}
}
// return the outcome of the game
return GomokuModel.Outcome.GAME_NOT_OVER;
}
// TODO add init fn
@Override
public void newGame() {
// in order to keep everything cleaner Create a Player player1 player2
if (winner != null){
current = winner;
// Create a new empty array
initMovesArray();
// create board string
createBoardString();
if (current.isComputer()) {
compterTurn();
altPlayer();
}
} else{
// Create a new empty array
initMovesArray();
// create board string
createBoardString();
}
}
/**
* returns a string that represents the board
*/
@Override
public String boardString() {
return sb.toString();
}
/* Visually inspect how to check for a win
* TABLE 1
* [x,y] [x,y] [x,y]
* 1) rightward [3,3], [4,3], [5,3]
* 2) downward [3,3], [3,4], [3,5]
* 3) right-diagonal [3,3], [4,4], [5,5]
* 4) left-diagonal [3,3], [2,4], [1,5]
*/
private boolean rDiagonal(int r, int c){
Square _sq = moves[r][c];
int w = 0;
//increment in both col and row see TABLE 1
for (int x = c, y = r; x < GomokuModel.NUM_HSQUARES && y < GomokuModel.NUM_VSQUARES; x++,y++) {
if (_sq.toChar() == moves[y][x].toChar()) {
w++;
} else {
break;
}
}
return (w >= GomokuModel.SQUARES_IN_LINE_FOR_WIN);
}
private boolean lDiagonal(int r, int c){
Square _sq = moves[r][c];
int w = 0;
// there is no need to check out of the board or wrap
if(r == 0 || c == 0) { return false; }
// System.out.println(r + ' ' + c);
//check diag.
for (int x = c, y = r; x < GomokuModel.NUM_HSQUARES && y < GomokuModel.NUM_VSQUARES; x--,y++) {
if (_sq.toChar() == moves[y][x].toChar()) {
w++;
} else {
break;
}
}
return (w >= GomokuModel.SQUARES_IN_LINE_FOR_WIN);
}
private boolean rightward(int r, int c){
Square _sq = moves[r][c];
int w = 0;
//we only want to increment the column and search from that col. onwards
for (int i = c; i < GomokuModel.NUM_HSQUARES; i++) {
if (_sq.toChar() == moves[r][i].toChar()) {
w++;
} else {
break;
}
}
return (w >= GomokuModel.SQUARES_IN_LINE_FOR_WIN);
}
private boolean downward(int r, int c){
Square _sq = moves[r][c];
int w = 0;
for (int i = r; i < GomokuModel.NUM_VSQUARES; i++) {
if (_sq.toChar() == moves[i][c].toChar()) {
w++;
} else {
break;
}
}
return (w >= GomokuModel.SQUARES_IN_LINE_FOR_WIN);
}
/**
* this checks for a win and returns if a win was detected.
* works by scanning the board top down, when a non-empty square found it looks in all directions.
* @return true if win detected
*
*/
private boolean isWin(){
for (int i = 0; i < GomokuModel.NUM_VSQUARES; i++) {
for (int j = 0; j < GomokuModel.NUM_HSQUARES; j++) {
if (moves[i][j].toChar() != Square.EMPTY.toChar()) {
if (downward(i, j) || rightward(i, j) || lDiagonal(i, j) || rDiagonal(i, j)) {
System.out.println("win");
return true;
}
}
}
}
return false;
}
/**
* This initializes the board string based on the
* <code>GomokuModel.NUM_VSQUARES</code> and <code>GomokuModel.NUM_HSQUARES</code>.
* Rather than a straight concat this uses the a StringBuilder.
*/
private void createBoardString(){
StringBuilder _sb = new StringBuilder();
for (int i = 0; i < GomokuModel.NUM_VSQUARES; i++) {
for (int j = 0; j < GomokuModel.NUM_HSQUARES; j++) {
// getting the char value of square, then append using string builder.
_sb.append(moves[i][j].toChar());
}
}
sb = _sb;
}
/**
* This initializes a 2d array with <code>Square.EMPTY</code>.
*/
private void initMovesArray() {
// Creating new array and filling it with empty values
moves = new Square[GomokuModel.NUM_VSQUARES][GomokuModel.NUM_HSQUARES];
for (int i = 0; i < GomokuModel.NUM_VSQUARES; i++) {
for (int j = 0; j < GomokuModel.NUM_HSQUARES; j++) {
moves[i][j] = Square.EMPTY;
}
}
}
private void altPlayer(){
//alternate players
if (playerNum == 0) {
current = players[++playerNum];
} else{
current = players[--playerNum];
}
}
private void compterTurn(){
int [] mv = new ComputerAI(moves).getMove(); // calling ai, setting the board, & getting a move
moves[mv[0]][mv[1]] = current.getSymbol(); // add to board
// System.out.print(mv[0]+ " " + mv[1]);
createBoardString(); //update the board
}
}