-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpgrade.java
More file actions
244 lines (221 loc) · 8.6 KB
/
Copy pathUpgrade.java
File metadata and controls
244 lines (221 loc) · 8.6 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
/**
* Kylie Gun
* 400524717
* McMaster University
* December 4, 2024
* 2OP3 - Object Oriented Programming Fall 2024
*/
import A6_P1.TicTacToe.Board;
import A6_P1.TicTacToe.Player;
import A6_P1.TicTacToe.HumanPlayer;
import A6_P1.TicTacToe.ComputerPlayer;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* Upgrade class extends the functionality of TicTacToe to include a dynamic grid size (N x N)
* and a customizable winning condition (M symbols in a row).
*/
public class Upgrade {
private final Board board; // The game board
private final Player player1; // First player
private final Player player2; // Second player
private final int N; // Size of the grid
private final int M; // Number of consecutive symbols required to win
/**
* Constructor for the Upgrade class.
*
* @param player1 First player
* @param player2 Second player
* @param N Size of the grid (NxN)
* @param M Winning condition (M consecutive symbols)
*/
public Upgrade(Player player1, Player player2, int N, int M) {
this.N = N;
this.M = M;
this.player1 = player1;
this.player2 = player2;
// Initialize the game board with a dynamic size
this.board = new Board();
char[][] dynamicGrid = new char[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
dynamicGrid[i][j] = ' '; // Set each cell to empty
}
}
board.setGrid(dynamicGrid); // Update the board with the new grid
}
/**
* Checks if the given symbol has achieved a winning condition.
*
* @param symbol The symbol to check for victory
* @return True if the symbol satisfies the winning condition, false otherwise
*/
private boolean checkWinner(char symbol) {
char[][] grid = board.getGrid();
// Check rows for M consecutive symbols
for (int i = 0; i < N; i++) {
for (int j = 0; j <= N - M; j++) {
boolean win = true;
for (int k = 0; k < M; k++) {
if (grid[i][j + k] != symbol) {
win = false;
break;
}
}
if (win) return true;
}
}
// Check columns for M consecutive symbols
for (int i = 0; i <= N - M; i++) {
for (int j = 0; j < N; j++) {
boolean win = true;
for (int k = 0; k < M; k++) {
if (grid[i + k][j] != symbol) {
win = false;
break;
}
}
if (win) return true;
}
}
// Check diagonals (top-left to bottom-right)
for (int i = 0; i <= N - M; i++) {
for (int j = 0; j <= N - M; j++) {
boolean win = true;
for (int k = 0; k < M; k++) {
if (grid[i + k][j + k] != symbol) {
win = false;
break;
}
}
if (win) return true;
}
}
// Check diagonals (top-right to bottom-left)
for (int i = 0; i <= N - M; i++) {
for (int j = M - 1; j < N; j++) {
boolean win = true;
for (int k = 0; k < M; k++) {
if (grid[i + k][j - k] != symbol) {
win = false;
break;
}
}
if (win) return true;
}
}
// No win condition met
return false;
}
/**
* Starts the game loop, managing turns and checking for game-end conditions.
*/
public void start() {
Scanner scanner = new Scanner(System.in);
Player currentPlayer = player1; // Start with the first player
boolean gameEnded = false; // Track whether the game is over
// Game loop
while (!gameEnded) {
board.display(); // Show the current state of the board
System.out.println("Player " + currentPlayer.getSymbol() + "'s turn.");
currentPlayer.getMove(board); // Get the player's move
// Check if the current player has won
if (checkWinner(currentPlayer.getSymbol())) {
board.display();
System.out.println("Player " + currentPlayer.getSymbol() + " wins!");
System.out.print("Do you want to play again? (yes/no): ");
String playAgain = scanner.next();
if (playAgain.equalsIgnoreCase("yes")) {
board.reset(); // Reset the board for a new game
currentPlayer = player1; // Restart with player 1
continue;
} else {
System.out.println("Thanks for playing!");
gameEnded = true;
}
}
// Check if the board is full (resulting in a draw)
else if (board.isFull()) {
board.display();
System.out.println("It's a draw!");
System.out.print("Do you want to play again? (yes/no): ");
String playAgain = scanner.next();
if (playAgain.equalsIgnoreCase("yes")) {
board.reset(); // Reset the board for a new game
currentPlayer = player1; // Restart with player 1
continue;
} else {
System.out.println("Thanks for playing!");
gameEnded = true;
}
} else {
// Switch to the next player
currentPlayer = (currentPlayer == player1) ? player2 : player1;
}
}
}
//Main method to initialize and start the game.
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Welcome message
System.out.println("Welcome to the upgraded Tic-Tac-Toe!");
// Get grid size (N)
int N;
while (true) {
try {
System.out.print("Enter the size of the grid (3 to 20): ");
N = scanner.nextInt();
if (N >= 3 && N <= 20) {
break;
} else {
System.out.println("Invalid grid size. Please choose a size between 3 and 20.");
}
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a number between 3 and 20.");
scanner.next(); // Clear the invalid input
}
}
// Winning condition (M consecutive symbols, default to N)
int M = N;
System.out.println("The winning condition set to " + M + " consecutive symbols.");
// Choose game mode
int choice = -1;
while (true) {
try {
System.out.println("Choose your game mode:");
System.out.println("1. Human vs. Human");
System.out.println("2. Human vs. Computer");
System.out.println("3. Computer vs. Computer");
choice = scanner.nextInt();
if (choice >= 1 && choice <= 3) {
break;
} else {
System.out.println("Invalid option. Please choose a number between 1 and 3.");
}
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a number between 1 and 3.");
scanner.next(); // Clear the invalid input
}
}
Player player1, player2;
// Initialize players based on user choice
switch (choice) {
case 1 -> {
player1 = new HumanPlayer('X');
player2 = new HumanPlayer('O');
}
case 2 -> {
player1 = new HumanPlayer('X');
player2 = new ComputerPlayer('O');
}
case 3 -> {
player1 = new ComputerPlayer('X');
player2 = new ComputerPlayer('O');
}
default -> throw new IllegalStateException("Unexpected value: " + choice);
}
// Start the game
Upgrade game = new Upgrade(player1, player2, N, M);
game.start();
}
}