diff --git a/gui/src/Controller.java b/gui/src/Controller.java index 4925ef3b8efb12640cf47f167372ed92c56c3773..bb3033d24337543cc7837d1be5ad09c95171f16f 100644 --- a/gui/src/Controller.java +++ b/gui/src/Controller.java @@ -1,4 +1,6 @@ import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; import java.net.URL; import java.util.ResourceBundle; @@ -13,8 +15,9 @@ import javafx.scene.paint.Color; import javafx.scene.shape.Polygon; public class Controller implements Initializable { - private State state; + private GameState gameState; private Process gameProcess; + private PrintWriter gameInput; private Plateau plateau; @FXML @@ -32,14 +35,23 @@ public class Controller implements Initializable { c.setOnMouseEntered(new MyHexEnteredHandler()); } this.plateau = new Plateau(cases); + this.gameState = new GameState(); try { this.gameProcess = new ProcessBuilder("../bin/theturk").start(); + gameInput = new PrintWriter(new OutputStreamWriter(gameProcess.getOutputStream()), true); } catch (IOException e) { Alert alert = new Alert(Alert.AlertType.ERROR,"Can't run penguin program", ButtonType.OK); alert.showAndWait(); e.printStackTrace(); System.exit(1); } + UpdateThread upT = new UpdateThread(gameProcess, this.gameState); + upT.setDaemon(true); + upT.start(); + + //We tell the game that we will start + gameInput.print("h"); + gameInput.println(); } //public Polygon getPolygonFromCoordinates(double x, double y){ } diff --git a/gui/src/State.java b/gui/src/GameState.java similarity index 51% rename from gui/src/State.java rename to gui/src/GameState.java index 4301973e9751561cdd5a0fb98971494f0cc64103..db2776ffd1c662311ce4559f85ead5c0a7a9c771 100644 --- a/gui/src/State.java +++ b/gui/src/GameState.java @@ -1,6 +1,11 @@ import org.json.*; -public class State { - public void update (String json){};//TODO +public class GameState +{ + public void update (String json){ + System.out.println(json); + + + }; public int getPenguinPos (int i, Player player) { //TODO diff --git a/gui/src/UpdateThread.java b/gui/src/UpdateThread.java new file mode 100644 index 0000000000000000000000000000000000000000..9408ce5c085e2833a817fb8b50c893c930c7f151 --- /dev/null +++ b/gui/src/UpdateThread.java @@ -0,0 +1,49 @@ +import javafx.application.Platform; +import javafx.scene.control.Alert; +import javafx.scene.control.ButtonType; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +public class UpdateThread extends Thread +{ + BufferedReader reader; + GameState gameState; + + public UpdateThread(Process program, GameState gameState) + { + this.reader = new BufferedReader(new InputStreamReader(program.getInputStream())); + this.gameState = gameState; + } + + + public void run() + { + boolean running = true; + while(running) + try + { + String line = reader.readLine(); + //System.out.println(line); + if (line.contains("{")) + { + gameState.update(line.substring(line.indexOf("{"), line.lastIndexOf("}")+1)); + Platform.runLater(() -> + { + // TODO: 4/27/16 update GUI from state + }); + } + } catch (IOException e) + { + running = false; + Platform.runLater(() -> + { + Alert alert = new Alert(Alert.AlertType.ERROR, "Error during reading from penguin program!", ButtonType.FINISH); + e.printStackTrace(); + alert.showAndWait(); + System.exit(1); + }); + } + } +}