package controller; import javafx.application.Platform; import javafx.scene.control.Alert; import javafx.scene.control.ButtonType; import javafx.scene.control.Label; import model.GameState; import model.Player; import model.Tile; import view.TileView; import java.io.*; public class UpdateThread extends Thread { BufferedReader reader; PrintWriter writer; GameState gameState; Tile[] board; TileView[] boardView; Player humanPlayer; Label scoreRed, scoreBlue, turnLabel; public UpdateThread(Process program, GameState gameState, Tile[] board, TileView[] boardView, Player humanPlayer, Label scoreRed, Label scoreBlue, Label turnLabel) { this.reader = new BufferedReader(new InputStreamReader(program.getInputStream())); this.writer = new PrintWriter(new OutputStreamWriter(program.getOutputStream()), true); this.gameState = gameState; this.board = board; this.boardView = boardView; this.humanPlayer = humanPlayer; this.scoreRed = scoreRed; this.scoreBlue = scoreBlue; this.turnLabel = turnLabel; } public void run() { boolean gameRunning = true; while (gameRunning) { try { String line = reader.readLine(); System.out.println(line); if (line == null) { gameRunning = false; } else if (line.startsWith(Player.Red + " won") || line.startsWith(Player.Blue + " won") || line.startsWith("draw")) { //System.out.println("======= END OF GAME======="); Platform.runLater(() -> { Player computer = humanPlayer.equals(Player.Red) ? Player.Blue : Player.Red; String message = ""; if (line.startsWith(humanPlayer.toString())) //Human won message = "You won!!! I can't believe it!"; else if (line.startsWith(computer.toString())) message = "You just lost the penguin game."; else message = "That's a draw. Not bad!"; Alert alert = new Alert(Alert.AlertType.INFORMATION, message, ButtonType.OK); alert.showAndWait(); }); gameRunning = false; } else if (line.contains("{")) //Line contains JSON { //UPDATE MODEL gameState.update(line.substring(line.indexOf("{"), line.lastIndexOf("}") + 1)); //Extract JSON string for (int i = 0; i < board.length; i++) { board[i].setNbFish(gameState.getNbFish(i)); board[i].setPenguinPresence(Tile.PenguinPresence.NO_PENGUIN); } for (int i = 0; i < 4; i++) { board[gameState.getPenguinPos(Player.Red, i)].setPenguinPresence(Tile.PenguinPresence.RED_PENGUIN); board[gameState.getPenguinPos(Player.Blue, i)].setPenguinPresence(Tile.PenguinPresence.BLUE_PENGUIN); } //UPDATE VIEW for (int i = 0; i < boardView.length; i++) boardView[i].update(); Platform.runLater(() -> { scoreRed.setText(Integer.toString(gameState.getScore(Player.Red))); scoreBlue.setText(Integer.toString(gameState.getScore(Player.Blue))); turnLabel.setText(gameState.getCurrent_player()+ "'s turn"); }); if (gameState.getCurrent_player() == humanPlayer) { if (!gameState.getCanPlay(humanPlayer)) { Platform.runLater(() -> { Alert alert = new Alert(Alert.AlertType.INFORMATION, "You can't play any move!", ButtonType.OK); alert.showAndWait(); writer.println("0"); //This pass the turn }); } } } } catch (IOException e) { gameRunning = false; Platform.runLater(() -> { Alert alert = new Alert(Alert.AlertType.ERROR, "Error during reading from penguin program!", ButtonType.FINISH); alert.showAndWait(); e.printStackTrace(); Platform.exit(); }); } catch (Throwable e) { gameRunning = false; Platform.runLater(() -> { Alert alert = new Alert(Alert.AlertType.ERROR, "Unhandled exception in update thread: " + e.getMessage(), ButtonType.FINISH); alert.showAndWait(); e.printStackTrace(); Platform.exit(); }); } } } }