TicTacToeSinglePlayerController.java (3759B) download
1package nl.isygameclient.controllers.games.tictactoe;
2
3import com.jfoenix.controls.JFXButton;
4import com.jfoenix.controls.JFXComboBox;
5
6import java.util.ArrayList;
7import java.util.List;
8import java.util.stream.Stream;
9
10import javafx.event.ActionEvent;
11import javafx.fxml.FXML;
12import javafx.scene.control.Label;
13import javafx.scene.layout.GridPane;
14import nl.isygameclient.controllers.games.GameController;
15import nl.isygameclient.models.Player;
16import nl.isygameclient.models.PlayerManager;
17import nl.isygameclient.models.games.tictactoe.TicTacToe;
18import nl.isygameclient.util.StageHandler;
19import nl.isygameclient.util.Vector2D;
20
21public class TicTacToeSinglePlayerController extends GameController {
22 private final ArrayList<JFXButton> boardButtons = new ArrayList<>();
23
24 @FXML
25 public JFXComboBox<String> difficultyCombo;
26 @FXML
27 public JFXComboBox<String> playingAsCombo;
28 @FXML
29 public JFXComboBox<String> opponentCombo;
30
31 @FXML
32 protected Label currentPlayer;
33 @FXML
34 public Label gameOverText;
35
36 @FXML
37 protected GridPane grid;
38
39 private Thread gameThread;
40
41 @FXML
42 protected void initialize() {
43 initializeGame();
44 initializeBoard();
45 updateCurrentPlayerLabel();
46 }
47
48 private void initializeGame() {
49// var players = new ArrayList<>(List.of(new Player("player1", "x"), new Player("player2", "o")));
50// var manager = new PlayerManager(0, players);
51// game = new TicTacToe(manager);
52
53 }
54
55
56 private void initializeBoard() {
57 var board = game.getBoard();
58 for (int y = 0; y < board.getWidth(); y++) {
59 for (int x = 0; x < board.getHeight(); x++) {
60 JFXButton button = new JFXButton();
61
62 var index = x + "," + y;
63 button.setId(index);
64 button.setMinSize(200.0, 200.0);
65 var styleClass = button.getStyleClass();
66 styleClass.add("ttt-button");
67 styleClass.add("display-large");
68 button.setOnAction((ActionEvent event) -> onMoveButtonClick(button));
69 boardButtons.add(button);
70 grid.add(button, x, y);
71 }
72 }
73 }
74
75 private void updateCurrentPlayerLabel() {
76 currentPlayer.setText(game.getPlayerManager().getCurrentPlayer().getPlayingAs());
77 }
78
79 private void onMoveButtonClick(JFXButton button) {
80 // Move
81 var id = button.getId().split(",");
82 var index = Stream.of(id).mapToInt(Integer::parseInt).toArray();
83 var manager = game.getPlayerManager();
84 var currentPlayer = manager.getCurrentPlayer();
85 if (game.isMoveValid(currentPlayer, new Vector2D<>(index[0], index[1]))) {
86 game.move(currentPlayer, new Vector2D<>(index[0], index[1]));
87 button.setText(currentPlayer.getPlayingAs());
88 updateCurrentPlayerLabel();
89 manager.nextPlayer();
90 }
91
92 // Game Over
93 if (game.isGameOver()) {
94 onGameOver();
95 }
96 }
97
98 private void onGameOver() {
99 disableBoardButtons();
100 System.out.println("Game Over");
101 if (game.isDraw()) {
102 System.out.println("Draw!");
103 gameOverText.setText("Draw!");
104 gameOverText.setVisible(true);
105 } else {
106 System.out.printf("%s, Is the Winner!\n\n", game.getWinners().get(0));
107 gameOverText.setText(String.format("%s, is the Winner!\n\n", game.getWinners().get(0)));
108 gameOverText.setVisible(true);
109 }
110 }
111
112 private void clearBoardButtons() {
113 for (JFXButton button : boardButtons) {
114 button.setText("");
115 }
116 }
117
118 private void disableBoardButtons() {
119 for (JFXButton button : boardButtons) {
120 button.setDisable(true);
121 }
122 }
123
124 private void enableBoardButtons() {
125 for (JFXButton button : boardButtons) {
126 button.setDisable(false);
127 }
128 }
129
130 @FXML
131 protected void onNewGameButtonClick() {
132 // Make new Game
133 game.restart();
134
135 clearBoardButtons();
136 enableBoardButtons();
137 gameOverText.setVisible(false);
138 }
139
140 @FXML
141 protected void onMainMenuButtonClick() {
142 StageHandler.get().changeSceneOfStage("Game","views/Games/TicTacToe/TicTacToeMainMenu.fxml");
143 }
144}