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

Misc changes to GUI

parent 187c2d61
No related branches found
No related tags found
No related merge requests found
......@@ -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) {
......
......@@ -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){ }
......
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;
}
}
......@@ -11,5 +11,8 @@ public class Plateau {
}
}
public Case[] getGrille()
{
return grille;
}
}
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)
{
......
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