OthelloSinglePlayerController.java (4406B) download
1package nl.isygameclient.controllers.games.othello;
2
3import com.jfoenix.controls.JFXButton;
4import javafx.event.ActionEvent;
5import javafx.fxml.FXML;
6import javafx.fxml.Initializable;
7import javafx.scene.control.Label;
8import javafx.scene.layout.GridPane;
9import javafx.scene.paint.Color;
10import javafx.scene.shape.Circle;
11import nl.isygameclient.controllers.games.GameController;
12import nl.isygameclient.models.Player;
13import nl.isygameclient.models.PlayerManager;
14import nl.isygameclient.models.games.othello.Othello;
15import nl.isygameclient.util.Vector2D;
16
17import java.net.URL;
18import java.util.*;
19import java.util.stream.Stream;
20
21public class OthelloSinglePlayerController extends GameController implements Initializable {
22
23 private JFXButton[][] boardButtons;
24
25 @FXML
26 private GridPane boardGrid;
27
28 @Override
29 public void initialize(URL url, ResourceBundle resourceBundle) {
30// var players = List.of(new Player("player1", "white"), new Player("player2", "black"));
31// var manager = new PlayerManager(0, new ArrayList<>(players));
32// game = new Othello(manager);
33 initializeBoard();
34 updateButtons();
35 }
36
37 private void initializeBoard() {
38 var board = game.getBoard();
39 boardButtons = new JFXButton[board.getHeight()][board.getWidth()];
40 for (int i = 0; i < board.getHeight(); i++) {
41 for (int j = 0; j < board.getWidth(); j++) {
42 JFXButton button = new JFXButton();
43 var index = i + "," + j;
44 button.setId(index);
45 button.setMinSize(60.0, 60.0);
46 button.setMaxSize(60.0, 60.0);
47
48 var styleClass = button.getStyleClass();
49 styleClass.add("othello-button");
50 styleClass.add("headline-small");
51 button.setOnAction((ActionEvent event) -> onMoveButtonClick(button));
52 boardButtons[i][j] = button;
53 boardGrid.add(button, i, j);
54 }
55 }
56 }
57
58 private void updateButtons() {
59 clearBoardButtons();
60 var board = game.getBoard();
61 for (int i = 0; i < board.getHeight(); i++) {
62 for (int j = 0; j < board.getWidth(); j++) {
63 var index = board.get(new Vector2D<>(j,i));
64 if (index == null) continue;
65 if (Objects.equals( "white", index.getPlayingAs())) {
66 addStone(boardButtons[i][j], Color.WHITE);
67 } else if(Objects.equals( "black", index.getPlayingAs())) {
68 addStone(boardButtons[i][j], Color.BLACK);
69 }
70 }
71 }
72 }
73
74 private void addStone(JFXButton button, Color color) {
75 var circle = new Circle();
76 circle.setRadius(20);
77 circle.setFill(color);
78 button.setGraphic(circle);
79 }
80
81 private void onMoveButtonClick(JFXButton button) {
82 // Move
83 var id = button.getId().split(",");
84 var index = Stream.of(id).mapToInt(Integer::parseInt).toArray();
85 var manager = game.getPlayerManager();
86 var currentPlayer = manager.getCurrentPlayer();
87 if (game.getValidMoves(currentPlayer).isEmpty()) {
88 manager.nextPlayer();
89 return;
90 }
91 if (game.isMoveValid(currentPlayer, new Vector2D<>(index[0], index[1]))) {
92 game.move(currentPlayer, new Vector2D<>(index[0], index[1]));
93 updateButtons();
94 manager.nextPlayer();
95 }
96
97 if (game.isGameOver()) {
98 disableBoardButtons();
99 }
100 }
101
102 private void clearBoardButtons() {
103 for (JFXButton[] row : boardButtons) {
104 for (JFXButton button : row) {
105 button.setGraphic(null);
106 }
107 }
108 }
109
110 private void disableBoardButtons() {
111 for (JFXButton[] row : boardButtons) {
112 for (JFXButton button : row) {
113 button.setDisable(true);
114 }
115 }
116 }
117
118 private void enableBoardButtons() {
119 for (JFXButton[] row : boardButtons) {
120 for (JFXButton button : row) {
121 button.setDisable(false);
122 }
123 }
124 }
125
126 public void onNewGameButtonClick(ActionEvent actionEvent) {
127 game.restart();
128 updateButtons();
129 enableBoardButtons();
130 }
131 public void onMainMenuButtonClick(ActionEvent actionEvent) {
132 }
133
134}