diff --git a/src/main/java/inf112/fiasko/roborally/objects/IInteractableGame.java b/src/main/java/inf112/fiasko/roborally/objects/IInteractableGame.java index 91ee231..5f873b0 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/IInteractableGame.java +++ b/src/main/java/inf112/fiasko/roborally/objects/IInteractableGame.java @@ -28,20 +28,47 @@ public interface IInteractableGame { */ String getWinningPlayerName(); - void reciveAllProgrammes(ProgamsContainer programs) throws InterruptedException; + /** + * Continues turn when programs for all players are received from the server + * @param programs The programs container received from the server + * @throws InterruptedException If interrupted during sleep + */ + void receiveAllPrograms(ProgamsContainer programs) throws InterruptedException; - void recivedStayInPowerdown(PowerdownContainer powerdowns); - - List getProgram(); - - int getProgramSize(); - - void setPlayerHand(ProgrammingCardDeck playerHand); + /** + * Continues turn when stay in power down is received from all players + * @param powerDowns The power down container received from the server + */ + void receiveStayInPowerDown(PowerdownContainer powerDowns); + /** + * Gets the hand of this player + * @return The hand of this player + */ ProgrammingCardDeck getPlayerHand(); + /** + * Sets the hand of this player + * @param playerHand The new hand of this player + */ + void setPlayerHand(ProgrammingCardDeck playerHand); + + /** + * Gets the amount of cards the player can choose for their program + * @return The size of the player's next program + */ + int getProgramSize(); + + /** + * Gets the program of this player + * @return The program of this player + */ + List getProgram(); + + /** + * Sets the program of this player + * @param program The program of this player + */ void setProgram(List program); - - } diff --git a/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java b/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java index c5769f6..1a6d216 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java +++ b/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java @@ -1,8 +1,6 @@ package inf112.fiasko.roborally.objects; import com.esotericsoftware.kryonet.Connection; -import inf112.fiasko.roborally.elementproperties.Action; -import inf112.fiasko.roborally.elementproperties.Direction; import inf112.fiasko.roborally.elementproperties.GameState; import inf112.fiasko.roborally.elementproperties.Position; import inf112.fiasko.roborally.elementproperties.RobotID; @@ -16,11 +14,8 @@ import inf112.fiasko.roborally.utility.DeckLoaderUtil; import java.io.IOException; import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.concurrent.TimeUnit; /** * This class represent a game which is drawable using libgdx @@ -40,22 +35,6 @@ public class RoboRallyGame implements IRoboRallyGame { private ProgrammingCardDeck playerHand; private Phase phase; - public ProgrammingCardDeck getPlayerHand() { - return playerHand; - } - - public void setPlayerHand(ProgrammingCardDeck playerHand) { - this.playerHand = playerHand; - } - - public List getProgram() { - return program; - } - - public void setProgram(List program) { - this.program = program; - } - /** * Instantiates a new Robo Rally game * @param playerList A list of all the players participating in the game @@ -81,9 +60,6 @@ public class RoboRallyGame implements IRoboRallyGame { this.phase = new Phase(gameBoard, playerList, 600, this); } - - - /** * Instantiates a new Robo Rally game * @param playerList A list of all the players participating in the game @@ -144,22 +120,73 @@ public class RoboRallyGame implements IRoboRallyGame { this.gameState = gameState; } - /** - * Gets the name of the player playing this instance of the game - * @return The name of this player - */ - public String getPlayerName() { - return playerName; + @Override + public ProgrammingCardDeck getPlayerHand() { + return playerHand; } - /** - * Sets the name of the player playing this instance of the game - * @param playerName The new name of this player - */ - public void setPlayerName(String playerName) { - this.playerName = playerName; + @Override + public void setPlayerHand(ProgrammingCardDeck playerHand) { + this.playerHand = playerHand; } + @Override + public List getProgram() { + return program; + } + + @Override + public int getProgramSize() { + Player player = getPlayerFromName(playerName); + if (player != null) { + return Math.min(5, 5 - gameBoard.getRobotDamage(player.getRobotID()) + 4); + } + return -1; + } + + @Override + public void setProgram(List program) { + this.program = program; + } + + @Override + public void receiveAllPrograms(ProgamsContainer programs) throws InterruptedException { + //Reads data from server and updates player objects + Map> programMap = programs.getProgram(); + Map powerDown = programs.getPowerdown(); + String playerName; + for (Player player : playerList) { + playerName = player.getName(); + player.setInProgram(programMap.get(playerName)); + player.setPowerDownNextRound(powerDown.get(playerName)); + } + //Runs 5 phases + setGameState(GameState.RUNNING_PROGRAMS); + phase.runPhase(1); + phase.runPhase(2); + phase.runPhase(3); + phase.runPhase(4); + phase.runPhase(5); + + // Repair robots on repair tiles + repairAllRobotsOnRepairTiles(); + //Updates the host's card deck + if (host) { + updateLockedProgrammingCardsForAllPlayers(); + removeNonLockedProgrammingCardsFromPlayers(); + } + sendAllDeadPlayersToServer(); + // TODO: If this player is in power down, ask if it shall continue + } + + @Override + public void receiveStayInPowerDown(PowerdownContainer powerDowns) { + for (Player player : playerList) { + player.setPowerDownNextRound(powerDowns.getPowerdown().get(player.getName())); + } + respawnRobots(); + resetHasTouchedFlagThisTurnForAllRobots(); + } /** * Gets the name of the player that won the game @@ -211,75 +238,25 @@ public class RoboRallyGame implements IRoboRallyGame { mainDeck = DeckLoaderUtil.loadProgrammingCardsDeck(); } - new Thread(() -> { - try { - runTurn(); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - }).start(); + new Thread(this::runTurn).start(); } catch (IOException e) { e.printStackTrace(); } } - /** - * Gets a player object given a player name - * @param name The name of the player to get - * @return The corresponding player object or null if no such object exists - */ - private Player getPlayerFromName(String name) { - for (Player player : playerList) { - if (player.getName().equals(name)) { - return player; - } - } - return null; - } - - public int getProgramSize(){ - Player player = getPlayerFromName(playerName); - if (player != null) { - return Math.min(5, 5 - gameBoard.getRobotDamage(player.getRobotID()) + 4); - } - return -1; - } - /** * Runs all the steps of one turn in the game - * @throws InterruptedException If interrupted while trying to sleep */ - private void runTurn() throws InterruptedException { - // The method should follow this sequence: - /* - Tilegne programeringskort - - Programmer roboten - - Gå i power down - - Kjør 5 faser - - Flagg + reprasjonstiles reparerer - - Fjerner ulåste programmeringskort - - Spør om de i power down skal fortsette i power down - - Respawn roboter - */ - + private void runTurn() { // Sets the power down status to true on robots that have players who planned one this turn. // Resets players power down for next turn to false. updateRobotPowerDown(); // Set damage of robots in power down to 0 gameBoard.executePowerdown(); if (host) { + //Distributes programming cards for all players, and sends a deck to each player distributeProgrammingCardsToPlayers(); - if (server == null) { - System.out.println("Serveren er null"); - } - for (Connection connection: server.getPlayerNames().keySet()) { + for (Connection connection : server.getPlayerNames().keySet()) { String playerName = server.getPlayerNames().get(connection); Player player = getPlayerFromName(playerName); if (player != null && player.getPlayerDeck() != null) { @@ -287,50 +264,7 @@ public class RoboRallyGame implements IRoboRallyGame { } } } - setGameState(GameState.JUST_BEFORE_CHOOSING_CARDS); - - // TODO: Make program for this player, if not in power down - // TODO: Ask player for new power down - // Run the phases of the game - - - // TODO: If this player is in power down, ask if it shall continue - // Respawn dead robots, as long as they have more lives left - } - - public void recivedStayInPowerdown(PowerdownContainer powerdowns){ - for (Player player:playerList) { - player.setPowerDownNextRound(powerdowns.getPowerdown().get(player.getName())); - } - respawnRobots(); - resetHasTouchedFlagThisTurnForAllRobots(); - } - - public void reciveAllProgrammes(ProgamsContainer programs) throws InterruptedException { - Map> progs = programs.getProgram(); - Map powerdown = programs.getPowerdown(); - String playername; - for (Player player:playerList) { - playername = player.getName(); - player.setInProgram(progs.get(playername)); - player.setPowerDownNextRound(powerdown.get(playername)); - } - setGameState(GameState.RUNNING_PROGRAMS); - phase.runPhase(1); - phase.runPhase(2); - phase.runPhase(3); - phase.runPhase(4); - phase.runPhase(5); - - // Repair robots on repair tiles - repairAllRobotsOnRepairTiles(); - if (host) { - updateLockedProgrammingCardsForAllPlayers(); - removeNonLockedProgrammingCardsFromPlayers(); - } - // TODO: If this player is in power down, ask if it shall continue - - + setGameState(GameState.LOADING); } /** @@ -472,4 +406,18 @@ public class RoboRallyGame implements IRoboRallyGame { private void setRobotPowerDown(Player player, Boolean powerDownStatus) { gameBoard.setPowerDown(player.getRobotID(), powerDownStatus); } + + /** + * Gets a player object given a player name + * @param name The name of the player to get + * @return The corresponding player object or null if no such object exists + */ + private Player getPlayerFromName(String name) { + for (Player player : playerList) { + if (player.getName().equals(name)) { + return player; + } + } + return null; + } } \ No newline at end of file