Advertisement
tdudzik

Untitled

Feb 27th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.85 KB | None | 0 0
  1. package io.github.tdudzik.pokergame.core.domain;
  2.  
  3. import io.github.tdudzik.pokergame.core.domain.arbiter.Arbiter;
  4. import io.github.tdudzik.pokergame.core.domain.dealer.Dealer;
  5. import io.github.tdudzik.pokergame.core.domain.deck.Card;
  6. import io.github.tdudzik.pokergame.core.domain.deck.Deck;
  7. import io.github.tdudzik.pokergame.core.domain.player.Player;
  8. import io.github.tdudzik.pokergame.core.domain.player.PlayerId;
  9. import javaslang.collection.HashMap;
  10. import javaslang.collection.HashSet;
  11. import javaslang.collection.Map;
  12. import javaslang.collection.Set;
  13. import javaslang.control.Option;
  14.  
  15. public class Game {
  16.  
  17.     private GameState currentGameState = GameState.GATHERING_PLAYERS;
  18.     private int requiredPlayersCount = 2;
  19.  
  20.     private final Dealer dealer = new Dealer(Deck.full());
  21.     private final Arbiter arbiter = new Arbiter();
  22.     private Map<PlayerId, Player> players = HashMap.empty();
  23.     private Set<Player> playersThatExchangedCards = HashSet.empty();
  24.  
  25.  
  26.     public void addPlayer(Player player) {
  27.         if (currentGameState != GameState.GATHERING_PLAYERS) {
  28.             throw new IllegalStateException();
  29.         }
  30.  
  31.         players = players.put(player.getPlayerId(), player);
  32.  
  33.         if (players.size() == requiredPlayersCount) {
  34.             currentGameState = GameState.DEALING_CARDS;
  35.             dealCards();
  36.         }
  37.     }
  38.  
  39.     public void dealCards() {
  40.         if (currentGameState != GameState.DEALING_CARDS) {
  41.             throw new IllegalStateException();
  42.         }
  43.  
  44.         dealer.dealCards(players.values().toSet());
  45.         currentGameState = GameState.EXCHANGING_CARDS;
  46.     }
  47.  
  48.     public void exchangeCards(PlayerId playerId, Set<Card> cards) {
  49.         if (currentGameState != GameState.EXCHANGING_CARDS) {
  50.             throw new IllegalStateException();
  51.         }
  52.  
  53.         Option<Player> player = players.get(playerId);
  54.         if (player.isEmpty()) {
  55.             throw new IllegalStateException("There is no player with the ID: " + playerId);
  56.         }
  57.  
  58.         if (playersThatExchangedCards.contains(player.get())) {
  59.             throw new IllegalStateException("Player cannot exchange cards twice");
  60.         }
  61.  
  62.         dealer.exchangeCard(player.get(), cards);
  63.         playersThatExchangedCards = playersThatExchangedCards.add(player.get());
  64.  
  65.         if (playersThatExchangedCards.size() == players.size()) {
  66.             currentGameState = GameState.RESULT_EVALUATION;
  67.         }
  68.     }
  69.  
  70.     public void evaluateResult() {
  71.         if (currentGameState != GameState.RESULT_EVALUATION) {
  72.             throw new IllegalStateException();
  73.         }
  74.  
  75.         Set<Player> winners = arbiter.findWinners(players.values().toSet());
  76.         currentGameState = GameState.GAME_OVER;
  77.     }
  78.  
  79.     private enum GameState {
  80.  
  81.         GATHERING_PLAYERS, DEALING_CARDS, EXCHANGING_CARDS, RESULT_EVALUATION, GAME_OVER
  82.  
  83.     }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement