diff --git a/gui/src/Case.java b/gui/src/Case.java index 20845cdceb89188f022546bea35dfeb6d2eead93..44288d96e8edc0ab6c73e9f53d874909232e953d 100644 --- a/gui/src/Case.java +++ b/gui/src/Case.java @@ -8,12 +8,44 @@ public class Case { private Polygon caseFX; + public void setNbFish(int nbFish) + { + this.nbFish = nbFish; + } + + public void setPenguins(Penguins penguins) + { + this.penguins = penguins; + } + + public int getRang() + { + + return rang; + } + + public int getNbFish() + { + return nbFish; + } + + public Penguins getPenguins() + { + return penguins; + } + + public Polygon getCaseFX() + { + return caseFX; + } + public Case(int rang, Penguins penguins, Polygon caseFX) { this.rang = rang; this.penguins = penguins; this.caseFX = caseFX; this.nbFish = 0; + } public Case(int rang, int nbFish, Penguins penguins, Polygon caseFX) { diff --git a/gui/src/Controller.java b/gui/src/Controller.java index bb3033d24337543cc7837d1be5ad09c95171f16f..7b1432a5adc5bf9b06fd5828d01808937b02fb5a 100644 --- a/gui/src/Controller.java +++ b/gui/src/Controller.java @@ -31,8 +31,8 @@ public class Controller implements Initializable { Polygon[] cases = {case0, case1, case2, case3, case4, case5, case6, case7, case8, case9, case10, case11, case12, case13, case14, case15, case16, case17, case18, case19, case20, case21, case22, case23, case24, case25, case26, case27, case28, case29, case30, case31, case32, case33, case34, case35, case36, case37, case38, case39, case40, case41, case42, case43, case44, case45, case46, case47, case48, case49, case50, case51, case52, case53, case54, case55, case56, case57, case58, case59}; for(Polygon c : cases) { - c.setOnMouseClicked(new MyClickHandler()); - c.setOnMouseEntered(new MyHexEnteredHandler()); + //c.setOnMouseClicked(new MyClickHandler()); + //c.setOnMouseEntered(new MyHexEnteredHandler()); } this.plateau = new Plateau(cases); this.gameState = new GameState(); @@ -45,13 +45,14 @@ public class Controller implements Initializable { e.printStackTrace(); System.exit(1); } - UpdateThread upT = new UpdateThread(gameProcess, this.gameState); + UpdateThread upT = new UpdateThread(gameProcess, this.gameState, this.plateau); upT.setDaemon(true); upT.start(); //We tell the game that we will start gameInput.print("h"); gameInput.println(); + gameInput.println("0"); } //public Polygon getPolygonFromCoordinates(double x, double y){ } diff --git a/gui/src/GameState.java b/gui/src/GameState.java index db2776ffd1c662311ce4559f85ead5c0a7a9c771..c838e6e85fc7f76cc06ab3677a320b46ae150001 100644 --- a/gui/src/GameState.java +++ b/gui/src/GameState.java @@ -1,15 +1,91 @@ import org.json.*; + +import java.util.HashMap; +import java.util.Map; + public class GameState { - public void update (String json){ - System.out.println(json); + long[] fish; + boolean can_play_red, can_play_blue; + Player current_player; + Map<Player, int[]> penguins; + Map<Player, Integer> score; + + public GameState() + { + fish = new long[3]; + penguins = new HashMap<>(); + penguins.put(Player.Red, new int[4]); + penguins.put(Player.Blue, new int[4]); + score = new HashMap<>(); + score.put(Player.Red, 0); + score.put(Player.Blue, 0); + } + public void update (String jsonString){ + //System.out.println(jsonString); + JSONObject json = new JSONObject(jsonString); + can_play_red = json.getJSONObject("can_play").getBoolean("red"); + can_play_blue = json.getJSONObject("can_play").getBoolean("blue"); + current_player = Player.valueOf(json.getString("current_player")); + fish[0] = json.getJSONObject("bitboards").getLong("onefish"); + fish[1] = json.getJSONObject("bitboards").getLong("twofish"); + fish[2] = json.getJSONObject("bitboards").getLong("threefish"); + score.put(Player.Red, json.getJSONObject("score").getInt("red")); + score.put(Player.Blue, json.getJSONObject("score").getInt("blue")); + JSONArray jsonPeng = json.getJSONObject("penguins").getJSONArray("red"); + for(int i = 0; i < jsonPeng.length(); i++) + penguins.get(Player.Red)[i] = jsonPeng.getInt(i); + jsonPeng = json.getJSONObject("penguins").getJSONArray("blue"); + for(int i = 0; i < jsonPeng.length(); i++) + penguins.get(Player.Blue)[i] = jsonPeng.getInt(i); }; + + /** + * @param tileNb The number of the tile we want to know the value of + * @return The number of fish on the tile, or 0 if empty + */ + public int getNbFish(int tileNb) + { + if(((fish[0] >>> tileNb) & 1) == 1) + return 1; + else if (((fish[1] >>> tileNb) & 1) == 1) + return 2; + else if (((fish[2] >>> tileNb) & 1) == 1) + return 3; + return 0; + } + public int getPenguinPos (int i, Player player) { //TODO return -1; } + public boolean isCan_play_red() + { + return can_play_red; + } + + public boolean isCan_play_blue() + { + return can_play_blue; + } + + public Player getCurrent_player() + { + return current_player; + } + + public int getScore(Player player) + { + return score.get(player); + } + + enum Player + { + Red,Blue; + } + } diff --git a/gui/src/Plateau.java b/gui/src/Plateau.java index a8cc1ff8d03b999a86b525f0e857095dc81a6958..64cb8d54bce9f0186a2351628c63c40db6ec197d 100644 --- a/gui/src/Plateau.java +++ b/gui/src/Plateau.java @@ -11,5 +11,8 @@ public class Plateau { } } - + public Case[] getGrille() + { + return grille; + } } diff --git a/gui/src/UpdateThread.java b/gui/src/UpdateThread.java index 9408ce5c085e2833a817fb8b50c893c930c7f151..efccd0534f5ccbb5137ef6e2a9d6cfedfb6c5e77 100644 --- a/gui/src/UpdateThread.java +++ b/gui/src/UpdateThread.java @@ -1,6 +1,7 @@ import javafx.application.Platform; import javafx.scene.control.Alert; import javafx.scene.control.ButtonType; +import javafx.scene.paint.Color; import java.io.BufferedReader; import java.io.IOException; @@ -10,11 +11,13 @@ public class UpdateThread extends Thread { BufferedReader reader; GameState gameState; + Plateau plateau; - public UpdateThread(Process program, GameState gameState) + public UpdateThread(Process program, GameState gameState, Plateau plateau) { this.reader = new BufferedReader(new InputStreamReader(program.getInputStream())); this.gameState = gameState; + this.plateau = plateau; } @@ -25,14 +28,38 @@ public class UpdateThread extends Thread try { String line = reader.readLine(); - //System.out.println(line); - if (line.contains("{")) + System.out.println(line); + if(line == null) + running = false; + else { - gameState.update(line.substring(line.indexOf("{"), line.lastIndexOf("}")+1)); - Platform.runLater(() -> + if (line.contains("{")) { - // TODO: 4/27/16 update GUI from state - }); + gameState.update(line.substring(line.indexOf("{"), line.lastIndexOf("}") + 1)); + Platform.runLater(() -> + { + for(Case c : plateau.getGrille()) + { + c.setNbFish(gameState.getNbFish(c.getRang())); + switch (c.getNbFish()) + { + case 0: + c.getCaseFX().setFill(Color.BLUE); + break; + case 1: + c.getCaseFX().setFill(Color.GREEN); + break; + case 2: + c.getCaseFX().setFill(Color.YELLOW); + break; + case 3: + c.getCaseFX().setFill(Color.RED); + break; + } + } + // TODO: 4/27/16 update GUI from state + }); + } } } catch (IOException e) {