Fikser noen problemer

Fikser et problem der kortvelgingsmenyen ikke dukker opp på når sending av elementer går for raskt
Fikser tilstand til kortvelgingsskjermen slik at en ikke mister valgte kort når en ser på brettet
Fikser problemer med å fjerne døde spillere
Avslutter spillet på korrekt måte når alle spillere er døde
Fjerner udp portnummer siden UDP ikke blir brukt
This commit is contained in:
Kristian Knarvik 2020-04-20 21:43:47 +02:00
parent 13730dd592
commit 17a3d4f27a
17 changed files with 143 additions and 107 deletions

View File

@ -40,6 +40,10 @@ public enum GameState {
/**
* Indicates that the game is currently waiting for something
*/
LOADING
LOADING,
/**
* Indicates that the game is no longer running
*/
EXITED
}

View File

@ -23,9 +23,28 @@ public class ScreenManager {
private WinnerScreen winnerScreen;
private CardChoiceScreen cardChoiceScreen;
/**
* Gets a new instance of the card choice screen
*
* @param roboRallyWrapper The Robo Rally launcher instance to use
* @return A new card choice screen instance
*/
public synchronized CardChoiceScreen getNewCardChoiceScreen(RoboRallyWrapper roboRallyWrapper) {
this.cardChoiceScreen = new CardChoiceScreen(roboRallyWrapper);
return cardChoiceScreen;
}
/**
* Gets an instance of the card choice screen
*
* @param roboRallyWrapper The Robo Rally launcher instance to use
* @return A card choice screen instance
*/
public synchronized CardChoiceScreen getCardChoiceScreen(RoboRallyWrapper roboRallyWrapper) {
return new CardChoiceScreen(roboRallyWrapper);
if (this.cardChoiceScreen == null) {
this.cardChoiceScreen = new CardChoiceScreen(roboRallyWrapper);
}
return cardChoiceScreen;
}
/**

View File

@ -77,14 +77,20 @@ public class BoardActiveScreen extends AbstractScreen implements InputProcessor
roboRallyWrapper.batch.end();
// Checks if there has been found a winning player and then changes the screen to display the winning screen
if (roboRallyWrapper.roboRallyGame.getGameState() == GameState.GAME_IS_WON) {
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getWinnerScreen(roboRallyWrapper));
} else if (roboRallyWrapper.roboRallyGame.getGameState() == GameState.CHOOSING_STAY_IN_POWER_DOWN) {
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getPowerDownScreen(roboRallyWrapper));
} else if (roboRallyWrapper.roboRallyGame.getGameState() == GameState.LOADING){
roboRallyWrapper.client.sendElement(false);
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getLoadingScreen(roboRallyWrapper));
switch (roboRallyWrapper.roboRallyGame.getGameState()) {
case GAME_IS_WON:
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getWinnerScreen(roboRallyWrapper));
break;
case CHOOSING_STAY_IN_POWER_DOWN:
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getPowerDownScreen(roboRallyWrapper));
break;
case LOADING:
roboRallyWrapper.client.sendElement(false);
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getLoadingScreen(roboRallyWrapper));
break;
default:
//Ignored
break;
}
}

View File

@ -63,7 +63,10 @@ public class LoadingScreen extends AbstractScreen {
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getLoadingScreen(this.roboRallyWrapper));
break;
case CHOOSING_CARDS:
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getCardChoiceScreen(this.roboRallyWrapper));
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getNewCardChoiceScreen(this.roboRallyWrapper));
break;
case EXITED:
roboRallyWrapper.quit("All players died. Cannot continue playing.");
break;
default:
System.out.println("The loading screen doesn't know what to do with " + gameState);

View File

@ -84,17 +84,18 @@ public class PowerDownScreen extends AbstractScreen {
switch (roboRallyWrapper.roboRallyGame.getGameState()) {
case CHOOSING_STAY_IN_POWER_DOWN:
roboRallyWrapper.roboRallyGame.setGameState(GameState.TURN_CLEANUP);
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getLoadingScreen(this.roboRallyWrapper));
roboRallyWrapper.client.sendElement(bool);
break;
case CHOOSING_POWER_DOWN:
roboRallyWrapper.roboRallyGame.setGameState(GameState.LOADING);
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getLoadingScreen(this.roboRallyWrapper));
roboRallyWrapper.client.sendElement(new ProgramAndPowerdownRequest(bool,
roboRallyWrapper.roboRallyGame.getProgram()));
break;
default:
throw new IllegalStateException("The game is in an unexpected state. Cannot continue.");
}
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getLoadingScreen(this.roboRallyWrapper));
}
@Override

View File

@ -23,7 +23,7 @@ public class RoboRallyClient {
client = new Client();
client.start();
NetworkUtil.registerClasses(client.getKryo());
client.connect(5000, ipAddress, 54555, 54777);
client.connect(5000, ipAddress, 54555);
client.addListener(new RoboRallyClientListener(wrapper));
}
@ -35,9 +35,6 @@ public class RoboRallyClient {
*/
public void sendElement(Object object) {
try {
if(!client.isConnected()){
client.reconnect();
}
client.sendTCP(object);
} catch (Exception e) {
e.printStackTrace();

View File

@ -39,20 +39,20 @@ class RoboRallyClientListener extends Listener {
GameStartInfo info = (GameStartInfo) object;
wrapper.roboRallyGame = new RoboRallyGame(info.getPlayerList(), info.getBoardName(),
wrapper.server != null, info.getPlayerName(), wrapper.server);
} else if (object instanceof ProgrammingCardDeck) {
wrapper.roboRallyGame.setGameState(GameState.CHOOSING_CARDS);
new Thread(() -> wrapper.roboRallyGame.setPlayerHand((ProgrammingCardDeck) object)).start();
} else if (object instanceof ProgrammingCardDeck) {
wrapper.roboRallyGame.setGameState(GameState.CHOOSING_CARDS);
new Thread(() -> wrapper.roboRallyGame.setPlayerHand((ProgrammingCardDeck) object)).start();
} else if (object instanceof ProgamsContainer) {
new Thread(() -> {
try {
wrapper.roboRallyGame.receiveAllPrograms((ProgamsContainer) object);
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
} else if (object instanceof PowerDownContainer) {
wrapper.roboRallyGame.receiveAllPrograms((ProgamsContainer) object);
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
} else if (object instanceof PowerDownContainer) {
new Thread(() -> wrapper.roboRallyGame.receiveStayInPowerDown((PowerDownContainer) object)).start();
}
}
}
}

View File

@ -25,7 +25,7 @@ public class RoboRallyServer {
server = new Server();
server.start();
NetworkUtil.registerClasses(server.getKryo());
server.bind(54555, 54777);
server.bind(54555);
listener = new RoboRallyServerListener(this);
server.addListener(listener);
}

View File

@ -13,7 +13,9 @@ public class PowerDownContainer {
* DO NOT USE!
* Kryonet demands a no args constructor or else it throws a exception
*/
public PowerDownContainer(){}
public PowerDownContainer() {
}
/**
* Instantiates a new power down container
*

View File

@ -164,34 +164,35 @@ public class Board {
* @param powerDown The status of the power down
*/
public void setPowerDown(RobotID robotID, Boolean powerDown) {
if(robots.containsKey(robotID)){
if (robots.containsKey(robotID)) {
robots.get(robotID).setPowerDown(powerDown);
}
else if (getRobotFromDeadRobots(robotID)!=null) {
} else if (getRobotFromDeadRobots(robotID) != null) {
getRobotFromDeadRobots(robotID).setPowerDown(powerDown);
}
}
public void setBackupPositionOfRobot(RobotID robotID, Position pos){
public void setBackupPositionOfRobot(RobotID robotID, Position pos) {
robots.get(robotID).setBackupPosition(pos);
}
/**
* Gets the power down status of the robot
*
* @param robotID The robot id of the robot
* @return The power down status of the robot
*/
public boolean getPowerDown(RobotID robotID) {
if(robots.containsKey(robotID)){
if (robots.containsKey(robotID)) {
return robots.get(robotID).isInPowerDown();
}
else if (getRobotFromDeadRobots(robotID)!=null){
return getRobotFromDeadRobots(robotID).isInPowerDown();
} else if (getRobotFromDeadRobots(robotID) != null) {
return getRobotFromDeadRobots(robotID).isInPowerDown();
}
return false;
}
private Robot getRobotFromDeadRobots(RobotID robotID){
for (Robot robot:deadRobots) {
if (robot.getRobotId()==robotID){
private Robot getRobotFromDeadRobots(RobotID robotID) {
for (Robot robot : deadRobots) {
if (robot.getRobotId() == robotID) {
return robot;
}
}
@ -445,7 +446,7 @@ public class Board {
int flagNr = flagID.getTileTypeID() % 16;
if (flagNr - 1 == robot.getLastFlagVisited()) {
robot.setLastFlagVisited(flagNr);
setHasTouchedFlagThisTurnFromRobotID(robotID, true);
setHasTouchedFlagThisTurn(robotID, true);
}
}
@ -828,7 +829,7 @@ public class Board {
* @param robotID The robot to be checked
* @return The flag last visited in a number
*/
public int getLastFlagVisitedFromRobotID(RobotID robotID) {
public int getLastFlagVisited(RobotID robotID) {
return robots.get(robotID).getLastFlagVisited();
}
@ -838,7 +839,7 @@ public class Board {
* @param robotID The robot to be checked
* @param hasTouched If the robot has touched a flag this turn
*/
public void setHasTouchedFlagThisTurnFromRobotID(RobotID robotID, boolean hasTouched) {
public void setHasTouchedFlagThisTurn(RobotID robotID, boolean hasTouched) {
robots.get(robotID).setHasTouchedFlagThisTurn(hasTouched);
}
@ -848,7 +849,7 @@ public class Board {
* @param robotID The robot to be checked
* @return If the robot has touched a flag this turn
*/
public boolean isHasTouchedFlagThisTurnFromRobotID(RobotID robotID) {
public boolean hasTouchedFlagThisTurn(RobotID robotID) {
return robots.get(robotID).hasTouchedFlagThisTurn();
}

View File

@ -31,6 +31,13 @@ public interface InteractableGame {
*/
String getWinningPlayerName();
/**
* Sets the name of the player that won the game
*
* @param winningPlayerName The player winning the game
*/
void setWinningPlayerName(String winningPlayerName);
/**
* Continues turn when programs for all players are received from the server
*
@ -81,11 +88,4 @@ public interface InteractableGame {
*/
void setProgram(List<ProgrammingCard> program);
/**
* Sets the name of the player that won the game
*
* @param winningPlayerName The player winning the game
*/
void setWinningPlayerName(String winningPlayerName);
}

View File

@ -69,7 +69,7 @@ public class Phase {
continue;
}
RobotID robotID = gameBoard.getRobotOnPosition(flagPosition);
if (gameBoard.isHasTouchedFlagThisTurnFromRobotID(robotID)) {
if (gameBoard.hasTouchedFlagThisTurn(robotID)) {
continue;
}
gameBoard.updateFlagOnRobot(robotID, flag.getElement().getTileType());
@ -79,6 +79,7 @@ public class Phase {
/**
* Fires all lasers on the game board
*
* @throws InterruptedException If it gets interrupted while trying to sleep
*/
public void fireAllLasers() throws InterruptedException {
@ -197,7 +198,7 @@ public class Phase {
* @param numberOfFlags The number of flags on the map
*/
private void checkIfPlayerWon(RobotID robotID, int numberOfFlags) {
if (victoryCheck(gameBoard.getLastFlagVisitedFromRobotID(robotID), numberOfFlags)) {
if (victoryCheck(gameBoard.getLastFlagVisited(robotID), numberOfFlags)) {
for (Player player : playerList) {
if (player.getRobotID() != robotID) {
continue;

View File

@ -71,6 +71,19 @@ public class Player {
return program;
}
/**
* Sets the Players program to the given list of programing cards
*
* @param cardList list the size of 5 with programing cards
*/
public void setProgram(List<ProgrammingCard> cardList) {
if (cardList.size() != 5) {
throw new IllegalArgumentException("The program must contain exactly 5 cards.");
} else {
program = new ArrayList<>(cardList);
}
}
/**
* Gives you the player hand/deck
*
@ -116,17 +129,4 @@ public class Player {
this.powerDownNextRound = powerDownStatus;
}
/**
* Sets the Players program to the given list of programing cards
*
* @param cardList list the size of 5 with programing cards
*/
public void setProgram(List<ProgrammingCard> cardList) {
if (cardList.size() != 5) {
throw new IllegalArgumentException("The program must contain exactly 5 cards.");
} else {
program = new ArrayList<>(cardList);
}
}
}

View File

@ -169,22 +169,17 @@ public class RoboRallyGame implements DrawableGame, InteractableGame {
//Updates the host's card deck
if (host) {
//TODO: Fix updateLockedProgrammingCardsForAllPlayers throwing NullPointerException on robotDamage
updateLockedProgrammingCardsForAllPlayers();
removeNonLockedProgrammingCardsFromPlayers();
}
sendAllDeadPlayersToServer();
// TODO: If this player is in power down, ask if it shall continue
if(gameBoard.getPowerDown(getPlayerFromName(this.playerName).getRobotID())){
if (gameBoard.getPowerDown(getPlayerFromName(this.playerName).getRobotID())) {
setGameState(GameState.CHOOSING_STAY_IN_POWER_DOWN);
} else {
setGameState(GameState.LOADING);
}
updateRobotPowerDown();
//updateRobotPowerDown(); Should this be here?
}
@Override
@ -193,6 +188,7 @@ public class RoboRallyGame implements DrawableGame, InteractableGame {
player.setPowerDownNextRound(powerDowns.getPowerDown().get(player.getName()));
}
respawnRobots();
sendAllDeadPlayersToServer();
resetHasTouchedFlagThisTurnForAllRobots();
setGameState(GameState.BEGINNING_OF_GAME);
runTurn();
@ -285,6 +281,7 @@ public class RoboRallyGame implements DrawableGame, InteractableGame {
updateRobotPowerDown();
// Set damage of robots in power down to 0
gameBoard.executePowerDown();
setGameState(GameState.LOADING);
if (host) {
//Distributes programming cards for all players, and sends a deck to each player
distributeProgrammingCardsToPlayers();
@ -296,7 +293,6 @@ public class RoboRallyGame implements DrawableGame, InteractableGame {
}
}
}
setGameState(GameState.LOADING);
}
/**
@ -306,9 +302,15 @@ public class RoboRallyGame implements DrawableGame, InteractableGame {
if (host) {
server.setDeadPlayers(gameBoard.getRealDeadRobots());
}
//Removes dead players from playerList
playerList.removeIf((player) -> gameBoard.getRealDeadRobots().contains(player.getRobotID()));
if(playerList.isEmpty()){
System.exit(1);
if (playerList.isEmpty()) {
setGameState(GameState.EXITED);
try {
Thread.sleep(100000000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

View File

@ -31,14 +31,6 @@ public class Robot {
this.facingDirection = Direction.NORTH;
}
/**
* setBackupPosition
* @param backupPosition
*/
public void setBackupPosition(Position backupPosition) {
this.backupPosition = backupPosition;
}
/**
* True if the robot has touched a flag in the current turn
*
@ -112,15 +104,6 @@ public class Robot {
return inPowerDown;
}
/**
* Set the robot's last visited flag to the new flag and places its backup on the flag's position
*
* @param currentFlag The flag the robot is standing on
*/
public void setLastFlagVisited(int currentFlag) {
this.lastFlagVisited = currentFlag;
}
/**
* Gets the last flag the robot visited
*
@ -130,6 +113,15 @@ public class Robot {
return lastFlagVisited;
}
/**
* Set the robot's last visited flag to the new flag and places its backup on the flag's position
*
* @param currentFlag The flag the robot is standing on
*/
public void setLastFlagVisited(int currentFlag) {
this.lastFlagVisited = currentFlag;
}
/**
* Gets the robot's backup position
*
@ -139,6 +131,15 @@ public class Robot {
return backupPosition;
}
/**
* setBackupPosition
*
* @param backupPosition
*/
public void setBackupPosition(Position backupPosition) {
this.backupPosition = backupPosition;
}
/**
* Gets the robot ID
*

View File

@ -24,6 +24,11 @@ public class FakeGame implements InteractableGame {
return winningPlayerName;
}
@Override
public void setWinningPlayerName(String winningPlayerName) {
this.winningPlayerName = winningPlayerName;
}
@Override
public void receiveAllPrograms(ProgamsContainer programs) throws InterruptedException {
//Not needed for testing
@ -58,9 +63,4 @@ public class FakeGame implements InteractableGame {
public void setProgram(List<ProgrammingCard> program) {
//Not needed for testing
}
@Override
public void setWinningPlayerName(String winningPlayerName) {
this.winningPlayerName = winningPlayerName;
}
}

View File

@ -14,14 +14,15 @@ import java.util.List;
import static junit.framework.TestCase.*;
public class PhaseTest {
List<Robot> robots = new ArrayList<>();
private Phase phase;
private Board board;
private Position robot1Position = new Position(2, 2);
private Position robot2Position = new Position(3, 2);
private Position robot3Position = new Position(7 , 2);
private Position robot4Position = new Position(3 , 8);
private Position robot5Position = new Position(2 , 14);
private Position robot6Position = new Position(2 , 15);
private Position robot3Position = new Position(7, 2);
private Position robot4Position = new Position(3, 8);
private Position robot5Position = new Position(2, 14);
private Position robot6Position = new Position(2, 15);
private Robot robot1 = new Robot(RobotID.ROBOT_1, robot1Position);
private Robot robot2 = new Robot(RobotID.ROBOT_2, robot2Position);
private Robot robot3 = new Robot(RobotID.ROBOT_3, robot3Position);
@ -35,8 +36,6 @@ public class PhaseTest {
private Player player5 = new Player(RobotID.ROBOT_5, "Player 5");
private Player player6 = new Player(RobotID.ROBOT_6, "Player 6");
List<Robot> robots = new ArrayList<>();
@Before
public void setUp() {
robots.add(robot1);
@ -72,7 +71,7 @@ public class PhaseTest {
FakeGame testGame = new FakeGame();
List<Robot> robot = new ArrayList<>();
List<Player> player = new ArrayList<>();
robot.add(new Robot(RobotID.ROBOT_1, new Position(0,0)));
robot.add(new Robot(RobotID.ROBOT_1, new Position(0, 0)));
player.add(new Player(RobotID.ROBOT_1, "Player 1"));
try {
@ -86,7 +85,7 @@ public class PhaseTest {
}
@Test
public void robotRegistersFlagWhenOnCorrectOne() {
public void robotRegistersFlagWhenOnCorrectOne() {
assertEquals(robot3.getLastFlagVisited(), 0);
assertFalse(robot3.hasTouchedFlagThisTurn());
phase.checkAllFlags();
@ -107,7 +106,7 @@ public class PhaseTest {
public void actionDoesPerformAnAction() throws InterruptedException {
assertEquals(robot4.getRobotId(), board.getRobotOnPosition(robot4Position));
phase.makeMove(RobotID.ROBOT_4, Action.MOVE_1);
assertEquals(robot4.getRobotId(),board.getRobotOnPosition(new Position(3, 7)));
assertEquals(robot4.getRobotId(), board.getRobotOnPosition(new Position(3, 7)));
}
@Test
@ -126,7 +125,7 @@ public class PhaseTest {
ProgrammingCard card9 = new ProgrammingCard(13, Action.MOVE_1);
ProgrammingCard card10 = new ProgrammingCard(14, Action.MOVE_1);
ProgrammingCard card11 = new ProgrammingCard(100, Action.ROTATE_LEFT);
ProgrammingCard card12= new ProgrammingCard(200, Action.ROTATE_LEFT);
ProgrammingCard card12 = new ProgrammingCard(200, Action.ROTATE_LEFT);
ProgrammingCard card13 = new ProgrammingCard(300, Action.ROTATE_LEFT);
ProgrammingCard card14 = new ProgrammingCard(400, Action.ROTATE_LEFT);
ProgrammingCard card15 = new ProgrammingCard(500, Action.ROTATE_LEFT);