/// <summary> /// displays the cards in hand /// </summary> /// <param name="hand">hand to display</param> /// <param name="turn">participant identifier</param> private void ConvertCards(Hand hand, Enums.Participant turn) { List<string> images = new List<string>(); string name; foreach (Card card in hand.Cards) { // get the name of the card in hand name = card.Suit.ToString().Substring(0, 2) + (int)card.FaceValue; // use URI pack string to get image location images.Add("pack://application:,,,/CST407FinalBlackJack;component/Resources/deck/" + name.ToLower() + ".png"); } switch (turn) { // player case Enums.Participant.Player: for (int i = 0; i < images.Count; i++) { // check for hand count if (images.Count < Constants.HAND_SIZE) // set the images imgPlayerHand[i].Source = new ImageSourceConverter().ConvertFromString(images[i]) as ImageSource; } break; // AI case Enums.Participant.AI: for (int i = 0; i < images.Count; i++) { if (images.Count < Constants.HAND_SIZE) imgBotHand[i].Source = new ImageSourceConverter().ConvertFromString(images[i]) as ImageSource; } break; // dealer case Enums.Participant.Dealer: for (int i = 0; i < images.Count; i++) { if (images.Count < Constants.HAND_SIZE) imgDealerHand[i].Source = new ImageSourceConverter().ConvertFromString(images[i]) as ImageSource; } break; } }
/// <summary> /// deal cards to each hand in play /// </summary> private void DealHands() { // check for bet if (playerHuman.Bet > 0) { // update instruction text lblDeal.Text = "Press R to deal"; // initialize hands playerHand = new Hand(); dealerHand = new Hand(); botHand = new Hand(); // show hand value display if (lblHand.Visibility == Visibility.Hidden || lblHandBot.Visibility == Visibility.Hidden || lblHandDealer.Visibility == Visibility.Hidden) { lblHand.Visibility = Visibility.Visible; lblHandBot.Visibility = Visibility.Visible; lblHandDealer.Visibility = Visibility.Visible; } // add 2 cards to each hand for (int i = 0; i < 2; i++) { // add card botHand.AddCard(mainDeck.Draw()); playerHand.AddCard(mainDeck.Draw()); dealerHand.AddCard(mainDeck.Draw()); } // update hand value display lblHand.Content = PlayerDisplay(); lblHandBot.Content = botHand.GetBestHand().ToString(); // only give hand value of 1 card lblHandDealer.Content = dealerHand.ConvertFaceValue(dealerHand.Cards[0]); // check the first card for ace, if true update possible hand values if (dealerHand.Cards[0].FaceValue == Enums.FaceValue.ace) lblHandDealer.Content += "/11"; // hide cards from last round HideCards(); // update card displays ConvertCards(playerHand, Enums.Participant.Player); ConvertCards(botHand, Enums.Participant.AI); ConvertCards(dealerHand, Enums.Participant.Dealer); // reveal only 1 card ShowCardBack(imgDealerHand, 1); // check for win condition if (playerHand.HasBlackJack()) { AITurn(); CollectChips(CheckWin(playerHand), playerHuman); } } else MessageBox.Show("Please place your bet"); }
/// <summary> /// calculates the end result state for input hand against dealer /// </summary> /// <param name="hand">hand to calculate against dealer</param> /// <returns></returns> private Enums.EndResult CheckWin(Hand hand) { // return value Enums.EndResult result = Enums.EndResult.Waiting; // hand values int playerHandValue = hand.GetBestHand(); int dealerHandValue = dealerHand.GetBestHand(); // win or push, player has blackjack if (hand.HasBlackJack()) { // dealer has blackjack aswell if (dealerHand.HasBlackJack()) result = Enums.EndResult.Push; else result = Enums.EndResult.PlayerBlackJack; } // dealer blackjack else if (dealerHand.HasBlackJack()) result = Enums.EndResult.DealerBlackJack; // push, equal hand value else if (playerHandValue == dealerHandValue) result = Enums.EndResult.Push; // loss, dealer has greater hand value w/o bust else if (dealerHandValue <= 21 && playerHandValue < dealerHandValue) result = Enums.EndResult.DealerWin; // loss, player bust else if (playerHandValue > 21) result = Enums.EndResult.PlayerBust; // win, player has greater hand value w/o bust else if (playerHandValue <= 21 && playerHandValue > dealerHandValue) result = Enums.EndResult.PlayerWin; // win, dealer bust else if (dealerHandValue > 21) result = Enums.EndResult.DealerBust; return result; }