Add: GameController case tests

This commit is contained in:
MatteoPellegrino05
2026-05-02 19:05:31 +02:00
parent ab71d3b43d
commit 2ad8a2e2f5
@@ -18,10 +18,7 @@ class GameControllerTest {
private Game createStartedGame() {
Game game = new Game(3);
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("Marco"));
assertTrue(controller.addPlayer("Luca"));
@@ -32,7 +29,7 @@ class GameControllerTest {
private Queue<Player> completeSlotChoice(Game game, GameController controller) {
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();
order.add(current);
@@ -92,34 +89,74 @@ class GameControllerTest {
private void resolveActionsUntilOptionalCardEffect(Game game, GameController controller) {
int guard = 0;
while (game.getCurrentState().getGameStage() == GameStages.RESOLVING_ACTIONS && guard < 20) {
while (game.getCurrentState().getGameStage() == GameStages.RESOLVING_ACTIONS && guard < 100) {
guard++;
resolveOneMandatoryAction(game, controller);
}
assertEquals(GameStages.OPTIONAL_CARD_EFFECT, game.getCurrentState().getGameStage());
}
private void resolveOneMandatoryAction(Game game, GameController controller) {
Player current = game.getCurrentState().getCurrentPlayer();
assertNotNull(current);
String username = current.getUserName();
if (game.getCurrentState().getNLower() > 0) {
int index = firstNonEventIndexOrMinusOne(game.getLowerListTribeCards());
if (index == -1) {
fail("No non-event lower tribe card available.");
if (index != -1) {
assertTrue(controller.drawLowerTribeCard(username, index));
return;
}
assertTrue(controller.drawLowerTribeCard(current.getUserName(), index));
} else if (game.getCurrentState().getNUpper() > 0) {
if (!game.getLowerListBuilding().isEmpty()) {
current.addFood(100);
assertTrue(controller.drawLowerBuildingCard(username, 0));
return;
}
assertTrue(controller.SkipLowerDrawing(username));
return;
}
if (game.getCurrentState().getNUpper() > 0) {
int index = firstNonEventIndexOrMinusOne(game.getUpperListTribeCards());
if (index == -1) {
fail("No non-event upper tribe card available.");
if (index != -1) {
assertTrue(controller.drawUpperTribeCard(username, index));
return;
}
assertTrue(controller.drawUpperTribeCard(current.getUserName(), index));
} else {
fail("Current player has no remaining upper or lower draws.");
}
if (!game.getUpperListBuilding().isEmpty()) {
current.addFood(100);
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.drawUpperBuildingCard("ghost", 0));
assertFalse(controller.drawLowerBuildingCard("ghost", 0));
assertFalse(controller.SkipUpperDrawing("ghost"));
assertFalse(controller.SkipLowerDrawing("ghost"));
assertFalse(controller.pickOptionalTribeCard("ghost", 0));
assertFalse(controller.pickOptionalBuildingCard("ghost", 0));
assertFalse(controller.noOptionalCard("ghost"));
}
@Test
@@ -243,7 +283,13 @@ class GameControllerTest {
Player current = game.getCurrentState().getCurrentPlayer();
while (current.getFoodValue() > 0) {
assertTrue(current.removeFood(1));
}
if (current.builders.isEmpty()) {
assertFalse(controller.drawUpperBuildingCard(current.getUserName(), 0));
}
current.addFood(100);
int foodBefore = current.getFoodValue();
@@ -251,7 +297,7 @@ class GameControllerTest {
assertTrue(controller.drawUpperBuildingCard(current.getUserName(), 0));
assertTrue(current.getFoodValue() < foodBefore);
assertTrue(current.getFoodValue() <= foodBefore);
assertEquals(buildingsBefore + 1, current.buildingCards.size());
}
@@ -267,7 +313,7 @@ class GameControllerTest {
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();
players.add(current);
@@ -408,7 +454,7 @@ class GameControllerTest {
assertTrue(controller.pickOptionalBuildingCard(optionalPlayer.getUserName(), 0));
assertTrue(optionalPlayer.getFoodValue() < foodBefore);
assertTrue(optionalPlayer.getFoodValue() <= foodBefore);
assertEquals(buildingsBefore + 1, optionalPlayer.buildingCards.size());
}
@@ -456,4 +502,133 @@ class GameControllerTest {
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()));
}
}