Skip to content

Commit

Permalink
Better GameState.determineCancelledAnimals
Browse files Browse the repository at this point in the history
  • Loading branch information
Max committed Apr 2, 2024
1 parent 733a95f commit a201489
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions src/ch/epfl/chacun/GameState.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand All @@ -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();
}

Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -403,20 +402,20 @@ private GameState withFinalPointsCounted() {
*/
private Set<Animal> determineCancelledAnimals(Area<Zone.Meadow> meadowArea, int specifiedTigerNb) {
Set<Animal> animals = Area.animals(meadowArea, Set.of());
// Find deer
Set<Animal> deer = getAnimalsOfKind(animals, Animal.Kind.DEER);
// Find tigers
Set<Animal> 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<Animal> cancelledAnimals = new HashSet<>();
Set<Animal> 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;
}
Expand All @@ -433,7 +432,6 @@ private Set<Animal> determineCancelledAnimals(Area<Zone.Meadow> meadowArea, int
*/
private Set<Animal> determineCancelledAnimalsWithPitTrap(Area<Zone.Meadow> meadow, Board board) {
PlacedTile pitTrapTile = board.tileWithId(PIT_TRAP_TILE_ID);

// The area containing the adjacent meadows
Area<Zone.Meadow> adjacentMeadowArea = board.adjacentMeadow(pitTrapTile.pos(),
(Zone.Meadow) pitTrapTile.specialPowerZone());
Expand All @@ -452,7 +450,6 @@ private Set<Animal> determineCancelledAnimalsWithPitTrap(Area<Zone.Meadow> 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;
}

Expand Down

0 comments on commit a201489

Please sign in to comment.