Fikser rekkefølge for metoder og flytter kommentarer fra klasse til interface

This commit is contained in:
Kristian Knarvik 2020-04-17 11:43:47 +02:00
parent c7710deac5
commit 29261f037f
2 changed files with 119 additions and 144 deletions

View File

@ -28,20 +28,47 @@ public interface IInteractableGame {
*/ */
String getWinningPlayerName(); 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); /**
* Continues turn when stay in power down is received from all players
List<ProgrammingCard> getProgram(); * @param powerDowns The power down container received from the server
*/
int getProgramSize(); void receiveStayInPowerDown(PowerdownContainer powerDowns);
void setPlayerHand(ProgrammingCardDeck playerHand);
/**
* Gets the hand of this player
* @return The hand of this player
*/
ProgrammingCardDeck getPlayerHand(); 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<ProgrammingCard> getProgram();
/**
* Sets the program of this player
* @param program The program of this player
*/
void setProgram(List<ProgrammingCard> program); void setProgram(List<ProgrammingCard> program);
} }

View File

@ -1,8 +1,6 @@
package inf112.fiasko.roborally.objects; package inf112.fiasko.roborally.objects;
import com.esotericsoftware.kryonet.Connection; 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.GameState;
import inf112.fiasko.roborally.elementproperties.Position; import inf112.fiasko.roborally.elementproperties.Position;
import inf112.fiasko.roborally.elementproperties.RobotID; import inf112.fiasko.roborally.elementproperties.RobotID;
@ -16,11 +14,8 @@ import inf112.fiasko.roborally.utility.DeckLoaderUtil;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit;
/** /**
* This class represent a game which is drawable using libgdx * This class represent a game which is drawable using libgdx
@ -40,22 +35,6 @@ public class RoboRallyGame implements IRoboRallyGame {
private ProgrammingCardDeck playerHand; private ProgrammingCardDeck playerHand;
private Phase phase; private Phase phase;
public ProgrammingCardDeck getPlayerHand() {
return playerHand;
}
public void setPlayerHand(ProgrammingCardDeck playerHand) {
this.playerHand = playerHand;
}
public List<ProgrammingCard> getProgram() {
return program;
}
public void setProgram(List<ProgrammingCard> program) {
this.program = program;
}
/** /**
* 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 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); this.phase = new Phase(gameBoard, playerList, 600, this);
} }
/** /**
* 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 playerList A list of all the players participating in the game
@ -144,22 +120,73 @@ public class RoboRallyGame implements IRoboRallyGame {
this.gameState = gameState; this.gameState = gameState;
} }
/** @Override
* Gets the name of the player playing this instance of the game public ProgrammingCardDeck getPlayerHand() {
* @return The name of this player return playerHand;
*/
public String getPlayerName() {
return playerName;
} }
/** @Override
* Sets the name of the player playing this instance of the game public void setPlayerHand(ProgrammingCardDeck playerHand) {
* @param playerName The new name of this player this.playerHand = playerHand;
*/
public void setPlayerName(String playerName) {
this.playerName = playerName;
} }
@Override
public List<ProgrammingCard> 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<ProgrammingCard> program) {
this.program = program;
}
@Override
public void receiveAllPrograms(ProgamsContainer programs) throws InterruptedException {
//Reads data from server and updates player objects
Map<String, List<ProgrammingCard>> programMap = programs.getProgram();
Map<String, Boolean> 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 * Gets the name of the player that won the game
@ -211,75 +238,25 @@ public class RoboRallyGame implements IRoboRallyGame {
mainDeck = DeckLoaderUtil.loadProgrammingCardsDeck(); mainDeck = DeckLoaderUtil.loadProgrammingCardsDeck();
} }
new Thread(() -> { new Thread(this::runTurn).start();
try {
runTurn();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}).start();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); 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 * Runs all the steps of one turn in the game
* @throws InterruptedException If interrupted while trying to sleep
*/ */
private void runTurn() throws InterruptedException { private void runTurn() {
// The method should follow this sequence:
/*
Tilegne programeringskort
Programmer roboten
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
*/
// Sets the power down status to true on robots that have players who planned one this turn. // 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. // Resets players power down for next turn to false.
updateRobotPowerDown(); updateRobotPowerDown();
// Set damage of robots in power down to 0 // Set damage of robots in power down to 0
gameBoard.executePowerdown(); gameBoard.executePowerdown();
if (host) { if (host) {
//Distributes programming cards for all players, and sends a deck to each player
distributeProgrammingCardsToPlayers(); distributeProgrammingCardsToPlayers();
if (server == null) { for (Connection connection : server.getPlayerNames().keySet()) {
System.out.println("Serveren er null");
}
for (Connection connection: server.getPlayerNames().keySet()) {
String playerName = server.getPlayerNames().get(connection); String playerName = server.getPlayerNames().get(connection);
Player player = getPlayerFromName(playerName); Player player = getPlayerFromName(playerName);
if (player != null && player.getPlayerDeck() != null) { if (player != null && player.getPlayerDeck() != null) {
@ -287,50 +264,7 @@ public class RoboRallyGame implements IRoboRallyGame {
} }
} }
} }
setGameState(GameState.JUST_BEFORE_CHOOSING_CARDS); setGameState(GameState.LOADING);
// 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<String,List<ProgrammingCard>> progs = programs.getProgram();
Map<String,Boolean> 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
} }
/** /**
@ -472,4 +406,18 @@ public class RoboRallyGame implements IRoboRallyGame {
private void setRobotPowerDown(Player player, Boolean powerDownStatus) { private void setRobotPowerDown(Player player, Boolean powerDownStatus) {
gameBoard.setPowerDown(player.getRobotID(), 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;
}
} }