diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/Game.java b/src/main/java/it/polimi/ingsw/gc14/Model/Game.java index c04e25b..58c09ce 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/Game.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/Game.java @@ -589,26 +589,29 @@ public class Game implements Serializable { { OptionalCardQueue.add(e.getKey()); } - currentState.PlayerUpdate(OptionalCardQueue.poll(), null); - if(currentState.getCurrentPlayer()==null) - { - currentState.GameStageUpdate(GameStages.RESOLVING_EVENT); - if(currentState.getRound()<10) - { - nextRound(); - } - else - { - EventResolution(); - currentState.GameStageUpdate(GameStages.ENDING); - endGame(); - } + Player optionalPlayer = OptionalCardQueue.poll(); + if (optionalPlayer != null) { + currentState.PlayerUpdate(optionalPlayer, null); return; } + + currentState.GameStageUpdate(GameStages.RESOLVING_EVENT); + + if (currentState.getRound() < 10) { + nextRound(); + currentState.PlayerUpdate(orderLogicCard.pull(), null); + currentState.GameStageUpdate(GameStages.SLOT_CHOICE); + } else { + EventResolution(); + currentState.GameStageUpdate(GameStages.ENDING); + endGame(); + } + return; + } } - } + if (GameStages.OPTIONAL_CARD_EFFECT == currentState.getGameStage()) { Player optionalPlayer = OptionalCardQueue.poll(); @@ -619,13 +622,17 @@ public class Game implements Serializable { currentState.GameStageUpdate(GameStages.RESOLVING_EVENT); - if (currentState.getRound() < 10) { - nextRound(); - } else { - EventResolution(); - currentState.GameStageUpdate(GameStages.ENDING); - endGame(); - } + if (currentState.getRound() < 10) { + nextRound(); + currentState.PlayerUpdate(orderLogicCard.pull(), null); + currentState.GameStageUpdate(GameStages.SLOT_CHOICE); + } else { + EventResolution(); + currentState.GameStageUpdate(GameStages.ENDING); + endGame(); + } + + return; } } diff --git a/src/test/java/it/polimi/ingsw/gc14/Model/GameTest.java b/src/test/java/it/polimi/ingsw/gc14/Model/GameTest.java index 4709a9b..9346b9b 100644 --- a/src/test/java/it/polimi/ingsw/gc14/Model/GameTest.java +++ b/src/test/java/it/polimi/ingsw/gc14/Model/GameTest.java @@ -954,32 +954,75 @@ class GameTest { } @Test - void fullGameShouldEndAfterTenRoundsUsingOnlyPublicGameFlow() { - Game game = new Game(3); + void fullGameShouldEndAfterTenRoundsForTwoThreeAndFourPlayersUsingOnlyPublicGameFlow() { + for (int nPlayers : new int[]{2, 3, 4}) { + Game game = new Game(nPlayers); - assertTrue(game.addPlayer(new Player("p1"))); - assertTrue(game.addPlayer(new Player("p2"))); - assertTrue(game.addPlayer(new Player("p3"))); + for (int i = 1; i <= nPlayers; i++) { + assertTrue(game.addPlayer(new Player("p" + nPlayers + "_" + i))); + } - assertEquals(GameStages.SLOT_CHOICE, game.getCurrentState().getGameStage()); + assertEquals(GameStages.SLOT_CHOICE, game.getCurrentState().getGameStage()); + assertEquals(1, game.getCurrentState().getRound()); + assertNotNull(game.getCurrentState().getCurrentPlayer()); - int guard = 0; + int guard = 0; - while (game.getCurrentState().getGameStage() != GameStages.ENDED && guard < 20) { - guard++; + while (game.getCurrentState().getGameStage() != GameStages.ENDED && guard < 15) { + guard++; - playOneFullRound(game); + int roundBefore = game.getCurrentState().getRound(); + + playOneFullRound(game); + + if (roundBefore < 10) { + assertEquals( + GameStages.SLOT_CHOICE, + game.getCurrentState().getGameStage(), + "After a non-final round, the game must return to SLOT_CHOICE." + ); + + assertEquals( + roundBefore + 1, + game.getCurrentState().getRound(), + "The round number must increase by one after each completed round." + ); + + assertNotNull( + game.getCurrentState().getCurrentPlayer(), + "At the beginning of the next round there must be a current player." + ); + } else { + assertEquals( + GameStages.ENDED, + game.getCurrentState().getGameStage(), + "After round 10, the game must end." + ); + + assertEquals( + 10, + game.getCurrentState().getRound(), + "The game must end at round 10." + ); + } + } assertTrue( - game.getCurrentState().getGameStage() == GameStages.SLOT_CHOICE - || game.getCurrentState().getGameStage() == GameStages.ENDED, - "After a full round, the game should either start the next slot choice phase or end." + guard < 15, + "Possible infinite loop while playing the full game with " + nPlayers + " players." + ); + assertEquals( + GameStages.ENDED, + game.getCurrentState().getGameStage(), + "The game must end after round 10 with " + nPlayers + " players." + ); + + assertEquals( + 10, + game.getCurrentState().getRound(), + "The game must end at round 10 with " + nPlayers + " players." ); } - - assertTrue(guard < 20, "Possible infinite loop while playing the full game."); - assertEquals(GameStages.ENDED, game.getCurrentState().getGameStage()); - assertEquals(10, game.getCurrentState().getRound()); } @Test @@ -1039,23 +1082,46 @@ class GameTest { completeSlotChoice(game); resolveActionsUntilOptionalCardEffect(game); - int guard = 0; - - while (game.getCurrentState().getGameStage() == GameStages.OPTIONAL_CARD_EFFECT && guard < 10) { - guard++; - - Player current = game.getCurrentState().getCurrentPlayer(); - assertNotNull(current); - - assertTrue(game.NoOptionalCard(current)); - } - - assertTrue(guard < 10, "Possible infinite loop during optional phase."); + resolveOptionalPhaseIfPresent(game); assertEquals(GameStages.SLOT_CHOICE, game.getCurrentState().getGameStage()); assertEquals(roundBefore + 1, game.getCurrentState().getRound()); assertNotNull(game.getCurrentState().getCurrentPlayer()); } + @Test + void fourPlayerGameShouldAllowPlayingSlotG() { + Game game = new Game(4); + + addPlayers(game, 4, "slot_g_"); + + Player playerOnG = completeSlotChoiceAndAdvanceToPlayerOnSlot(game, 'G'); + + assertNotNull(playerOnG); + assertEquals(GameStages.RESOLVING_ACTIONS, game.getCurrentState().getGameStage()); + assertEquals(playerOnG, game.getCurrentState().getCurrentPlayer()); + assertEquals('G', game.getCurrentState().getSlot().getSlotId()); + + assertEquals(1, game.getCurrentState().getNLower()); + assertEquals(2, game.getCurrentState().getNUpper()); + + resolveOneMandatoryAction(game); + assertEquals(0, game.getCurrentState().getNLower()); + assertEquals(2, game.getCurrentState().getNUpper()); + + resolveOneMandatoryAction(game); + assertEquals(0, game.getCurrentState().getNLower()); + assertEquals(1, game.getCurrentState().getNUpper()); + + resolveOneMandatoryAction(game); + + assertFalse( + game.getCurrentState().getGameStage() == GameStages.RESOLVING_ACTIONS + && playerOnG.equals(game.getCurrentState().getCurrentPlayer()) + && game.getCurrentState().getSlot().getSlotId() == 'G', + "After resolving all actions of slot G, the game must not still be resolving slot G for the same player." + ); + } + } \ No newline at end of file