private async void Hit(object sender, RoutedEventArgs e) { Card nextCard = deck.PickCard(); player.Score += nextCard.Value; switch (player.CardCount) { case 2: PCard3.Source = Card.CardToImage(nextCard); break; case 3: PCard4.Source = Card.CardToImage(nextCard); break; case 4: PCard5.Source = Card.CardToImage(nextCard); break; case 5: PCard6.Source = Card.CardToImage(nextCard); break; case 6: PCard7.Source = Card.CardToImage(nextCard); break; case 7: PCard8.Source = Card.CardToImage(nextCard); break; case 8: PCard9.Source = Card.CardToImage(nextCard); break; } UpdateScore(); player.CardCount++; //If player blackjacks or player busts, show the dealer card and end the game. Else re-enable the action buttons. if (player.Score == 21 && player.Score > dealerScore) { announcer.Text = $"Blackjack! You win!{Environment.NewLine}+${player.Blackjack() - player.BetAmt}"; await Reset(); } else if (player.Score == 21 && player.Score == dealerScore) { announcer.Text = $"Push.{Environment.NewLine}+$0"; await Reset(); } else if (player.Score > 21) { announcer.Text = $"Bust! You lose.{Environment.NewLine}-${player.BetAmt}"; await Reset(); } }
private async Task Reset() { UpdateStats(); ShowAnnouncer(); DisableActions(); DCard1.Source = Card.CardToImage(hidden); ShowScore(true); await Task.Delay(3000); HideScore(); PCard1.Source = null; PCard2.Source = null; PCard3.Source = null; PCard4.Source = null; PCard5.Source = null; PCard6.Source = null; PCard7.Source = null; PCard8.Source = null; DCard1.Source = null; DCard2.Source = null; DCard3.Source = null; DCard4.Source = null; DCard5.Source = null; DCard6.Source = null; DCard7.Source = null; DCard8.Source = null; player.BetAmt = 0; player.Score = 0; player.CardCount = 2; deck = new Deck(); dealerScore = 0; UpdateStats(); if (player.IsBroke()) { announcer.Text = $"You're in{Environment.NewLine}debt!"; await Task.Delay(5000); Application.Current.Shutdown(); System.Diagnostics.Process.Start(Application.ResourceAssembly.Location); } else { announcer.Text = "Place your bet!"; EnableBets(); } }
public async Task SpreadCards() { EnableActions(); ShowScore(false); Card pc1 = deck.PickCard(); Card pc2 = deck.PickCard(); Card dc1 = deck.PickCard(); Card dc2 = deck.PickCard(); dealerScore = dc1.Value + dc2.Value; player.Score = pc1.Value; PCard1.Source = Card.CardToImage(pc1); UpdateScore(); await Task.Delay(500); player.Score += pc2.Value; UpdateScore(); PCard2.Source = Card.CardToImage(pc2); await Task.Delay(500); DCard1.Source = Card.DealerCardToImage(dc1); //This is the initially hidden dealer card. hidden = new Card(dc1.Name, dc1.Suit, dc1.Value); //We need to save it, because we'll need it in another method. await Task.Delay(500); DCard2.Source = Card.CardToImage(dc2); //Check if player blackjack or if dealer blackjack, show the dealer card and end the game. Else enable the action buttons. if (player.Score == 21 && dealerScore == 21) { announcer.Text = $"Push.{Environment.NewLine}+$0"; await Reset(); } else if (player.Score == 21) { announcer.Text = $"Blackjack! You win!{Environment.NewLine}+${player.Blackjack()}"; await Reset(); } else if (dealerScore == 21) { announcer.Text = $"Dealer blackjack! You lose.{Environment.NewLine}-${player.BetAmt}"; await Reset(); } }
private async Task Stand() { DisableActions(); DCard1.Source = Card.CardToImage(hidden); ShowScore(true); int cardCount = 2; while (dealerScore < 17) { //Automated hit algorithm. Card nextCard = deck.PickCard(); dealerScore += nextCard.Value; await Task.Delay(500); switch (cardCount) { case 2: DCard3.Source = Card.CardToImage(nextCard); break; case 3: DCard4.Source = Card.CardToImage(nextCard); break; case 4: DCard5.Source = Card.CardToImage(nextCard); break; case 5: DCard6.Source = Card.CardToImage(nextCard); break; case 6: DCard7.Source = Card.CardToImage(nextCard); break; case 7: DCard8.Source = Card.CardToImage(nextCard); break; case 8: DCard9.Source = Card.CardToImage(nextCard); break; } cardCount++; UpdateScore(); } //If score isn't blackjack for any sides, or if the player hasn't gone bust from hitting, one of the following will apply: if (dealerScore > 21) { announcer.Text = $"Dealer bust! You win!{Environment.NewLine}+${player.Win() - player.BetAmt}"; } else if (dealerScore > player.Score) { announcer.Text = $"Dealer wins.{Environment.NewLine}-${player.BetAmt}"; } else if (dealerScore < player.Score) { announcer.Text = $"You win!{Environment.NewLine}+${player.Win() - player.BetAmt}"; } else { announcer.Text = $"Push.{Environment.NewLine}+$0"; } await Reset(); }