Skip to content

Commit

Permalink
Multiple GameState fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Max committed Apr 2, 2024
1 parent 1cfdaa5 commit 0964611
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/ch/epfl/chacun/GameState.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,21 @@ public GameState withPlacedTile(PlacedTile placedTile) {
GameState normalNextGameState = new GameState(players, updatedDeck, nextTileToPlace,
board.withNewTile(placedTile), Action.PLACE_TILE, messageBoard)
.withTurnFinishedIfOccupationImpossible();

// Handle the MENHIR tiles special traits
return switch (placedTile.tile().id()) {
/*
The shaman tile allows the player to retake one of his pawns.
*/
case SHAMAN_TILE_ID -> {
if (board.occupantCount(currentPlayer(), Occupant.Kind.PAWN) >= 1)
yield new GameState(players, tileDecks, null, board, Action.RETAKE_PAWN, messageBoard);
yield normalNextGameState;
}
/*
The hunting trap tile allows the player to gets the points corresponding
to the animals present in the adjacent meadow.
*/
case HUNTING_TRAP_TILE_ID -> {
Area<Zone.Meadow> adjacentMeadows = board.adjacentMeadow(placedTile.pos(),
(Zone.Meadow) placedTile.specialPowerZone());
Expand All @@ -187,12 +195,19 @@ public GameState withPlacedTile(PlacedTile placedTile) {
yield new GameState(players, updatedDeck, nextTileToPlace, updatedBoard,
Action.PLACE_TILE, updatedMessageBoard).withTurnFinishedIfOccupationImpossible();
}
/*
The logboat tile allows the player to obtain points that depend on the number
of lakes in the hydrographic network containing it.
*/
case LOGBOAT_TILE_ID -> {
Area<Zone.Water> area = board.riverSystemArea((Zone.Water) placedTile.specialPowerZone());
MessageBoard updatedMessageBoard = messageBoard.withScoredLogboat(currentPlayer(), area);
yield new GameState(players, updatedDeck, nextTileToPlace, board,
Action.PLACE_TILE, updatedMessageBoard).withTurnFinishedIfOccupationImpossible();
}
/*
Default next game state
*/
default -> normalNextGameState;
};
}
Expand All @@ -211,13 +226,9 @@ yield new GameState(players, updatedDeck, nextTileToPlace, board,
public GameState withOccupantRemoved(Occupant occupant) {
Preconditions.checkArgument(nextAction == Action.RETAKE_PAWN);
Preconditions.checkArgument(occupant == null || occupant.kind() == Occupant.Kind.PAWN);
// Remove the occupant from the board if it is not null
Board updatedBoard = occupant == null ? board : board.withoutOccupant(occupant);
// If the occupant is null, the player can't do the action OCCUPY_TILE
Action nextAction = occupant == null ? Action.PLACE_TILE : Action.OCCUPY_TILE;
GameState updatedGameState = new GameState(players, tileDecks, null, updatedBoard, nextAction, messageBoard);
// Check if the occupation is possible
return updatedGameState.withTurnFinishedIfOccupationImpossible();
return new GameState(players, tileDecks, null,
updatedBoard, Action.OCCUPY_TILE, messageBoard).withTurnFinished();
}

/**
Expand Down

0 comments on commit 0964611

Please sign in to comment.