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,16 +589,20 @@ 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();
Player optionalPlayer = OptionalCardQueue.poll();
if (optionalPlayer != null) {
currentState.PlayerUpdate(optionalPlayer, null);
return;
}
else
{
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();
@@ -606,9 +610,8 @@ public class Game implements Serializable {
return;
}
return;
}
}
if (GameStages.OPTIONAL_CARD_EFFECT == currentState.getGameStage()) {
Player optionalPlayer = OptionalCardQueue.poll();
@@ -621,12 +624,16 @@ public class Game implements Serializable {
if (currentState.getRound() < 10) {
nextRound();
currentState.PlayerUpdate(orderLogicCard.pull(), null);
currentState.GameStageUpdate(GameStages.SLOT_CHOICE);
} else {
EventResolution();
currentState.GameStageUpdate(GameStages.ENDING);
endGame();
}
return;
}
}
@@ -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(1, game.getCurrentState().getRound());
assertNotNull(game.getCurrentState().getCurrentPlayer());
int guard = 0;
while (game.getCurrentState().getGameStage() != GameStages.ENDED && guard < 20) {
while (game.getCurrentState().getGameStage() != GameStages.ENDED && guard < 15) {
guard++;
int roundBefore = game.getCurrentState().getRound();
playOneFullRound(game);
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."
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(guard < 20, "Possible infinite loop while playing the full game.");
assertEquals(GameStages.ENDED, game.getCurrentState().getGameStage());
assertEquals(10, game.getCurrentState().getRound());
assertTrue(
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."
);
}
}
@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."
);
}
}