summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/Game.java20
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/api/Table.java19
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/boards/Board.java12
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/cards/HandRotationDirection.java5
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/moves/MoveType.java6
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/moves/PlayFreeCardMove.java39
-rw-r--r--src/test/java/org/luxons/sevenwonders/game/cards/HandsTest.java26
7 files changed, 88 insertions, 39 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/game/Game.java b/src/main/java/org/luxons/sevenwonders/game/Game.java
index 94bd039a..bc9c3b17 100644
--- a/src/main/java/org/luxons/sevenwonders/game/Game.java
+++ b/src/main/java/org/luxons/sevenwonders/game/Game.java
@@ -15,7 +15,6 @@ 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.HandRotationDirection;
import org.luxons.sevenwonders.game.cards.Hands;
import org.luxons.sevenwonders.game.effects.SpecialAbility;
import org.luxons.sevenwonders.game.moves.Move;
@@ -41,8 +40,6 @@ public class Game {
private Hands hands;
- private int currentAge = 0;
-
public Game(long id, Settings settings, List<Player> players, List<Board> boards, Decks decks) {
this.id = id;
this.settings = settings;
@@ -67,8 +64,8 @@ public class Game {
}
private void startNewAge() {
- currentAge++;
- hands = decks.deal(currentAge, table.getNbPlayers());
+ table.increaseCurrentAge();
+ hands = decks.deal(table.getCurrentAge(), table.getNbPlayers());
}
public List<PlayerTurnInfo> getTurnInfo() {
@@ -79,8 +76,6 @@ public class Game {
PlayerTurnInfo pti = new PlayerTurnInfo(player, table);
List<HandCard> hand = hands.createHand(table, player.getIndex());
pti.setHand(hand);
- pti.setCurrentAge(currentAge);
- pti.setHandRotationDirection(getHandRotationDirection());
Action action = determineAction(hand, table.getBoard(player.getIndex()));
pti.setAction(action);
pti.setMessage(action.getMessage());
@@ -138,7 +133,7 @@ public class Game {
}
} else if (!hands.maxOneCardRemains()) {
// we don't rotate hands if some player can play his last card (with the special ability)
- hands.rotate(getHandRotationDirection());
+ hands.rotate(table.getHandRotationDirection());
}
}
@@ -207,16 +202,11 @@ public class Game {
}
private void executeEndOfAgeEvents() {
- table.resolveMilitaryConflicts(currentAge);
- }
-
- private HandRotationDirection getHandRotationDirection() {
- // clockwise at age 1, and alternating
- return currentAge % 2 == 0 ? HandRotationDirection.LEFT : HandRotationDirection.RIGHT;
+ table.resolveMilitaryConflicts();
}
private boolean endOfGameReached() {
- return endOfAgeReached() && currentAge == LAST_AGE;
+ return endOfAgeReached() && table.getCurrentAge() == LAST_AGE;
}
public ScoreBoard computeScore() {
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 a87ea6ae..8b831527 100644
--- a/src/main/java/org/luxons/sevenwonders/game/api/Table.java
+++ b/src/main/java/org/luxons/sevenwonders/game/api/Table.java
@@ -4,6 +4,7 @@ import java.util.List;
import org.luxons.sevenwonders.game.boards.Board;
import org.luxons.sevenwonders.game.boards.RelativeBoardPosition;
+import org.luxons.sevenwonders.game.cards.HandRotationDirection;
import org.luxons.sevenwonders.game.moves.Move;
/**
@@ -16,6 +17,8 @@ public class Table {
private final List<Board> boards;
+ private int currentAge = 0;
+
private List<Move> lastPlayedMoves;
public Table(List<Board> boards) {
@@ -47,11 +50,23 @@ public class Table {
this.lastPlayedMoves = lastPlayedMoves;
}
- public void resolveMilitaryConflicts(int age) {
+ public int getCurrentAge() {
+ return currentAge;
+ }
+
+ public void increaseCurrentAge() {
+ this.currentAge++;
+ }
+
+ public HandRotationDirection getHandRotationDirection() {
+ return HandRotationDirection.forAge(currentAge);
+ }
+
+ public void resolveMilitaryConflicts() {
for (int i = 0; i < nbPlayers; i++) {
Board board1 = getBoard(i);
Board board2 = getBoard((i + 1) % nbPlayers);
- resolveConflict(board1, board2, age);
+ resolveConflict(board1, board2, currentAge);
}
}
diff --git a/src/main/java/org/luxons/sevenwonders/game/boards/Board.java b/src/main/java/org/luxons/sevenwonders/game/boards/Board.java
index 31bb6c9f..ab557d38 100644
--- a/src/main/java/org/luxons/sevenwonders/game/boards/Board.java
+++ b/src/main/java/org/luxons/sevenwonders/game/boards/Board.java
@@ -2,7 +2,9 @@ package org.luxons.sevenwonders.game.boards;
import java.util.ArrayList;
import java.util.EnumSet;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import java.util.Set;
import org.luxons.sevenwonders.game.Player;
@@ -35,6 +37,8 @@ public class Board {
private final Set<SpecialAbility> specialAbilities = EnumSet.noneOf(SpecialAbility.class);
+ private Map<Integer, Boolean> consumedFreeCards = new HashMap<>();
+
private Card copiedGuild;
private int gold;
@@ -118,6 +122,14 @@ public class Board {
return specialAbilities.contains(specialAbility);
}
+ public boolean canPlayFreeCard(int age) {
+ return hasSpecial(SpecialAbility.ONE_FREE_PER_AGE) && !consumedFreeCards.getOrDefault(age, false);
+ }
+
+ public void consumeFreeCard(int age) {
+ consumedFreeCards.put(age, true);
+ }
+
public void setCopiedGuild(Card copiedGuild) {
if (copiedGuild.getColor() != Color.PURPLE) {
throw new IllegalArgumentException("The given card '" + copiedGuild + "' is not a Guild card");
diff --git a/src/main/java/org/luxons/sevenwonders/game/cards/HandRotationDirection.java b/src/main/java/org/luxons/sevenwonders/game/cards/HandRotationDirection.java
index 2055ea4e..4d33a8db 100644
--- a/src/main/java/org/luxons/sevenwonders/game/cards/HandRotationDirection.java
+++ b/src/main/java/org/luxons/sevenwonders/game/cards/HandRotationDirection.java
@@ -12,4 +12,9 @@ public enum HandRotationDirection {
public int getIndexOffset() {
return indexOffset;
}
+
+ public static HandRotationDirection forAge(int age) {
+ // clockwise at age 1, and alternating
+ return age % 2 == 0 ? HandRotationDirection.LEFT : HandRotationDirection.RIGHT;
+ }
}
diff --git a/src/main/java/org/luxons/sevenwonders/game/moves/MoveType.java b/src/main/java/org/luxons/sevenwonders/game/moves/MoveType.java
index 4887c6f1..bf64344d 100644
--- a/src/main/java/org/luxons/sevenwonders/game/moves/MoveType.java
+++ b/src/main/java/org/luxons/sevenwonders/game/moves/MoveType.java
@@ -10,6 +10,12 @@ public enum MoveType {
return new PlayCardMove(playerIndex, card, move);
}
},
+ PLAY_FREE {
+ @Override
+ public Move resolve(int playerIndex, Card card, PlayerMove move) {
+ return new PlayFreeCardMove(playerIndex, card, move);
+ }
+ },
UPGRADE_WONDER {
@Override
public Move resolve(int playerIndex, Card card, PlayerMove move) {
diff --git a/src/main/java/org/luxons/sevenwonders/game/moves/PlayFreeCardMove.java b/src/main/java/org/luxons/sevenwonders/game/moves/PlayFreeCardMove.java
new file mode 100644
index 00000000..fb28b09c
--- /dev/null
+++ b/src/main/java/org/luxons/sevenwonders/game/moves/PlayFreeCardMove.java
@@ -0,0 +1,39 @@
+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 PlayFreeCardMove extends CardFromHandMove {
+
+ PlayFreeCardMove(int playerIndex, Card card, PlayerMove move) {
+ super(playerIndex, card, move);
+ }
+
+ @Override
+ public boolean isValid(Table table, List<Card> playerHand) {
+ if (!super.isValid(table, playerHand)) {
+ return false;
+ }
+ Board board = table.getBoard(getPlayerIndex());
+ return board.canPlayFreeCard(table.getCurrentAge());
+ }
+
+ @Override
+ public void place(Table table, List<Card> discardedCards, Settings settings) {
+ Board board = table.getBoard(getPlayerIndex());
+ board.addCard(getCard());
+ }
+
+ @Override
+ public void activate(Table table, List<Card> discardedCards, Settings settings) {
+ // only apply effects, without paying the cost
+ getCard().getEffects().forEach(e -> e.apply(table, getPlayerIndex()));
+ Board board = table.getBoard(getPlayerIndex());
+ board.consumeFreeCard(table.getCurrentAge());
+ }
+}
diff --git a/src/test/java/org/luxons/sevenwonders/game/cards/HandsTest.java b/src/test/java/org/luxons/sevenwonders/game/cards/HandsTest.java
index 4475fd3b..494b9e4c 100644
--- a/src/test/java/org/luxons/sevenwonders/game/cards/HandsTest.java
+++ b/src/test/java/org/luxons/sevenwonders/game/cards/HandsTest.java
@@ -105,42 +105,24 @@ public class HandsTest {
}
@Test
- public void rotate_doesNotMoveWhenOffsetIsZero() {
+ public void rotate_movesOfCorrectOffset_right() {
Hands hands = createHands(3, 7);
- Hands rotated = hands.rotate(0);
- assertEquals(rotated.get(0), hands.get(0));
- assertEquals(rotated.get(1), hands.get(1));
- assertEquals(rotated.get(2), hands.get(2));
- }
-
- @Test
- public void rotate_movesOfCorrectOffset_positive() {
- Hands hands = createHands(3, 7);
- Hands rotated = hands.rotate(1);
+ Hands rotated = hands.rotate(HandRotationDirection.RIGHT);
assertEquals(rotated.get(1), hands.get(0));
assertEquals(rotated.get(2), hands.get(1));
assertEquals(rotated.get(0), hands.get(2));
}
@Test
- public void rotate_movesOfCorrectOffset_negative() {
+ public void rotate_movesOfCorrectOffset_left() {
Hands hands = createHands(3, 7);
- Hands rotated = hands.rotate(-1);
+ Hands rotated = hands.rotate(HandRotationDirection.LEFT);
assertEquals(rotated.get(2), hands.get(0));
assertEquals(rotated.get(0), hands.get(1));
assertEquals(rotated.get(1), hands.get(2));
}
@Test
- public void rotate_movesOfCorrectOffset_negative2() {
- Hands hands = createHands(3, 7);
- Hands rotated = hands.rotate(-2);
- assertEquals(rotated.get(1), hands.get(0));
- assertEquals(rotated.get(2), hands.get(1));
- assertEquals(rotated.get(0), hands.get(2));
- }
-
- @Test
public void createHand_containsAllCards() {
List<Card> hand0 = TestUtils.createSampleCards(0, 5);
List<Card> hand1 = TestUtils.createSampleCards(5, 10);
bgstack15