public bool Bet(string name, double wager) { var player = GetPlayer(name); VerifyIsPlayersTurn(player); if (player.Folded) { throw new Exception($"{player.Name} cannot bet after folding"); } if (wager + player.CurrentBet < MinBet && wager != player.Chips) { throw new Exception($"{player.Name} attempted to bet below min bet of {MinBet}"); } if (player.Chips < wager) { throw new Exception($"{player.Name} attempt to bet {wager} and only has {player.Chips}"); } // Remove the player from queue. BetQueue.RemoveFirst(); if (wager + player.CurrentBet > MinBet) { // Need to requeue other players. var startAt = PlayerAfterRecursive(player); if (BetQueue.Count > 0) { startAt = PlayerAfterRecursive(BetQueue.Last.Value); } while (startAt.Position != player.Position) { BetQueue.AddLast(startAt); startAt = PlayerAfterRecursive(startAt); } } // Add the players bet to pot. AdjustPotPlayerChips(player, wager); if (BetQueue.Count == 0) { NextStage(); return(false); } else { return(true); } }
private void RequeuePlayers() { var firstPlayer = PlayerAfterRecursive(Dealer); var startAt = firstPlayer; do { if (startAt != Dealer || (startAt == Dealer && !Dealer.Folded)) { BetQueue.AddLast(startAt); startAt = PlayerAfterRecursive(startAt); } } while (startAt.Position != firstPlayer.Position); }
public void Start() { var enoughPlayers = Players.Where(x => x.Chips > 0).Count() > 1; if (!enoughPlayers) { throw new Exception("Not enough players with chips to play"); } foreach (var p in Players) { p.AllIn = false; p.Folded = false; p.Hand[0] = null; p.Hand[1] = null; } Dealer = Dealer == null ? Players[0] : PlayerAfterRecursive(Dealer); LittleBlind = PlayerAfterRecursive(Dealer); BigBlind = PlayerAfterRecursive(LittleBlind); for (int i = 0; i < Table.Length; i++) { Table[i] = null; } Deck = GetNewDeck(); State = State.Start; Pots = new List <Pot>() { new Pot() }; currentPotIndex = 0; Pots[currentPotIndex].EligiblePlayers = Players.Where(x => x.Chips > 0).ToList(); ShowPlayerCards = new List <Player>(); // Add everyone to the bet queue. var nextToBet = PlayerAfterRecursive(BigBlind); while (nextToBet.Position != LittleBlind.Position) { BetQueue.AddLast(nextToBet); nextToBet = PlayerAfterRecursive(nextToBet); } BetQueue.AddLast(LittleBlind); // Need to add him back in since he only betted 25 so far. AdjustPotPlayerChips(LittleBlind, BigBlindAmount / 2.0); AdjustPotPlayerChips(BigBlind, BigBlindAmount); Deal(0); Deal(1); }
public void NextStage() { //todo: verify enough players to continue play. if (State == State.Start) { SetPot(currentPotIndex); RequeuePlayers(); Table[0] = NextCardInDeck(); Table[1] = NextCardInDeck(); Table[2] = NextCardInDeck(); State = State.Flop; if (BetQueue.Count() <= 1) { BetQueue.Clear(); } } else if (State == State.Flop) { SetPot(currentPotIndex); RequeuePlayers(); Table[3] = NextCardInDeck(); State = State.Turn; if (BetQueue.Count() == 1) { BetQueue.Clear(); } } else if (State == State.Turn) { SetPot(currentPotIndex); RequeuePlayers(); Table[4] = NextCardInDeck(); State = State.River; if (BetQueue.Count() == 1) { BetQueue.Clear(); } } else if (State == State.River) { SetPot(currentPotIndex); PlayerHands = new Dictionary <string, Hand>(); ShowPlayerCards = new List <Player>(); foreach (var player in GetPlayersStillInGame()) { PlayerHands.Add(player.Name, new Hand(GetPlayerCards(player.Name).ToArray(), Table)); } BetQueue.Clear(); State = State.DeterminingWinner; var playerThatNeedToHideShow = new List <Player>(); foreach (var pot in Pots) { var testPlayerShowCards = Dealer; var firstPlayerIn = false; Hand bestHand = null; do { testPlayerShowCards = PlayerAfter(testPlayerShowCards.Position); if (!testPlayerShowCards.Folded && testPlayerShowCards.Chips != 0 && testPlayerShowCards.Hand[0] != null) { if (!playerThatNeedToHideShow.Contains(testPlayerShowCards)) { if (!firstPlayerIn) { firstPlayerIn = true; bestHand = PlayerHands[testPlayerShowCards.Name]; ShowPlayerCards.Add(testPlayerShowCards); } else { if (PlayerHands[testPlayerShowCards.Name] < bestHand) { playerThatNeedToHideShow.Add(testPlayerShowCards); } else { ShowPlayerCards.Add(testPlayerShowCards); if (PlayerHands[testPlayerShowCards.Name] > bestHand) { bestHand = PlayerHands[testPlayerShowCards.Name]; } } } } } } while (testPlayerShowCards != Dealer); } var requeuePlayer = Dealer; do { requeuePlayer = PlayerAfter(requeuePlayer.Position); if (playerThatNeedToHideShow.Contains(requeuePlayer)) { BetQueue.AddLast(requeuePlayer); } } while (requeuePlayer != Dealer); } else if (State == State.DeterminingWinner) { SetPot(currentPotIndex); State = State.DistributingPot; foreach (var pot in Pots) { double share = 0; Player winner = null; Hand winningHand = null; var winners = new List <Player>(); if (pot.EligiblePlayers.Count() == 1) { winners.Add(pot.EligiblePlayers[0]); } else { foreach (var p in pot.EligiblePlayers) { var hand = PlayerHands[p.Name]; if (winner == null || hand > winningHand) { winners.Clear(); winningHand = hand; winner = p; winners.Add(winner); } else if (hand == winningHand) { winners.Add(p); } } } share = pot.PotAmount / winners.Count; foreach (var w in winners) { w.CurrentBet += share; } } var allWinners = Players.Where(x => x.CurrentBet > 0); if (GetPlayersStillInGame().Count() == 1) { allWinners.ElementAt(0).Hand[0] = null; allWinners.ElementAt(0).Hand[1] = null; var newHandHistory = new HandHistory(); newHandHistory.PlayerName = allWinners.ElementAt(0).Name; newHandHistory.MoneyWon = allWinners.ElementAt(0).CurrentBet; if (HandHistory.Count == 50) { HandHistory.RemoveAt(0); } HandHistory.Add(newHandHistory); } else { foreach (var handWinner in allWinners) { var newHandHistory = new HandHistory(); newHandHistory.PlayerName = handWinner.Name; newHandHistory.MoneyWon = handWinner.CurrentBet; newHandHistory.Hand = PlayerHands[newHandHistory.PlayerName]; HandHistory.Add(newHandHistory); } } Pots.Clear(); } else if (State == State.DistributingPot) { foreach (var p in Players) { p.Chips += p.CurrentBet; p.Hand[0] = null; p.Hand[1] = null; } for (int i = 0; i < Table.Length; i++) { Table[i] = null; } State = State.GameOver; } else { Start(); } if (State != State.DistributingPot && State != State.Start) { foreach (var p in Players) { p.CurrentBet = 0; } MinBet = 0; // Allows checks. } }