diff --git a/src/main/java/me/ars/pokerbot/irc/Irc.java b/src/main/java/me/ars/pokerbot/irc/Irc.java index 3de738f..083caae 100644 --- a/src/main/java/me/ars/pokerbot/irc/Irc.java +++ b/src/main/java/me/ars/pokerbot/irc/Irc.java @@ -1,6 +1,6 @@ package me.ars.pokerbot.irc; -import me.ars.pokerbot.poker.Player; +import me.ars.pokerbot.poker.PlayerIdentifier; import java.util.List; @@ -12,7 +12,7 @@ public interface Irc { * @param player * @param message */ - void message(Player player, String message); + void message(PlayerIdentifier player, String message); - void gameEnded(List players); + void gameEnded(List players); } diff --git a/src/main/java/me/ars/pokerbot/irc/IrcPlayer.java b/src/main/java/me/ars/pokerbot/irc/IrcPlayer.java index 68b52a7..fa6ab34 100644 --- a/src/main/java/me/ars/pokerbot/irc/IrcPlayer.java +++ b/src/main/java/me/ars/pokerbot/irc/IrcPlayer.java @@ -1,8 +1,8 @@ package me.ars.pokerbot.irc; -import me.ars.pokerbot.poker.Player; +import me.ars.pokerbot.poker.PlayerIdentifier; -public class IrcPlayer extends Player { +public class IrcPlayer extends PlayerIdentifier { private String nick; private String login; diff --git a/src/main/java/me/ars/pokerbot/irc/IrcStateCallback.java b/src/main/java/me/ars/pokerbot/irc/IrcStateCallback.java index bfe1b1d..ef262e2 100644 --- a/src/main/java/me/ars/pokerbot/irc/IrcStateCallback.java +++ b/src/main/java/me/ars/pokerbot/irc/IrcStateCallback.java @@ -2,7 +2,7 @@ import me.ars.pokerbot.poker.Card; import me.ars.pokerbot.poker.Hand; -import me.ars.pokerbot.poker.Player; +import me.ars.pokerbot.poker.PlayerIdentifier; import me.ars.pokerbot.poker.StateCallback; import java.util.Arrays; @@ -67,7 +67,7 @@ private String renderCard(Card card) { return Formatting.BOLD + color + valueStr + suit + Formatting.CLEAR; } - private String renderNick(Player player) { + private String renderNick(PlayerIdentifier player) { return Formatting.BOLD + player.getName() + Formatting.CLEAR; } @@ -78,17 +78,17 @@ private String renderHand(Hand hand) { } @Override - public void playerCalled(Player player, int money) { + public void playerCalled(PlayerIdentifier player, int money) { ircBot.message(channel, renderNick(player) + " called! (" + moneyString(money) + ")"); } @Override - public void playerRaised(Player player, int newRaise) { + public void playerRaised(PlayerIdentifier player, int newRaise) { ircBot.message(channel, renderNick(player) + " raised " + moneyString(newRaise) + "."); } @Override - public void playerChecked(Player player) { + public void playerChecked(PlayerIdentifier player) { // Too verbose. Skip } @@ -98,7 +98,7 @@ public void announce(String message) { } @Override - public void updateTable(List table, int pot, Player currentPlayer) { + public void updateTable(List table, int pot, PlayerIdentifier currentPlayer) { final String tableStr = table.isEmpty() ? "no cards" : table.stream() .map(this::renderCard).collect(Collectors.joining(", ")); if (currentPlayer == null) { @@ -110,32 +110,32 @@ public void updateTable(List table, int pot, Player currentPlayer) { } @Override - public void mustCallRaise(Player player, int amountOwed) { + public void mustCallRaise(PlayerIdentifier player, int amountOwed) { ircBot.message(channel, renderNick(player) + " must at least call last raise (" + moneyString(amountOwed) + ")."); } @Override - public void playerCannotRaise(Player player, int money) { + public void playerCannotRaise(PlayerIdentifier player, int money) { ircBot.message(channel, renderNick(player) + " doesn't have enough money to make the raise. They only have " + moneyString(money) + "."); } @Override - public void playerAllin(Player player) { + public void playerAllin(PlayerIdentifier player) { ircBot.message(channel, renderNick(player) + " goes all in!"); } @Override - public void playerFolded(Player player) { + public void playerFolded(PlayerIdentifier player) { // Too verbose. Skip. } @Override - public void playerCashedOut(Player player, int money) { + public void playerCashedOut(PlayerIdentifier player, int money) { ircBot.message(channel, renderNick(player) + " cashed out with " + moneyString(money) + "!"); } @Override - public void showPlayerCards(Player player, Card card1, Card card2, Card spyCard) { + public void showPlayerCards(PlayerIdentifier player, Card card1, Card card2, Card spyCard) { final StringBuilder sb = new StringBuilder(); sb.append("[").append(channel).append("] Your cards: ").append(renderCard(card1)).append(", ").append(renderCard(card2)); if (spyCard != null) { @@ -145,14 +145,14 @@ public void showPlayerCards(Player player, Card card1, Card card2, Card spyCard) } @Override - public void showPlayers(Map players) { + public void showPlayers(Map players) { ircBot.message(channel, players.keySet().stream() .map(player -> "[" + renderNick(player) + " - " + moneyString(players.get(player)) + "]") .collect(Collectors.joining(" "))); } @Override - public void revealPlayers(Map> reveal) { + public void revealPlayers(Map> reveal) { ircBot.message(channel, reveal.keySet().stream() .map(player -> "[" + renderNick(player) + " - " + renderCard(reveal.get(player).get(0)) + ", " + @@ -161,7 +161,7 @@ public void revealPlayers(Map> reveal) { } @Override - public void declareWinner(Player player, Hand winningHand, int pot) { + public void declareWinner(PlayerIdentifier player, Hand winningHand, int pot) { final StringBuilder sb = new StringBuilder(); sb.append(renderNick(player)).append(" wins ").append(moneyString(pot)); if (winningHand != null) { @@ -172,15 +172,15 @@ public void declareWinner(Player player, Hand winningHand, int pot) { } @Override - public void declareSplitPot(List winners, Hand.HandType handType, int pot) { + public void declareSplitPot(List winners, Hand.HandType handType, int pot) { ircBot.message(channel, "Split pot between " - + winners.stream().map(Player::getName).collect(Collectors.joining(", ")) + + winners.stream().map(PlayerIdentifier::getName).collect(Collectors.joining(", ")) + " (each with a " + handType + ")."); } @Override - public void declarePlayerTurn(Player player) { + public void declarePlayerTurn(PlayerIdentifier player) { ircBot.message(channel, renderNick(player) + "'s turn!"); } @@ -190,12 +190,12 @@ public void collectAnte(int ante) { } @Override - public void collectBlinds(Player bigBlindPlayer, int bigBlind, Player smallBlindPlayer, int smallBlind) { + public void collectBlinds(PlayerIdentifier bigBlindPlayer, int bigBlind, PlayerIdentifier smallBlindPlayer, int smallBlind) { ircBot.message(channel, "Collecting blinds (" + moneyString(bigBlind) + " from " + renderNick(bigBlindPlayer) + ", " + moneyString(smallBlind) + " from " + renderNick(smallBlindPlayer) + ")"); } @Override - public void gameEnded(List oldPlayers) { + public void gameEnded(List oldPlayers) { ircBot.gameEnded(oldPlayers); } } diff --git a/src/main/java/me/ars/pokerbot/irc/KittehBot.java b/src/main/java/me/ars/pokerbot/irc/KittehBot.java index 7733b1a..d2163ce 100644 --- a/src/main/java/me/ars/pokerbot/irc/KittehBot.java +++ b/src/main/java/me/ars/pokerbot/irc/KittehBot.java @@ -2,7 +2,7 @@ import io.netty.handler.ssl.util.InsecureTrustManagerFactory; import me.ars.pokerbot.config.BotConfig; -import me.ars.pokerbot.poker.Player; +import me.ars.pokerbot.poker.PlayerIdentifier; import me.ars.pokerbot.poker.Table; import me.ars.pokerbot.stats.Roster; import me.ars.pokerbot.stats.Stats; @@ -194,7 +194,7 @@ public void onMessage(String channel, String sender, String login, String hostna break; } case "players": { - final Collection players = table.getPlayers(); + final Collection players = table.getPlayers(); if (players.isEmpty()) { message(channel, "No joined players."); break; @@ -204,13 +204,13 @@ public void onMessage(String channel, String sender, String login, String hostna message( channel, "Now playing: " - + players.stream().map(p -> p.getName() + " $" + p.getMoney()) + + players.stream().map(PlayerIdentifier::getName) .collect(Collectors.joining(", ")) + "."); } else { message( channel, "Joined players: " - + players.stream().map(Player::getName) + + players.stream().map(PlayerIdentifier::getName) .collect(Collectors.joining(", ")) + "."); } break; @@ -258,7 +258,7 @@ public void onMessage(String channel, String sender, String login, String hostna if (!table.isGameInProgress()) { break; } - final List oldPlayers = table.getPlayers(); + final List oldPlayers = table.getPlayers(); table.stopGame(); for (Table otherTable: tables.values()) { oldPlayers.removeAll(otherTable.getPlayers()); @@ -408,7 +408,7 @@ public void message(String channel, String message) { } @Override - public void message(Player player, String message) { + public void message(PlayerIdentifier player, String message) { if (ircClient == null) { logError("Bot not yet connected to irc."); return; @@ -439,7 +439,7 @@ private void setUpTable(String channel) { } @Override - public void gameEnded(List oldPlayers) { + public void gameEnded(List oldPlayers) { for (Table otherTable: tables.values()) { oldPlayers.removeAll(otherTable.getPlayers()); } diff --git a/src/main/java/me/ars/pokerbot/poker/Hand.java b/src/main/java/me/ars/pokerbot/poker/Hand.java index 897184b..16463de 100644 --- a/src/main/java/me/ars/pokerbot/poker/Hand.java +++ b/src/main/java/me/ars/pokerbot/poker/Hand.java @@ -54,7 +54,7 @@ public String toString() { /* * the player with the hand */ - private final Player player; + private final PlayerState player; /* * the type of hand @@ -66,8 +66,8 @@ public String toString() { */ private final Card[] bestHand; - private Hand(final Player player, final HandType type, - final Card... bestHand) { + private Hand(final PlayerState player, final HandType type, + final Card... bestHand) { if (bestHand.length != 5) { throw new IllegalArgumentException("Invalid hand size: " + bestHand.length); } @@ -77,12 +77,12 @@ private Hand(final Player player, final HandType type, this.bestHand = bestHand; } - Hand(final Player player, final HandType type, - final List bestHandList) { + Hand(final PlayerState player, final HandType type, + final List bestHandList) { this(player, type, bestHandList.toArray(new Card[bestHandList.size()])); } - public Player getPlayer() { + PlayerState getPlayer() { return player; } @@ -113,7 +113,7 @@ private static boolean isStraightAt(List uniqueCards, int index) { /* * @param cards the 7 cards to be analyzed */ - public static Hand getBestHand(Player player, Card... cards) { + static Hand getBestHand(PlayerState player, Card... cards) { final Hand hand1 = bestHand(player, cards); if (containsValue(cards, 14)) { try { @@ -132,7 +132,7 @@ public static Hand getBestHand(Player player, Card... cards) { return hand1; } - private static Hand bestHand(Player player, Card... cards) { + private static Hand bestHand(PlayerState player, Card... cards) { Arrays.sort(cards); int[] suitFreqs = new int[4]; diff --git a/src/main/java/me/ars/pokerbot/poker/Player.java b/src/main/java/me/ars/pokerbot/poker/Player.java deleted file mode 100644 index 9001f99..0000000 --- a/src/main/java/me/ars/pokerbot/poker/Player.java +++ /dev/null @@ -1,125 +0,0 @@ -package me.ars.pokerbot.poker; - -import java.util.Objects; - -public class Player { - /** - * Immutable unique identifier for this player. - */ - private final String uniqueIdentifier; - private int money; - private Card card1, card2; - private boolean active = true; - private boolean folded = false; - private boolean isAllIn = false; - - /** - * Creates a new Player object. - * - * @param uniqueIdentifier An identifier for this player that will not change over the course of the game. - */ - public Player(String uniqueIdentifier) { - this.uniqueIdentifier = uniqueIdentifier; - } - - @Override - public String toString() { - return getName(); - } - - public final boolean isAllIn() { - return isAllIn; - } - - final void setAllIn(boolean allIn) { - isAllIn = allIn; - } - - /** - * Returns the name of the player. - * This returns the unique identifier by default. You should override this if it would make more sense in your - * implementation, for instance to instead return an irc nickname (which is something that could change). - * - * @return Name of the player - */ - public String getName() { - return uniqueIdentifier; - } - - public final boolean isNotPlaying() { - return folded || !active; - } - - public final int getMoney() { - return money; - } - - final void setMoney(int money) { - this.money = money; - } - - public final boolean isBroke() { - return money == 0; - } - - final void newHand() { - folded = false; - } - - final void receiveCards(Card card1, Card card2) { - this.card1 = card1; - this.card2 = card2; - } - - public final Card getCard1() { - return card1; - } - - public final Card getCard2() { - return card2; - } - - public final boolean isFolded() { - return folded; - } - - public boolean isActive() { - return active; - } - - final int bet(int amount) { - if (!active) - return 0; - money -= amount; - return amount; - } - - final void win(int pot) { - if (!active) - return; - - money += pot; - } - - final void cashout() { - fold(); - active = false; - } - - final void fold() { - folded = true; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Player player = (Player) o; - return uniqueIdentifier.equals(player.uniqueIdentifier); - } - - @Override - public int hashCode() { - return Objects.hash(uniqueIdentifier); - } -} diff --git a/src/main/java/me/ars/pokerbot/poker/PlayerIdentifier.java b/src/main/java/me/ars/pokerbot/poker/PlayerIdentifier.java new file mode 100644 index 0000000..abbf18f --- /dev/null +++ b/src/main/java/me/ars/pokerbot/poker/PlayerIdentifier.java @@ -0,0 +1,58 @@ +package me.ars.pokerbot.poker; + +import java.util.Collection; +import java.util.Objects; + +public class PlayerIdentifier { + /** + * Immutable unique identifier for this player. + */ + private final String uniqueIdentifier; + + /** + * Creates a new Player object. + * + * @param uniqueIdentifier An identifier for this player that will not change over the course of the game. + */ + public PlayerIdentifier(String uniqueIdentifier) { + this.uniqueIdentifier = uniqueIdentifier; + } + + @Override + public String toString() { + return getName(); + } + + /** + * Returns the name of the player. + * This returns the unique identifier by default. You should override this if it would make more sense in your + * implementation, for instance to instead return an irc nickname (which is something that could change). + * + * @return Name of the player + */ + public String getName() { + return uniqueIdentifier; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + PlayerIdentifier player = (PlayerIdentifier) o; + return uniqueIdentifier.equals(player.uniqueIdentifier); + } + + @Override + public int hashCode() { + return Objects.hash(uniqueIdentifier); + } + + public static PlayerState findState(Collection collection, PlayerIdentifier identifier) { + for(PlayerState state : collection) { + if (state.getIdentifier().equals(identifier)) { + return state; + } + } + return null; + } +} diff --git a/src/main/java/me/ars/pokerbot/poker/PlayerState.java b/src/main/java/me/ars/pokerbot/poker/PlayerState.java new file mode 100644 index 0000000..78a194b --- /dev/null +++ b/src/main/java/me/ars/pokerbot/poker/PlayerState.java @@ -0,0 +1,99 @@ +package me.ars.pokerbot.poker; + +class PlayerState { + private final PlayerIdentifier identifier; + private int money; + private Card card1, card2; + private boolean active = true; + private boolean folded = false; + private boolean isAllIn = false; + + public PlayerState(PlayerIdentifier identifier) { + this.identifier = identifier; + } + + public PlayerState(String name) { + this(new PlayerIdentifier(name)); + } + + public PlayerIdentifier getIdentifier() { + return identifier; + } + + public String getName() { + return identifier.getName(); + } + + public final boolean isAllIn() { + return isAllIn; + } + + final void setAllIn(boolean allIn) { + isAllIn = allIn; + } + + public final boolean isNotPlaying() { + return folded || !active; + } + + public final int getMoney() { + return money; + } + + final void setMoney(int money) { + this.money = money; + } + + public final boolean isBroke() { + return money == 0; + } + + final void newHand() { + folded = false; + } + + final void receiveCards(Card card1, Card card2) { + this.card1 = card1; + this.card2 = card2; + } + + public final Card getCard1() { + return card1; + } + + public final Card getCard2() { + return card2; + } + + public final boolean isFolded() { + return folded; + } + + public boolean isActive() { + return active; + } + + final int bet(int amount) { + if (!active) + return 0; + money -= amount; + return amount; + } + + final void win(int pot) { + if (!active) + return; + + money += pot; + } + + final void cashout() { + fold(); + active = false; + } + + final void fold() { + folded = true; + } + +} diff --git a/src/main/java/me/ars/pokerbot/poker/Pot.java b/src/main/java/me/ars/pokerbot/poker/Pot.java index e3d2e61..538afd9 100644 --- a/src/main/java/me/ars/pokerbot/poker/Pot.java +++ b/src/main/java/me/ars/pokerbot/poker/Pot.java @@ -2,16 +2,16 @@ import java.util.*; -public class Pot { +class Pot { private static final int MAX_RECURSIONS = 64; - private final Map contributions; + private final Map contributions; private final boolean isMainPot; private Pot sidePot; private int currentBet; - public Pot() { + Pot() { contributions = new HashMap<>(); currentBet = 0; sidePot = null; @@ -25,7 +25,7 @@ private Pot(int currentBet) { this.isMainPot = false; } - private void addContribution(Player player, int money) { + private void addContribution(PlayerState player, int money) { if (!contributions.containsKey(player)) { contributions.put(player, money); } else { @@ -34,7 +34,7 @@ private void addContribution(Player player, int money) { } } - public int getContribution(Player player) { + public int getContribution(PlayerState player) { return contributions.getOrDefault(player, 0); } @@ -58,7 +58,7 @@ public int getTotalBets() { return bet; } - public int getTotalContribution(Player player) { + public int getTotalContribution(PlayerState player) { int contribution = 0; if (contributions.containsKey(player)) { contribution += contributions.get(player); @@ -69,13 +69,13 @@ public int getTotalContribution(Player player) { return contribution; } - public int getTotalOwed(Player player) { + public int getTotalOwed(PlayerState player) { final int totalBet = getTotalBets(); final int contributions = getTotalContribution(player); return totalBet - contributions; } - private int getOwed(Player player) { + private int getOwed(PlayerState player) { final int bet = getCurrentBet(); final int contributions = getContribution(player); return bet - contributions; @@ -88,7 +88,7 @@ public void newTurn() { } } - public boolean checkPlayer(Player player) { + public boolean checkPlayer(PlayerState player) { if (player.isAllIn()) { return true; } @@ -104,13 +104,13 @@ public void reset() { sidePot = null; } - public void collectAnte(Player player, int ante) { + public void collectAnte(PlayerState player, int ante) { currentBet = ante; addContribution(player, player.bet(ante)); System.out.println("Collecting ante from " + player + ", total paid: " + getTotalContribution(player)); } - public int collectBigBlind(Player player, int bigBlind) { + public int collectBigBlind(PlayerState player, int bigBlind) { raise(player, bigBlind); System.out.println("Collecting big blind (" + bigBlind + ") from " + player); return bigBlind; @@ -120,20 +120,20 @@ static int calculateSmallBlind(int bigBlind) { return (int) Math.ceil(((double) bigBlind) / 2); } - public int collectSmallBlind(Player player, int bigBlind) { + public int collectSmallBlind(PlayerState player, int bigBlind) { final int smallBlind = calculateSmallBlind(bigBlind); addContribution(player, player.bet(smallBlind)); System.out.println("Collecting small blind (" + smallBlind + ") from " + player); return smallBlind; } - public Set getParticipants() { + public Set getParticipants() { return contributions.keySet(); } public int getMoney() { int cash = 0; - for(Player player: contributions.keySet()) { + for(PlayerState player: contributions.keySet()) { cash += contributions.get(player); } return cash; @@ -155,7 +155,7 @@ public Pot getSidePot() { return sidePot; } - public int raise(Player player, int amount) { + public int raise(PlayerState player, int amount) { final int totalRaised; final int owed; @@ -185,12 +185,12 @@ public int raise(Player player, int amount) { return totalRaised-owed; } - public void allIn(Player player) { + public void allIn(PlayerState player) { player.setAllIn(true); raise(player, player.getMoney()); } - public int call(Player player) { + public int call(PlayerState player) { final int previousContribution = getTotalContribution(player); final int owed = getTotalOwed(player); final int callAmount = Math.min(owed, player.getMoney()); @@ -201,7 +201,7 @@ public int call(Player player) { return getTotalContribution(player) - previousContribution; } - private void call(Player player, int amount, int recursion) { + private void call(PlayerState player, int amount, int recursion) { final int currentContribution = getContribution(player); if (currentContribution < 0) { throw new IllegalStateException("Current contribution for player '" + @@ -266,12 +266,12 @@ private void call(Player player, int amount, int recursion) { } } - private void createSidePot(Player creatingPlayer) { + private void createSidePot(PlayerState creatingPlayer) { System.out.println("Creating a new side pot"); final int newBet = getTotalContribution(creatingPlayer); final int difference = currentBet - newBet; sidePot = new Pot(newBet); - for (Player participant : getParticipants()) { + for (PlayerState participant : getParticipants()) { if (!participant.equals(creatingPlayer)) { final int individualContribution = getContribution(participant); if (individualContribution < newBet) { @@ -307,9 +307,9 @@ public String toString() { /** * Split up the winnings of this pot to multiple players */ - public void splitPot(Set winners) { + public void splitPot(Set winners) { int winnings = getMoney() / winners.size(); - for(Player winner: winners) { + for(PlayerState winner: winners) { winner.win(winnings); } } @@ -317,7 +317,7 @@ public void splitPot(Set winners) { /** * Returns true if the player does not owe anything to the pot. */ - public boolean playerCleared(Player player) { + public boolean playerCleared(PlayerState player) { return getTotalOwed(player) == 0; } } diff --git a/src/main/java/me/ars/pokerbot/poker/StateCallback.java b/src/main/java/me/ars/pokerbot/poker/StateCallback.java index 922e56d..e42ddb3 100644 --- a/src/main/java/me/ars/pokerbot/poker/StateCallback.java +++ b/src/main/java/me/ars/pokerbot/poker/StateCallback.java @@ -10,7 +10,7 @@ public interface StateCallback { * @param player Player who called * @param money Amount called */ - void playerCalled(Player player, int money); + void playerCalled(PlayerIdentifier player, int money); /** * A player has raised a bet. @@ -18,14 +18,14 @@ public interface StateCallback { * @param player Player who raised * @param newRaise Amount that was raised */ - void playerRaised(Player player, int newRaise); + void playerRaised(PlayerIdentifier player, int newRaise); /** * A player has checked. * * @param player Checking player */ - void playerChecked(Player player); + void playerChecked(PlayerIdentifier player); /** * Announce a message to all players. @@ -41,7 +41,7 @@ public interface StateCallback { * @param pot Current pot on the table * @param currentPlayer Current players turn */ - void updateTable(List table, int pot, Player currentPlayer); + void updateTable(List table, int pot, PlayerIdentifier currentPlayer); /** * Notify that a player must call a raise. @@ -49,7 +49,7 @@ public interface StateCallback { * @param player Player who needs to call * @param amountOwed Amount of money needed to call */ - void mustCallRaise(Player player, int amountOwed); + void mustCallRaise(PlayerIdentifier player, int amountOwed); /** * Announce that a player could not raise the specified bet. @@ -57,21 +57,21 @@ public interface StateCallback { * @param player Player who tried to bet * @param money Amount of money they actually had */ - void playerCannotRaise(Player player, int money); + void playerCannotRaise(PlayerIdentifier player, int money); /** * A player has gone all in. * * @param player Player who went all in. */ - void playerAllin(Player player); + void playerAllin(PlayerIdentifier player); /** * A player has folded. * * @param player Folding player */ - void playerFolded(Player player); + void playerFolded(PlayerIdentifier player); /** * A player cashed out and left the table. @@ -79,7 +79,7 @@ public interface StateCallback { * @param player Cashing out player * @param money Amount of money they walked away with */ - void playerCashedOut(Player player, int money); + void playerCashedOut(PlayerIdentifier player, int money); /** * Show the player the two cards they were dealt. @@ -89,21 +89,21 @@ public interface StateCallback { * @param card2 Second card * @param spycard If playing with spycards */ - void showPlayerCards(Player player, Card card1, Card card2, Card spycard); + void showPlayerCards(PlayerIdentifier player, Card card1, Card card2, Card spycard); /** * Display the currently playing players and the money they have * * @param players Map of players to how much money they have */ - void showPlayers(Map players); + void showPlayers(Map players); /** * Reveals the hands of the supplied players. * * @param reveal Players mapped to their own cards */ - void revealPlayers(Map> reveal); + void revealPlayers(Map> reveal); /** * Declare that a player has won the pot @@ -112,7 +112,7 @@ public interface StateCallback { * @param winningHand Their winning hand * @param pot Money they've won from the pot */ - void declareWinner(Player winner, Hand winningHand, int pot); + void declareWinner(PlayerIdentifier winner, Hand winningHand, int pot); /** * Declare that there are multiple winners splitting the pot @@ -121,14 +121,14 @@ public interface StateCallback { * @param handType The hand type they had in common * @param pot The pot they are splitting */ - void declareSplitPot(List winners, Hand.HandType handType, int pot); + void declareSplitPot(List winners, Hand.HandType handType, int pot); /** * Declares who's turn it is. * * @param player Current player */ - void declarePlayerTurn(Player player); + void declarePlayerTurn(PlayerIdentifier player); /** * Declare that ante is being collected @@ -145,12 +145,12 @@ public interface StateCallback { * @param smallBlindPlayer The player that pays the small blind * @param smallBlind How big the small blind is */ - void collectBlinds(Player bigBlindPlayer, int bigBlind, Player smallBlindPlayer, int smallBlind); + void collectBlinds(PlayerIdentifier bigBlindPlayer, int bigBlind, PlayerIdentifier smallBlindPlayer, int smallBlind); /** * Declare that the game has ended. * * @param oldPlayers Players that have left the ended game */ - void gameEnded(List oldPlayers); + void gameEnded(List oldPlayers); } diff --git a/src/main/java/me/ars/pokerbot/poker/Table.java b/src/main/java/me/ars/pokerbot/poker/Table.java index ab7585c..c6ef7b9 100644 --- a/src/main/java/me/ars/pokerbot/poker/Table.java +++ b/src/main/java/me/ars/pokerbot/poker/Table.java @@ -1,6 +1,5 @@ package me.ars.pokerbot.poker; -import me.ars.pokerbot.irc.IrcPlayer; import me.ars.pokerbot.stats.Roster; import me.ars.pokerbot.config.GameConfig; @@ -8,12 +7,14 @@ import java.util.*; import java.util.stream.Collectors; +import static me.ars.pokerbot.poker.PlayerIdentifier.findState; + public class Table { private final StateCallback callback; - private final List players = new ArrayList<>(); + private final List players = new ArrayList<>(); private final Queue deck = new ArrayDeque<>(52); private final List table = new ArrayList<>(5); - private final Queue buyInPlayers = new ArrayDeque<>(); + private final Queue buyInPlayers = new ArrayDeque<>(); private final Roster roster; private final GameConfig config; private final Pot mainPot; @@ -30,16 +31,22 @@ public Table(StateCallback callback, Roster roster, GameConfig config) { this.mainPot = new Pot(); } - private boolean verifyCurrentPlayer(Player player) { + private boolean verifyCurrentPlayer(PlayerState player) { if (player == null) return false; return (player.equals(getCurrentPlayer())); } - public Player getCurrentPlayer() { + + private PlayerState getCurrentPlayer() { System.out.println("Current turn index: " + turnIndex + " out of " + players.size() + " players. StartPlayer is " + startPlayer + "."); return players.get(turnIndex); } + public PlayerIdentifier getCurrentPlayerId() { + System.out.println("Current turn index: " + turnIndex + " out of " + players.size() + " players. StartPlayer is " + startPlayer + "."); + return players.get(turnIndex).getIdentifier(); + } + public Calendar getLastActivity() { return lastActivity; } @@ -52,8 +59,8 @@ public boolean isGameInProgress() { return gameInProgress; } - public List getPlayers() { - return players; + public List getPlayers() { + return players.stream().map(PlayerState::getIdentifier).collect(Collectors.toList()); } public void showCurrent() { @@ -61,19 +68,20 @@ public void showCurrent() { callback.announce("Not currently playing."); return; } - final Player currentPlayer = getCurrentPlayer(); - callback.updateTable(table, mainPot.getMoney(), currentPlayer); - callback.announce(currentPlayer.getName() + " has $" + currentPlayer.getMoney()); + final PlayerState currentPlayer = getCurrentPlayer(); + callback.updateTable(table, mainPot.getMoney(), currentPlayer.getIdentifier()); + callback.announce(currentPlayer.getIdentifier().getName() + " has $" + currentPlayer.getMoney()); } /** * Incoming 'call' from [player] */ - public boolean call(Player player) { + public boolean call(PlayerIdentifier playerId) { + final PlayerState player = findState(players, playerId); if (!verifyCurrentPlayer(player)) return false; setActivity(); final int amount = mainPot.call(player); - callback.playerCalled(player, amount); + callback.playerCalled(player.getIdentifier(), amount); if (isEveryoneAllin()) { revealHands(players); } @@ -86,19 +94,21 @@ public boolean call(Player player) { * * @return True if the player successfully checks. */ - public boolean check(Player player) { - if (!verifyCurrentPlayer(player)) return false; + public boolean check(PlayerIdentifier playerId) { + final PlayerState player = findState(players, playerId); + if (player == null || !verifyCurrentPlayer(player)) return false; setActivity(); + final boolean checked = mainPot.checkPlayer(player); System.out.println(player + " could check: " + checked); if (checked) { - callback.playerChecked(player); + callback.playerChecked(player.getIdentifier()); nextTurn(); } else { System.err.println(player + " cannot check, they owe " + mainPot.getTotalOwed(player)); - callback.mustCallRaise(player, mainPot.getTotalOwed(player)); + callback.mustCallRaise(player.getIdentifier(), mainPot.getTotalOwed(player)); } return checked; } @@ -108,18 +118,19 @@ public boolean check(Player player) { * * @return True if the raise was successful */ - public boolean raise(Player player, int raise) { + public boolean raise(PlayerIdentifier playerId, int raise) { + final PlayerState player = findState(players, playerId); if (!verifyCurrentPlayer(player)) return false; setActivity(); final int result = mainPot.raise(player, raise); if (result != -1) { - callback.playerRaised(player, result); + callback.playerRaised(player.getIdentifier(), result); lastIndex = lastUnfolded(turnIndex - 1); nextTurn(); return true; } else { - callback.playerCannotRaise(player, player.getMoney()); + callback.playerCannotRaise(player.getIdentifier(), player.getMoney()); return false; } } @@ -127,11 +138,12 @@ public boolean raise(Player player, int raise) { /** * Incoming allin from [player] */ - public void allIn(Player player) { + public void allIn(PlayerIdentifier playerId) { + final PlayerState player = findState(players, playerId); if (!verifyCurrentPlayer(player)) return; setActivity(); mainPot.allIn(player); - callback.playerAllin(player); + callback.playerAllin(player.getIdentifier()); lastIndex = lastUnfolded(turnIndex - 1); if (isEveryoneAllin()) { revealHands(players); @@ -142,45 +154,48 @@ public void allIn(Player player) { /** * Incoming fold from [player] */ - public void fold(Player player) { + public void fold(PlayerIdentifier playerId) { + final PlayerState player = findState(players, playerId); if (!verifyCurrentPlayer(player)) return; setActivity(); player.fold(); - callback.playerFolded(player); + callback.playerFolded(player.getIdentifier()); final boolean nextTurn = !checkForWinByFold(); if (nextTurn) { nextTurn(); } } - public void cashout(Player player) { + public void cashout(PlayerIdentifier playerId) { + final PlayerState player = findState(players, playerId); + if (player == null) return; System.out.println("Cashing out " + player); setActivity(); player.cashout(); - callback.playerCashedOut(player, player.getMoney()); - roster.modifyMoney(player.getName(), player.getMoney() - config.startStash); + callback.playerCashedOut(player.getIdentifier(), player.getMoney()); + roster.modifyMoney(player.getIdentifier().getName(), player.getMoney() - config.startStash); final boolean nextTurn = !checkForWinByFold(); if (isGameInProgress() && verifyCurrentPlayer(player) && nextTurn) { nextTurn(); } } - public void registerPlayer(Player player) { + public void registerPlayer(PlayerIdentifier player) { if (gameInProgress) { callback.announce("A game is already in progress! Use the buyin command if you still want to join"); return; } addPlayer(player, true); - player.setMoney(config.startStash); } - private boolean addPlayer(Player newPlayer, boolean verbose) { - if (players.contains(newPlayer)) { + private boolean addPlayer(PlayerIdentifier newPlayer, boolean verbose) { + if (players.stream().anyMatch(p -> p.getIdentifier().equals(newPlayer))) { if (verbose) callback.announce(newPlayer.getName() + " has already joined."); return false; } - players.add(newPlayer); - newPlayer.setMoney(config.startStash); + final PlayerState player = new PlayerState(newPlayer); + players.add(player); + player.setMoney(config.startStash); if (verbose) callback.announce(newPlayer.getName() + " has joined the game."); return true; } @@ -192,9 +207,9 @@ private static Card pickRandomCard(Random random, Card... cards) { private void deal() { final boolean spyCards = config.spyCards != null && config.spyCards; final Random random = new Random(); - Player unlucky = null; + PlayerState unlucky = null; Card phony = null; - for (Player player : players) { + for (PlayerState player : players) { final Card card1 = deck.poll(); final Card card2 = deck.poll(); player.receiveCards(card1, card2); @@ -204,34 +219,34 @@ private void deal() { phony = deck.poll(); unlucky = players.get(random.nextInt(players.size())); } - for (Player player : players) { + for (PlayerState player : players) { Card spyCard = null; if (spyCards) { if (player.equals(unlucky)) { spyCard = phony; } else { - Player randPlayer = player; + PlayerState randPlayer = player; while (randPlayer.equals(player)) { randPlayer = players.get(random.nextInt(players.size())); } spyCard = pickRandomCard(random, randPlayer.getCard1(), randPlayer.getCard2()); } } - callback.showPlayerCards(player, player.getCard1(), player.getCard2(), spyCard); + callback.showPlayerCards(player.getIdentifier(), player.getCard1(), player.getCard2(), spyCard); } } private void setupHand() { - for (Player player : players) { + for (PlayerState player : players) { player.setAllIn(false); if (player.isBroke()) { player.cashout(); - roster.modifyMoney(player.getName(), -config.startStash); + roster.modifyMoney(player.getIdentifier().getName(), -config.startStash); } } if (!buyInPlayers.isEmpty()) { - for (Player newPlayer : buyInPlayers) { + for (PlayerIdentifier newPlayer : buyInPlayers) { addPlayer(newPlayer, false); //roster.trackGame(newPlayer); } @@ -245,10 +260,10 @@ private void setupHand() { e.printStackTrace(); } - final Iterator playerIter = players.iterator(); + final Iterator playerIter = players.iterator(); while (playerIter.hasNext()) { - Player player = playerIter.next(); + PlayerState player = playerIter.next(); if (!player.isActive()) { playerIter.remove(); } @@ -263,7 +278,7 @@ private void setupHand() { callback.announce("Starting new hand..."); - for (Player player : players) { + for (PlayerState player : players) { player.newHand(); } @@ -276,10 +291,10 @@ private void setupHand() { lastIndex = lastUnfolded(startPlayer - 1); mainPot.reset(); - callback.showPlayers(players.stream().collect(Collectors.toMap((player) -> player, Player::getMoney))); + callback.showPlayers(players.stream().collect(Collectors.toMap(PlayerState::getIdentifier, PlayerState::getMoney))); deal(); collectForcedBets(); - sendStatus(getCurrentPlayer()); + sendStatus(getCurrentPlayer().getIdentifier()); } private void incrementStartPlayer() { @@ -299,7 +314,7 @@ public int getStartPlayer() { private void nextTurn() { mainPot.newTurn(); - final Player player = getCurrentPlayer(); + final PlayerState player = getCurrentPlayer(); if (isEveryoneAllin() || turnIndex == lastIndex && (player.isFolded() || player.isBroke() || mainPot.playerCleared(player))) { if (table.size() == 5) { @@ -315,7 +330,7 @@ private void nextTurn() { } } - Player nextPlayer; + PlayerState nextPlayer; do { turnIndex = wrappedIncrement(turnIndex); @@ -328,14 +343,14 @@ private void nextTurn() { callback.announce(nextPlayer.getName() + " is all-in, next player..."); nextTurn(); } else { - sendStatus(nextPlayer); + sendStatus(nextPlayer.getIdentifier()); } } private boolean isEveryoneAllin() { int activePlayers = 0; int allinPlayers = 0; - for (Player player : players) { + for (PlayerState player : players) { if (player.isAllIn()) allinPlayers++; if (!player.isNotPlaying()) activePlayers++; } @@ -343,9 +358,9 @@ private boolean isEveryoneAllin() { } private void checkWinners(Pot pot) { - final Set participants = pot.getParticipants(); + final Set participants = pot.getParticipants(); List hands = new ArrayList<>(participants.size()); - for (Player p : participants) { + for (PlayerState p : participants) { final Card[] playerCards = table.toArray(new Card[7]); playerCards[5] = p.getCard1(); playerCards[6] = p.getCard2(); @@ -355,7 +370,7 @@ private void checkWinners(Pot pot) { hands.sort(Collections.reverseOrder()); Iterator orderedHands = hands.iterator(); Hand winningHand; - Player winner1; + PlayerState winner1; do { winningHand = orderedHands.next(); @@ -378,10 +393,10 @@ private void checkWinners(Pot pot) { int numWinners = winners.size(); if (numWinners == 1) { - callback.declareWinner(winner1, winningHand, pot.getMoney()); + callback.declareWinner(winner1.getIdentifier(), winningHand, pot.getMoney()); winner1.win(pot.getMoney()); } else { - callback.declareSplitPot(winners.stream().map(Hand::getPlayer) + callback.declareSplitPot(winners.stream().map(Hand::getPlayer).map(PlayerState::getIdentifier) .collect(Collectors.toList()), winningHand.getHandType(), pot.getMoney()); pot.splitPot(winners.stream().map(Hand::getPlayer).collect(Collectors.toSet())); } @@ -394,21 +409,21 @@ private void checkWinners(Pot pot) { /** * Reveals non-folded hands of the supplied players. */ - private void revealHands(Collection currentPlayers) { - final Map> reveal = new HashMap<>(); - for (Player p : currentPlayers) { + private void revealHands(Collection currentPlayers) { + final Map> reveal = new HashMap<>(); + for (PlayerState p : currentPlayers) { if (!p.isFolded()) { final List cards = new ArrayList<>(); cards.add(p.getCard1()); cards.add(p.getCard2()); - reveal.put(p, cards); + reveal.put(p.getIdentifier(), cards); } } callback.revealPlayers(reveal); } - private void sendStatus(Player player) { + private void sendStatus(PlayerIdentifier player) { callback.updateTable(table, mainPot.getMoney(), player); callback.declarePlayerTurn(player); } @@ -417,7 +432,7 @@ private void collectForcedBets() { if (config.ante != null && config.ante != 0) { callback.collectAnte(config.ante); - for (Player player : players) { + for (PlayerState player : players) { mainPot.collectAnte(player, config.ante); } } @@ -425,15 +440,15 @@ private void collectForcedBets() { final int oldTurnIndex = turnIndex; final int oldLastIndex = lastIndex; final int blindPlayer = turnIndex; - final Player smallBlindPlayer = players.get(turnIndex); + final PlayerState smallBlindPlayer = players.get(turnIndex); final int smallBlind = mainPot.collectSmallBlind(smallBlindPlayer, config.bigBlind); lastIndex = lastUnfolded(turnIndex - 1); turnIndex = wrappedIncrement(turnIndex); - final Player bigBlindPlayer = players.get(wrappedIncrement(blindPlayer)); + final PlayerState bigBlindPlayer = players.get(wrappedIncrement(blindPlayer)); final int bigBlind = mainPot.collectBigBlind(bigBlindPlayer, config.bigBlind); lastIndex = lastUnfolded(turnIndex - 1); turnIndex = wrappedIncrement(turnIndex); - callback.collectBlinds(bigBlindPlayer, bigBlind, smallBlindPlayer, smallBlind); + callback.collectBlinds(bigBlindPlayer.getIdentifier(), bigBlind, smallBlindPlayer.getIdentifier(), smallBlind); turnIndex = oldTurnIndex; lastIndex = oldLastIndex; } @@ -451,10 +466,10 @@ private void draw() { public void startGame() { callback.announce("Starting game with: " - + players.stream().map(Player::getName) + + players.stream().map(PlayerState::getName) .collect(Collectors.joining(", ")) + "."); - for (Player player : players) { + for (PlayerState player : players) { roster.trackGame(player.getName()); } @@ -467,11 +482,11 @@ public void stopGame() { System.out.println("Stopping game"); gameInProgress = false; if (players.size() == 1) { - final Player winner = players.get(0); + final PlayerState winner = players.get(0); roster.modifyMoney(winner.getName(), winner.getMoney() - config.startStash); } else { int highscore = 0; - for (Player player: players) { + for (PlayerState player: players) { final int playerMoney = player.getMoney(); roster.modifyMoney(player.getName(), playerMoney - config.startStash); if (playerMoney > highscore) { @@ -479,12 +494,12 @@ public void stopGame() { } } } - final List oldPlayers = new ArrayList<>(players); + final List oldPlayers = new ArrayList<>(players); players.clear(); deck.clear(); table.clear(); - callback.gameEnded(oldPlayers); + callback.gameEnded(oldPlayers.stream().map(PlayerState::getIdentifier).collect(Collectors.toList())); try { roster.saveRoster(); @@ -495,9 +510,9 @@ public void stopGame() { } private boolean checkForWinByFold() { - Player last = null; + PlayerState last = null; int numPlayersLeft = players.size(); - for (Player player : players) { + for (PlayerState player : players) { if (player.isNotPlaying()) numPlayersLeft--; else @@ -509,7 +524,7 @@ private boolean checkForWinByFold() { if (numPlayersLeft == 1) { System.out.println("Have a winner: " + last); final int totalMoney = mainPot.getTotalMoney(); - callback.declareWinner(last, null, totalMoney); + callback.declareWinner(last.getIdentifier(), null, totalMoney); last.win(totalMoney); incrementStartPlayer(); setupHand(); @@ -520,7 +535,7 @@ private boolean checkForWinByFold() { } private void ensureNotAllFolded() { - for (Player player : players) { + for (PlayerState player : players) { if (!player.isFolded()) return; } @@ -561,36 +576,37 @@ public void clearPlayers() { players.clear(); } - public void unjoin(Player player) { + public void unjoin(PlayerIdentifier player) { if (isGameInProgress()) { if (buyInPlayers.contains(player)) { callback.announce(player.getName() + ": Your buyin was nulled."); buyInPlayers.remove(player); } else { - if (players.contains(player)) { + if (players.stream().anyMatch(p -> p.getIdentifier().equals(player))) { callback.announce(player.getName() + ": Cannot unjoin game in progress. Use cashout command."); } else { callback.announce(player.getName() + ": You are not part of the active game."); } } } else { - if (players.contains(player)) { - callback.announce(player.getName() + ": You have unjoined."); - System.out.println(player.getName() + " unjoined."); - players.remove(player); + final PlayerState p = findState(players, player); + if (p != null && players.contains(p)) { + callback.announce(p.getName() + ": You have unjoined."); + System.out.println(p.getName() + " unjoined."); + players.remove(p); } else { callback.announce(player.getName() + ": You never joined."); } } } - public void buyin(Player newPlayer) { + public void buyin(PlayerIdentifier newPlayer) { if (!gameInProgress) { callback.announce(newPlayer.getName() + ": Game hasn't started yet, putting you up for the game"); registerPlayer(newPlayer); return; } - if (players.contains(newPlayer)) { + if (players.stream().anyMatch(p -> p.getIdentifier().equals(newPlayer))) { callback.announce(newPlayer.getName() + ": You're already in the game."); return; } @@ -720,8 +736,8 @@ public void configure(String option, String newValue) { /** * Notify the table that the Player has disconnected from the game. */ - public void playerLeft(Player player) { - if (!players.contains(player)) return; + public void playerLeft(PlayerIdentifier player) { + if (players.stream().noneMatch(p -> p.getIdentifier().equals(player))) return; if (isGameInProgress()) { cashout(player); diff --git a/src/test/java/me/ars/pokerbot/poker/HandTest.java b/src/test/java/me/ars/pokerbot/poker/HandTest.java index c13b45f..15c3091 100644 --- a/src/test/java/me/ars/pokerbot/poker/HandTest.java +++ b/src/test/java/me/ars/pokerbot/poker/HandTest.java @@ -11,7 +11,7 @@ public class HandTest { - private final Player player = new Player("tester"); + private final PlayerState player = new PlayerState("tester"); @Test public void testHighCard() { @@ -301,7 +301,7 @@ public void weirdPairTest() { 16:15 < Poker> Reveal: [player1 - 6♣, 2♥] [player2 - 2♣, 2♠] 16:15 < Poker> player2 wins with the hand 3♦, Q♥, J♦, 8♥, 2♠ (one pair)! */ - final Player player2 = new Player("player2"); + final PlayerState player2 = new PlayerState("player2"); final Card card1 = new Card(8, Suit.HEARTS); final Card card2 = new Card(12, Suit.DIAMONDS); final Card card3 = new Card(3, Suit.DIAMONDS); diff --git a/src/test/java/me/ars/pokerbot/poker/PotTest.java b/src/test/java/me/ars/pokerbot/poker/PotTest.java index ba851f7..440b990 100644 --- a/src/test/java/me/ars/pokerbot/poker/PotTest.java +++ b/src/test/java/me/ars/pokerbot/poker/PotTest.java @@ -8,9 +8,9 @@ public class PotTest { - private Player player1; - private Player player2; - private Player player3; + private PlayerState player1; + private PlayerState player2; + private PlayerState player3; private static final int ANTE = 5; @@ -22,11 +22,11 @@ public static Set toSet(T... stuff) { @Before public void setup() { - player1 = new Player("player1"); + player1 = new PlayerState(new PlayerIdentifier("player1")); player1.setMoney(200); - player2 = new Player("player2"); + player2 = new PlayerState(new PlayerIdentifier("player2")); player2.setMoney(200); - player3 = new Player("player3"); + player3 = new PlayerState(new PlayerIdentifier("player3")); player3.setMoney(200); } @@ -38,7 +38,7 @@ public void testAnte() { pot.collectAnte(player3, ANTE); Assert.assertEquals(ANTE * 3, pot.getMoney()); Assert.assertEquals(ANTE, pot.getCurrentBet()); - Set expectedList = toSet(player1, player2, player3); + Set expectedList = toSet(player1, player2, player3); Assert.assertEquals(expectedList, pot.getParticipants()); Assert.assertFalse("There shouldn't be a side pot", pot.hasSidePot()); } @@ -51,7 +51,7 @@ public void testAnteAndRaise() { pot.collectAnte(player3, ANTE); Assert.assertEquals(ANTE * 3, pot.getMoney()); Assert.assertEquals(ANTE, pot.getCurrentBet()); - final Set expectedList = toSet(player1, player2, player3); + final Set expectedList = toSet(player1, player2, player3); Assert.assertEquals(expectedList, pot.getParticipants()); Assert.assertFalse("There shouldn't be a side pot", pot.hasSidePot()); pot.checkPlayer(player1); @@ -86,7 +86,7 @@ public void testAnteRaiseAndFold() { pot.collectAnte(player2, ANTE); Assert.assertEquals(ANTE * 2, pot.getMoney()); Assert.assertEquals(ANTE, pot.getCurrentBet()); - final Set expectedList = toSet(player1, player2); + final Set expectedList = toSet(player1, player2); Assert.assertEquals(expectedList, pot.getParticipants()); Assert.assertFalse("There shouldn't be a side pot", pot.hasSidePot()); pot.checkPlayer(player1); @@ -114,7 +114,7 @@ public void testCheckingRound() { pot.checkPlayer(player1); pot.checkPlayer(player2); pot.checkPlayer(player3); - Set expectedSet = toSet(player1, player2, player3); + Set expectedSet = toSet(player1, player2, player3); Assert.assertEquals(expectedSet, pot.getParticipants()); Assert.assertEquals(ANTE*3, pot.getMoney()); Assert.assertFalse("There shouldn't be a side pot", pot.hasSidePot()); @@ -134,7 +134,7 @@ public void testRaisingAndCallingOnAnteRound() { Assert.assertEquals("There should be 215 in the pot",(ANTE * 3)+200, pot.getMoney()); Assert.assertEquals("The current bet should remain unchanged at 105",ANTE + 100, pot.getCurrentBet()); pot.call(player3); - Set expectedList = toSet(player1, player2, player3); + Set expectedList = toSet(player1, player2, player3); Assert.assertEquals(expectedList, pot.getParticipants()); Assert.assertEquals((ANTE * 3)+300, pot.getMoney()); Assert.assertEquals(100 + ANTE, pot.getCurrentBet()); @@ -143,9 +143,9 @@ public void testRaisingAndCallingOnAnteRound() { @Test public void testMakingSidePots() { - final Player scrooge = new Player("Scrooge"); + final PlayerState scrooge = new PlayerState(new PlayerIdentifier("Scrooge")); scrooge.setMoney(200); - final Player donald = new Player("Donald"); + final PlayerState donald = new PlayerState(new PlayerIdentifier("Donald")); donald.setMoney(100); final Pot pot = new Pot(); pot.raise(scrooge, 150); @@ -155,22 +155,22 @@ public void testMakingSidePots() { Assert.assertTrue("There should be a sidepot", pot.hasSidePot()); final Pot sidePot = pot.getSidePot(); Assert.assertEquals("There should be 50 in the side pot", 50, sidePot.getMoney()); - Set everyone = toSet(scrooge, donald); + Set everyone = toSet(scrooge, donald); Assert.assertEquals("Everyone should be in the main pot", everyone, pot.getParticipants()); - Set loneList = toSet(scrooge); + Set loneList = toSet(scrooge); Assert.assertEquals("Scrooge should be alone in the side pot", loneList, sidePot.getParticipants()); Assert.assertEquals("The total pot size should be 250", 250, pot.getTotalMoney()); } @Test public void testMakingSidePotsThreePlayers() { - final Player scrooge = new Player("Scrooge"); + final PlayerState scrooge = new PlayerState("Scrooge"); scrooge.setMoney(500); - final Player gearloose = new Player("Gearloose"); + final PlayerState gearloose = new PlayerState("Gearloose"); gearloose.setMoney(200); - final Player donald = new Player("Donald"); + final PlayerState donald = new PlayerState("Donald"); donald.setMoney(50); - final Set everyone = toSet(scrooge, gearloose, donald); + final Set everyone = toSet(scrooge, gearloose, donald); final Pot pot = new Pot(); pot.raise(scrooge, 100); pot.call(gearloose); @@ -181,19 +181,19 @@ public void testMakingSidePotsThreePlayers() { final Pot sidePot = pot.getSidePot(); Assert.assertEquals("There should be 100 in the side pot", 100, sidePot.getMoney()); Assert.assertEquals("Everyone should be in the main pot", everyone, pot.getParticipants()); - Set sidePotList = toSet(scrooge, gearloose); + Set sidePotList = toSet(scrooge, gearloose); Assert.assertEquals("Scrooge and Gearloose should be in the side pot", sidePotList, sidePot.getParticipants()); } @Test public void testAllins() { - final Player scrooge = new Player("Scrooge"); + final PlayerState scrooge = new PlayerState("Scrooge"); scrooge.setMoney(500); - final Player gearloose = new Player("Gearloose"); + final PlayerState gearloose = new PlayerState("Gearloose"); gearloose.setMoney(200); - final Player donald = new Player("Donald"); + final PlayerState donald = new PlayerState("Donald"); donald.setMoney(50); - final Set everyone = toSet(scrooge, gearloose, donald); + final Set everyone = toSet(scrooge, gearloose, donald); final Pot pot = new Pot(); pot.raise(scrooge, 100); pot.call(gearloose); @@ -204,7 +204,7 @@ public void testAllins() { final Pot sidePot = pot.getSidePot(); Assert.assertEquals("There should be 100 in the side pot", 100, sidePot.getMoney()); Assert.assertEquals("Everyone should be in the main pot", everyone, pot.getParticipants()); - Set sidePotList = toSet(scrooge, gearloose); + Set sidePotList = toSet(scrooge, gearloose); Assert.assertEquals("Scrooge and Gearloose should be in the side pot", sidePotList, sidePot.getParticipants()); pot.newTurn(); pot.checkPlayer(scrooge); @@ -220,15 +220,15 @@ public void testAllins() { @Test public void testMultipleSidepots() { - final Player scrooge = new Player("Scrooge"); + final PlayerState scrooge = new PlayerState("Scrooge"); scrooge.setMoney(500); - final Player gearloose = new Player("Gearloose"); + final PlayerState gearloose = new PlayerState("Gearloose"); gearloose.setMoney(200); - final Player donald = new Player("Donald"); + final PlayerState donald = new PlayerState("Donald"); donald.setMoney(50); - final Set everyone = toSet(scrooge, gearloose, donald); - final Set secondPotParticipants = toSet(scrooge, gearloose); - final Set thirdPotParticipants = toSet(scrooge); + final Set everyone = toSet(scrooge, gearloose, donald); + final Set secondPotParticipants = toSet(scrooge, gearloose); + final Set thirdPotParticipants = toSet(scrooge); final Pot pot = new Pot(); pot.raise(scrooge, 100); pot.call(gearloose); @@ -253,7 +253,7 @@ public void testMultipleSidepots() { @Test public void testReRaiseAndSplitPot() { final Pot pot = new Pot(); - final Set expectedList = toSet(player1, player2); + final Set expectedList = toSet(player1, player2); pot.collectAnte(player1, ANTE); pot.collectAnte(player2, ANTE); Assert.assertEquals(ANTE, pot.getCurrentBet()); diff --git a/src/test/java/me/ars/pokerbot/poker/TableTest.java b/src/test/java/me/ars/pokerbot/poker/TableTest.java index 024f07c..9881393 100644 --- a/src/test/java/me/ars/pokerbot/poker/TableTest.java +++ b/src/test/java/me/ars/pokerbot/poker/TableTest.java @@ -46,11 +46,11 @@ public void shortGameWithAnte() { Mockito.doAnswer(invocation -> { Object[] args = invocation.getArguments(); Object mock = invocation.getMock(); - Player winner = (Player)args[0]; + PlayerIdentifier winner = (PlayerIdentifier)args[0]; System.out.println(winner + " has won!"); hadWinner.set(true); return null; - }).when(callback).declareWinner(any(Player.class), any(Hand.class), anyInt()); + }).when(callback).declareWinner(any(PlayerIdentifier.class), any(Hand.class), anyInt()); Mockito.doAnswer(invocation -> { Object[] args = invocation.getArguments(); @@ -63,16 +63,16 @@ public void shortGameWithAnte() { Mockito.doAnswer(invocation -> { Object[] args = invocation.getArguments(); Object mock = invocation.getMock(); - Player currentPlayer = (Player)args[2]; + PlayerIdentifier currentPlayer = (PlayerIdentifier)args[2]; List cards = (List)args[0]; final String tableStr = cards.isEmpty() ? "no cards" : cards.stream() .map(Card::toString).collect(Collectors.joining(", ")); System.out.println(currentPlayer + " - " + tableStr); return null; - }).when(callback).updateTable(anyList(),anyInt(),any(Player.class)); + }).when(callback).updateTable(anyList(),anyInt(),any(PlayerIdentifier.class)); - final Player player1 = new Player("player1"); - final Player player2 = new Player("player2"); + final PlayerIdentifier player1 = new PlayerIdentifier("player1"); + final PlayerIdentifier player2 = new PlayerIdentifier("player2"); table.registerPlayer(player1); table.registerPlayer(player2); table.startGame(); @@ -90,109 +90,109 @@ public void shortGameWithAnte() { @Test public void testTurnOrderWithAnte() { table = new Table(callback, roster, anteConfig()); - final Player player1 = new Player("player1"); - final Player player2 = new Player("player2"); + final PlayerIdentifier player1 = new PlayerIdentifier("player1"); + final PlayerIdentifier player2 = new PlayerIdentifier("player2"); table.registerPlayer(player1); table.registerPlayer(player2); table.startGame(); - Assert.assertEquals("The first joined player should be the first to play", player1, table.getCurrentPlayer()); + Assert.assertEquals("The first joined player should be the first to play", player1, table.getCurrentPlayerId()); table.check(player1); - Assert.assertEquals("Player 2 should be next", player2, table.getCurrentPlayer()); + Assert.assertEquals("Player 2 should be next", player2, table.getCurrentPlayerId()); table.check(player2); - Assert.assertEquals("Player 1 should be next", player1, table.getCurrentPlayer()); + Assert.assertEquals("Player 1 should be next", player1, table.getCurrentPlayerId()); table.check(player1); - Assert.assertEquals("Player 2 should be next", player2, table.getCurrentPlayer()); + Assert.assertEquals("Player 2 should be next", player2, table.getCurrentPlayerId()); table.raise(player2, 5); - Assert.assertEquals("It should be player 1's turn after player 2 has raised", player1, table.getCurrentPlayer()); + Assert.assertEquals("It should be player 1's turn after player 2 has raised", player1, table.getCurrentPlayerId()); table.call(player1); - Assert.assertEquals("It should be back to Player 1's turn", player1, table.getCurrentPlayer()); + Assert.assertEquals("It should be back to Player 1's turn", player1, table.getCurrentPlayerId()); } @Test public void testTurnOrderWithBlinds() { - final Player p1 = new Player("player1"); - final Player p2 = new Player("player2"); + final PlayerIdentifier p1 = new PlayerIdentifier("player1"); + final PlayerIdentifier p2 = new PlayerIdentifier("player2"); table.registerPlayer(p1); table.registerPlayer(p2); table.startGame(); - Assert.assertEquals("The first joined player should be the first to play", p1, table.getCurrentPlayer()); + Assert.assertEquals("The first joined player should be the first to play", p1, table.getCurrentPlayerId()); table.check(p1); Assert.assertEquals("Player 1 cannot check without calling the blind, it should still be player 1s turn", - p1, table.getCurrentPlayer()); + p1, table.getCurrentPlayerId()); table.call(p1); - Assert.assertEquals("After player 1 calls, it should be player 2", p2, table.getCurrentPlayer()); + Assert.assertEquals("After player 1 calls, it should be player 2", p2, table.getCurrentPlayerId()); table.check(p2); Assert.assertEquals("Player 2 should be able to check, and it should be back to player 1", p1, - table.getCurrentPlayer()); + table.getCurrentPlayerId()); } @Test public void testCashoutDuringGame() { - final Player p1 = new Player("player1"); - final Player p2 = new Player("player2"); - final Player p3 = new Player("player3"); + final PlayerIdentifier p1 = new PlayerIdentifier("player1"); + final PlayerIdentifier p2 = new PlayerIdentifier("player2"); + final PlayerIdentifier p3 = new PlayerIdentifier("player3"); table.registerPlayer(p1); table.registerPlayer(p2); table.registerPlayer(p3); table.startGame(); - Assert.assertEquals("The first joined player should be the first to play", p1, table.getCurrentPlayer()); + Assert.assertEquals("The first joined player should be the first to play", p1, table.getCurrentPlayerId()); table.call(p1); - Assert.assertEquals("After player 1 calls, it should be player 2", p2, table.getCurrentPlayer()); + Assert.assertEquals("After player 1 calls, it should be player 2", p2, table.getCurrentPlayerId()); table.playerLeft(p3); - Assert.assertEquals("After player 3 leaves, it should still be player 2", p2, table.getCurrentPlayer()); + Assert.assertEquals("After player 3 leaves, it should still be player 2", p2, table.getCurrentPlayerId()); table.check(p2); Assert.assertEquals("Player 2 should be able to check, and it should be back to player 1", p1, - table.getCurrentPlayer()); + table.getCurrentPlayerId()); } @Test public void testCashoutDuringGame2() { - final Player p1 = new Player("player1"); - final Player p2 = new Player("player2"); - final Player p3 = new Player("player3"); + final PlayerIdentifier p1 = new PlayerIdentifier("player1"); + final PlayerIdentifier p2 = new PlayerIdentifier("player2"); + final PlayerIdentifier p3 = new PlayerIdentifier("player3"); table.registerPlayer(p1); table.registerPlayer(p2); table.registerPlayer(p3); table.startGame(); - Assert.assertEquals("The first joined player should be the first to play", p1, table.getCurrentPlayer()); + Assert.assertEquals("The first joined player should be the first to play", p1, table.getCurrentPlayerId()); table.playerLeft(p1); - Assert.assertEquals("After player 1 leaves, it should be player 2", p2, table.getCurrentPlayer()); + Assert.assertEquals("After player 1 leaves, it should be player 2", p2, table.getCurrentPlayerId()); table.check(p2); - Assert.assertEquals("After player 2 checks, it should be player 3", p3, table.getCurrentPlayer()); + Assert.assertEquals("After player 2 checks, it should be player 3", p3, table.getCurrentPlayerId()); boolean couldCall = table.call(p3); Assert.assertTrue("Player 3 should be able to call the blind", couldCall); Assert.assertEquals("It should be back to player 2s turn", p2, - table.getCurrentPlayer()); + table.getCurrentPlayerId()); table.check(p2); table.check(p3); table.check(p2); table.check(p3); - Assert.assertEquals("After player 3 checks, it should be player 2", p2, table.getCurrentPlayer()); + Assert.assertEquals("After player 3 checks, it should be player 2", p2, table.getCurrentPlayerId()); } @Test public void testOtherPlayersCashingOut() { final Helper hadWinner = new Helper(false); - final Player p1 = new Player("player1"); - final Player p2 = new Player("player2"); - final Player p3 = new Player("player3"); + final PlayerIdentifier p1 = new PlayerIdentifier("player1"); + final PlayerIdentifier p2 = new PlayerIdentifier("player2"); + final PlayerIdentifier p3 = new PlayerIdentifier("player3"); Mockito.doAnswer(invocation -> { Object[] args = invocation.getArguments(); Object mock = invocation.getMock(); - Player winner = (Player)args[0]; + PlayerIdentifier winner = (PlayerIdentifier)args[0]; Assert.assertEquals("player1 should be the winner", p1, winner); hadWinner.set(true); return null; - }).when(callback).declareWinner(any(Player.class), isNull(), anyInt()); + }).when(callback).declareWinner(any(PlayerIdentifier.class), isNull(), anyInt()); table.registerPlayer(p1); table.registerPlayer(p2); table.registerPlayer(p3); table.startGame(); - Assert.assertEquals("The first joined player should be the first to play", p1, table.getCurrentPlayer()); + Assert.assertEquals("The first joined player should be the first to play", p1, table.getCurrentPlayerId()); table.playerLeft(p2); - Assert.assertEquals("After player 2 leaves, it should still be player 1", p1, table.getCurrentPlayer()); + Assert.assertEquals("After player 2 leaves, it should still be player 1", p1, table.getCurrentPlayerId()); table.playerLeft(p3); Assert.assertTrue("The game must have had a winner", hadWinner.get()); Assert.assertFalse("The game should be over", table.isGameInProgress()); @@ -205,11 +205,11 @@ public void testFlopOrder() { Mockito.doAnswer(invocation -> { Object[] args = invocation.getArguments(); Object mock = invocation.getMock(); - Player winner = (Player)args[0]; + PlayerIdentifier winner = (PlayerIdentifier)args[0]; System.out.println(winner + " has won!"); hadWinner.set(true); return null; - }).when(callback).declareWinner(any(Player.class), any(Hand.class), anyInt()); + }).when(callback).declareWinner(any(PlayerIdentifier.class), any(Hand.class), anyInt()); Mockito.doAnswer(invocation -> { Object[] args = invocation.getArguments(); @@ -222,48 +222,48 @@ public void testFlopOrder() { Mockito.doAnswer(invocation -> { Object[] args = invocation.getArguments(); Object mock = invocation.getMock(); - Player currentPlayer = (Player)args[2]; + PlayerIdentifier currentPlayer = (PlayerIdentifier)args[2]; List cards = (List)args[0]; final String tableStr = cards.isEmpty() ? "no cards" : cards.stream() .map(Card::toString).collect(Collectors.joining(", ")); System.out.println(currentPlayer + " - " + tableStr); return null; - }).when(callback).updateTable(anyList(),anyInt(),any(Player.class)); - final Player p1 = new Player("player1"); - final Player p2 = new Player("player2"); + }).when(callback).updateTable(anyList(),anyInt(),any(PlayerIdentifier.class)); + final PlayerIdentifier p1 = new PlayerIdentifier("player1"); + final PlayerIdentifier p2 = new PlayerIdentifier("player2"); table.registerPlayer(p1); table.registerPlayer(p2); table.startGame(); - Assert.assertEquals("The first joined player should be the first to play", p1, table.getCurrentPlayer()); + Assert.assertEquals("The first joined player should be the first to play", p1, table.getCurrentPlayerId()); Assert.assertEquals("StartPlayer should be 0", 0, table.getStartPlayer()); table.call(p1); - Assert.assertEquals("After player 1 calls, it should be player 2", p2, table.getCurrentPlayer()); + Assert.assertEquals("After player 1 calls, it should be player 2", p2, table.getCurrentPlayerId()); table.check(p2); - Assert.assertEquals("It should be player 1s turn on the first flop.", p1, table.getCurrentPlayer()); + Assert.assertEquals("It should be player 1s turn on the first flop.", p1, table.getCurrentPlayerId()); table.check(p1); table.check(p2); - Assert.assertEquals("It should be player 1s turn on the first turn.", p1, table.getCurrentPlayer()); + Assert.assertEquals("It should be player 1s turn on the first turn.", p1, table.getCurrentPlayerId()); table.check(p1); table.check(p2); - Assert.assertEquals("It should be player 1s turn on the first river.", p1, table.getCurrentPlayer()); + Assert.assertEquals("It should be player 1s turn on the first river.", p1, table.getCurrentPlayerId()); table.check(p1); table.check(p2); Assert.assertTrue("The game must have had a winner", hadWinner.get()); Assert.assertEquals("StartPlayer should be 1", 1, table.getStartPlayer()); - Assert.assertEquals("Player2 should be the starting player on the next hand.", p2, table.getCurrentPlayer()); + Assert.assertEquals("Player2 should be the starting player on the next hand.", p2, table.getCurrentPlayerId()); table.call(p2); - Assert.assertEquals("After player 2 calls, it should be player 1", p1, table.getCurrentPlayer()); + Assert.assertEquals("After player 2 calls, it should be player 1", p1, table.getCurrentPlayerId()); final boolean couldCall = table.call(p1); Assert.assertTrue("Player 1 should be able to call", couldCall); - Assert.assertEquals("It should be player 2s turn on the second flop.", p2, table.getCurrentPlayer()); + Assert.assertEquals("It should be player 2s turn on the second flop.", p2, table.getCurrentPlayerId()); table.check(p2); table.check(p1); - Assert.assertEquals("It should be player 2s turn on the second turn.", p2, table.getCurrentPlayer()); + Assert.assertEquals("It should be player 2s turn on the second turn.", p2, table.getCurrentPlayerId()); table.check(p2); table.check(p1); - Assert.assertEquals("It should be player 2s turn on the second river.", p2, table.getCurrentPlayer()); + Assert.assertEquals("It should be player 2s turn on the second river.", p2, table.getCurrentPlayerId()); table.check(p2); table.check(p1); Assert.assertTrue("The game must have had a winner", hadWinner.get());