-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
165 lines (149 loc) · 5.76 KB
/
Game.java
File metadata and controls
165 lines (149 loc) · 5.76 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
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import java.util.Random;
public class Game {
private Board board; // Board instance
private ScoreKeeper scoreKeeper; // ScoreKeeper instance
private char currentPlayer; // Current player ('R' for Red, 'B' for Blue)
private GridPane gridPane; // GridPane for the game board GUI
private Circle[][] circles; // 2D array to keep track of Circle nodes
private Label turnLabel; // Label to display whose turn it is
private boolean isSinglePlayer; // Flag to indicate single-player mode
public Game(boolean isSinglePlayer) {
this.isSinglePlayer = isSinglePlayer;
board = new Board();
scoreKeeper = new ScoreKeeper();
currentPlayer = 'R'; // Start with Red
turnLabel = new Label("Red's turn");
circles = new Circle[Board.ROWS][Board.COLS];
}
// Method to get the game board GUI
public BorderPane getBoardGUI() {
BorderPane borderPane = new BorderPane();
gridPane = new GridPane();
// Initialize the board GUI with empty circles
for (int row = 0; row < Board.ROWS; row++) {
for (int col = 0; col < Board.COLS; col++) {
Circle circle = createCircle(row, col);
circles[row][col] = circle; // Store the circle in the 2D array
gridPane.add(circle, col, row);
}
}
borderPane.setCenter(gridPane);
borderPane.setTop(turnLabel);
return borderPane;
}
// Method to create a circle representing a cell on the board
private Circle createCircle(int row, int col) {
Circle circle = new Circle(35, Color.WHITE); // Adjusted size to fit within the cell
circle.setStroke(Color.BLACK); // the circumference of th cirlce to black
circle.setOnMouseClicked(event -> {
if (currentPlayer == 'R' || !isSinglePlayer) {
makeMove(col);
}
});
return circle;
}
// Method to handle a move by the current player
private void makeMove(int col) {
int row = board.getEmptyRow(col);
if (row != -1) { // Check if the move is valid
if (board.makeMove(col, currentPlayer)) {
updateBoardGUI(); // Update the board GUI
if (board.checkWin(currentPlayer)) {
showAlert(currentPlayer + " wins!");
if (currentPlayer == 'R') {
scoreKeeper.incrementRedWins();
} else {
scoreKeeper.incrementBlueWins();
}
scoreKeeper.saveScores();
resetGame(); // Reset the game after a win
} else if (board.isBoardFull()) {
showAlert("It's a tie!");
scoreKeeper.incrementTies();
scoreKeeper.saveScores();
resetGame(); // Reset the game after a tie
} else {
switchPlayer();
updateTurnLabel();
if (isSinglePlayer && currentPlayer == 'B') {
makeRandomMove();
}
}
} else {
showAlert("Invalid move. Try again.");
}
}
}
// Method to make a random move for the computer
private void makeRandomMove() {
Random random = new Random();
int col;
do {
col = random.nextInt(Board.COLS);
} while (board.getEmptyRow(col) == -1);
makeMove(col);
}
// Method to update the board GUI after a move
private void updateBoardGUI() {
for (int row = 0; row < Board.ROWS; row++) {
for (int col = 0; col < Board.COLS; col++) {
Circle circle = circles[row][col];
if (circle != null) {
if (board.getBoard()[row][col] == 'R') {
circle.setFill(Color.RED);
} else if (board.getBoard()[row][col] == 'B') {
circle.setFill(Color.BLUE);
} else {
circle.setFill(Color.WHITE);
}
}
}
}
}
// Method to show an alert dialog
private void showAlert(String message) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setContentText(message);
alert.showAndWait();
}
// Method to switch the current player
public void switchPlayer() {
if (currentPlayer == 'R') {
currentPlayer = 'B'; // Switch to Blue
} else {
currentPlayer = 'R'; // Switch to Red
}
}
// Method to update the turn label at the top left
// To notify
private void updateTurnLabel() {
if (currentPlayer == 'R') {
turnLabel.setText("Red's turn");
} else {
turnLabel.setText("Blue's turn");
}
}
// Method to reset the game
private void resetGame() {
board.initializeBoard(); // Reset the board
currentPlayer = 'R'; // Set the current player to Red
updateBoardGUI(); // Update the board to show the reset board
updateTurnLabel(); // Update the turn label at the top
// Make all circles white to reset the board
for (int row = 0; row < Board.ROWS; row++) {
for (int col = 0; col < Board.COLS; col++) {
Circle circle = circles[row][col];
if (circle != null) {
circle.setFill(Color.WHITE);
}
}
}
}
}