Skip to content

Commit

Permalink
FIX: Prevent error in AI-only game due to no tile to drop in the play…
Browse files Browse the repository at this point in the history
…er hand.
  • Loading branch information
tsaglam committed Jan 28, 2022
1 parent 925a039 commit eae9992
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/main/java/carcassonne/control/state/StatePlacing.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package carcassonne.control.state;

import java.util.Collection;
import java.util.Optional;

import carcassonne.model.Player;
Expand Down Expand Up @@ -78,11 +79,12 @@ public void skip() {
}

private void skipPlacingTile() {
Tile tile = getTileToDrop();
tileStack.putBack(tile);
if (!round.getActivePlayer().dropTile(tile)) {
throw new IllegalStateException("Cannot drop tile " + tile + "from player " + round.getActivePlayer());
}
getTileToDrop().ifPresent(it -> {
tileStack.putBack(it);
if (!round.getActivePlayer().dropTile(it)) {
throw new IllegalStateException("Cannot drop tile " + it + "from player " + round.getActivePlayer());
}
});
if (!round.getActivePlayer().isComputerControlled()) {
views.onMainView(it -> it.resetPlacementHighlights());
}
Expand Down Expand Up @@ -116,11 +118,15 @@ private void placeTileWithAI(Player player) {
});
}

private Tile getTileToDrop() {
private Optional<Tile> getTileToDrop() {
if (round.getActivePlayer().isComputerControlled()) {
return playerAI.chooseTileToDrop(round.getActivePlayer().getHandOfTiles());
Collection<Tile> handOfTiles = round.getActivePlayer().getHandOfTiles();
if (handOfTiles.isEmpty()) {
return Optional.empty();
}
return Optional.of(playerAI.chooseTileToDrop(handOfTiles));
}
return views.getSelectedTile();
return Optional.of(views.getSelectedTile());
}

private void waitForUI() {
Expand Down

0 comments on commit eae9992

Please sign in to comment.