summaryrefslogtreecommitdiff
path: root/backend/src/main
diff options
context:
space:
mode:
authorJoffrey BION <joffrey.bion@gmail.com>2017-05-10 08:28:37 +0200
committerJoffrey BION <joffrey.bion@gmail.com>2017-05-10 08:28:37 +0200
commit12873636258bb39436886e9fc42fd34a39a34cc6 (patch)
tree336c647d0130af69fc077ccf0ac880a6f05d79ad /backend/src/main
parentAdd neighbour guild cards info to help for COPY_GUILD action (diff)
downloadseven-wonders-12873636258bb39436886e9fc42fd34a39a34cc6.tar.gz
seven-wonders-12873636258bb39436886e9fc42fd34a39a34cc6.tar.bz2
seven-wonders-12873636258bb39436886e9fc42fd34a39a34cc6.zip
Change move validation system and fix chained cards validation
Diffstat (limited to 'backend/src/main')
-rw-r--r--backend/src/main/java/org/luxons/sevenwonders/game/Game.java13
-rw-r--r--backend/src/main/java/org/luxons/sevenwonders/game/cards/Card.java15
-rw-r--r--backend/src/main/java/org/luxons/sevenwonders/game/moves/BuildWonderMove.java11
-rw-r--r--backend/src/main/java/org/luxons/sevenwonders/game/moves/CardFromHandMove.java9
-rw-r--r--backend/src/main/java/org/luxons/sevenwonders/game/moves/CopyGuildMove.java15
-rw-r--r--backend/src/main/java/org/luxons/sevenwonders/game/moves/InvalidMoveException.java8
-rw-r--r--backend/src/main/java/org/luxons/sevenwonders/game/moves/Move.java2
-rw-r--r--backend/src/main/java/org/luxons/sevenwonders/game/moves/PlayCardMove.java13
-rw-r--r--backend/src/main/java/org/luxons/sevenwonders/game/moves/PlayFreeCardMove.java12
9 files changed, 60 insertions, 38 deletions
diff --git a/backend/src/main/java/org/luxons/sevenwonders/game/Game.java b/backend/src/main/java/org/luxons/sevenwonders/game/Game.java
index c34880d2..24cde764 100644
--- a/backend/src/main/java/org/luxons/sevenwonders/game/Game.java
+++ b/backend/src/main/java/org/luxons/sevenwonders/game/Game.java
@@ -17,6 +17,7 @@ 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;
@@ -112,11 +113,7 @@ public class Game {
private void validate(Move move) throws InvalidMoveException {
List<Card> hand = hands.get(move.getPlayerIndex());
- if (!move.isValid(table, hand)) {
- throw new InvalidMoveException(
- "Player " + move.getPlayerIndex() + " cannot play the card " + move.getCard().getName()
- + " with the given resources");
- }
+ move.validate(table, hand);
}
public boolean areAllPlayersReady() {
@@ -227,10 +224,4 @@ public class Game {
super("Player " + playerIndex + " is not ready to play");
}
}
-
- private static class InvalidMoveException extends IllegalArgumentException {
- InvalidMoveException(String message) {
- super(message);
- }
- }
}
diff --git a/backend/src/main/java/org/luxons/sevenwonders/game/cards/Card.java b/backend/src/main/java/org/luxons/sevenwonders/game/cards/Card.java
index da877f30..084d19a5 100644
--- a/backend/src/main/java/org/luxons/sevenwonders/game/cards/Card.java
+++ b/backend/src/main/java/org/luxons/sevenwonders/game/cards/Card.java
@@ -73,18 +73,25 @@ public class Card {
this.back = back;
}
+ private boolean isAllowedOnBoard(Board board) {
+ return !board.isPlayed(name); // cannot play twice the same card
+ }
+
public boolean isChainableOn(Board board) {
- return board.isPlayed(chainParent);
+ return isAllowedOnBoard(board) && board.isPlayed(chainParent);
}
public boolean isFreeFor(Board board) {
- return requirements.areMetWithoutNeighboursBy(board) && requirements.getGold() == 0;
+ 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 (board.isPlayed(name)) {
- return false; // cannot play twice the same card
+ if (!isAllowedOnBoard(board)) {
+ return false;
}
return isChainableOn(board) || requirements.couldBeMetBy(table, playerIndex);
}
diff --git a/backend/src/main/java/org/luxons/sevenwonders/game/moves/BuildWonderMove.java b/backend/src/main/java/org/luxons/sevenwonders/game/moves/BuildWonderMove.java
index bddd6ec6..f1cb50b3 100644
--- a/backend/src/main/java/org/luxons/sevenwonders/game/moves/BuildWonderMove.java
+++ b/backend/src/main/java/org/luxons/sevenwonders/game/moves/BuildWonderMove.java
@@ -15,12 +15,13 @@ public class BuildWonderMove extends CardFromHandMove {
}
@Override
- public boolean isValid(Table table, List<Card> playerHand) {
- if (!super.isValid(table, playerHand)) {
- return false;
- }
+ public void validate(Table table, List<Card> playerHand) throws InvalidMoveException {
+ super.validate(table, playerHand);
Board board = table.getBoard(getPlayerIndex());
- return board.getWonder().isNextStageBuildable(table, getPlayerIndex(), getBoughtResources());
+ if (!board.getWonder().isNextStageBuildable(table, getPlayerIndex(), getBoughtResources())) {
+ throw new InvalidMoveException(
+ String.format("Player %d cannot upgrade his wonder with the given resources", getPlayerIndex()));
+ }
}
@Override
diff --git a/backend/src/main/java/org/luxons/sevenwonders/game/moves/CardFromHandMove.java b/backend/src/main/java/org/luxons/sevenwonders/game/moves/CardFromHandMove.java
index 7bbee1e5..1794966d 100644
--- a/backend/src/main/java/org/luxons/sevenwonders/game/moves/CardFromHandMove.java
+++ b/backend/src/main/java/org/luxons/sevenwonders/game/moves/CardFromHandMove.java
@@ -13,8 +13,11 @@ public abstract class CardFromHandMove extends Move {
}
@Override
- public boolean isValid(Table table, List<Card> playerHand) {
- return playerHand.contains(getCard());
+ public void validate(Table table, List<Card> playerHand) throws InvalidMoveException {
+ if (!playerHand.contains(getCard())) {
+ throw new InvalidMoveException(
+ String.format("Player %d does not have the card '%s' in his hand", getPlayerIndex(),
+ getCard().getName()));
+ }
}
-
}
diff --git a/backend/src/main/java/org/luxons/sevenwonders/game/moves/CopyGuildMove.java b/backend/src/main/java/org/luxons/sevenwonders/game/moves/CopyGuildMove.java
index 5ebde772..a93670c5 100644
--- a/backend/src/main/java/org/luxons/sevenwonders/game/moves/CopyGuildMove.java
+++ b/backend/src/main/java/org/luxons/sevenwonders/game/moves/CopyGuildMove.java
@@ -18,17 +18,24 @@ public class CopyGuildMove extends Move {
}
@Override
- public boolean isValid(Table table, List<Card> playerHand) {
+ public void validate(Table table, List<Card> playerHand) throws InvalidMoveException {
Board board = table.getBoard(getPlayerIndex());
if (!board.hasSpecial(SpecialAbility.COPY_GUILD)) {
- return false;
+ throw new InvalidMoveException(
+ String.format("Player %d does not have the ability to copy guild cards", getPlayerIndex()));
}
if (getCard().getColor() != Color.PURPLE) {
- return false;
+ throw new InvalidMoveException(
+ String.format("Player %d cannot copy card %s because it is not a guild card", getPlayerIndex(),
+ getCard().getName()));
}
boolean leftNeighbourHasIt = neighbourHasTheCard(table, RelativeBoardPosition.LEFT);
boolean rightNeighbourHasIt = neighbourHasTheCard(table, RelativeBoardPosition.RIGHT);
- return leftNeighbourHasIt || rightNeighbourHasIt;
+ if (!leftNeighbourHasIt && !rightNeighbourHasIt) {
+ throw new InvalidMoveException(
+ String.format("Player %d cannot copy card %s because none of his neighbour has it",
+ getPlayerIndex(), getCard().getName()));
+ }
}
private boolean neighbourHasTheCard(Table table, RelativeBoardPosition position) {
diff --git a/backend/src/main/java/org/luxons/sevenwonders/game/moves/InvalidMoveException.java b/backend/src/main/java/org/luxons/sevenwonders/game/moves/InvalidMoveException.java
new file mode 100644
index 00000000..58190274
--- /dev/null
+++ b/backend/src/main/java/org/luxons/sevenwonders/game/moves/InvalidMoveException.java
@@ -0,0 +1,8 @@
+package org.luxons.sevenwonders.game.moves;
+
+public class InvalidMoveException extends IllegalArgumentException {
+
+ public InvalidMoveException(String message) {
+ super(message);
+ }
+}
diff --git a/backend/src/main/java/org/luxons/sevenwonders/game/moves/Move.java b/backend/src/main/java/org/luxons/sevenwonders/game/moves/Move.java
index 8b6b60a8..40c61eea 100644
--- a/backend/src/main/java/org/luxons/sevenwonders/game/moves/Move.java
+++ b/backend/src/main/java/org/luxons/sevenwonders/game/moves/Move.java
@@ -42,7 +42,7 @@ public abstract class Move {
return boughtResources;
}
- public abstract boolean isValid(Table table, List<Card> playerHand);
+ public abstract void validate(Table table, List<Card> playerHand) throws InvalidMoveException;
public abstract void place(Table table, List<Card> discardedCards, Settings settings);
diff --git a/backend/src/main/java/org/luxons/sevenwonders/game/moves/PlayCardMove.java b/backend/src/main/java/org/luxons/sevenwonders/game/moves/PlayCardMove.java
index 05f707c7..82052981 100644
--- a/backend/src/main/java/org/luxons/sevenwonders/game/moves/PlayCardMove.java
+++ b/backend/src/main/java/org/luxons/sevenwonders/game/moves/PlayCardMove.java
@@ -15,12 +15,15 @@ public class PlayCardMove extends CardFromHandMove {
}
@Override
- public boolean isValid(Table table, List<Card> playerHand) {
- if (!super.isValid(table, playerHand)) {
- return false;
- }
+ public void validate(Table table, List<Card> playerHand) throws InvalidMoveException {
+ super.validate(table, playerHand);
Board board = table.getBoard(getPlayerIndex());
- return getCard().getRequirements().areMetWithHelpBy(board, getBoughtResources());
+ if (!getCard().isChainableOn(board) && !getCard().getRequirements()
+ .areMetWithHelpBy(board, getBoughtResources())) {
+ throw new InvalidMoveException(
+ String.format("Player %d cannot play the card %s with the given resources", getPlayerIndex(),
+ getCard().getName()));
+ }
}
@Override
diff --git a/backend/src/main/java/org/luxons/sevenwonders/game/moves/PlayFreeCardMove.java b/backend/src/main/java/org/luxons/sevenwonders/game/moves/PlayFreeCardMove.java
index fb28b09c..35f38ce2 100644
--- a/backend/src/main/java/org/luxons/sevenwonders/game/moves/PlayFreeCardMove.java
+++ b/backend/src/main/java/org/luxons/sevenwonders/game/moves/PlayFreeCardMove.java
@@ -15,12 +15,14 @@ public class PlayFreeCardMove extends CardFromHandMove {
}
@Override
- public boolean isValid(Table table, List<Card> playerHand) {
- if (!super.isValid(table, playerHand)) {
- return false;
- }
+ public void validate(Table table, List<Card> playerHand) throws InvalidMoveException {
+ super.validate(table, playerHand);
Board board = table.getBoard(getPlayerIndex());
- return board.canPlayFreeCard(table.getCurrentAge());
+ if (!board.canPlayFreeCard(table.getCurrentAge())) {
+ throw new InvalidMoveException(
+ String.format("Player %d cannot play the card %s for free", getPlayerIndex(),
+ getCard().getName()));
+ }
}
@Override
bgstack15