From e93ad8bbe1ebb937e42e74446e1e4fef715b44fc Mon Sep 17 00:00:00 2001 From: jbion Date: Mon, 26 Dec 2016 15:47:45 +0100 Subject: Replace switches on MoveType by multiple Move implementations --- .../sevenwonders/actions/PrepareCardAction.java | 2 +- .../java/org/luxons/sevenwonders/game/Game.java | 58 ++++++---------------- .../org/luxons/sevenwonders/game/api/Move.java | 55 -------------------- .../org/luxons/sevenwonders/game/api/MoveType.java | 5 -- .../luxons/sevenwonders/game/api/PlayerMove.java | 40 +++++++++++++++ .../org/luxons/sevenwonders/game/api/Table.java | 1 + .../sevenwonders/game/moves/BuildWonderMove.java | 31 ++++++++++++ .../sevenwonders/game/moves/DiscardMove.java | 29 +++++++++++ .../org/luxons/sevenwonders/game/moves/Move.java | 50 +++++++++++++++++++ .../luxons/sevenwonders/game/moves/MoveType.java | 27 ++++++++++ .../sevenwonders/game/moves/PlayCardMove.java | 29 +++++++++++ .../luxons/sevenwonders/game/wonders/Wonder.java | 10 +++- 12 files changed, 232 insertions(+), 105 deletions(-) delete mode 100644 src/main/java/org/luxons/sevenwonders/game/api/Move.java delete mode 100644 src/main/java/org/luxons/sevenwonders/game/api/MoveType.java create mode 100644 src/main/java/org/luxons/sevenwonders/game/api/PlayerMove.java create mode 100644 src/main/java/org/luxons/sevenwonders/game/moves/BuildWonderMove.java create mode 100644 src/main/java/org/luxons/sevenwonders/game/moves/DiscardMove.java create mode 100644 src/main/java/org/luxons/sevenwonders/game/moves/Move.java create mode 100644 src/main/java/org/luxons/sevenwonders/game/moves/MoveType.java create mode 100644 src/main/java/org/luxons/sevenwonders/game/moves/PlayCardMove.java diff --git a/src/main/java/org/luxons/sevenwonders/actions/PrepareCardAction.java b/src/main/java/org/luxons/sevenwonders/actions/PrepareCardAction.java index 66ab8691..a59f08ee 100644 --- a/src/main/java/org/luxons/sevenwonders/actions/PrepareCardAction.java +++ b/src/main/java/org/luxons/sevenwonders/actions/PrepareCardAction.java @@ -1,6 +1,6 @@ package org.luxons.sevenwonders.actions; -import org.luxons.sevenwonders.game.api.Move; +import org.luxons.sevenwonders.game.moves.Move; public class PrepareCardAction { diff --git a/src/main/java/org/luxons/sevenwonders/game/Game.java b/src/main/java/org/luxons/sevenwonders/game/Game.java index a2ebf618..7b79f716 100644 --- a/src/main/java/org/luxons/sevenwonders/game/Game.java +++ b/src/main/java/org/luxons/sevenwonders/game/Game.java @@ -7,13 +7,14 @@ import java.util.Map; import java.util.stream.Collectors; import java.util.stream.IntStream; -import org.luxons.sevenwonders.game.api.Move; +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.Decks; import org.luxons.sevenwonders.game.cards.Hands; +import org.luxons.sevenwonders.game.moves.Move; public class Game { @@ -70,9 +71,11 @@ public class Game { return pti; } - public void prepareCard(Move move) throws InvalidMoveException { + public void prepareCard(int playerIndex, PlayerMove playerMove) throws InvalidMoveException { + Card card = decks.getCard(playerMove.getCardName()); + Move move = playerMove.getType().resolve(playerIndex, card, playerMove); validate(move); - preparedMoves.put(move.getPlayerIndex(), move); + preparedMoves.put(playerIndex, move); } private void validate(Move move) throws InvalidMoveException { @@ -80,14 +83,14 @@ public class Game { if (hand == null) { throw new InvalidMoveException("Invalid player index " + move.getPlayerIndex()); } - Card card = decks.getCard(move.getCardName()); + Card card = move.getCard(); if (!hand.contains(card)) { throw new InvalidMoveException( - "Player " + move.getPlayerIndex() + " does not have the card " + move.getCardName()); + "Player " + move.getPlayerIndex() + " does not have the card " + move.getCard().getName()); } - if (!move.isValid(table, card)) { + if (!move.isValid(table)) { throw new InvalidMoveException( - "Player " + move.getPlayerIndex() + " cannot play the card " + move.getCardName()); + "Player " + move.getPlayerIndex() + " cannot play the card " + move.getCard().getName()); } } @@ -137,8 +140,8 @@ public class Game { private void placePreparedCards(List playedMoves) { playedMoves.forEach(move -> { - placeCard(move); - removeFromHand(move.getPlayerIndex(), move.getCardName()); + move.place(table, discardedCards, settings); + removeFromHand(move.getPlayerIndex(), move.getCard()); }); } @@ -146,43 +149,12 @@ public class Game { return hands.maxOneCardRemains(); } - private void placeCard(Move move) { - Card card = decks.getCard(move.getCardName()); - switch (move.getType()) { - case PLAY: - table.placeCard(move.getPlayerIndex(), card); - break; - case UPGRADE_WONDER: - table.buildWonderStage(move.getPlayerIndex(), card.getBack()); - break; - case DISCARD: - discardedCards.add(card); - break; - } - } - - private void removeFromHand(int playerIndex, String cardName) { - Card card = decks.getCard(cardName); - List hand = hands.get(playerIndex); - hand.remove(card); + private void removeFromHand(int playerIndex, Card card) { + hands.get(playerIndex).remove(card); } private void activatePlayedCards(List playedMoves) { - playedMoves.forEach(this::activateCard); - } - - private void activateCard(Move move) { - switch (move.getType()) { - case PLAY: - table.activateCard(move.getPlayerIndex(), decks.getCard(move.getCardName()), move.getBoughtResources()); - break; - case UPGRADE_WONDER: - table.activateCurrentWonderStage(move.getPlayerIndex(), move.getBoughtResources()); - break; - case DISCARD: - table.giveGoldForDiscarded(move.getPlayerIndex(), settings.getDiscardedCardGold()); - break; - } + playedMoves.forEach(move -> move.activate(table, discardedCards, settings)); } private boolean endOfAgeReached() { diff --git a/src/main/java/org/luxons/sevenwonders/game/api/Move.java b/src/main/java/org/luxons/sevenwonders/game/api/Move.java deleted file mode 100644 index abf0e27d..00000000 --- a/src/main/java/org/luxons/sevenwonders/game/api/Move.java +++ /dev/null @@ -1,55 +0,0 @@ -package org.luxons.sevenwonders.game.api; - -import java.util.ArrayList; -import java.util.List; - -import org.luxons.sevenwonders.game.cards.Card; -import org.luxons.sevenwonders.game.resources.BoughtResources; - -public class Move { - - private int playerIndex; - - private String cardName; - - private MoveType type; - - private List boughtResources = new ArrayList<>(); - - public int getPlayerIndex() { - return playerIndex; - } - - public void setPlayerIndex(int playerIndex) { - this.playerIndex = playerIndex; - } - - public String getCardName() { - return cardName; - } - - public void setCardName(String cardName) { - this.cardName = cardName; - } - - public MoveType getType() { - return type; - } - - public void setType(MoveType type) { - this.type = type; - } - - public List getBoughtResources() { - return boughtResources; - } - - public void setBoughtResources(List boughtResources) { - this.boughtResources = boughtResources; - } - - public boolean isValid(Table table, Card resolvedCard) { - return type == MoveType.DISCARD || resolvedCard.getRequirements() - .isAffordedBy(table, playerIndex, boughtResources); - } -} diff --git a/src/main/java/org/luxons/sevenwonders/game/api/MoveType.java b/src/main/java/org/luxons/sevenwonders/game/api/MoveType.java deleted file mode 100644 index 66f2331a..00000000 --- a/src/main/java/org/luxons/sevenwonders/game/api/MoveType.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.luxons.sevenwonders.game.api; - -public enum MoveType { - PLAY, UPGRADE_WONDER, DISCARD -} diff --git a/src/main/java/org/luxons/sevenwonders/game/api/PlayerMove.java b/src/main/java/org/luxons/sevenwonders/game/api/PlayerMove.java new file mode 100644 index 00000000..44e32c7f --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/api/PlayerMove.java @@ -0,0 +1,40 @@ +package org.luxons.sevenwonders.game.api; + +import java.util.ArrayList; +import java.util.List; + +import org.luxons.sevenwonders.game.moves.MoveType; +import org.luxons.sevenwonders.game.resources.BoughtResources; + +public class PlayerMove { + + private String cardName; + + private MoveType type; + + private List boughtResources = new ArrayList<>(); + + public String getCardName() { + return cardName; + } + + public void setCardName(String cardName) { + this.cardName = cardName; + } + + public MoveType getType() { + return type; + } + + public void setType(MoveType type) { + this.type = type; + } + + public List getBoughtResources() { + return boughtResources; + } + + public void setBoughtResources(List boughtResources) { + this.boughtResources = boughtResources; + } +} 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 9655cb36..7c3ff34e 100644 --- a/src/main/java/org/luxons/sevenwonders/game/api/Table.java +++ b/src/main/java/org/luxons/sevenwonders/game/api/Table.java @@ -2,6 +2,7 @@ package org.luxons.sevenwonders.game.api; import java.util.List; +import org.luxons.sevenwonders.game.moves.Move; import org.luxons.sevenwonders.game.boards.Board; import org.luxons.sevenwonders.game.boards.RelativeBoardPosition; import org.luxons.sevenwonders.game.cards.Card; diff --git a/src/main/java/org/luxons/sevenwonders/game/moves/BuildWonderMove.java b/src/main/java/org/luxons/sevenwonders/game/moves/BuildWonderMove.java new file mode 100644 index 00000000..2e682fde --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/moves/BuildWonderMove.java @@ -0,0 +1,31 @@ +package org.luxons.sevenwonders.game.moves; + +import java.util.List; + +import org.luxons.sevenwonders.game.Settings; +import org.luxons.sevenwonders.game.api.PlayerMove; +import org.luxons.sevenwonders.game.api.Table; +import org.luxons.sevenwonders.game.boards.Board; +import org.luxons.sevenwonders.game.cards.Card; + +public class BuildWonderMove extends Move { + + BuildWonderMove(int playerIndex, Card card, PlayerMove move) { + super(playerIndex, card, move); + } + + public boolean isValid(Table table) { + Board board = table.getBoard(getPlayerIndex()); + return board.getWonder().isNextStageBuildable(table, getPlayerIndex(), getBoughtResources()); + } + + @Override + public void place(Table table, List discardedCards, Settings settings) { + table.buildWonderStage(getPlayerIndex(), getCard().getBack()); + } + + @Override + public void activate(Table table, List discardedCards, Settings settings) { + table.activateCurrentWonderStage(getPlayerIndex(), getBoughtResources()); + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/moves/DiscardMove.java b/src/main/java/org/luxons/sevenwonders/game/moves/DiscardMove.java new file mode 100644 index 00000000..9e4bcd68 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/moves/DiscardMove.java @@ -0,0 +1,29 @@ +package org.luxons.sevenwonders.game.moves; + +import java.util.List; + +import org.luxons.sevenwonders.game.Settings; +import org.luxons.sevenwonders.game.api.PlayerMove; +import org.luxons.sevenwonders.game.api.Table; +import org.luxons.sevenwonders.game.cards.Card; + +public class DiscardMove extends Move { + + DiscardMove(int playerIndex, Card card, PlayerMove move) { + super(playerIndex, card, move); + } + + public boolean isValid(Table table) { + return true; + } + + @Override + public void place(Table table, List discardedCards, Settings settings) { + discardedCards.add(getCard()); + } + + @Override + public void activate(Table table, List discardedCards, Settings settings) { + table.giveGoldForDiscarded(getPlayerIndex(), settings.getDiscardedCardGold()); + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/moves/Move.java b/src/main/java/org/luxons/sevenwonders/game/moves/Move.java new file mode 100644 index 00000000..584589a8 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/moves/Move.java @@ -0,0 +1,50 @@ +package org.luxons.sevenwonders.game.moves; + +import java.util.ArrayList; +import java.util.List; + +import org.luxons.sevenwonders.game.Settings; +import org.luxons.sevenwonders.game.api.PlayerMove; +import org.luxons.sevenwonders.game.api.Table; +import org.luxons.sevenwonders.game.cards.Card; +import org.luxons.sevenwonders.game.resources.BoughtResources; + +public abstract class Move { + + private int playerIndex; + + private Card card; + + private MoveType type; + + private List boughtResources = new ArrayList<>(); + + Move(int playerIndex, Card card, PlayerMove move) { + this.playerIndex = playerIndex; + this.card = card; + this.type = move.getType(); + this.boughtResources = move.getBoughtResources(); + } + + public int getPlayerIndex() { + return playerIndex; + } + + public Card getCard() { + return card; + } + + public MoveType getType() { + return type; + } + + public List getBoughtResources() { + return boughtResources; + } + + public abstract boolean isValid(Table table); + + public abstract void place(Table table, List discardedCards, Settings settings); + + public abstract void activate(Table table, List discardedCards, Settings settings); +} diff --git a/src/main/java/org/luxons/sevenwonders/game/moves/MoveType.java b/src/main/java/org/luxons/sevenwonders/game/moves/MoveType.java new file mode 100644 index 00000000..ff521bae --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/moves/MoveType.java @@ -0,0 +1,27 @@ +package org.luxons.sevenwonders.game.moves; + +import org.luxons.sevenwonders.game.api.PlayerMove; +import org.luxons.sevenwonders.game.cards.Card; + +public enum MoveType { + PLAY { + @Override + public Move resolve(int playerIndex, Card card, PlayerMove move) { + return new PlayCardMove(playerIndex, card, move); + } + }, + UPGRADE_WONDER { + @Override + public Move resolve(int playerIndex, Card card, PlayerMove move) { + return new BuildWonderMove(playerIndex, card, move); + } + }, + DISCARD { + @Override + public Move resolve(int playerIndex, Card card, PlayerMove move) { + return new DiscardMove(playerIndex, card, move); + } + }; + + public abstract Move resolve(int playerIndex, Card card, PlayerMove move); +} diff --git a/src/main/java/org/luxons/sevenwonders/game/moves/PlayCardMove.java b/src/main/java/org/luxons/sevenwonders/game/moves/PlayCardMove.java new file mode 100644 index 00000000..ee4dbb11 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/moves/PlayCardMove.java @@ -0,0 +1,29 @@ +package org.luxons.sevenwonders.game.moves; + +import java.util.List; + +import org.luxons.sevenwonders.game.Settings; +import org.luxons.sevenwonders.game.api.PlayerMove; +import org.luxons.sevenwonders.game.api.Table; +import org.luxons.sevenwonders.game.cards.Card; + +public class PlayCardMove extends Move { + + PlayCardMove(int playerIndex, Card card, PlayerMove move) { + super(playerIndex, card, move); + } + + public boolean isValid(Table table) { + return getCard().getRequirements().isAffordedBy(table, getPlayerIndex(), getBoughtResources()); + } + + @Override + public void place(Table table, List discardedCards, Settings settings) { + table.placeCard(getPlayerIndex(), getCard()); + } + + @Override + public void activate(Table table, List discardedCards, Settings settings) { + table.activateCard(getPlayerIndex(), getCard(), getBoughtResources()); + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java b/src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java index 15b2b863..d526cf8b 100644 --- a/src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java +++ b/src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java @@ -3,9 +3,9 @@ package org.luxons.sevenwonders.game.wonders; import java.util.Arrays; import java.util.List; -import org.luxons.sevenwonders.game.resources.BoughtResources; import org.luxons.sevenwonders.game.api.Table; import org.luxons.sevenwonders.game.cards.CardBack; +import org.luxons.sevenwonders.game.resources.BoughtResources; import org.luxons.sevenwonders.game.resources.ResourceType; public class Wonder { @@ -63,6 +63,14 @@ public class Wonder { this.image = image; } + public boolean isNextStageBuildable(Table table, int playerIndex, List boughtResources) { + int nextLevel = getNbBuiltStages(); + if (nextLevel == stages.size()) { + return false; + } + return getNextStage().isBuildable(table, playerIndex, boughtResources); + } + public void buildLevel(CardBack cardBack) { getNextStage().build(cardBack); } -- cgit