Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package io.github.tdudzik.pokergame.core.domain;
- import io.github.tdudzik.pokergame.core.domain.arbiter.Arbiter;
- import io.github.tdudzik.pokergame.core.domain.dealer.Dealer;
- import io.github.tdudzik.pokergame.core.domain.deck.Card;
- import io.github.tdudzik.pokergame.core.domain.deck.Deck;
- import io.github.tdudzik.pokergame.core.domain.player.Player;
- import io.github.tdudzik.pokergame.core.domain.player.PlayerId;
- import javaslang.collection.HashMap;
- import javaslang.collection.HashSet;
- import javaslang.collection.Map;
- import javaslang.collection.Set;
- import javaslang.control.Option;
- public class Game {
- private GameState currentGameState = GameState.GATHERING_PLAYERS;
- private int requiredPlayersCount = 2;
- private final Dealer dealer = new Dealer(Deck.full());
- private final Arbiter arbiter = new Arbiter();
- private Map<PlayerId, Player> players = HashMap.empty();
- private Set<Player> playersThatExchangedCards = HashSet.empty();
- public void addPlayer(Player player) {
- if (currentGameState != GameState.GATHERING_PLAYERS) {
- throw new IllegalStateException();
- }
- players = players.put(player.getPlayerId(), player);
- if (players.size() == requiredPlayersCount) {
- currentGameState = GameState.DEALING_CARDS;
- dealCards();
- }
- }
- public void dealCards() {
- if (currentGameState != GameState.DEALING_CARDS) {
- throw new IllegalStateException();
- }
- dealer.dealCards(players.values().toSet());
- currentGameState = GameState.EXCHANGING_CARDS;
- }
- public void exchangeCards(PlayerId playerId, Set<Card> cards) {
- if (currentGameState != GameState.EXCHANGING_CARDS) {
- throw new IllegalStateException();
- }
- Option<Player> player = players.get(playerId);
- if (player.isEmpty()) {
- throw new IllegalStateException("There is no player with the ID: " + playerId);
- }
- if (playersThatExchangedCards.contains(player.get())) {
- throw new IllegalStateException("Player cannot exchange cards twice");
- }
- dealer.exchangeCard(player.get(), cards);
- playersThatExchangedCards = playersThatExchangedCards.add(player.get());
- if (playersThatExchangedCards.size() == players.size()) {
- currentGameState = GameState.RESULT_EVALUATION;
- }
- }
- public void evaluateResult() {
- if (currentGameState != GameState.RESULT_EVALUATION) {
- throw new IllegalStateException();
- }
- Set<Player> winners = arbiter.findWinners(players.values().toSet());
- currentGameState = GameState.GAME_OVER;
- }
- private enum GameState {
- GATHERING_PLAYERS, DEALING_CARDS, EXCHANGING_CARDS, RESULT_EVALUATION, GAME_OVER
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement