/// <summary> /// Processes a form that contains data about two poker hands that need to /// be evaluated and the data and view needed by the MVC framework to render /// this evaluation for the user. /// </summary> /// <param name="PokerHandsForm"> /// The form data that contains information about the hands being submitted /// for evaluation. Must not be null. /// </param> /// <returns> /// The view, coupled with the evaluation data needed to render that view that /// the MVC framework should use to display the results of evaluating the poker hands. /// </returns> public ActionResult PokerEvaluation(FormCollection PokerHandsForm) { PokerEvaluationViewModel viewModel = viewModelFactory(); PokerHand player1Hand = pokerHandFactory(); PokerHand player2Hand = pokerHandFactory(); try { if (PokerHandsForm == null) { throw new ArgumentNullException("PokerHandsForm"); } for (int handBeingProcessed = 1; handBeingProcessed <= 2; handBeingProcessed++) { PokerHand targetHand = handBeingProcessed == 1 ? player1Hand : player2Hand; String playerNameField = String.Format("player-{0}-name", handBeingProcessed.ToString()); if (!PokerHandsForm.AllKeys.Contains(playerNameField) || String.IsNullOrEmpty(PokerHandsForm[playerNameField]) || String.IsNullOrEmpty(PokerHandsForm[playerNameField].Trim())) { throw new Exception( String.Format( "No name present for player {0}.", handBeingProcessed.ToString())); } targetHand.PlayerName = PokerHandsForm[playerNameField]; for (int cardBeingProcessed = 1; cardBeingProcessed <= 5; cardBeingProcessed++) { Card card = cardFactory(); String cardRankField = String.Format( "rank-player-{0}-card-{1}", handBeingProcessed.ToString(), cardBeingProcessed.ToString()); String cardSuitField = String.Format( "suit-player-{0}-card-{1}", handBeingProcessed.ToString(), cardBeingProcessed.ToString()); if (!PokerHandsForm.AllKeys.Contains(cardRankField) || String.IsNullOrEmpty(PokerHandsForm[cardRankField])) { throw new Exception( String.Format( "Missing rank value for card {0} for player \"{1}.\"", cardBeingProcessed.ToString(), targetHand.PlayerName)); } if (!PokerHandsForm.AllKeys.Contains(cardSuitField) || String.IsNullOrEmpty(PokerHandsForm[cardSuitField])) { throw new Exception( String.Format( "Missing suit value for card {0} for player \"{1}.\"", cardBeingProcessed.ToString(), targetHand.PlayerName)); } CardRank rank = CardRank.Ace; if (!Enum.TryParse <CardRank>(PokerHandsForm[cardRankField], true, out rank)) { throw new Exception( String.Format( "Invalid card rank value \"{0}\".", PokerHandsForm[cardRankField])); } CardSuit suit = CardSuit.Clubs; if (!Enum.TryParse <CardSuit>(PokerHandsForm[cardSuitField], true, out suit)) { throw new Exception( String.Format( "Invalid card suit value \"{0}\".", PokerHandsForm[cardSuitField])); } card.Rank = rank; card.Suit = suit; targetHand.Cards.Add(card); } } viewModel.Verdict = pokerHandJudge.Judge(player1Hand, player2Hand); if (viewModel.Verdict.VerdictType == PokerHandVerdictType.Tie) { viewModel.TiedHands = new List <PokerHand>() { player1Hand, player2Hand } } ; else { viewModel.RunnerUpHand = viewModel.Verdict.WinningHand == player1Hand ? player2Hand : player1Hand; } } catch (Exception ex) { viewModel.ThrownException = ex; } return(View(viewModel)); }
public void WinByHandTypeTest() { PokerHand fullHouseHand = new PokerHand() { PlayerName = "Full House Hand" }; PokerHand threeOfAKindHand = new PokerHand() { PlayerName = "Three of a Kind Hand" }; fullHouseHand.Cards.Add(new Card() { Rank = CardRank.Jack, Suit = CardSuit.Hearts }); fullHouseHand.Cards.Add(new Card() { Rank = CardRank.Jack, Suit = CardSuit.Diamonds }); fullHouseHand.Cards.Add(new Card() { Rank = CardRank.King, Suit = CardSuit.Hearts }); fullHouseHand.Cards.Add(new Card() { Rank = CardRank.King, Suit = CardSuit.Diamonds }); fullHouseHand.Cards.Add(new Card() { Rank = CardRank.Jack, Suit = CardSuit.Clubs }); threeOfAKindHand.Cards.Add(new Card() { Rank = CardRank.Eight, Suit = CardSuit.Clubs }); threeOfAKindHand.Cards.Add(new Card() { Rank = CardRank.Queen, Suit = CardSuit.Diamonds }); threeOfAKindHand.Cards.Add(new Card() { Rank = CardRank.Queen, Suit = CardSuit.Hearts }); threeOfAKindHand.Cards.Add(new Card() { Rank = CardRank.Five, Suit = CardSuit.Clubs }); threeOfAKindHand.Cards.Add(new Card() { Rank = CardRank.Queen, Suit = CardSuit.Clubs }); PokerHandVerdict verdict = testJudge.Judge(fullHouseHand, threeOfAKindHand); Assert.AreEqual( PokerHandVerdictType.WinByHandType, verdict.VerdictType, "Incorrect verdict type determined."); Assert.AreEqual( new KeyValuePair <PokerHandType, PokerHandType>( PokerHandType.FullHouse, PokerHandType.ThreeOfAKind), verdict.DecidingHandTypePair, "Incorrect winning hand type pair determined."); Assert.AreEqual( fullHouseHand, verdict.WinningHand, "Incorrect hand determined to be the winner."); }