Merge pull request #72 from rubenpirreram/Game-test-fix

Add: GameController case tests
This commit is contained in:
rubenpirreram
2026-05-02 19:06:15 +02:00
committed by GitHub
@@ -18,10 +18,7 @@ class GameControllerTest {
private Game createStartedGame() { private Game createStartedGame() {
Game game = new Game(3); Game game = new Game(3);
GameController controller = new GameController(game); GameController controller = new GameController(game);
assertEquals( controller.getModel(),game);
controller = new GameController();
controller.setModel(game);
assertEquals( controller.getModel(),game);
assertTrue(controller.addPlayer("Giorgio")); assertTrue(controller.addPlayer("Giorgio"));
assertTrue(controller.addPlayer("Marco")); assertTrue(controller.addPlayer("Marco"));
assertTrue(controller.addPlayer("Luca")); assertTrue(controller.addPlayer("Luca"));
@@ -32,7 +29,7 @@ class GameControllerTest {
private Queue<Player> completeSlotChoice(Game game, GameController controller) { private Queue<Player> completeSlotChoice(Game game, GameController controller) {
Queue<Player> order = new LinkedList<>(); Queue<Player> order = new LinkedList<>();
for (int i = 0; i < 3; i++) { for (int i = 0; i < game.getNPlayers(); i++) {
Player current = game.getCurrentState().getCurrentPlayer(); Player current = game.getCurrentState().getCurrentPlayer();
order.add(current); order.add(current);
@@ -92,34 +89,74 @@ class GameControllerTest {
private void resolveActionsUntilOptionalCardEffect(Game game, GameController controller) { private void resolveActionsUntilOptionalCardEffect(Game game, GameController controller) {
int guard = 0; int guard = 0;
while (game.getCurrentState().getGameStage() == GameStages.RESOLVING_ACTIONS && guard < 20) { while (game.getCurrentState().getGameStage() == GameStages.RESOLVING_ACTIONS && guard < 100) {
guard++; guard++;
resolveOneMandatoryAction(game, controller);
}
assertEquals(GameStages.OPTIONAL_CARD_EFFECT, game.getCurrentState().getGameStage());
}
private void resolveOneMandatoryAction(Game game, GameController controller) {
Player current = game.getCurrentState().getCurrentPlayer(); Player current = game.getCurrentState().getCurrentPlayer();
assertNotNull(current); assertNotNull(current);
String username = current.getUserName();
if (game.getCurrentState().getNLower() > 0) { if (game.getCurrentState().getNLower() > 0) {
int index = firstNonEventIndexOrMinusOne(game.getLowerListTribeCards()); int index = firstNonEventIndexOrMinusOne(game.getLowerListTribeCards());
if (index == -1) { if (index != -1) {
fail("No non-event lower tribe card available."); assertTrue(controller.drawLowerTribeCard(username, index));
return;
} }
assertTrue(controller.drawLowerTribeCard(current.getUserName(), index)); if (!game.getLowerListBuilding().isEmpty()) {
} else if (game.getCurrentState().getNUpper() > 0) { current.addFood(100);
assertTrue(controller.drawLowerBuildingCard(username, 0));
return;
}
assertTrue(controller.SkipLowerDrawing(username));
return;
}
if (game.getCurrentState().getNUpper() > 0) {
int index = firstNonEventIndexOrMinusOne(game.getUpperListTribeCards()); int index = firstNonEventIndexOrMinusOne(game.getUpperListTribeCards());
if (index == -1) { if (index != -1) {
fail("No non-event upper tribe card available."); assertTrue(controller.drawUpperTribeCard(username, index));
return;
} }
assertTrue(controller.drawUpperTribeCard(current.getUserName(), index)); if (!game.getUpperListBuilding().isEmpty()) {
} else { current.addFood(100);
fail("Current player has no remaining upper or lower draws."); assertTrue(controller.drawUpperBuildingCard(username, 0));
} return;
} }
assertEquals(GameStages.OPTIONAL_CARD_EFFECT, game.getCurrentState().getGameStage()); assertTrue(controller.SkipUpperDrawing(username));
return;
}
fail("Current player has no remaining draw actions.");
}
private Queue<Player> completeSlotChoiceWithSlots(Game game, GameController controller, int... slotIndexes) {
assertEquals(game.getNPlayers(), slotIndexes.length);
Queue<Player> order = new LinkedList<>();
for (int i = 0; i < game.getNPlayers(); i++) {
Player current = game.getCurrentState().getCurrentPlayer();
order.add(current);
assertTrue(controller.slotChoice(current.getUserName(), slotIndexes[i]));
}
assertEquals(GameStages.RESOLVING_ACTIONS, game.getCurrentState().getGameStage());
return order;
} }
@@ -150,8 +187,11 @@ class GameControllerTest {
assertFalse(controller.drawLowerTribeCard("ghost", 0)); assertFalse(controller.drawLowerTribeCard("ghost", 0));
assertFalse(controller.drawUpperBuildingCard("ghost", 0)); assertFalse(controller.drawUpperBuildingCard("ghost", 0));
assertFalse(controller.drawLowerBuildingCard("ghost", 0)); assertFalse(controller.drawLowerBuildingCard("ghost", 0));
assertFalse(controller.SkipUpperDrawing("ghost"));
assertFalse(controller.SkipLowerDrawing("ghost"));
assertFalse(controller.pickOptionalTribeCard("ghost", 0)); assertFalse(controller.pickOptionalTribeCard("ghost", 0));
assertFalse(controller.pickOptionalBuildingCard("ghost", 0)); assertFalse(controller.pickOptionalBuildingCard("ghost", 0));
assertFalse(controller.noOptionalCard("ghost"));
} }
@Test @Test
@@ -243,7 +283,13 @@ class GameControllerTest {
Player current = game.getCurrentState().getCurrentPlayer(); Player current = game.getCurrentState().getCurrentPlayer();
while (current.getFoodValue() > 0) {
assertTrue(current.removeFood(1));
}
if (current.builders.isEmpty()) {
assertFalse(controller.drawUpperBuildingCard(current.getUserName(), 0)); assertFalse(controller.drawUpperBuildingCard(current.getUserName(), 0));
}
current.addFood(100); current.addFood(100);
int foodBefore = current.getFoodValue(); int foodBefore = current.getFoodValue();
@@ -251,7 +297,7 @@ class GameControllerTest {
assertTrue(controller.drawUpperBuildingCard(current.getUserName(), 0)); assertTrue(controller.drawUpperBuildingCard(current.getUserName(), 0));
assertTrue(current.getFoodValue() < foodBefore); assertTrue(current.getFoodValue() <= foodBefore);
assertEquals(buildingsBefore + 1, current.buildingCards.size()); assertEquals(buildingsBefore + 1, current.buildingCards.size());
} }
@@ -267,7 +313,7 @@ class GameControllerTest {
Queue<Player> players = new LinkedList<>(); Queue<Player> players = new LinkedList<>();
for (int i = 0; i < 3; i++) { for (int i = 0; i < game.getNPlayers(); i++) {
Player current = game.getCurrentState().getCurrentPlayer(); Player current = game.getCurrentState().getCurrentPlayer();
players.add(current); players.add(current);
@@ -408,7 +454,7 @@ class GameControllerTest {
assertTrue(controller.pickOptionalBuildingCard(optionalPlayer.getUserName(), 0)); assertTrue(controller.pickOptionalBuildingCard(optionalPlayer.getUserName(), 0));
assertTrue(optionalPlayer.getFoodValue() < foodBefore); assertTrue(optionalPlayer.getFoodValue() <= foodBefore);
assertEquals(buildingsBefore + 1, optionalPlayer.buildingCards.size()); assertEquals(buildingsBefore + 1, optionalPlayer.buildingCards.size());
} }
@@ -456,4 +502,133 @@ class GameControllerTest {
assertFalse(controller.slotChoice(second.getUserName(), 0)); assertFalse(controller.slotChoice(second.getUserName(), 0));
} }
@Test
void noOptionalCardShouldWorkDuringOptionalCardEffectState() {
Game game = createStartedGame();
GameController controller = new GameController(game);
giveOptionalEffectToAllPlayers(game);
completeSlotChoice(game, controller);
resolveActionsUntilOptionalCardEffect(game, controller);
Player optionalPlayer = game.getCurrentState().getCurrentPlayer();
assertNotNull(optionalPlayer);
assertTrue(optionalPlayer.buildingCards.stream()
.anyMatch(building -> building.getEffectId() == 12));
assertTrue(controller.noOptionalCard(optionalPlayer.getUserName()));
}
@Test
void constructorsGetModelAndSetModelShouldWork() {
Game game = new Game(3);
GameController controller = new GameController(game);
assertEquals(game, controller.getModel());
GameController emptyController = new GameController();
emptyController.setModel(game);
assertEquals(game, emptyController.getModel());
}
@Test
void slotChoiceShouldReturnFalseForInvalidIndexes() {
Game game = createStartedGame();
GameController controller = new GameController(game);
Player current = game.getCurrentState().getCurrentPlayer();
assertFalse(controller.slotChoice(current.getUserName(), -1));
assertFalse(controller.slotChoice(current.getUserName(), 999));
}
@Test
void optionalCardMethodsShouldReturnFalseForInvalidIndexesDuringOptionalCardEffectState() {
Game game = createStartedGame();
GameController controller = new GameController(game);
giveOptionalEffectToAllPlayers(game);
completeSlotChoice(game, controller);
resolveActionsUntilOptionalCardEffect(game, controller);
Player optionalPlayer = game.getCurrentState().getCurrentPlayer();
assertNotNull(optionalPlayer);
String username = optionalPlayer.getUserName();
assertFalse(controller.pickOptionalTribeCard(username, -1));
assertFalse(controller.pickOptionalTribeCard(username, 999));
assertFalse(controller.pickOptionalBuildingCard(username, -1));
assertFalse(controller.pickOptionalBuildingCard(username, 999));
}
@Test
void optionalCardMethodsShouldReturnFalseForWrongPlayer() {
Game game = createStartedGame();
GameController controller = new GameController(game);
giveOptionalEffectToAllPlayers(game);
completeSlotChoice(game, controller);
resolveActionsUntilOptionalCardEffect(game, controller);
Player current = game.getCurrentState().getCurrentPlayer();
assertNotNull(current);
Player wrongPlayer = game.getPlayers().stream()
.filter(player -> !player.equals(current))
.findFirst()
.orElse(null);
assertNotNull(wrongPlayer);
assertFalse(controller.pickOptionalTribeCard(wrongPlayer.getUserName(), 0));
assertFalse(controller.pickOptionalBuildingCard(wrongPlayer.getUserName(), 0));
assertFalse(controller.noOptionalCard(wrongPlayer.getUserName()));
}
@Test
void skipLowerDrawingShouldReturnFalseWhenLowerCharacterIsAvailable() {
Game game = createStartedGame();
GameController controller = new GameController(game);
completeSlotChoiceWithSlots(game, controller, 0, 1, 2);
Player current = game.getCurrentState().getCurrentPlayer();
assertNotNull(current);
assertEquals(1, game.getCurrentState().getNLower());
assertEquals(0, game.getCurrentState().getNUpper());
assertFalse(game.getLowerListTribeCards().isEmpty());
assertTrue(firstNonEventIndexOrMinusOne(game.getLowerListTribeCards()) != -1);
assertFalse(controller.SkipLowerDrawing(current.getUserName()));
}
@Test
void skipUpperDrawingShouldReturnFalseWhenUpperCharacterIsAvailable() {
Game game = createStartedGame();
GameController controller = new GameController(game);
completeSlotChoiceWithSlots(game, controller, 1, 2, 3);
Player current = game.getCurrentState().getCurrentPlayer();
assertNotNull(current);
assertEquals(0, game.getCurrentState().getNLower());
assertEquals(1, game.getCurrentState().getNUpper());
assertFalse(game.getUpperListTribeCards().isEmpty());
assertTrue(firstNonEventIndexOrMinusOne(game.getUpperListTribeCards()) != -1);
assertFalse(controller.SkipUpperDrawing(current.getUserName()));
}
} }