public void AddCardTest() { BlackJackHand hand = new BlackJackHand(false); hand.AddCard(new Card(Card.CardSuites.Diamond, Card.CardRanks.Ace)); Assert.AreEqual(hand.HandStatus, BlackJackHand.HandStatusTypes.None); Assert.AreEqual(hand.HandValue, 11); hand.AddCard(new Card(Card.CardSuites.Diamond, Card.CardRanks.Ace)); Assert.AreEqual(hand.HandStatus, BlackJackHand.HandStatusTypes.None); Assert.AreEqual(hand.HandValue, 12); Assert.IsTrue(hand.IsSplittingAllowed); hand.AddCard(new Card(Card.CardSuites.Club, Card.CardRanks.Eight)); Assert.AreEqual(hand.HandStatus, BlackJackHand.HandStatusTypes.None); Assert.AreEqual(hand.HandValue, 20); hand.AddCard(new Card(Card.CardSuites.Club, Card.CardRanks.Eight)); Assert.AreEqual(hand.HandStatus, BlackJackHand.HandStatusTypes.None); Assert.AreEqual(hand.HandValue, 18); hand.AddCard(new Card(Card.CardSuites.Spade, Card.CardRanks.Queen)); Assert.AreEqual(hand.HandStatus, BlackJackHand.HandStatusTypes.Busted); Assert.AreEqual(hand.HandValue, 28); try { hand.AddCard(new Card(Card.CardSuites.Heart, Card.CardRanks.Six)); Assert.Fail("Should have occured InvalidGameOperationException"); } catch (InvalidGameActionException igaEx) { } }
void player_ActionRequested(Player player, BlackJackHand hand) { bool blnInvalidInput = false; do { PrintToConsole(String.Format("Please Select Action:\n\t 1. Hit\n\t 2. Stand\n\t 3. Double{0}\n\t 9. Surrender", (hand.IsSplittingAllowed ? "\n\t 4. Split" : "")), null); Console.Write("Input : "); string strInput = Console.ReadLine(); int intInput = 0; Int32.TryParse(strInput, out intInput); blnInvalidInput = false; switch (intInput) { case 1: player.Hit(hand); break; case 2: player.Stand(hand); break; case 3: player.Double(hand); break; case 4: player.Split(hand); break; case 9: player.Surrender(hand); break; default: // Either non-numeric input or invalid option. { PrintToConsole("Invalid Option. Please enter valid option.\n", ConsoleColor.Red); blnInvalidInput = true; } break; } } while (blnInvalidInput); ////Update score on screen if player's turn is not getting over. ////When player's turn is over, dealer will trigger score updated trigger. //if (hand.HandStatus == BlackJackHand.HandStatusTypes.None || (hand != player.SplittedHand && player.SplittedHand != null)) //{ // PrintToConsole(dealer.GetScoreCard(), null); //} }
public void SplitHandTest() { BlackJackHand hand = new BlackJackHand(false); hand.AddCard(new Card(Card.CardSuites.Diamond, Card.CardRanks.Jack)); hand.AddCard(new Card(Card.CardSuites.Diamond, Card.CardRanks.Queen)); Assert.IsTrue(hand.IsSplittingAllowed); BlackJackHand handSplitted = hand.SplitHand(); Assert.IsNotNull(handSplitted); Assert.AreNotSame(handSplitted, hand); Assert.IsFalse(hand.IsSplittingAllowed); Assert.IsFalse(handSplitted.IsSplittingAllowed); try { hand.SplitHand(); Assert.Fail("Should have thrown InvalidGameException as hand is already splitted."); } catch (InvalidGameActionException igaEx) { } }
/// <summary> /// Resets current game and initiates new game. /// </summary> public virtual void NewGame() { Hand = new BlackJackHand(false); }
/// <summary> /// constructor to initialize instance of generic player. /// </summary> /// <param name="name">Name of the player.</param> public GenericPlayer(string name) { this.Name = name; Hand = new BlackJackHand(false); }
private string PrepareStringForHand(BlackJackHand hand, bool revealLastCard) { StringBuilder sb = new StringBuilder(); Card[] cards = hand.GetCards(); for (int i = 0; i < cards.Length; i++) { if (i < (cards.Length - 1) || revealLastCard) { sb.AppendLine(String.Format("\t{0}. {1}", i + 1, cards[i].ToString())); } else { sb.AppendLine(String.Format("\t{0}. XXX", i + 1)); } } if (revealLastCard) { sb.AppendLine(String.Format("Hand value: {0} {1}", hand.HandValue, ((hand.HandStatus != BlackJackHand.HandStatusTypes.None) ? ("(" + hand.HandStatus.ToString() + ")") : ""))); } return sb.ToString(); }
/// <summary> /// Split hand. /// </summary> /// <returns>Newly created hand after splitting.</returns> /// <exception cref="InvalidGameException">Thrown when hand is not in state when it's allowed to split. Check IsSplitAllowed property to check whether splitting is allowed or not.</exception> public BlackJackHand SplitHand() { if (IsSplittingAllowed) { BlackJackHand splittedHand = new BlackJackHand(true); splittedHand.AddCard(lstCards[1]); this.lstCards.Remove(lstCards[1]); this.IsSplittingAllowed = false; this.isSplittedHand = true; ReCalculateHandValue(); return splittedHand; } else { throw new Exceptions.InvalidGameActionException("Hand can not be splitted in current state. Check IsSplitAllowed property to check whether splitting is allowed or not."); } }
/// <summary> /// Takes double action for player. /// </summary> /// <param name="hand">Hand on which double action is requested.</param> /// <exception cref="InvalidGameActionException">Thrown when hand is busted, standed or blackjacked.</exception> public void Double(BlackJackHand hand) { this.Hit(hand); if (hand.HandStatus != BlackJackHand.HandStatusTypes.Busted) { this.Stand(hand); } }
private void PlayHand(BlackJackHand hand) { while (hand.HandStatus == DomainObjects.BlackJackHand.HandStatusTypes.None) { dealer.TriggerUpdateScoreBoard(); if (ActionRequested != null) { ActionRequested(this, hand); } else { throw new BJException("ActionRequested event is not subscribed. Please subscribe this event to provide input."); } } }
/// <summary> /// Requests a new card from dealer and adds it to hand. /// </summary> /// <param name="hand">Hand to add card in.</param> private void PullCard(BlackJackHand hand) { hand.AddCard(dealer.DealCard()); }
/// <summary> /// Surrenders the player from the game. /// </summary> /// <exception cref="InvalidGameActionException">Thrown when trying to surrender hand while it is busted, standed or blackjacked.</exception> public void Surrender(BlackJackHand hand) { this.Hand.Surrender(); }
/// <summary> /// Method to stand player. /// </summary> /// <param name="hand">Hand to apply stand action on.</param> /// <exception cref="InvalidGameActionException">Thrown when trying to stand hand while it is busted, standed or blackjacked.</exception> public void Stand(BlackJackHand hand) { hand.Stand(); }
/// <summary> /// Splits hand of the player. /// </summary> /// <param name="hand">Hand being splitted.</param> /// <exception cref="InvalidOperationException">Thrown when hand is not in state when it's allowed to split. Check IsSplitAllowed property to check whether splitting is allowed or not.</exception> public void Split(BlackJackHand hand) { if (!this.IsSplitted) { if (hand.IsSplittingAllowed) { SplittedHand = hand.SplitHand(); } else { throw new InvalidGameActionException("Splitting not allowed for the hand."); } } else { throw new InvalidGameActionException("Splitting is only allowed once at start of the game."); } }
public override void NewGame() { base.NewGame(); this.SplittedHand = null; }
/// <summary> /// Method to take hit action. /// </summary> /// <param name="hand">Hand on which hit is requested.</param> /// <exception cref="InvalidGameActionException">Thrown when hand is busted and player tries to add more cards to it.</exception> public void Hit(BlackJackHand hand) { hand.AddCard(dealer.DealCard()); }