-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameUI.java
More file actions
137 lines (119 loc) · 4.76 KB
/
GameUI.java
File metadata and controls
137 lines (119 loc) · 4.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
package com.example.csce314ffl;
import javafx.application.Platform;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class GameUI {
private GameBoard gameBoard;
private AI ai;
private Stage primaryStage;
private GridPane grid;
public GameUI(GameBoard gameBoard, AI ai, Stage primaryStage) {
this.gameBoard = gameBoard;
this.ai = ai;
this.primaryStage = primaryStage;
this.grid = new GridPane();
initializeBoard();
}
private void initializeBoard() {
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setBackground(new Background(new BackgroundFill(Color.BLUE, CornerRadii.EMPTY, null)));
for (int row = 0; row < gameBoard.getRows(); row++) {
for (int col = 0; col < gameBoard.getColumns(); col++) {
Button button = createButton(col);
grid.add(button, col, row);
}
}
addControlButtons();
Scene scene = new Scene(grid);
primaryStage.setTitle("Connect Four");
primaryStage.setScene(scene);
primaryStage.show();
}
private Button createButton(int col) {
Button button = new Button();
button.setMinSize(50, 50);
button.setMaxSize(50, 50);
button.setStyle("-fx-background-radius: 25; -fx-background-color: #FFFFFF;");
button.setOnAction(event -> handleButtonClick(col));
return button;
}
private void addControlButtons() {
// Save button
Button saveButton = new Button("Save");
saveButton.setOnAction(event -> gameBoard.saveGame("game_save.dat"));
grid.add(saveButton, gameBoard.getColumns(), 0);
// Load button
Button loadButton = new Button("Load");
loadButton.setOnAction(event -> {
gameBoard.loadGame("game_save.dat");
updateBoard();
});
grid.add(loadButton, gameBoard.getColumns(), 1);
// Reset button
Button resetButton = new Button("Reset");
resetButton.setOnAction(event -> resetGame());
grid.add(resetButton, gameBoard.getColumns(), 2);
}
private void handleButtonClick(int col) {
Platform.runLater(() -> {
if (gameBoard.dropChip(col, Chip.PLAYER)) {
updateBoard();
if (gameBoard.checkForWinner()) {
showAlert("Player wins!");
resetGame();
} else if (gameBoard.isBoardFull()) {
showAlert("Stalemate! The game is a draw.");
resetGame();
} else {
ai.playMove(); // Let AI make a move
updateBoard();
if (gameBoard.checkForWinner()) {
showAlert("AI wins!");
resetGame();
} else if (gameBoard.isBoardFull()) {
showAlert("Stalemate! The game is a draw.");
resetGame();
}
}
} else {
showAlert("Column is full. Try a different one.");
}
});
}
private void updateBoard() {
for (int row = 0; row < gameBoard.getRows(); row++) {
for (int col = 0; col < gameBoard.getColumns(); col++) {
Button button = (Button) grid.getChildren().get(row * gameBoard.getColumns() + col);
Chip chip = gameBoard.getBoardChip(row, col);
if (chip == Chip.PLAYER) {
button.setStyle("-fx-background-radius: 25; -fx-background-color: #FFFF00; -fx-border-color: #000000;");
} else if (chip == Chip.AI) {
button.setStyle("-fx-background-radius: 25; -fx-background-color: #FF0000; -fx-border-color: #000000;");
} else {
button.setStyle("-fx-background-radius: 25; -fx-background-color: #FFFFFF; -fx-border-color: #000000;");
}
}
}
}
private void resetGame() {
gameBoard.resetBoard();
updateBoard();
}
private void showAlert(String message) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Connect Four");
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
}