From b5eae63892f7eeb764dd79286ed29f2ab9e05f7e Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Thu, 16 Apr 2020 11:46:07 +0200 Subject: [PATCH] Legger til og forbedrer kommentarer i RoboRallyGame --- .../roborally/objects/RoboRallyGame.java | 142 +++++++++++------- 1 file changed, 89 insertions(+), 53 deletions(-) diff --git a/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java b/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java index 5cbd62f..1ec8c9c 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java +++ b/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java @@ -29,6 +29,7 @@ public class RoboRallyGame implements IRoboRallyGame { private List> conveyorBelts; private List> fastConveyorBelts; private List> repairTiles; + private List> flags; private final List playerList; private final boolean host; private Deck mainDeck; @@ -38,50 +39,19 @@ public class RoboRallyGame implements IRoboRallyGame { private RoboRallyServer server; private String winningPlayerName; - - - public String getPlayerName() { - return playerName; - } - - public void setPlayerName(String playerName) { + /** + * Instantiates a new Robo Rally game + * @param playerList A list of all the players participating in the game + * @param boardName The playerName of the board to use + * @param host Whether this player is the host + * @param playerName The name of the player of this instance of the game + * @param client The client used to send data to the server + * @param server The server if this player is host. Should be null otherwise + * @param debug Whether this game is to use the debugging board + */ + public RoboRallyGame(List playerList, String boardName, boolean host, String playerName, + RoboRallyClient client, RoboRallyServer server, boolean debug) { this.playerName = playerName; - } - - - public String getWinningPlayerName() { - return winningPlayerName; - } - - public void setWinningPlayerName(String winningPlayerName) { - this.winningPlayerName = winningPlayerName; - } - - /** - * Returns the gameState of the game - * @return the gameState of the game - */ - @Override - public GameState getGameState(){ - return gameState; - } - - /** - * Sets the gameState of the game - * @param gameState the gameState - */ - @Override - public void setGameState(GameState gameState) { - this.gameState = gameState; - } - - /** - * Instantiates a new robo rally game - * @param debug Whether to start the game in debugging mode - */ - public RoboRallyGame(List playerList, String boardName, boolean host, String name, RoboRallyClient client, - RoboRallyServer server, boolean debug) { - this.playerName = name; this.host = host; this.playerList = playerList; this.client = client; @@ -94,10 +64,16 @@ public class RoboRallyGame implements IRoboRallyGame { } /** - * Instantiates a new robo rally game + * Instantiates a new Robo Rally game + * @param playerList A list of all the players participating in the game + * @param boardName The playerName of the board to use + * @param host Whether this player is the host + * @param playerName The name of the player of this instance of the game + * @param client The client used to send data to the server + * @param server The server if this player is host. Should be null otherwise */ - public RoboRallyGame(List playerList, String boardName, boolean host, String playerName, RoboRallyClient client, - RoboRallyServer server) { + public RoboRallyGame(List playerList, String boardName, boolean host, String playerName, + RoboRallyClient client, RoboRallyServer server) { this.playerName = playerName; this.host = host; this.playerList = playerList; @@ -136,6 +112,41 @@ public class RoboRallyGame implements IRoboRallyGame { return gameBoard.getAliveRobots(); } + @Override + public GameState getGameState(){ + return gameState; + } + + @Override + public void setGameState(GameState gameState) { + 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; + } + + /** + * 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; + } + + + /** + * Gets the name of the player that won the game + * @return The name of the winning player + */ + public String getWinningPlayerName() { + return winningPlayerName; + } + /** * Makes the game thread wait a given time amount before continuing. * @throws InterruptedException If interrupted while trying to sleep. @@ -218,10 +229,18 @@ public class RoboRallyGame implements IRoboRallyGame { TileType.CONVEYOR_BELT_SLOW_SIDE_ENTRANCES)); repairTiles = gameBoard.getPositionsOfTileOnBoard(TileType.FLAG_1, TileType.FLAG_2, TileType.FLAG_3, TileType.FLAG_4, TileType.WRENCH, TileType.WRENCH_AND_HAMMER); + flags = gameBoard.getPositionsOfTileOnBoard(TileType.FLAG_1, + TileType.FLAG_2, TileType.FLAG_3, TileType.FLAG_4); } - private Player getPlayerFromName(String name){ - for (Player player:playerList) { - if(player.getName().equals(name)){ + + /** + * 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; } } @@ -295,6 +314,9 @@ public class RoboRallyGame implements IRoboRallyGame { resetHasTouchedFlagThisTurnForAllRobots(); } + /** + * Sends information about players no longer part of the game to the server + */ private void sendAllDeadPlayersToServer() { if (host) { server.setDeadPlayers(gameBoard.getRealDeadRobots()); @@ -385,6 +407,14 @@ public class RoboRallyGame implements IRoboRallyGame { } } + /** + * Sets the name of the player that won the game + * @param winningPlayerName The player winning the game + */ + private void setWinningPlayerName(String winningPlayerName) { + this.winningPlayerName = winningPlayerName; + } + /** * Runs one phase as defined in the Robo Rally rulebook * @param phaseNumber The number of the phase to run @@ -550,9 +580,7 @@ public class RoboRallyGame implements IRoboRallyGame { * Checks all flags for robots. Tries to update the flag of the robot. */ private void checkAllFlags() { - List> listOfFlags = gameBoard.getPositionsOfTileOnBoard(TileType.FLAG_1, - TileType.FLAG_2, TileType.FLAG_3, TileType.FLAG_4); - for (BoardElementContainer flag:listOfFlags) { + for (BoardElementContainer flag:flags) { Position flagPosition = flag.getPosition(); if (gameBoard.hasRobotOnPosition(flagPosition)) { RobotID robotID = gameBoard.getRobotOnPosition(flagPosition); @@ -562,7 +590,7 @@ public class RoboRallyGame implements IRoboRallyGame { } gameBoard.updateFlagOnRobot(robotID, flag.getElement().getTileType()); robot.setHasTouchedFlagThisTurn(true); - if (victoryCheck(robot.getLastFlagVisited(), listOfFlags.size())) { + if (victoryCheck(robot.getLastFlagVisited(), flags.size())) { for (Player player : playerList) { if (player.getRobotID() != robotID) { continue; @@ -576,9 +604,17 @@ public class RoboRallyGame implements IRoboRallyGame { } } + /** + * Checks whether a player has won + * @param lastFlagVisited The last flag the player visited + * @param lastFlag The last flag of the board + * @return True if the player has visited the last flag + */ private boolean victoryCheck(int lastFlagVisited, int lastFlag) { return (lastFlagVisited == lastFlag); } + + /** * Fires all lasers on the game board */