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