public PlayerHand tieBreaker(HandRank rank, PlayerHand hand1, PlayerHand hand2) { this.hand = hand1; int hand1rank = map[rank](this); this.hand = hand2; int hand2rank = map[rank](this); if (hand1rank > hand2rank) { return(hand1); } else if (hand2rank > hand1rank) { return(hand2); } else { return(null); } }
public void setHand(PlayerHand hand) { this.hand = hand; }
public void StartGame() { List <PlayingUser> winners = new List <PlayingUser>(); List <string> userNames = new List <string>(); gamePref.SetStatus("Active"); foreach (PlayingUser player in waitingList) { players.Add(player); playerBets.Add(player, player.GetCredit()); } foreach (PlayingUser player in players) { player.GainPerRound.Add(0); } waitingList.Clear(); foreach (SpectatingUser spec in specWaitingList) { spectators.Add(spec); } specWaitingList.Clear(); updatePlayerBets(0, true); //SystemLogger.Log("game started","GameLogs.log"); //if (gamePref.GetMinPlayers() > GetNumberOfPlayers()) // throw new InvalidOperationException("Can't start game with less than the minimum number of players"); Thread.Sleep(1000); int potInt = pot[0]; //announce game started gameEnded = false; gameDeck.Shuffle(); //send the array of players to all the observers PushStartGameMove(); //Thread.Sleep(2000); //waiting for observers updatePlayerBets(0, false); //request small blind PlayingUser smallBlindPlayer = players.ElementAt(0); int smallBlindBet = smallBlindPlayer.GetBlind(gamePref.GetsB()); bettingRound += smallBlindBet; playerBets[smallBlindPlayer] = smallBlindBet; //send small blind move PushBetMove(smallBlindPlayer, smallBlindBet); //request big blind PlayingUser bigBlindPlayer = players.ElementAt(1); int bigBlindBet = bigBlindPlayer.GetBlind(gamePref.GetbB()); bettingRound += bigBlindBet; playerBets[bigBlindPlayer] = bigBlindBet; //send big blind move PushBetMove(bigBlindPlayer, bigBlindBet); //draw hands foreach (PlayingUser player in players) { PlayerHand ph = gameDeck.drawPlayerHand(); player.SetHand(ph); NotificationService.Instance.setHand(player.GetUserName(), ph, id); playerHands.Add(player.GetUserName(), ph); //only username } //request bet from rest of players TraversePlayers(2 % players.Count); if (gameEnded) //WAS gameEnded { GiveWinnings(); PushMoveToObservers(new EndGameMove(playerHands)); winners = DetermineWinner(); userNames = spectators.Union(players).Select(user => user.GetUserName()).ToList(); NotificationService.Instance.pushWinners(userNames, winners.Select(winner => winner.GetUserName()).ToList(), id); //ResetGame(); goto GameEnd; } cards = new Card[5]; Card[] flop = gameDeck.drawFlop(); cards[0] = flop[0]; cards[1] = flop[1]; cards[2] = flop[2]; PushMoveToObservers(new NewCardMove(cards)); TraversePlayers(0); if (gameEnded) { GiveWinnings(); PushMoveToObservers(new EndGameMove(playerHands)); winners = DetermineWinner(); userNames = spectators.Union(players).Select(user => user.GetUserName()).ToList(); NotificationService.Instance.pushWinners(userNames, winners.Select(winner => winner.GetUserName()).ToList(), id); //ResetGame(); goto GameEnd; } cards[3] = gameDeck.DrawTableCard(); PushMoveToObservers(new NewCardMove(cards)); TraversePlayers(0); if (gameEnded) { GiveWinnings(); PushMoveToObservers(new EndGameMove(playerHands)); winners = DetermineWinner(); userNames = spectators.Union(players).Select(user => user.GetUserName()).ToList(); NotificationService.Instance.pushWinners(userNames, winners.Select(winner => winner.GetUserName()).ToList(), id); //ResetGame(); goto GameEnd; } cards[4] = gameDeck.DrawTableCard(); PushMoveToObservers(new NewCardMove(cards)); TraversePlayers(0); if (gameEnded) { GiveWinnings(); PushMoveToObservers(new EndGameMove(playerHands)); winners = DetermineWinner(); userNames = spectators.Union(players).Select(user => user.GetUserName()).ToList(); NotificationService.Instance.pushWinners(userNames, winners.Select(winner => winner.GetUserName()).ToList(), id); //ResetGame(); goto GameEnd; } PushMoveToObservers(new EndGameMove(playerHands)); winners = DetermineWinner(); userNames = spectators.Union(players).Select(user => user.GetUserName()).ToList(); NotificationService.Instance.pushWinners(userNames, winners.Select(winner => winner.GetUserName()).ToList(), id); foreach (PlayingUser player in winners) { player.ReceiveWinnings(pot[0] / winners.Count); } foreach (PlayingUser player in players) { if (!winners.Contains(player)) { player.SetRoundsLost(player.GetRoundsLost() + 1); } } GameEnd: gamePref.Status = "Inactive"; ResetGame(); Thread.Sleep(10000); if ((waitingList.Count + playerBets.Count) >= gamePref.GetMinPlayers()) { StartGame(); } }
private List <PlayingUser> DetermineWinner() { Dictionary <PlayingUser, CardAnalyzer.HandRank> playerScores = new Dictionary <PlayingUser, CardAnalyzer.HandRank>(); ca.setCardArray(cards); foreach (PlayingUser player in players) { if (player.GetStatus() != "Fold") { ca.setHand(player.GetHand()); playerScores.Add(player, ca.analyze()); if (playerScores[player] < player.GetBestHand()) { player.SetBestHand(playerScores[player]); } } } CardAnalyzer.HandRank minValue = CardAnalyzer.HandRank.HighCard; foreach (CardAnalyzer.HandRank rank in playerScores.Values) { if (rank < minValue) { minValue = rank; } } Dictionary <PlayingUser, CardAnalyzer.HandRank> bestPlayerScores = new Dictionary <PlayingUser, CardAnalyzer.HandRank>(); foreach (PlayingUser player in playerScores.Keys) { if (playerScores[player] == minValue) { bestPlayerScores.Add(player, playerScores[player]); } } List <PlayingUser> winners = new List <PlayingUser>(); foreach (PlayingUser player in bestPlayerScores.Keys) { if (winners.Count == 0) { winners.Add(player); } else { PlayerHand winner = ca.tieBreaker(minValue, winners.First().GetHand(), player.GetHand()); if (winner == null) { winners.Add(player); } else if (winner == player.GetHand()) { winners.Clear(); winners.Add(player); } } } return(winners); }