Skip to content
Snippets Groups Projects
Commit 187c2d61 authored by Bariatti Francesco's avatar Bariatti Francesco
Browse files

Created update thread

parent ad440533
No related branches found
No related tags found
No related merge requests found
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){ }
......
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
......
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);
});
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment