summaryrefslogtreecommitdiff
path: root/game-engine/src/main/java/org/luxons
diff options
context:
space:
mode:
Diffstat (limited to 'game-engine/src/main/java/org/luxons')
-rw-r--r--game-engine/src/main/java/org/luxons/sevenwonders/game/Game.java233
-rw-r--r--game-engine/src/main/java/org/luxons/sevenwonders/game/scoring/ScoreBoard.java6
2 files changed, 4 insertions, 235 deletions
diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/Game.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/Game.java
deleted file mode 100644
index 1d88fb9f..00000000
--- a/game-engine/src/main/java/org/luxons/sevenwonders/game/Game.java
+++ /dev/null
@@ -1,233 +0,0 @@
-package org.luxons.sevenwonders.game;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import org.luxons.sevenwonders.game.api.Action;
-import org.luxons.sevenwonders.game.api.HandCard;
-import org.luxons.sevenwonders.game.api.PlayerMove;
-import org.luxons.sevenwonders.game.api.PlayerTurnInfo;
-import org.luxons.sevenwonders.game.api.Table;
-import org.luxons.sevenwonders.game.boards.Board;
-import org.luxons.sevenwonders.game.cards.Card;
-import org.luxons.sevenwonders.game.cards.CardBack;
-import org.luxons.sevenwonders.game.cards.Decks;
-import org.luxons.sevenwonders.game.cards.Hands;
-import org.luxons.sevenwonders.game.effects.SpecialAbility;
-import org.luxons.sevenwonders.game.moves.InvalidMoveException;
-import org.luxons.sevenwonders.game.moves.Move;
-import org.luxons.sevenwonders.game.scoring.ScoreBoard;
-
-public class Game {
-
- private static final int LAST_AGE = 3;
-
- private final long id;
-
- private final Settings settings;
-
- private final int nbPlayers;
-
- private final Table table;
-
- private final Decks decks;
-
- private final List<Card> discardedCards;
-
- private final Map<Integer, Move> preparedMoves;
-
- private Map<Integer, PlayerTurnInfo> currentTurnInfo;
-
- private Hands hands;
-
- public Game(long id, Settings settings, int nbPlayers, List<Board> boards, Decks decks) {
- this.id = id;
- this.settings = settings;
- this.nbPlayers = nbPlayers;
- this.table = new Table(boards);
- this.decks = decks;
- this.discardedCards = new ArrayList<>();
- this.currentTurnInfo = new HashMap<>();
- this.preparedMoves = new HashMap<>();
- startNewAge();
- }
-
- public long getId() {
- return id;
- }
-
- public Settings getSettings() {
- return settings;
- }
-
- private void startNewAge() {
- table.increaseCurrentAge();
- hands = decks.deal(table.getCurrentAge(), table.getNbPlayers());
- startNewTurn();
- }
-
- private void startNewTurn() {
- currentTurnInfo.clear();
- for (int i = 0; i < nbPlayers; i++) {
- currentTurnInfo.put(i, createPlayerTurnInfo(i));
- }
- }
-
- private PlayerTurnInfo createPlayerTurnInfo(int playerIndex) {
- List<HandCard> hand = hands.createHand(table, playerIndex);
- Action action = determineAction(hand, table.getBoard(playerIndex));
-
- List<Card> neighbourGuildCards = Collections.emptyList();
- if (action == Action.PICK_NEIGHBOR_GUILD) {
- neighbourGuildCards = table.getNeighbourGuildCards(playerIndex);
- }
-
- return new PlayerTurnInfo(playerIndex, table, action, hand, neighbourGuildCards);
- }
-
- public Collection<PlayerTurnInfo> getCurrentTurnInfo() {
- return currentTurnInfo.values();
- }
-
- private Action determineAction(List<HandCard> hand, Board board) {
- if (endOfGameReached() && board.hasSpecial(SpecialAbility.COPY_GUILD)) {
- List<Card> neighbourGuildCards = table.getNeighbourGuildCards(board.getPlayerIndex());
- return neighbourGuildCards.isEmpty() ? Action.WAIT : Action.PICK_NEIGHBOR_GUILD;
- } else if (hand.size() == 1 && board.hasSpecial(SpecialAbility.PLAY_LAST_CARD)) {
- return Action.PLAY_LAST;
- } else if (hand.size() == 2 && board.hasSpecial(SpecialAbility.PLAY_LAST_CARD)) {
- return Action.PLAY_2;
- } else if (hand.isEmpty()) {
- return Action.WAIT;
- } else {
- return Action.PLAY;
- }
- }
-
- public CardBack prepareMove(int playerIndex, PlayerMove playerMove) throws InvalidMoveException {
- Card card = decks.getCard(table.getCurrentAge(), playerMove.getCardName());
- Move move = playerMove.getType().resolve(playerIndex, card, playerMove);
- validate(move);
- preparedMoves.put(playerIndex, move);
- return card.getBack();
- }
-
- private void validate(Move move) throws InvalidMoveException {
- List<Card> hand = hands.get(move.getPlayerIndex());
- move.validate(table, hand);
- }
-
- public boolean allPlayersPreparedTheirMove() {
- long nbExpectedMoves = currentTurnInfo.values().stream().filter(pti -> pti.getAction() != Action.WAIT).count();
- return preparedMoves.size() == nbExpectedMoves;
- }
-
- public Table playTurn() {
- makeMoves();
- if (endOfAgeReached()) {
- executeEndOfAgeEvents();
- if (!endOfGameReached()) {
- startNewAge();
- }
- } else {
- rotateHandsIfRelevant();
- startNewTurn();
- }
- return table;
- }
-
- private void rotateHandsIfRelevant() {
- // we don't rotate hands if some player can play his last card (with the special ability)
- if (!hands.maxOneCardRemains()) {
- hands = hands.rotate(table.getHandRotationDirection());
- }
- }
-
- private void makeMoves() {
- List<Move> playedMoves = mapToList(preparedMoves);
-
- // all cards from this turn need to be placed before executing any effect
- // because effects depending on played cards need to take the ones from the current turn into account too
- placePreparedCards(playedMoves);
-
- // same goes for the discarded cards during the last turn, which should be available for special actions
- if (hands.maxOneCardRemains()) {
- discardLastCardsOfHands();
- }
-
- activatePlayedCards(playedMoves);
-
- table.setLastPlayedMoves(playedMoves);
- preparedMoves.clear();
- }
-
- private static List<Move> mapToList(Map<Integer, Move> movesPerPlayer) {
- List<Move> moves = new ArrayList<>(movesPerPlayer.size());
- for (int p = 0; p < movesPerPlayer.size(); p++) {
- Move move = movesPerPlayer.get(p);
- if (move == null) {
- throw new MissingPreparedMoveException(p);
- }
- moves.add(move);
- }
- return moves;
- }
-
- private void placePreparedCards(List<Move> playedMoves) {
- playedMoves.forEach(move -> {
- move.place(table, discardedCards, settings);
- removeFromHand(move.getPlayerIndex(), move.getCard());
- });
- }
-
- private void discardLastCardsOfHands() {
- for (int i = 0; i < nbPlayers; i++) {
- Board board = table.getBoard(i);
- if (!board.hasSpecial(SpecialAbility.PLAY_LAST_CARD)) {
- discardHand(i);
- }
- }
- }
-
- private void discardHand(int playerIndex) {
- List<Card> hand = hands.get(playerIndex);
- discardedCards.addAll(hand);
- hands = hands.discard(playerIndex);
- }
-
- private void removeFromHand(int playerIndex, Card card) {
- hands.get(playerIndex).remove(card);
- }
-
- private void activatePlayedCards(List<Move> playedMoves) {
- playedMoves.forEach(move -> move.activate(table, discardedCards, settings));
- }
-
- private boolean endOfAgeReached() {
- return hands.isEmpty();
- }
-
- private void executeEndOfAgeEvents() {
- table.resolveMilitaryConflicts();
- }
-
- private boolean endOfGameReached() {
- return endOfAgeReached() && table.getCurrentAge() == LAST_AGE;
- }
-
- public ScoreBoard computeScore() {
- ScoreBoard scoreBoard = new ScoreBoard();
- table.getBoards().stream().map(b -> b.computePoints(table)).forEach(scoreBoard::add);
- return scoreBoard;
- }
-
- private static class MissingPreparedMoveException extends IllegalStateException {
- MissingPreparedMoveException(int playerIndex) {
- super("Player " + playerIndex + " is not ready to play");
- }
- }
-}
diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/scoring/ScoreBoard.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/scoring/ScoreBoard.java
index e7cdaedd..80f5e510 100644
--- a/game-engine/src/main/java/org/luxons/sevenwonders/game/scoring/ScoreBoard.java
+++ b/game-engine/src/main/java/org/luxons/sevenwonders/game/scoring/ScoreBoard.java
@@ -1,5 +1,6 @@
package org.luxons.sevenwonders.game.scoring;
+import java.util.Collection;
import java.util.Comparator;
import java.util.PriorityQueue;
@@ -10,8 +11,9 @@ public class ScoreBoard {
private PriorityQueue<PlayerScore> scores;
- public ScoreBoard() {
- scores = new PriorityQueue<>(comparator);
+ public ScoreBoard(Collection<PlayerScore> scores) {
+ this.scores = new PriorityQueue<>(comparator);
+ this.scores.addAll(scores);
}
public void add(PlayerScore score) {
bgstack15