summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjbion <joffrey.bion@amadeus.com>2016-12-26 15:47:45 +0100
committerjbion <joffrey.bion@amadeus.com>2016-12-26 15:47:45 +0100
commite93ad8bbe1ebb937e42e74446e1e4fef715b44fc (patch)
tree24e7837887b89ae6a628d51491ab4e99155b7908
parentMake LobbyController always send a list of lobbies on /topic/games (diff)
downloadseven-wonders-e93ad8bbe1ebb937e42e74446e1e4fef715b44fc.tar.gz
seven-wonders-e93ad8bbe1ebb937e42e74446e1e4fef715b44fc.tar.bz2
seven-wonders-e93ad8bbe1ebb937e42e74446e1e4fef715b44fc.zip
Replace switches on MoveType by multiple Move implementations
-rw-r--r--src/main/java/org/luxons/sevenwonders/actions/PrepareCardAction.java2
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/Game.java58
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/api/MoveType.java5
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/api/PlayerMove.java (renamed from src/main/java/org/luxons/sevenwonders/game/api/Move.java)19
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/api/Table.java1
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/moves/BuildWonderMove.java31
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/moves/DiscardMove.java29
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/moves/Move.java50
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/moves/MoveType.java27
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/moves/PlayCardMove.java29
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java10
11 files changed, 194 insertions, 67 deletions
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<Move> 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<Card> hand = hands.get(playerIndex);
- hand.remove(card);
+ private void removeFromHand(int playerIndex, Card card) {
+ hands.get(playerIndex).remove(card);
}
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()), 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/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/Move.java b/src/main/java/org/luxons/sevenwonders/game/api/PlayerMove.java
index abf0e27d..44e32c7f 100644
--- a/src/main/java/org/luxons/sevenwonders/game/api/Move.java
+++ b/src/main/java/org/luxons/sevenwonders/game/api/PlayerMove.java
@@ -3,12 +3,10 @@ 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.moves.MoveType;
import org.luxons.sevenwonders.game.resources.BoughtResources;
-public class Move {
-
- private int playerIndex;
+public class PlayerMove {
private String cardName;
@@ -16,14 +14,6 @@ public class Move {
private List<BoughtResources> boughtResources = new ArrayList<>();
- public int getPlayerIndex() {
- return playerIndex;
- }
-
- public void setPlayerIndex(int playerIndex) {
- this.playerIndex = playerIndex;
- }
-
public String getCardName() {
return cardName;
}
@@ -47,9 +37,4 @@ public class Move {
public void setBoughtResources(List<BoughtResources> 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/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<Card> discardedCards, Settings settings) {
+ table.buildWonderStage(getPlayerIndex(), getCard().getBack());
+ }
+
+ @Override
+ public void activate(Table table, List<Card> 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<Card> discardedCards, Settings settings) {
+ discardedCards.add(getCard());
+ }
+
+ @Override
+ public void activate(Table table, List<Card> 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> 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<BoughtResources> getBoughtResources() {
+ return boughtResources;
+ }
+
+ public abstract boolean isValid(Table table);
+
+ public abstract void place(Table table, List<Card> discardedCards, Settings settings);
+
+ public abstract void activate(Table table, List<Card> 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<Card> discardedCards, Settings settings) {
+ table.placeCard(getPlayerIndex(), getCard());
+ }
+
+ @Override
+ public void activate(Table table, List<Card> 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> boughtResources) {
+ int nextLevel = getNbBuiltStages();
+ if (nextLevel == stages.size()) {
+ return false;
+ }
+ return getNextStage().isBuildable(table, playerIndex, boughtResources);
+ }
+
public void buildLevel(CardBack cardBack) {
getNextStage().build(cardBack);
}
bgstack15