From 253b897dc6ecae055d39aa1a9a3c71527eea3849 Mon Sep 17 00:00:00 2001 From: Francesco Bariatti <francesco.bariatti@insa-rennes.fr> Date: Wed, 11 May 2016 17:00:02 +0200 Subject: [PATCH] Added generation of random start --- gui/src/controller/Controller.java | 61 ++++++++++++++++++++++-- gui/src/controller/UpdateThread.java | 19 ++------ gui/src/model/GameState.java | 70 +++++++++++++++++++++++++++- gui/src/view/view.fxml | 2 +- 4 files changed, 132 insertions(+), 20 deletions(-) diff --git a/gui/src/controller/Controller.java b/gui/src/controller/Controller.java index 5b49df3..c17fdf6 100644 --- a/gui/src/controller/Controller.java +++ b/gui/src/controller/Controller.java @@ -24,6 +24,7 @@ import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.URL; import java.util.List; +import java.util.Random; import java.util.ResourceBundle; public class Controller implements Initializable @@ -38,6 +39,7 @@ public class Controller implements Initializable private TileView[] boardView; private Player humanPlayer; private int selectedTile; + private int penguinGenCounter; //Used when creating a random state @FXML private Polygon tile0, tile1, tile2, tile3, tile4, tile5, tile6, tile7, tile8, tile9, tile10, tile11, tile12, tile13, tile14, tile15, tile16, tile17, tile18, tile19, tile20, tile21, tile22, tile23, tile24, tile25, tile26, tile27, tile28, tile29, tile30, tile31, tile32, tile33, tile34, tile35, tile36, tile37, tile38, tile39, tile40, tile41, tile42, tile43, tile44, tile45, tile46, tile47, tile48, tile49, tile50, tile51, tile52, tile53, tile54, tile55, tile56, tile57, tile58, tile59; @@ -62,7 +64,6 @@ public class Controller implements Initializable board[i] = new Tile(i, Tile.PenguinPresence.NO_PENGUIN); boardView[i] = new TileView(fxTiles[i], labels[i], board[i]); } -// this.selectedTile = -1; this.gameState = new GameState(); try { @@ -108,13 +109,32 @@ public class Controller implements Initializable try { new JSONObject(result); //Verify if it is valid json (if not, an exception is thrown) + //WE DO WANT TO LOAD A STATE gameInput.println(result); Platform.runLater(() -> startGame()); } catch (JSONException e) { - System.out.println("TODO: RANDOM STATE"); - System.exit(0); + //WE DON'T WANT TO LOAD A STATE + gameState.clearFish(); + gameState.setScore(Player.Red, 0); + gameState.setScore(Player.Blue, 0); + //Generating random fish values + for (int i = 0; i < 60; i++) + { + int nbFish = new Random().nextInt(3) + 1; + gameState.setFish(i, nbFish); + board[i].setNbFish(nbFish); + boardView[i].update(); + } + //Adding listener to add penguins + penguinGenCounter = 0; + for (TileView t : boardView) + { + PlacePenguinClickHandler phandl = new PlacePenguinClickHandler(t.getModelTile().getNumber()); + t.getFxTile().setOnMouseClicked(phandl); + t.getFishLabel().setOnMouseClicked(phandl); + } } } @@ -131,6 +151,7 @@ public class Controller implements Initializable gameInput.println("h"); else gameInput.println("c"); + gameState.setHumanPlayer(humanPlayer); this.selectedTile = -1; for (TileView t : boardView) { @@ -142,6 +163,40 @@ public class Controller implements Initializable } + private class PlacePenguinClickHandler implements EventHandler<MouseEvent> + { + public int tileNumber; + + public PlacePenguinClickHandler(int tileNumber) + { + this.tileNumber = tileNumber; + } + + @Override + public void handle(MouseEvent event) + { + if (!board[tileNumber].getPenguinPresence().equals(Tile.PenguinPresence.NO_PENGUIN)) + return; + if (penguinGenCounter < 4) + { + gameState.setPenguin(Player.Red, penguinGenCounter, tileNumber); + board[tileNumber].setPenguinPresence(Tile.PenguinPresence.RED_PENGUIN); + boardView[tileNumber].update(); + } else if (penguinGenCounter < 8) + { + gameState.setPenguin(Player.Blue, penguinGenCounter - 4, tileNumber); + board[tileNumber].setPenguinPresence(Tile.PenguinPresence.BLUE_PENGUIN); + boardView[tileNumber].update(); + } + penguinGenCounter++; + if (penguinGenCounter >= 8) + { + gameInput.println(gameState.toGameInputJSON()); + Platform.runLater(() -> startGame()); + } + } + } + /** * Event handler that will listen for a click on a tile during the game and move/select the penguin. * It is associated with a TileView diff --git a/gui/src/controller/UpdateThread.java b/gui/src/controller/UpdateThread.java index ad6c6f1..995e1c4 100644 --- a/gui/src/controller/UpdateThread.java +++ b/gui/src/controller/UpdateThread.java @@ -18,7 +18,6 @@ public class UpdateThread extends Thread GameState gameState; Tile[] board; TileView[] boardView; - Player humanPlayer; Label scoreRed, scoreBlue, turnLabel; public UpdateThread(Process program, GameState gameState, Tile[] board, TileView[] boardView, Label scoreRed, Label scoreBlue, Label turnLabel) @@ -33,16 +32,6 @@ public class UpdateThread extends Thread this.turnLabel = turnLabel; } - public Player getHumanPlayer() - { - return humanPlayer; - } - - public void setHumanPlayer(Player humanPlayer) - { - this.humanPlayer = humanPlayer; - } - public void run() { boolean gameRunning = true; @@ -59,9 +48,9 @@ public class UpdateThread extends Thread { //System.out.println("======= END OF GAME======="); Platform.runLater(() -> { - Player computer = humanPlayer.equals(Player.Red) ? Player.Blue : Player.Red; + Player computer = gameState.getHumanPlayer().equals(Player.Red) ? Player.Blue : Player.Red; String message = ""; - if (line.startsWith(humanPlayer.toString())) //Human won + if (line.startsWith(gameState.getHumanPlayer().toString())) //Human won message = "You won!!! I can't believe it!"; else if (line.startsWith(computer.toString())) message = "You just lost the penguin game."; @@ -95,9 +84,9 @@ public class UpdateThread extends Thread turnLabel.setText(gameState.getCurrent_player()+ "'s turn"); }); - if (gameState.getCurrent_player() == humanPlayer) + if (gameState.getCurrent_player() == gameState.getHumanPlayer()) { - if (!gameState.getCanPlay(humanPlayer)) + if (!gameState.getCanPlay(gameState.getHumanPlayer())) { Platform.runLater(() -> { diff --git a/gui/src/model/GameState.java b/gui/src/model/GameState.java index 3d3da94..739e912 100644 --- a/gui/src/model/GameState.java +++ b/gui/src/model/GameState.java @@ -15,6 +15,7 @@ public class GameState Map<Player, Boolean> can_play; //If the red(blue) player can play or if it has no moves left Map<Player, int[]> penguins; //For every player, the list of his penguins Map<Player, Integer> score; //Score of every player + Player humanPlayer; public GameState() { @@ -50,7 +51,30 @@ public class GameState penguins.get(Player.Blue)[i] = jsonPeng.getInt(i); } - ; + + /** + * Set the amount of fish on one tile + * + * @param tileNb the tile on which we want to set the amount of fish + * @param fishNb The amount of fish (0-3) + */ + public void setFish(int tileNb, int fishNb) + { + for (int i = 0; i < 3; i++) + if (i == fishNb - 1) + fish[i] |= (long) 1 << tileNb; + else + fish[i] &= ~((long) 1 << tileNb); + } + + /** + * Reset fish bitboards + */ + public void clearFish() + { + for (int i = 0; i < fish.length; i++) + fish[i] = 0; + } /** * @param tileNb The number of the tile we want to know the value of @@ -145,6 +169,11 @@ public class GameState return -1; } + public void setPenguin(Player player, int penguinNumber, int penguin) + { + penguins.get(player)[penguinNumber] = penguin; + } + /** * @return The list of all tiles that could be reached by moving penguin penguinNb of player player */ @@ -168,6 +197,30 @@ public class GameState return result; } + /** + * @return The state as needed for initialisation of the game (just bitboards, penguins, and score) + */ + public String toGameInputJSON() + { + JSONObject result = new JSONObject(); + JSONObject jsonBitboards = new JSONObject(); + jsonBitboards.put("onefish", fish[0]); + jsonBitboards.put("twofish", fish[1]); + jsonBitboards.put("threefish", fish[2]); + result.put("bitboards", jsonBitboards); + result.put("current_player", Player.Red); + JSONObject jsonPenguins = new JSONObject(); + jsonPenguins.put("red", penguins.get(Player.Red)); + jsonPenguins.put("blue", penguins.get(Player.Blue)); + result.put("penguins", jsonPenguins); + JSONObject jsonScore = new JSONObject(); + jsonScore.put("red", score.get(Player.Red)); + jsonScore.put("blue", score.get(Player.Blue)); + result.put("score", jsonScore); + + return result.toString(); + } + public boolean getCanPlay(Player player) { return can_play.get(player); } public Player getCurrent_player() @@ -180,6 +233,21 @@ public class GameState return score.get(player); } + public Player getHumanPlayer() + { + return humanPlayer; + } + + public void setHumanPlayer(Player humanPlayer) + { + this.humanPlayer = humanPlayer; + } + + public void setScore(Player player, int value) + { + score.put(player, value); + } + public enum Direction { A, B, C, D, E, F; diff --git a/gui/src/view/view.fxml b/gui/src/view/view.fxml index 8ec6c08..eefa9ef 100644 --- a/gui/src/view/view.fxml +++ b/gui/src/view/view.fxml @@ -1012,7 +1012,7 @@ <Font size="39.0" /> </font> </Label> - <Label fx:id="turnLabel" layoutX="482.0" layoutY="227.0" text="Blue's turn yo!"> + <Label fx:id="turnLabel" layoutX="482.0" layoutY="227.0" text=" "> <font> <Font size="24.0" /> </font> -- GitLab