diff options
Diffstat (limited to 'src/main/java/org/luxons')
5 files changed, 128 insertions, 0 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/game/Game.java b/src/main/java/org/luxons/sevenwonders/game/Game.java new file mode 100644 index 00000000..08c34578 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/Game.java @@ -0,0 +1,81 @@ +package org.luxons.sevenwonders.game; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.luxons.sevenwonders.game.boards.Board; +import org.luxons.sevenwonders.game.data.GameData; +import org.luxons.sevenwonders.game.wonders.Wonder; + +public class Game { + + private final GameData data; + + private final Settings settings; + + private List<Player> players; + + private List<Board> boards; + + private State state = State.LOBBY; + + public Game(Settings settings, GameData data) { + this.settings = settings; + this.data = data; + this.players = new ArrayList<>(3); + this.boards = new ArrayList<>(3); + } + + public synchronized int addPlayer(Player player) { + if (hasStarted()) { + throw new GameAlreadyStartedException(); + } + if (maxPlayersReached()) { + throw new PlayerOverflowException(); + } + int playerId = players.size(); + players.add(player); + boards.add(new Board(pickWonder(), settings)); + return playerId; + } + + private boolean hasStarted() { + return state == State.PLAYING; + } + + private boolean maxPlayersReached() { + return players.size() >= data.getMaxPlayers(); + } + + private Wonder pickWonder() { + List<Wonder> availableWonders = new ArrayList<>(data.getWonders()); + removeAlreadyUsedWondersFrom(availableWonders); + Collections.shuffle(availableWonders); + return availableWonders.get(0); + } + + private void removeAlreadyUsedWondersFrom(List<Wonder> wonders) { + boards.stream().map(Board::getWonder).forEach(wonders::remove); + } + + public synchronized void startGame() { + if (!hasEnoughPlayers()) { + throw new PlayerUnderflowException(); + } + state = State.PLAYING; + } + + private boolean hasEnoughPlayers() { + return players.size() >= data.getMinPlayers(); + } + + public class GameAlreadyStartedException extends IllegalStateException { + } + + public class PlayerOverflowException extends IllegalStateException { + } + + public class PlayerUnderflowException extends IllegalStateException { + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/Player.java b/src/main/java/org/luxons/sevenwonders/game/Player.java new file mode 100644 index 00000000..2d35e120 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/Player.java @@ -0,0 +1,14 @@ +package org.luxons.sevenwonders.game; + +public class Player { + + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/Settings.java b/src/main/java/org/luxons/sevenwonders/game/Settings.java index 16bce141..e4065991 100644 --- a/src/main/java/org/luxons/sevenwonders/game/Settings.java +++ b/src/main/java/org/luxons/sevenwonders/game/Settings.java @@ -1,11 +1,25 @@ package org.luxons.sevenwonders.game; +import org.luxons.sevenwonders.game.wonders.WonderSide; + public class Settings { + private int nbPlayers = 3; + private int initialGold = 3; private int defaultTradingCost = 2; + private WonderSide wonderSide = WonderSide.A; + + public int getNbPlayers() { + return nbPlayers; + } + + public void setNbPlayers(int nbPlayers) { + this.nbPlayers = nbPlayers; + } + public int getInitialGold() { return initialGold; } @@ -21,4 +35,12 @@ public class Settings { public void setDefaultTradingCost(int defaultTradingCost) { this.defaultTradingCost = defaultTradingCost; } + + public WonderSide getWonderSide() { + return wonderSide; + } + + public void setWonderSide(WonderSide wonderSide) { + this.wonderSide = wonderSide; + } } diff --git a/src/main/java/org/luxons/sevenwonders/game/State.java b/src/main/java/org/luxons/sevenwonders/game/State.java new file mode 100644 index 00000000..0bd71d3a --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/State.java @@ -0,0 +1,6 @@ +package org.luxons.sevenwonders.game; + +public enum State { + LOBBY, + PLAYING +} diff --git a/src/main/java/org/luxons/sevenwonders/game/wonders/WonderSide.java b/src/main/java/org/luxons/sevenwonders/game/wonders/WonderSide.java new file mode 100644 index 00000000..4a59e648 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/wonders/WonderSide.java @@ -0,0 +1,5 @@ +package org.luxons.sevenwonders.game.wonders; + +public enum WonderSide { + A, B; +} |