diff options
Diffstat (limited to 'src/main')
4 files changed, 131 insertions, 53 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/game/Game.java b/src/main/java/org/luxons/sevenwonders/game/Game.java index e9962e10..3e0c2839 100644 --- a/src/main/java/org/luxons/sevenwonders/game/Game.java +++ b/src/main/java/org/luxons/sevenwonders/game/Game.java @@ -6,6 +6,7 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import org.luxons.sevenwonders.game.api.HandCard; import org.luxons.sevenwonders.game.api.PlayerTurnInfo; import org.luxons.sevenwonders.game.api.Table; import org.luxons.sevenwonders.game.boards.Board; @@ -56,34 +57,60 @@ public class Game { hands = decks.deal(currentAge, table.getNbPlayers()); } - public List<PlayerTurnInfo> startTurn() { - return hands.entrySet() - .stream() - .map(e -> table.createPlayerTurnInfo(e.getKey(), e.getValue())) - .peek(ptu -> ptu.setCurrentAge(currentAge)) + public List<PlayerTurnInfo> getTurnInfo() { + return hands.entrySet().stream().map(e -> createPlayerTurnInfo(e.getKey(), e.getValue())) .collect(Collectors.toList()); } + private PlayerTurnInfo createPlayerTurnInfo(int playerIndex, List<Card> cards) { + PlayerTurnInfo pti = new PlayerTurnInfo(playerIndex, table); + pti.setHand(createHand(playerIndex, cards)); + pti.setCurrentAge(currentAge); + return pti; + } + + private List<HandCard> createHand(int playerIndex, List<Card> cards) { + return cards.stream().map(c -> new HandCard(c, table, playerIndex)).collect(Collectors.toList()); + } + public void prepareCard(Move move) throws InvalidMoveException { + validate(move); + preparedMoves.put(move.getPlayerIndex(), move); + } + + private void validate(Move move) throws InvalidMoveException { + List<Card> hand = hands.get(move.getPlayerIndex()); + if (hand == null) { + throw new InvalidMoveException("Invalid player index " + move.getPlayerIndex()); + } + Card card = decks.getCard(move.getCardName()); + if (!hand.contains(card)) { + throw new InvalidMoveException( + "Player " + move.getPlayerIndex() + " does not have the card " + move.getCardName()); + } if (!move.isValid(table)) { - throw new InvalidMoveException(); + throw new InvalidMoveException( + "Player " + move.getPlayerIndex() + " cannot play the card " + move.getCardName()); } - preparedMoves.put(move.getPlayerIndex(), move); } public boolean areAllPlayersReady() { return preparedMoves.size() == players.size(); } - public List<Move> playTurn() { + public void playTurn() { List<Move> playedMoves = mapToList(preparedMoves); // cards need to be all placed first as some effects depend on just-played cards - placePreparedCards(); - playPreparedCards(); + placePreparedCards(playedMoves); + activatePlayedCards(playedMoves); + table.setLastPlayedMoves(playedMoves); preparedMoves.clear(); - - return playedMoves; + if (endOfAgeReached()) { + startNewAge(); + } else { + rotateHands(); + } } private static List<Move> mapToList(Map<Integer, Move> movesPerPlayer) { @@ -98,36 +125,63 @@ public class Game { return moves; } - private void placePreparedCards() { - preparedMoves.forEach((playerIndex, move) -> { - switch (move.getType()) { - case PLAY: - table.placeCard(playerIndex, decks.getCard(move.getCardName())); - break; - case UPGRADE_WONDER: - table.upgradeWonderStage(playerIndex); - break; - case DISCARD: - discardedCards.add(decks.getCard(move.getCardName())); - break; - } + private void placePreparedCards(List<Move> playedMoves) { + playedMoves.forEach(move -> { + placeCard(move); + Card card = decks.getCard(move.getCardName()); + hands.get(move.getPlayerIndex()).remove(card); }); } - private void playPreparedCards() { - preparedMoves.forEach((playerIndex, move) -> { - switch (move.getType()) { - case PLAY: - table.activateCard(playerIndex, decks.getCard(move.getCardName())); - break; - case UPGRADE_WONDER: - table.activateCurrentWonderStage(playerIndex); - break; - case DISCARD: - table.discard(playerIndex, settings.getDiscardedCardGold()); - break; - } - }); + private void placeCard(Move move) { + switch (move.getType()) { + case PLAY: + table.placeCard(move.getPlayerIndex(), decks.getCard(move.getCardName())); + break; + case UPGRADE_WONDER: + table.upgradeWonderStage(move.getPlayerIndex()); + break; + case DISCARD: + discardedCards.add(decks.getCard(move.getCardName())); + break; + } + } + + private void activatePlayedCards(List<Move> playedMoves) { + playedMoves.forEach(this::activateCard); + } + + private void activateCard(Move move) { + switch (move.getType()) { + case PLAY: + table.activateCard(move.getPlayerIndex(), decks.getCard(move.getCardName())); + break; + case UPGRADE_WONDER: + table.activateCurrentWonderStage(move.getPlayerIndex()); + break; + case DISCARD: + table.discard(move.getPlayerIndex(), settings.getDiscardedCardGold()); + break; + } + } + + private boolean endOfAgeReached() { + return hands.get(0).size() == 1; + } + + private void rotateHands() { + int offset = getHandRotationOffset(); + Map<Integer, List<Card>> newHands = new HashMap<>(hands.size()); + for (int i = 0; i < players.size(); i++) { + int newIndex = Math.floorMod(i + offset, players.size()); + newHands.put(newIndex, hands.get(i)); + } + hands = newHands; + } + + private int getHandRotationOffset() { + // clockwise at age 1, and alternating + return currentAge % 2 == 0 ? -1 : 1; } private static class MissingPreparedMoveException extends RuntimeException { @@ -137,5 +191,8 @@ public class Game { } private static class InvalidMoveException extends RuntimeException { + InvalidMoveException(String message) { + super(message); + } } } diff --git a/src/main/java/org/luxons/sevenwonders/game/Move.java b/src/main/java/org/luxons/sevenwonders/game/Move.java index ed9dfa07..f1fa97aa 100644 --- a/src/main/java/org/luxons/sevenwonders/game/Move.java +++ b/src/main/java/org/luxons/sevenwonders/game/Move.java @@ -49,7 +49,10 @@ public class Move { } public boolean isValid(Table table) { - // TODO + if (moveType == MoveType.DISCARD) { + return true; + } + // TODO create a version of the Move class with actual card data? return false; } } diff --git a/src/main/java/org/luxons/sevenwonders/game/api/Table.java b/src/main/java/org/luxons/sevenwonders/game/api/Table.java index 9d33c779..7603f532 100644 --- a/src/main/java/org/luxons/sevenwonders/game/api/Table.java +++ b/src/main/java/org/luxons/sevenwonders/game/api/Table.java @@ -1,8 +1,8 @@ package org.luxons.sevenwonders.game.api; import java.util.List; -import java.util.stream.Collectors; +import org.luxons.sevenwonders.game.Move; import org.luxons.sevenwonders.game.boards.Board; import org.luxons.sevenwonders.game.boards.RelativeBoardPosition; import org.luxons.sevenwonders.game.cards.Card; @@ -17,11 +17,17 @@ public class Table { private final List<Board> boards; + private List<Move> lastPlayedMoves; + public Table(List<Board> boards) { this.nbPlayers = boards.size(); this.boards = boards; } + public int getNbPlayers() { + return nbPlayers; + } + public List<Board> getBoards() { return boards; } @@ -34,8 +40,12 @@ public class Table { return boards.get(position.getIndexFrom(playerIndex, nbPlayers)); } - public int getNbPlayers() { - return nbPlayers; + public List<Move> getLastPlayedMoves() { + return lastPlayedMoves; + } + + public void setLastPlayedMoves(List<Move> lastPlayedMoves) { + this.lastPlayedMoves = lastPlayedMoves; } public void placeCard(int playerIndex, Card card) { @@ -61,14 +71,4 @@ public class Table { Board board = boards.get(playerIndex); board.setGold(board.getGold() + goldBonus); } - - public PlayerTurnInfo createPlayerTurnInfo(int playerIndex, List<Card> cards) { - PlayerTurnInfo pti = new PlayerTurnInfo(playerIndex, this); - pti.setHand(createHand(playerIndex, cards)); - return pti; - } - - private List<HandCard> createHand(int playerIndex, List<Card> cards) { - return cards.stream().map(c -> new HandCard(c, this, playerIndex)).collect(Collectors.toList()); - } } diff --git a/src/main/java/org/luxons/sevenwonders/game/cards/Card.java b/src/main/java/org/luxons/sevenwonders/game/cards/Card.java index e2d1d940..148d7ed0 100644 --- a/src/main/java/org/luxons/sevenwonders/game/cards/Card.java +++ b/src/main/java/org/luxons/sevenwonders/game/cards/Card.java @@ -1,6 +1,7 @@ package org.luxons.sevenwonders.game.cards; import java.util.List; +import java.util.Objects; import org.luxons.sevenwonders.game.api.Table; import org.luxons.sevenwonders.game.boards.Board; @@ -81,6 +82,23 @@ public class Card { } @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Card card = (Card)o; + return Objects.equals(name, card.name); + } + + @Override + public int hashCode() { + return Objects.hash(name); + } + + @Override public String toString() { return "Card{" + "name='" + name + '\'' + ", color=" + color + ", requirements=" + requirements + ", effects=" + effects + '}'; |