Fix: GameTest and Game

This commit is contained in:
MatteoPellegrino05
2026-04-28 19:39:48 +02:00
parent 29df367e85
commit d35d6d154c
2 changed files with 124 additions and 51 deletions
@@ -589,26 +589,29 @@ public class Game implements Serializable {
{ {
OptionalCardQueue.add(e.getKey()); OptionalCardQueue.add(e.getKey());
} }
currentState.PlayerUpdate(OptionalCardQueue.poll(), null); Player optionalPlayer = OptionalCardQueue.poll();
if(currentState.getCurrentPlayer()==null)
{
currentState.GameStageUpdate(GameStages.RESOLVING_EVENT);
if(currentState.getRound()<10)
{
nextRound();
}
else
{
EventResolution();
currentState.GameStageUpdate(GameStages.ENDING);
endGame();
}
if (optionalPlayer != null) {
currentState.PlayerUpdate(optionalPlayer, null);
return; 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; return;
}
} }
}
if (GameStages.OPTIONAL_CARD_EFFECT == currentState.getGameStage()) { if (GameStages.OPTIONAL_CARD_EFFECT == currentState.getGameStage()) {
Player optionalPlayer = OptionalCardQueue.poll(); Player optionalPlayer = OptionalCardQueue.poll();
@@ -619,13 +622,17 @@ public class Game implements Serializable {
currentState.GameStageUpdate(GameStages.RESOLVING_EVENT); currentState.GameStageUpdate(GameStages.RESOLVING_EVENT);
if (currentState.getRound() < 10) { if (currentState.getRound() < 10) {
nextRound(); nextRound();
} else { currentState.PlayerUpdate(orderLogicCard.pull(), null);
EventResolution(); currentState.GameStageUpdate(GameStages.SLOT_CHOICE);
currentState.GameStageUpdate(GameStages.ENDING); } else {
endGame(); EventResolution();
} currentState.GameStageUpdate(GameStages.ENDING);
endGame();
}
return;
} }
} }
@@ -954,32 +954,75 @@ class GameTest {
} }
@Test @Test
void fullGameShouldEndAfterTenRoundsUsingOnlyPublicGameFlow() { void fullGameShouldEndAfterTenRoundsForTwoThreeAndFourPlayersUsingOnlyPublicGameFlow() {
Game game = new Game(3); for (int nPlayers : new int[]{2, 3, 4}) {
Game game = new Game(nPlayers);
assertTrue(game.addPlayer(new Player("p1"))); for (int i = 1; i <= nPlayers; i++) {
assertTrue(game.addPlayer(new Player("p2"))); assertTrue(game.addPlayer(new Player("p" + nPlayers + "_" + i)));
assertTrue(game.addPlayer(new Player("p3"))); }
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) { while (game.getCurrentState().getGameStage() != GameStages.ENDED && guard < 15) {
guard++; 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( assertTrue(
game.getCurrentState().getGameStage() == GameStages.SLOT_CHOICE guard < 15,
|| game.getCurrentState().getGameStage() == GameStages.ENDED, "Possible infinite loop while playing the full game with " + nPlayers + " players."
"After a full round, the game should either start the next slot choice phase or end." );
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 @Test
@@ -1039,23 +1082,46 @@ class GameTest {
completeSlotChoice(game); completeSlotChoice(game);
resolveActionsUntilOptionalCardEffect(game); resolveActionsUntilOptionalCardEffect(game);
int guard = 0; resolveOptionalPhaseIfPresent(game);
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.");
assertEquals(GameStages.SLOT_CHOICE, game.getCurrentState().getGameStage()); assertEquals(GameStages.SLOT_CHOICE, game.getCurrentState().getGameStage());
assertEquals(roundBefore + 1, game.getCurrentState().getRound()); assertEquals(roundBefore + 1, game.getCurrentState().getRound());
assertNotNull(game.getCurrentState().getCurrentPlayer()); 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."
);
}
} }