Repository navigation
Expand file tree
/
Copy pathGomokuGame.java
More file actions
301 lines (263 loc) · 9.33 KB
/
Copy pathGomokuGame.java
File metadata and controls
301 lines (263 loc) · 9.33 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
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
package com.example.javagomokugame;
/**
* GomokuGame - Core game logic for the Gomoku (Five in a Row) game.
* Handles game rules, board management, win detection, and timing.
*/
public class GomokuGame {
private int[][] board; // 0: empty, 1: black stone, 2: white stone
private int currentPlayer; // 1: black player, 2: white player
private boolean gameOver; // Game completion flag
private int winner; // 0: draw, 1: black wins, 2: white wins
private int boardSize; // Size of the square board
private int maxMoves; // Maximum possible moves (boardSize * boardSize)
private int totalMoves; // Total moves made in current game
private int player1Moves; // Black player's move count
private int player2Moves; // White player's move count
private int[] longestStreak; // Longest consecutive stones for each player
private int timeLimit; // Time limit per move in seconds
private int[] remainingTime; // Remaining time for each player
// Game statistics across sessions
private int player1Wins = 0;
private int player2Wins = 0;
private int draws = 0;
/**
* Constructor with custom board size and time limit.
* @param boardSize Size of the game board (5-20)
* @param timeLimit Time limit per move in seconds
*/
public GomokuGame(int boardSize, int timeLimit) {
if (boardSize < 5 || boardSize > 20) {
throw new IllegalArgumentException("Board size should be between 5 and 20.");
}
this.boardSize = boardSize;
this.timeLimit = timeLimit;
maxMoves = boardSize * boardSize;
totalMoves = 0;
player1Moves = 0;
player2Moves = 0;
longestStreak = new int[]{0, 0};
remainingTime = new int[]{timeLimit, timeLimit};
board = new int[boardSize][boardSize];
currentPlayer = 1; // Black starts first
gameOver = false;
winner = 0;
}
/**
* Default constructor with standard 20x20 board and 30-second time limit.
*/
public GomokuGame() {
this(20, 30);
}
/**
* Resets the game to initial state.
* Clears board, resets all counters, and resets timers.
*/
public void reset() {
board = new int[boardSize][boardSize];
currentPlayer = 1;
gameOver = false;
winner = 0;
totalMoves = 0;
player1Moves = 0;
player2Moves = 0;
longestStreak = new int[]{0, 0};
remainingTime = new int[]{timeLimit, timeLimit};
}
/**
* Updates the remaining time for current player.
* @param seconds Seconds to subtract from current player's time
* @return true if time still remaining, false if time's up
*/
public synchronized boolean updateTime(int seconds) {
if (gameOver) {
return false;
}
int playerIndex = currentPlayer - 1;
if (playerIndex < 0 || playerIndex >= remainingTime.length) {
return false;
}
remainingTime[playerIndex] -= seconds;
if (remainingTime[playerIndex] <= 0) {
// Time's up - opponent wins
gameOver = true;
winner = 3 - currentPlayer;
if (currentPlayer == 1) {
player2Wins++;
} else {
player1Wins++;
}
return false;
}
return true;
}
/**
* Checks for winning condition from the specified position.
* Also updates the longest streak record for the player.
* @param x Row index of the placed stone
* @param y Column index of the placed stone
* @return true if the move results in a win, false otherwise
*/
public boolean checkWin(int x, int y) {
// Direction vectors: vertical, horizontal, diagonal, anti-diagonal
int[][] directions = {{0, 1}, {1, 0}, {1, 1}, {1, -1}};
int player = board[x][y];
int playerIndex = player - 1;
if (playerIndex < 0 || playerIndex >= longestStreak.length) {
return false;
}
for (int[] dir : directions) {
int count = 1;
int dx = dir[0];
int dy = dir[1];
// Check both directions
for (int step = -1; step <= 1; step += 2) {
for (int i = 1; i < 5; i++) {
int newX = x + i * dx * step;
int newY = y + i * dy * step;
if (!isValidPosition(newX, newY) || board[newX][newY] != player) {
break;
}
count++;
}
}
// Update longest streak record
if (count > longestStreak[playerIndex]) {
longestStreak[playerIndex] = count;
}
if (count >= 5) {
gameOver = true;
winner = player;
if (player == 1) {
player1Wins++;
} else {
player2Wins++;
}
return true;
}
}
return false;
}
/**
* Places a stone at the specified position.
* Validates move, updates board, checks for win or draw.
* @param x Row index
* @param y Column index
* @return MoveResult containing success status and error message if any
*/
public synchronized MoveResult move(int x, int y) {
if (gameOver) {
return new MoveResult(false, "Game is already over.");
}
if (!isValidPosition(x, y)) {
return new MoveResult(false, "Position is out of bounds.");
}
if (board[x][y] != 0) {
return new MoveResult(false, "This position already has a stone. Please choose an empty intersection.");
}
board[x][y] = currentPlayer;
totalMoves++;
if (currentPlayer == 1) {
player1Moves++;
} else {
player2Moves++;
}
if (totalMoves == maxMoves) {
gameOver = true;
winner = 0; // Draw
draws++;
return new MoveResult(true, null);
}
if (checkWin(x, y)) {
return new MoveResult(true, null);
}
// Switch player and reset opponent's time
currentPlayer = (currentPlayer == 1) ? 2 : 1;
int otherPlayerIndex = currentPlayer - 1;
remainingTime[otherPlayerIndex] = timeLimit;
return new MoveResult(true, null);
}
/**
* Inner class to represent the result of a move attempt.
*/
public static class MoveResult {
private final boolean success;
private final String errorMessage;
public MoveResult(boolean success, String errorMessage) {
this.success = success;
this.errorMessage = errorMessage;
}
public boolean isSuccess() {
return success;
}
public String getErrorMessage() {
return errorMessage;
}
}
/**
* Gets the number of moves made by a specific player.
* @param player Player number (1 for black, 2 for white)
* @return Number of moves made by the player
*/
public int getPlayerMoves(int player) {
return player == 1 ? player1Moves : player2Moves;
}
/**
* Gets the longest consecutive stones achieved by a player.
* @param player Player number (1 for black, 2 for white)
* @return Longest consecutive stones achieved by the player
*/
public int getLongestStreak(int player) {
int index = player - 1;
if (index < 0 || index >= longestStreak.length) {
return 0;
}
return longestStreak[index];
}
/**
* Gets the remaining time for a player.
* @param player Player number (1 for black, 2 for white)
* @return Remaining time in seconds for the player
*/
public int getRemainingTime(int player) {
int index = player - 1;
if (index < 0 || index >= remainingTime.length) {
return 0;
}
return remainingTime[index];
}
/**
* Gets the time limit per move.
* @return Time limit per move in seconds
*/
public int getTimeLimit() {
return timeLimit;
}
public int getPlayer1Wins() { return player1Wins; }
public int getPlayer2Wins() { return player2Wins; }
public int getDraws() { return draws; }
/**
* Checks if a position is within board boundaries.
* @param x Row index
* @param y Column index
* @return true if the position is valid, false otherwise
*/
public boolean isValidPosition(int x, int y) {
return x >= 0 && x < boardSize && y >= 0 && y < boardSize;
}
public boolean isGameOver() { return gameOver; }
public int getWinner() { return winner; }
/**
* Gets a copy of the game board to prevent external modification.
* @return Deep copy of the board array
*/
public int[][] getBoard() {
int[][] boardCopy = new int[boardSize][boardSize];
for (int i = 0; i < boardSize; i++) {
System.arraycopy(board[i], 0, boardCopy[i], 0, boardSize);
}
return boardCopy;
}
public int getCurrentPlayer() { return currentPlayer; }
public int getBoardSize() { return boardSize; }
public int getTotalMoves() { return totalMoves; }
}