From a2014891e291eeb7f73ae8ed670bdf4890827c33 Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 2 Apr 2024 15:48:12 +0200 Subject: [PATCH] Better GameState.determineCancelledAnimals --- src/ch/epfl/chacun/GameState.java | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/ch/epfl/chacun/GameState.java b/src/ch/epfl/chacun/GameState.java index c2a2c05..0609644 100644 --- a/src/ch/epfl/chacun/GameState.java +++ b/src/ch/epfl/chacun/GameState.java @@ -230,7 +230,7 @@ public GameState withOccupantRemoved(Occupant occupant) { Preconditions.checkArgument(occupant == null || occupant.kind() == Occupant.Kind.PAWN); Board updatedBoard = occupant == null ? board : board.withoutOccupant(occupant); return new GameState(players, tileDecks, null, - updatedBoard, Action.OCCUPY_TILE, messageBoard).withTurnFinished(); + updatedBoard, nextAction, messageBoard).withTurnFinished(); } /** @@ -247,7 +247,7 @@ public GameState withNewOccupant(Occupant occupant) { Preconditions.checkArgument(nextAction == Action.OCCUPY_TILE); Board updatedBoard = occupant == null ? board : board.withOccupant(occupant); GameState updatedState = new GameState( - players, tileDecks, null, updatedBoard, Action.OCCUPY_TILE, messageBoard); + players, tileDecks, null, updatedBoard, nextAction, messageBoard); return updatedState.withTurnFinished(); } @@ -263,8 +263,7 @@ private GameState withTurnFinishedIfOccupationImpossible() { || freeOccupantsCount(currentPlayer(), Occupant.Kind.PAWN) <= 0) { return withTurnFinished(); } - return new GameState( - players, tileDecks, null, board, Action.OCCUPY_TILE, messageBoard); + return new GameState(players, tileDecks, null, board, Action.OCCUPY_TILE, messageBoard); } /** @@ -403,20 +402,20 @@ private GameState withFinalPointsCounted() { */ private Set determineCancelledAnimals(Area meadowArea, int specifiedTigerNb) { Set animals = Area.animals(meadowArea, Set.of()); - // Find deer Set deer = getAnimalsOfKind(animals, Animal.Kind.DEER); - // Find tigers Set tigers = getAnimalsOfKind(animals, Animal.Kind.TIGER); // If the specified number of tigers is 0, take the number of tigers of the given meadow area specifiedTigerNb = specifiedTigerNb >= 0 ? specifiedTigerNb : tigers.size(); // Compute the number of deer to cancel int cancelledDeerNb = Math.min(specifiedTigerNb, deer.size()); - Set cancelledAnimals = new HashSet<>(); + Set cancelledAnimals = new HashSet<>(cancelledDeerNb); // Remove from the deer set and add to the cancelled animals set the correct number of deer for (int i = 0; i < cancelledDeerNb; ++i) { - Animal removedDeer = deer.stream().findFirst().get(); - deer.remove(removedDeer); - cancelledAnimals.add(removedDeer); + Animal removedDeer = deer.stream().findFirst().orElse(null); + if (removedDeer != null) { + deer.remove(removedDeer); + cancelledAnimals.add(removedDeer); + } } return cancelledAnimals; } @@ -433,7 +432,6 @@ private Set determineCancelledAnimals(Area meadowArea, int */ private Set determineCancelledAnimalsWithPitTrap(Area meadow, Board board) { PlacedTile pitTrapTile = board.tileWithId(PIT_TRAP_TILE_ID); - // The area containing the adjacent meadows Area adjacentMeadowArea = board.adjacentMeadow(pitTrapTile.pos(), (Zone.Meadow) pitTrapTile.specialPowerZone()); @@ -452,7 +450,6 @@ private Set determineCancelledAnimalsWithPitTrap(Area meado tigerNb -= getAnimalsOfKind(Area.animals(outOfReachMeadowArea, Set.of()), Animal.Kind.DEER).size(); // Add the remaining cancelled animals from the adjacent meadows cancelledAnimals.addAll(determineCancelledAnimals(adjacentMeadowArea, tigerNb)); - return cancelledAnimals; }