From 9049dd004df71619e08a350c0a6e49455041d31b Mon Sep 17 00:00:00 2001 From: Joffrey BION Date: Fri, 6 Jul 2018 00:43:50 +0200 Subject: Kotlin mig: cards package --- .../java/org/luxons/sevenwonders/game/Game.java | 6 +- .../org/luxons/sevenwonders/game/cards/Card.java | 128 --------------------- .../luxons/sevenwonders/game/cards/CardBack.java | 14 --- .../org/luxons/sevenwonders/game/cards/Color.java | 11 -- .../org/luxons/sevenwonders/game/cards/Decks.java | 65 ----------- .../game/cards/HandRotationDirection.java | 21 ---- .../org/luxons/sevenwonders/game/cards/Hands.java | 64 ----------- .../sevenwonders/game/cards/Requirements.java | 108 ----------------- 8 files changed, 3 insertions(+), 414 deletions(-) delete mode 100644 game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Card.java delete mode 100644 game-engine/src/main/java/org/luxons/sevenwonders/game/cards/CardBack.java delete mode 100644 game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Color.java delete mode 100644 game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Decks.java delete mode 100644 game-engine/src/main/java/org/luxons/sevenwonders/game/cards/HandRotationDirection.java delete mode 100644 game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Hands.java delete mode 100644 game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java (limited to 'game-engine/src/main/java') 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 index 2696adbf..1d88fb9f 100644 --- a/game-engine/src/main/java/org/luxons/sevenwonders/game/Game.java +++ b/game-engine/src/main/java/org/luxons/sevenwonders/game/Game.java @@ -109,7 +109,7 @@ public class Game { } public CardBack prepareMove(int playerIndex, PlayerMove playerMove) throws InvalidMoveException { - Card card = decks.getCard(playerMove.getCardName()); + Card card = decks.getCard(table.getCurrentAge(), playerMove.getCardName()); Move move = playerMove.getType().resolve(playerIndex, card, playerMove); validate(move); preparedMoves.put(playerIndex, move); @@ -143,7 +143,7 @@ public class Game { private void rotateHandsIfRelevant() { // we don't rotate hands if some player can play his last card (with the special ability) if (!hands.maxOneCardRemains()) { - hands.rotate(table.getHandRotationDirection()); + hands = hands.rotate(table.getHandRotationDirection()); } } @@ -196,7 +196,7 @@ public class Game { private void discardHand(int playerIndex) { List hand = hands.get(playerIndex); discardedCards.addAll(hand); - hand.clear(); + hands = hands.discard(playerIndex); } private void removeFromHand(int playerIndex, Card card) { diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Card.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Card.java deleted file mode 100644 index 2d2f6777..00000000 --- a/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Card.java +++ /dev/null @@ -1,128 +0,0 @@ -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; -import org.luxons.sevenwonders.game.effects.Effect; -import org.luxons.sevenwonders.game.resources.ResourceTransactions; - -public class Card { - - private final String name; - - private final Color color; - - private final Requirements requirements; - - private final List effects; - - private final String chainParent; - - private final List chainChildren; - - private final String image; - - private CardBack back; - - public Card(String name, Color color, Requirements requirements, List effects, String chainParent, - List chainChildren, String image) { - this.name = name; - this.color = color; - this.requirements = requirements; - this.chainParent = chainParent; - this.effects = effects; - this.chainChildren = chainChildren; - this.image = image; - } - - public String getName() { - return name; - } - - public Color getColor() { - return color; - } - - public String getChainParent() { - return chainParent; - } - - public Requirements getRequirements() { - return requirements; - } - - public List getEffects() { - return effects; - } - - public List getChainChildren() { - return chainChildren; - } - - public String getImage() { - return image; - } - - public CardBack getBack() { - return back; - } - - public void setBack(CardBack back) { - this.back = back; - } - - private boolean isAllowedOnBoard(Board board) { - return !board.isPlayed(name); // cannot play twice the same card - } - - public boolean isChainableOn(Board board) { - return isAllowedOnBoard(board) && board.isPlayed(chainParent); - } - - public boolean isFreeFor(Board board) { - if (!isAllowedOnBoard(board)) { - return false; - } - return isChainableOn(board) || (requirements.areMetWithoutNeighboursBy(board) && requirements.getGold() == 0); - } - - public boolean isPlayable(Table table, int playerIndex) { - Board board = table.getBoard(playerIndex); - if (!isAllowedOnBoard(board)) { - return false; - } - return isChainableOn(board) || requirements.areMetBy(table, playerIndex); - } - - public void applyTo(Table table, int playerIndex, ResourceTransactions transactions) { - Board playerBoard = table.getBoard(playerIndex); - if (!isChainableOn(playerBoard)) { - requirements.pay(table, playerIndex, transactions); - } - effects.forEach(e -> e.apply(table, playerIndex)); - } - - @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 + '}'; - } -} diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/CardBack.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/CardBack.java deleted file mode 100644 index f925b6c4..00000000 --- a/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/CardBack.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.luxons.sevenwonders.game.cards; - -public class CardBack { - - private final String image; - - public CardBack(String image) { - this.image = image; - } - - public String getImage() { - return image; - } -} diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Color.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Color.java deleted file mode 100644 index 80d06c55..00000000 --- a/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Color.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.luxons.sevenwonders.game.cards; - -public enum Color { - BROWN, - GREY, - YELLOW, - BLUE, - GREEN, - RED, - PURPLE -} diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Decks.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Decks.java deleted file mode 100644 index aa2b00bf..00000000 --- a/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Decks.java +++ /dev/null @@ -1,65 +0,0 @@ -package org.luxons.sevenwonders.game.cards; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class Decks { - - private Map> cardsPerAge = new HashMap<>(); - - public Decks(Map> cardsPerAge) { - this.cardsPerAge = cardsPerAge; - } - - public Card getCard(String cardName) throws CardNotFoundException { - return cardsPerAge.values() - .stream() - .flatMap(List::stream) - .filter(c -> c.getName().equals(cardName)) - .findAny() - .orElseThrow(() -> new CardNotFoundException(cardName)); - } - - public Hands deal(int age, int nbPlayers) { - List deck = getDeck(age); - validateNbCards(deck, nbPlayers); - return deal(deck, nbPlayers); - } - - private List getDeck(int age) { - List deck = cardsPerAge.get(age); - if (deck == null) { - throw new IllegalArgumentException("No deck found for age " + age); - } - return deck; - } - - private void validateNbCards(List deck, int nbPlayers) { - if (nbPlayers == 0) { - throw new IllegalArgumentException("Cannot deal cards between 0 players"); - } - if (deck.size() % nbPlayers != 0) { - throw new IllegalArgumentException( - String.format("Cannot deal %d cards evenly between %d players", deck.size(), nbPlayers)); - } - } - - private Hands deal(List deck, int nbPlayers) { - Map> hands = new HashMap<>(nbPlayers); - for (int i = 0; i < nbPlayers; i++) { - hands.put(i, new ArrayList<>()); - } - for (int i = 0; i < deck.size(); i++) { - hands.get(i % nbPlayers).add(deck.get(i)); - } - return new Hands(hands, nbPlayers); - } - - class CardNotFoundException extends RuntimeException { - CardNotFoundException(String message) { - super(message); - } - } -} diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/HandRotationDirection.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/HandRotationDirection.java deleted file mode 100644 index f3902fb5..00000000 --- a/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/HandRotationDirection.java +++ /dev/null @@ -1,21 +0,0 @@ -package org.luxons.sevenwonders.game.cards; - -public enum HandRotationDirection { - LEFT(-1), - RIGHT(1); - - private final int indexOffset; - - HandRotationDirection(int i) { - this.indexOffset = i; - } - - public int getIndexOffset() { - return indexOffset; - } - - public static HandRotationDirection forAge(int age) { - // clockwise (pass to the left) at age 1, and alternating - return age % 2 == 0 ? HandRotationDirection.RIGHT : HandRotationDirection.LEFT; - } -} diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Hands.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Hands.java deleted file mode 100644 index 4a8bc143..00000000 --- a/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Hands.java +++ /dev/null @@ -1,64 +0,0 @@ -package org.luxons.sevenwonders.game.cards; - -import java.util.HashMap; -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.Table; - -public class Hands { - - private final int nbPlayers; - - private Map> hands; - - Hands(Map> hands, int nbPlayers) { - this.hands = hands; - this.nbPlayers = nbPlayers; - } - - public List get(int playerIndex) { - if (!hands.containsKey(playerIndex)) { - throw new PlayerIndexOutOfBoundsException(playerIndex); - } - return hands.get(playerIndex); - } - - public List createHand(Table table, int playerIndex) { - return hands.get(playerIndex) - .stream() - .map(c -> new HandCard(c, table, playerIndex)) - .collect(Collectors.toList()); - } - - public Hands rotate(HandRotationDirection direction) { - Map> newHands = new HashMap<>(hands.size()); - for (int i = 0; i < nbPlayers; i++) { - int newIndex = Math.floorMod(i + direction.getIndexOffset(), nbPlayers); - newHands.put(newIndex, hands.get(i)); - } - return new Hands(newHands, nbPlayers); - } - - public boolean isEmpty() { - return hands.values().stream().allMatch(List::isEmpty); - } - - public boolean maxOneCardRemains() { - return hands.values().stream().mapToInt(List::size).max().orElse(0) <= 1; - } - - public List gatherAndClear() { - List remainingCards = hands.values().stream().flatMap(List::stream).collect(Collectors.toList()); - hands.clear(); - return remainingCards; - } - - class PlayerIndexOutOfBoundsException extends ArrayIndexOutOfBoundsException { - PlayerIndexOutOfBoundsException(int index) { - super(index); - } - } -} diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java deleted file mode 100644 index 8c7245ed..00000000 --- a/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java +++ /dev/null @@ -1,108 +0,0 @@ -package org.luxons.sevenwonders.game.cards; - -import org.luxons.sevenwonders.game.api.Table; -import org.luxons.sevenwonders.game.boards.Board; -import org.luxons.sevenwonders.game.resources.BestPriceCalculator; -import org.luxons.sevenwonders.game.resources.ResourceTransactions; -import org.luxons.sevenwonders.game.resources.Resources; - -public class Requirements { - - private int gold; - - private Resources resources = new Resources(); - - public int getGold() { - return gold; - } - - public void setGold(int gold) { - this.gold = gold; - } - - public Resources getResources() { - return resources; - } - - public void setResources(Resources resources) { - this.resources = resources; - } - - /** - * Returns whether the given board meets these requirements on its own. - * - * @param board - * the board to check - * - * @return true if the given board meets these requirements without any transaction with its neighbours - */ - boolean areMetWithoutNeighboursBy(Board board) { - return hasRequiredGold(board) && producesRequiredResources(board); - } - - /** - * Returns whether the given board meets these requirements, if the specified resources are bought from neighbours. - * - * @param board - * the board to check - * @param boughtResources - * the resources the player intends to buy - * - * @return true if the given board meets these requirements - */ - public boolean areMetWithHelpBy(Board board, ResourceTransactions boughtResources) { - if (!hasRequiredGold(board, boughtResources)) { - return false; - } - if (producesRequiredResources(board)) { - return true; - } - return producesRequiredResourcesWithHelp(board, boughtResources); - } - - /** - * Returns whether the given player's board meets these requirements, either on its own or by buying resources to - * neighbours. - * - * @param table - * the current game table - * @param playerIndex - * the index of the player to check - * - * @return true if the given player's board could meet these requirements - */ - boolean areMetBy(Table table, int playerIndex) { - Board board = table.getBoard(playerIndex); - if (!hasRequiredGold(board)) { - return false; - } - if (producesRequiredResources(board)) { - return true; - } - return BestPriceCalculator.bestPrice(resources, table, playerIndex) <= board.getGold() - gold; - } - - private boolean hasRequiredGold(Board board) { - return board.getGold() >= gold; - } - - private boolean hasRequiredGold(Board board, ResourceTransactions resourceTransactions) { - int resourcesPrice = board.getTradingRules().computeCost(resourceTransactions); - return board.getGold() >= gold + resourcesPrice; - } - - private boolean producesRequiredResources(Board board) { - return board.getProduction().contains(resources); - } - - private boolean producesRequiredResourcesWithHelp(Board board, ResourceTransactions transactions) { - Resources totalBoughtResources = transactions.asResources(); - Resources remainingResources = this.resources.minus(totalBoughtResources); - return board.getProduction().contains(remainingResources); - } - - public void pay(Table table, int playerIndex, ResourceTransactions transactions) { - table.getBoard(playerIndex).removeGold(gold); - transactions.execute(table, playerIndex); - } -} -- cgit