Beispiel #1
0
 public void Bet(int amount)
 {
     ValidationException.ThrowIfTrue(amount > Balance, "insufficient funds");
     Balance    -= amount;
     AmountBet  += amount;
     RoundStatus = Balance == 0 ? PlayerRoundStatus.AllIn : PlayerRoundStatus.HasBet;
 }
Beispiel #2
0
        public string Join(string name, int buyIn)
        {
            ValidationException.ThrowIfTrue(Players.Any(x => x.Name == name), "Name taken");
            ValidationException.ThrowIfTrue(Players.Count >= MaxPlayers, "Game already full");

            int[] availableSeats = Enumerable.Range(0, MaxPlayers)
                                   .Where(x => !Players.Any(p => p.SeatNumber == x))
                                   .ToArray();
            int seatIndex = new Random().Next(availableSeats.Count());

            var player = new Player(name, buyIn, availableSeats[seatIndex]);

            Players.Add(player);
            Players = Players.OrderBy(x => x.SeatNumber).ToList();

            return(player.Id);
        }
Beispiel #3
0
        public void MakeMove(string playerId, int?amount)
        {
            Player player = Players.FirstOrDefault(x => x.Id == playerId);

            ValidationException.ThrowIfTrue(player == null, "Invalid player ID");
            ValidationException.ThrowIfTrue(player.SeatNumber != NextSeatNumber, "Not your move");

            ProcessBet(player, amount);

            int highestBet = Players.Max(x => x.AmountBet);

            // All players folded - award money
            if (Players.Count(x => x.RoundStatus != PlayerRoundStatus.Folded) == 1)
            {
                Players.ForEach(x => x.EndRound());
                Pot = Players.Sum(x => x.TotalAmountBet);
                var winner = Players.First(x => x.RoundStatus != PlayerRoundStatus.Folded);
                winner.AwardWinnings(Pot);
                StartRound();
            }
            // All players have matched the bet, folded or are all in and no players are big blind.
            else if (Players.All(x =>
                                 (x.RoundStatus == PlayerRoundStatus.Checked) ||
                                 ((x.AmountBet == highestBet ||
                                   x.RoundStatus == PlayerRoundStatus.Folded ||
                                   x.RoundStatus == PlayerRoundStatus.AllIn) && x.RoundStatus != PlayerRoundStatus.BigBlind && highestBet != 0)))
            {
                Players.ForEach(x => x.EndRound());
                Pot               = Players.Sum(x => x.TotalAmountBet);
                NextSeatNumber    = NextPlayer(DealerSeatNumber).SeatNumber;
                BettingRound      = (BettingRound)((int)BettingRound + 1);
                MinimumMoveAmount = 0;
            }
            else
            {
                Player nextPlayer = NextActivePlayer(player.SeatNumber);
                NextSeatNumber = nextPlayer.SeatNumber;
                CalculateMinMoveAmount();
                // stay in current betting round
            }
        }
Beispiel #4
0
        private void ProcessBet(Player player, int?amount)
        {
            if (amount == null)
            {
                player.Fold();
                return;
            }

            int highestBet = Players.Max(x => x.AmountBet);
            int betAmount  = amount.Value;

            ValidationException.ThrowIfTrue(amount < MinimumMoveAmount, "Les than minimum move amount");

            if (betAmount == 0)
            {
                player.Check();
                return;
            }

            if (betAmount == MinimumMoveAmount)
            {
                player.Call(betAmount);
                return;
            }

            // less than minimum bet, not going all in
            ValidationException.ThrowIfTrue(
                betAmount + player.AmountBet <highestBet + MinimumBetIncrement && player.Balance> betAmount,
                "can not bet less than current bet");

            player.Bet(betAmount);

            // Bet has been made check increment increase
            int playersIncrement = player.AmountBet - highestBet;

            MinimumBetIncrement = Math.Max(playersIncrement, MinimumBetIncrement);
        }