TicTacToeMultiPlayerController.java (5419B) download
1package nl.isygameclient.controllers.games.tictactoe;
2
3import com.jfoenix.controls.JFXButton;
4import com.jfoenix.controls.JFXComboBox;
5import java.io.IOException;
6import java.net.URL;
7import java.util.ArrayList;
8import java.util.ResourceBundle;
9
10import javafx.application.Platform;
11import javafx.event.ActionEvent;
12import javafx.fxml.FXML;
13import javafx.fxml.Initializable;
14import javafx.scene.control.Label;
15import javafx.scene.control.TextField;
16import javafx.scene.layout.GridPane;
17import nl.isygameclient.controllers.games.GameController;
18import nl.isygameclient.models.games.tictactoe.TicTacToe;
19import nl.isygameclient.network.GameClient;
20import nl.isygameclient.network.GameType;
21import nl.isygameclient.network.Match;
22import nl.isygameclient.util.StageHandler;
23
24public class TicTacToeMultiPlayerController extends GameController implements Runnable, Initializable {
25 private final ArrayList<JFXButton> boardButtons = new ArrayList<>();
26 private final String[] players = { "X", "O" };
27
28 @FXML
29 private Label playingAgainstLabel;
30 @FXML
31 private TextField hostField;
32 @FXML
33 private TextField portField;
34 @FXML
35 private TextField nameField;
36 @FXML
37 private TextField opponentField;
38 @FXML
39 private JFXComboBox<String> playingAsCombo;
40 @FXML
41 private Label currentPlayer;
42 @FXML
43 private Label gameOverText;
44 @FXML
45 private GridPane grid;
46
47 private GameClient client;
48 private Match match;
49 private String playerSelf = "X", playerOther = "O";
50 private boolean running;
51
52 private Thread gameThread;
53
54 @Override
55 public void initialize(URL url, ResourceBundle resourceBundle) {
56 for (int i = 0; i < 3 * 3; i++) {
57 JFXButton button = new JFXButton();
58 button.setId(Integer.toString(i));
59 button.setMinSize(200.0, 200.0);
60 var styleClass = button.getStyleClass();
61 styleClass.add("ttt-button");
62 styleClass.add("display-large");
63 button.setOnAction((ActionEvent event) -> onMoveButtonClick(button));
64 boardButtons.set(i, button);
65 grid.add(button, i / 3, i % 3);
66 }
67
68 playingAsCombo.getItems().setAll(players);
69 playingAsCombo.getSelectionModel().selectFirst();
70
71 disableBoardButtons();
72 // currentPlayer.setText();
73 }
74
75 public void run() {
76 running = true;
77 try {
78 client = new GameClient(hostField.getText(), Integer.parseInt(portField.getText()));
79 client.login(nameField.getText());
80
81 if (opponentField.getText().length() == 0)
82 match = client.match(GameType.TICTACTOE);
83 else
84 match = client.match(GameType.TICTACTOE, opponentField.getText());
85
86 while (running) {
87 match.update();
88
89 if (!match.isStarted())
90 continue;
91
92 // game.setCurrentPlayer(match.isYourTurn() ? playerSelf : playerOther);
93 // game.move(Integer.parseInt(moveMap.get("move")));
94 Platform.runLater(() -> {
95 playingAgainstLabel.setText(match.getOpponent());
96
97 if (match.isYourTurn()) {
98 currentPlayer.setText(playerSelf);
99 enableBoardButtons();
100 } else {
101 currentPlayer.setText(playerOther);
102 disableBoardButtons();
103 }
104 for (int i = 0; i < 3 * 3; i++) {
105 switch (match.getMove(i)) {
106 case -1 -> boardButtons.get(i).setText(playerOther);
107 case 0 -> boardButtons.get(i).setText("");
108 case 1 -> boardButtons.get(i).setText(playerSelf);
109 }
110 }
111 });
112
113 if (match.getOutcome() == null)
114 continue;
115
116 running = false;
117
118 Platform.runLater(() -> {
119 switch (match.getOutcome().type) {
120 case DRAW:
121 System.out.println("Draw!");
122 gameOverText.setText("Draw!");
123 gameOverText.setVisible(true);
124 break;
125 case LOSS:
126 System.out.printf("%s, Is the Winner!\n\n", playerOther);
127 gameOverText.setText(String.format("%s, is the Winner!\n\n", playerOther));
128 gameOverText.setVisible(true);
129 break;
130 case WIN:
131 System.out.printf("%s, Is the Winner!\n\n", playerSelf);
132 gameOverText.setText(String.format("%s, is the Winner!\n\n", playerSelf));
133 gameOverText.setVisible(true);
134 default:
135 }
136 });
137 }
138 match.abort();
139 client.close();
140 } catch (IOException e) {
141 e.printStackTrace();
142 }
143 }
144
145 @FXML
146 private void onNewGameButtonClick() throws NumberFormatException, InterruptedException {
147 gameOverText.setVisible(false);
148
149 if (gameThread != null && running) {
150 running = false;
151 gameThread.join();
152 }
153 gameThread = new Thread(this);
154 gameThread.start();
155 }
156
157 private void onMoveButtonClick(JFXButton button) {
158 try {
159 // Move
160 int pos = Integer.parseInt(button.getId());
161 // if (game.isMoveValid(pos))
162 match.move(pos);
163 // currentPlayer.setText(playerOther);
164
165 // updateBoard();
166 // currentPlayer.setText(playerSelf);
167 } catch (Exception exc) {
168 throw new RuntimeException(exc);
169 }
170 }
171
172 private void disableBoardButtons() {
173 for (JFXButton button : boardButtons) {
174 button.setDisable(true);
175 }
176 }
177
178 private void enableBoardButtons() {
179 for (JFXButton button : boardButtons) {
180 button.setDisable(false);
181 }
182 }
183
184 @FXML
185 protected void onPlayingAsComboSelect() {
186 System.out.printf("Now playing As: %s\n", playingAsCombo.getValue());
187 switch (playingAsCombo.getValue()) {
188 case "X" -> {
189 playerSelf = "X";
190 playerOther = "O";
191 }
192 case "O" -> {
193 playerSelf = "O";
194 playerOther = "X";
195 }
196 }
197 }
198
199 @FXML
200 protected void onMainMenuButtonClick() {
201 StageHandler.get().changeSceneOfStage("Game","views/Games/TicTacToe/TicTacToeMainMenu.fxml");
202 }
203}