Repository navigation
Expand file tree
/
Copy pathGomokuGameConsole.java
More file actions
204 lines (177 loc) · 6.69 KB
/
Copy pathGomokuGameConsole.java
File metadata and controls
204 lines (177 loc) · 6.69 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
package com.example.javagomokugame;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
/**
* GomokuGameConsole - Console-based interface for Gomoku game.
*/
public class GomokuGameConsole {
private GomokuGame game;
private Scanner scanner;
private Timer timer;
private int[] remainingTime = new int[2];
public GomokuGameConsole() {
game = new GomokuGame();
scanner = new Scanner(System.in);
remainingTime[0] = game.getTimeLimit();
remainingTime[1] = game.getTimeLimit();
}
public void start() {
System.out.println("Gomoku Game Started!");
System.out.println("Board Size: " + game.getBoardSize() + "x" + game.getBoardSize());
System.out.println("Time Limit: " + game.getTimeLimit() + " seconds per move");
System.out.println("Input Format: row column (1-" + game.getBoardSize() + ")");
System.out.println("Enter 0 0 to exit the game");
System.out.println("------------------------");
startTimer();
try {
while (!game.isGameOver()) {
printBoard();
printGameStatus();
System.out.print("\nPlayer " + (game.getCurrentPlayer() == 1 ? "1(Black●)" : "2(White○)") +
"'s Turn (Time Remaining: " +
getRemainingTimeForCurrentPlayer() + "s): ");
// Input validation
if (!scanner.hasNextInt()) {
System.out.println("Invalid input! Please enter numbers only.");
scanner.next(); // Clear invalid input
continue;
}
int x = scanner.nextInt() - 1;
if (!scanner.hasNextInt()) {
System.out.println("Invalid input! Please enter numbers only.");
scanner.next(); // Clear invalid input
continue;
}
int y = scanner.nextInt() - 1;
if (x == -1 && y == -1) {
System.out.println("Game Exited");
break;
}
GomokuGame.MoveResult result = game.move(x, y);
if (result.isSuccess()) {
if (game.isGameOver()) {
stopTimer();
printResult();
} else {
resetTimer();
}
} else {
System.out.println(result.getErrorMessage());
}
}
} finally {
scanner.close();
}
}
private int getRemainingTimeForCurrentPlayer() {
int currentPlayer = game.getCurrentPlayer();
int index = currentPlayer - 1;
if (index < 0 || index >= remainingTime.length) {
return 0;
}
return remainingTime[index];
}
private void startTimer() {
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public synchronized void run() {
if (game.isGameOver()) {
return;
}
int currentPlayer = game.getCurrentPlayer();
int index = currentPlayer - 1;
if (index < 0 || index >= remainingTime.length) {
return;
}
remainingTime[index]--;
if (remainingTime[index] <= 0) {
System.out.println("\nPlayer " + currentPlayer + " Time's Up!");
stopTimer();
int winner = (currentPlayer == 1) ? 2 : 1;
System.out.println("Player " + winner + " Wins!");
// Set game over state instead of System.exit
game.reset(); // Force game end by resetting (or use a flag)
// Actually, we need to mark game as over properly
// Since we can't modify GomokuGame here, we'll just break the loop
System.exit(0);
}
}
}, 1000, 1000);
}
private void resetTimer() {
int currentPlayer = game.getCurrentPlayer();
int index = currentPlayer - 1;
if (index >= 0 && index < remainingTime.length) {
remainingTime[index] = game.getTimeLimit();
}
}
private void stopTimer() {
if (timer != null) {
timer.cancel();
}
}
private void printBoard() {
int boardSize = game.getBoardSize();
int[][] board = game.getBoard();
System.out.print("\n ");
for (int j = 0; j < boardSize; j++) {
System.out.printf("%2d", j + 1);
}
System.out.println();
System.out.print(" ┌");
for (int j = 0; j < boardSize; j++) {
System.out.print("──");
}
System.out.println("┐");
for (int i = 0; i < boardSize; i++) {
System.out.printf("%2d│", i + 1);
for (int j = 0; j < boardSize; j++) {
char symbol = getSymbol(board[i][j]);
System.out.print(" " + symbol);
}
System.out.println(" │");
}
System.out.print(" └");
for (int j = 0; j < boardSize; j++) {
System.out.print("──");
}
System.out.println("┘");
}
private char getSymbol(int player) {
if (player == 1) {
return '●';
} else if (player == 2) {
return '○';
} else {
return '+';
}
}
private void printGameStatus() {
System.out.println("\n--- Game Statistics ---");
System.out.println("Total Moves: " + game.getTotalMoves());
System.out.println("Black Moves: " + game.getPlayerMoves(1) +
" | Longest Streak: " + game.getLongestStreak(1));
System.out.println("White Moves: " + game.getPlayerMoves(2) +
" | Longest Streak: " + game.getLongestStreak(2));
System.out.println("Record: Black " + game.getPlayer1Wins() + " wins | " +
"White " + game.getPlayer2Wins() + " wins | " +
"Draws " + game.getDraws() + " times");
}
private void printResult() {
printBoard();
printGameStatus();
System.out.println("\n--- Game Over ---");
if (game.getWinner() == 0) {
System.out.println("It's a Draw!");
} else {
System.out.println("Player " + game.getWinner() +
(game.getWinner() == 1 ? "(Black●)" : "(White○)") +
" Wins!");
}
}
public static void main(String[] args) {
new GomokuGameConsole().start();
}
}