//if start button clicked private void btnStart_Click(object sender, RoutedEventArgs e) { //clears card value text txtBlCardValue.Text = ""; //check if the game has been restarted to 0 if (gameRestarted == true && gameStarted == false) { //check if nothing is in text box if (String.IsNullOrEmpty(txtBxEnterName.Text)) { txtBlNameFound.Text = "Enter Name First"; DialogHost.IsOpen = true; } else { //else start the game thus turing the game started bool too true gameStarted = true; playerFound = false; deck = new Deck(); #region PlayerHand //creates the players hand Hand playerHand = new Hand(deck); //draws 2 cards for player hand Card firstCard = deck.DrawCard(playerHand); Card secondCard = deck.DrawCard(playerHand); //gets the sums of these cards int firstCardNum = playerHand.AddValue(firstCard, playerSum); int secondCardNum = playerHand.AddValue(secondCard, playerSum); //equal them too overall player sum playerSum = firstCardNum + secondCardNum; //display sum playerSumString = playerSum.ToString(); txtBlPlayerTotal.Text = playerSumString; //display the image of the first 2 cards BitmapImage userFirstbitmapImage = Convert(firstCard.ReturnImage()); ImgUserFirstCard.Source = userFirstbitmapImage; BitmapImage userSecondbitmapImage = Convert(secondCard.ReturnImage()); ImgUserSecondCard.Source = userSecondbitmapImage; #endregion PlayerHand #region DealerHand //creates the dealers hand Hand dealerHand = new Hand(deck); //draws dealers first card Card dealerCard = deck.DrawCard(dealerHand); //gets card value and equal it too dealer sum dealerSum = dealerHand.AddValue(dealerCard, dealerSum); //display dealer sum dealerSumString = dealerSum.ToString(); txtBlDealerTotal.Text = dealerSumString; //display dealer card image BitmapImage dealerbitmapImage = Convert(dealerCard.ReturnImage()); ImgDealerFirstCard.Source = dealerbitmapImage; #endregion DealerHand //check if player is a returning one foreach (Player returningPlayer in players) { //check if player name can be found in player list if so turn returning player too true and display message if (returningPlayer.PlayerName == txtBxEnterName.Text) { playerReturned = true; txtBlNameFound.Text = "Returning Player"; DialogHost.IsOpen = true; } } //if player returning is true if (playerReturned == true) { //display there name foreach (Player returningPlayer in players) { if (returningPlayer.PlayerName == txtBxEnterName.Text) { returningPlayer.PlayerName = txtBxEnterName.Text; txtBlCurrentPlayer.Text = returningPlayer.PlayerName; } } //get whats turned into the txt box turn it into a string string x = txtBxEnterName.Text; gameInProgress = false; //compare the string too the list and see which matches foreach (Player currentPlayer in players) { //if the string matches the players name if (x == currentPlayer.PlayerName) { //if the player num is equal too 21 they win if (playerSum == 21) { Win(); return; } } } } else { //clears all wins losses and drwas for new player txtBlWin.Text = "0"; txtBlLosses.Text = "0"; txtBlDraws.Text = "0"; //if player is not a returning player create a new player Player newPlayer = new Player(); players.Add(newPlayer); newPlayer.PlayerName = txtBxEnterName.Text; txtBlCurrentPlayer.Text = newPlayer.PlayerName; string x = txtBxEnterName.Text; gameInProgress = false; foreach (Player currentPlayer in players) { if (x == currentPlayer.PlayerName) { if (playerSum == 21) { Win(); return; } } } } } } //if game has not been restarted display message else { txtBlNameFound.Text = "Restart Game and press start"; DialogHost.IsOpen = true; } }
//When double down is clicked private void btnDoubleDown_Click(object sender, RoutedEventArgs e) { if (gameRestarted == true && gameStarted == true) { if (String.IsNullOrEmpty(txtBxEnterName.Text)) { txtBlNameFound.Text = string.Format("Enter Name First"); DialogHost.IsOpen = true; } //if hit button has been pressed cant double down else if (ifHit == true) { txtBlNameFound.Text = string.Format("Cannot double down after hit was pressed"); DialogHost.IsOpen = true; } else { playerFound = false; string x = txtBxEnterName.Text; foreach (Player newPlayer in players) { if (newPlayer.PlayerName == x) { playerFound = true; //get 2 new cards add them too the total and display them playerHand = new Hand(deck); Card firstCardDoubleDown = deck.DrawCard(playerHand); Card secondCardDoubleDown = deck.DrawCard(playerHand); int firstCardDoubleDownNum = playerHand.AddValue(firstCardDoubleDown, playerSum); int secondCardDoubleDownNum = playerHand.AddValue(secondCardDoubleDown, playerSum); int total = (firstCardDoubleDownNum + secondCardDoubleDownNum) - playerSum; playerSum = total; playerSumString = playerSum.ToString(); txtBlPlayerTotal.Text = playerSumString; BitmapImage userFirstbitmapImage = Convert(firstCardDoubleDown.ReturnImage()); BitmapImage userSecondbitmapImage = Convert(secondCardDoubleDown.ReturnImage()); ImgUserThirdCard.Source = userFirstbitmapImage; ImgUserFourthCard.Source = userSecondbitmapImage; //if player sum over 21 they lose same as 21 they win otherwise the dealer can now play if (playerSum > 21) { Loss(); return; } else if (playerSum == 21) { Win(); return; } else { Dealer(); } } } if (playerFound == false) { txtBlNameFound.Text = string.Format("Player changed, please press start"); DialogHost.IsOpen = true; } } } else { txtBlNameFound.Text = string.Format("Restart Game and press Start"); DialogHost.IsOpen = true; } }
//Method for when it's the dealers turn public void Dealer() { //gets dealers second card displays total and card image #region DealerCard dealerHand = new Hand(deck); Card firstCard = deck.DrawCard(dealerHand); int firstCardNum = dealerHand.AddValue(firstCard, dealerSum); dealerSum = firstCardNum; dealerSumString = dealerSum.ToString(); txtBlDealerTotal.Text = dealerSumString; BitmapImage userFirstbitmapImage = Convert(firstCard.ReturnImage()); ImgDealerSecondCard.Source = userFirstbitmapImage; #endregion DealerCard //if dealer has exactly 21 you lose if (dealerSum == 21) { Loss(); return; } //if dealer's number is below 21 else if (dealerSum < 21) { //go through loop 10 times for (int i = 0; i < 10; i++) { //if dealer number more then player number and dealer number less then or equal to 21 you lose if (dealerSum > playerSum && dealerSum <= 21) { Loss(); return; } //if dealer number below 21 and also below the player's number else if (dealerSum <= 21 && dealerSum < playerSum) { Card newCard = deck.DrawCard(dealerHand); int newCardNum = dealerHand.AddValue(newCard, dealerSum); dealerSum = newCardNum; dealerSumString = dealerSum.ToString(); txtBlDealerTotal.Text = dealerSumString; BitmapImage dealerNewCardbitmapImage = Convert(newCard.ReturnImage()); //depending on if the place for the new card image is null display it this place if it is not null look at the next spot if (ImgDealerThirdCard.Source == null) { ImgDealerThirdCard.Source = dealerNewCardbitmapImage; } else if (ImgDealerFourthCard.Source == null) { ImgDealerFourthCard.Source = dealerNewCardbitmapImage; } else if (ImgDealerFifthCard.Source == null) { ImgDealerFifthCard.Source = dealerNewCardbitmapImage; } else { ImgDealerFirstCard.Source = dealerNewCardbitmapImage; } //if dealer number = too 21 and equal too player number it's a draw if (dealerSum == 21 && dealerSum == playerSum) { Draw(); return; } //if dealer number = to 21 and more then player number you lose else if (dealerSum == 21 && dealerSum > playerSum) { Loss(); return; } //if dealer number more then 21 you win else if (dealerSum > 21) { Win(); return; } } //if dealer number less than = 21 and less then player num else if (dealerSum <= 21 && dealerSum < playerSum) { Win(); return; } //if dealer number == player number its a draw else if (dealerSum == playerSum) { Draw(); return; } //else if dealer num is more than player num else if (dealerSum > playerSum) { Loss(); return; } //if dealer number is more than 21 else if (dealerSum > 21) { Win(); return; } } } }
//if btn has been clicked private void btnHitMe_Click(object sender, RoutedEventArgs e) { //if game has been restarted and the game has been started if (gameRestarted == true && gameInProgress == false && gameStarted == true) { //name has to be entred to use this button if (String.IsNullOrEmpty(txtBxEnterName.Text)) { txtBlNameFound.Text = "Enter Name First"; DialogHost.IsOpen = true; } else { //set player found too false playerFound = false; //get the name in the textbox string x = txtBxEnterName.Text; //check in the list if that name matches with the player that is playing foreach (Player newPlayer in players) { if (newPlayer.PlayerName == x) { //if player found set too true playerFound = true; ifHit = true; //get random card between 1 and 10 and add it too your total playerHand = new Hand(deck); Card HitCard = deck.DrawCard(playerHand); int HitCardNum = playerHand.AddValue(HitCard, playerSum); playerSum = HitCardNum; playerSumString = playerSum.ToString(); txtBlPlayerTotal.Text = playerSumString; BitmapImage userHitbitmapImage = Convert(HitCard.ReturnImage()); if (ImgUserThirdCard.Source == null) { ImgUserThirdCard.Source = userHitbitmapImage; } else if (ImgUserFourthCard.Source == null) { ImgUserFourthCard.Source = userHitbitmapImage; } else if (ImgUserFifthCard.Source == null) { ImgUserFifthCard.Source = userHitbitmapImage; } else { ImgUserFirstCard.Source = userHitbitmapImage; } //if player gets more then 21 they lose or if player gets exactly 21 they win if (playerSum > 21) { Loss(); return; } else if (playerSum == 21) { Win(); return; } } } //if player cant be found as the same player in the txtbx get a warning if (playerFound == false) { txtBlNameFound.Text = "Player changed, please press start"; DialogHost.IsOpen = true; } } } else { //warning too restart the game txtBlNameFound.Text = "Press Restart and then press start game"; DialogHost.IsOpen = true; } }