// Fig. 22.19: Blackjack.cs // Blackjack game that uses the Blackjack Web service. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net; using System.Collections; namespace Blackjack { public partial class BlackjackForm : Form { // reference to Web service private localhost.BlackjackService dealer; // string representing the dealer's cards private string dealersCards; // string representing the player's cards private string playersCards; private ArrayList cardBoxes; // list of PictureBoxes for card images private int currentPlayerCard; // player's current card number private int currentDealerCard; // dealer's current card number // enum representing the possible game outcomes public enum GameStatus { PUSH, // game ends in a tie LOSE, // player loses WIN, // player wins BLACKJACK // player has blackjack } // end enum GameStatus public BlackjackForm() { InitializeComponent(); } // end constructor // sets up the game private void BlackjackForm_Load( object sender, EventArgs e ) { // instantiate object allowing communication with Web service dealer = new localhost.BlackjackService(); // allow session state dealer.CookieContainer = new CookieContainer(); cardBoxes = new ArrayList(); // put PictureBoxes into cardBoxes cardBoxes.Add( pictureBox1 ); cardBoxes.Add( pictureBox2 ); cardBoxes.Add( pictureBox3 ); cardBoxes.Add( pictureBox4 ); cardBoxes.Add( pictureBox5 ); cardBoxes.Add( pictureBox6 ); cardBoxes.Add( pictureBox7 ); cardBoxes.Add( pictureBox8 ); cardBoxes.Add( pictureBox9 ); cardBoxes.Add( pictureBox10 ); cardBoxes.Add( pictureBox11 ); cardBoxes.Add( pictureBox12 ); cardBoxes.Add( pictureBox13 ); cardBoxes.Add( pictureBox14 ); cardBoxes.Add( pictureBox15 ); cardBoxes.Add( pictureBox16 ); cardBoxes.Add( pictureBox17 ); cardBoxes.Add( pictureBox18 ); cardBoxes.Add( pictureBox19 ); cardBoxes.Add( pictureBox20 ); cardBoxes.Add( pictureBox21 ); cardBoxes.Add( pictureBox22 ); } // end method BlackjackForm_Load // deals cards to dealer while dealer's total is less than 17, // then computes value of each hand and determines winner private void DealerPlay() { // while value of dealer's hand is below 17, // dealer must take cards while ( dealer.GetHandValue( dealersCards ) < 17 ) { dealersCards += '\t' + dealer.DealCard(); // deal new card // update GUI to show new card DisplayCard( currentDealerCard, "" ); currentDealerCard++; MessageBox.Show( "Dealer takes a card" ); } // end while int dealersTotal = dealer.GetHandValue( dealersCards ); int playersTotal = dealer.GetHandValue( playersCards ); // if dealer busted, player wins if ( dealersTotal > 21 ) { GameOver( GameStatus.WIN ); return; } // end if // if dealer and player have not exceeded 21, // higher score wins; equal scores is a push. if ( dealersTotal > playersTotal ) GameOver( GameStatus.LOSE ); else if ( playersTotal > dealersTotal ) GameOver( GameStatus.WIN ); else GameOver( GameStatus.PUSH ); } // end method DealerPlay // displays card represented by cardValue in specified PictureBox public void DisplayCard( int card, string cardValue ) { // retrieve appropriate PictureBox from ArrayList PictureBox displayBox = ( PictureBox )( cardBoxes[ card ] ); // if string representing card is empty, // set displayBox to display back of card if ( cardValue == "" ) { displayBox.Image = Image.FromFile( "blackjack_images/cardback.png" ); return; } // end if // retrieve face value of card from cardValue string face = cardValue.Substring( 0, cardValue.IndexOf( " " ) ); // retrieve the suit of the card from cardValue string suit = cardValue.Substring( cardValue.IndexOf( " " ) + 1 ); char suitLetter; // suit letter used to form image file name // determine the suit letter of the card switch ( Convert.ToInt32( suit ) ) { case 0: // clubs suitLetter = 'c'; break; case 1: // diamonds suitLetter = 'd'; break; case 2: // hearts suitLetter = 'h'; break; default: // spades suitLetter = 's'; break; } // end switch // set displayBox to display appropriate image displayBox.Image = Image.FromFile( "blackjack_images/" + face + suitLetter + ".png" ); } // end method DisplayCard // displays all player cards and shows // appropriate game status message public void GameOver( GameStatus winner ) { char[] tab = { '\t' }; string[] cards = dealersCards.Split( tab ); // display all the dealer's cards for ( int i = 0; i < cards.Length; i++ ) DisplayCard( i, cards[ i ] ); // display appropriate status image if ( winner == GameStatus.PUSH ) // push statusPictureBox.Image = Image.FromFile( "blackjack_images/tie.png" ); else if ( winner == GameStatus.LOSE ) // player loses statusPictureBox.Image = Image.FromFile( "blackjack_images/lose.png" ); else if ( winner == GameStatus.BLACKJACK ) // player has blackjack statusPictureBox.Image = Image.FromFile( "blackjack_images/blackjack.png" ); else // player wins statusPictureBox.Image = Image.FromFile( "blackjack_images/win.png" ); // display final totals for dealer and player dealerTotalLabel.Text = "Dealer: " + dealer.GetHandValue( dealersCards ); playerTotalLabel.Text = "Player: " + dealer.GetHandValue( playersCards ); // reset controls for new game stayButton.Enabled = false; hitButton.Enabled = false; dealButton.Enabled = true; } // end method GameOver // deal two cards each to dealer and player private void dealButton_Click( object sender, EventArgs e ) { string card; // stores a card temporarily until added to a hand // clear card images foreach ( PictureBox cardImage in cardBoxes ) cardImage.Image = null; statusPictureBox.Image = null; // clear status image dealerTotalLabel.Text = ""; // clear final total for dealer playerTotalLabel.Text = ""; // clear final total for player // create a new, shuffled deck on the remote machine dealer.Shuffle(); // deal two cards to player playersCards = dealer.DealCard(); // deal a card to player's hand // update GUI to display new card DisplayCard( 11, playersCards ); card = dealer.DealCard(); // deal a second card DisplayCard( 12, card ); // update GUI to display new card playersCards += '\t' + card; // add second card to player's hand // deal two cards to dealer, only display face of first card dealersCards = dealer.DealCard(); // deal a card to dealer's hand DisplayCard( 0, dealersCards ); // update GUI to display new card card = dealer.DealCard(); // deal a second card DisplayCard( 1, "" ); // update GUI to show face-down card dealersCards += '\t' + card; // add second card to dealer's hand stayButton.Enabled = true; // allow player to stay hitButton.Enabled = true; // allow player to hit dealButton.Enabled = false; // disable Deal Button // determine the value of the two hands int dealersTotal = dealer.GetHandValue( dealersCards ); int playersTotal = dealer.GetHandValue( playersCards ); // if hands equal 21, it is a push if ( dealersTotal == playersTotal && dealersTotal == 21 ) GameOver( GameStatus.PUSH ); else if ( dealersTotal == 21 ) // if dealer has 21, dealer wins GameOver( GameStatus.LOSE ); else if ( playersTotal == 21 ) // player has blackjack GameOver( GameStatus.BLACKJACK ); // next dealer card has index 2 in cardBoxes currentDealerCard = 2; // next player card has index 13 in cardBoxes currentPlayerCard = 13; } // end method dealButton // deal another card to player private void hitButton_Click( object sender, EventArgs e ) { // get player another card string card = dealer.DealCard(); // deal new card playersCards += '\t' + card; // add new card to player's hand // update GUI to show new card DisplayCard( currentPlayerCard, card ); currentPlayerCard++; // determine the value of the player's hand int total = dealer.GetHandValue( playersCards ); // if player exceeds 21, house wins if ( total > 21 ) GameOver( GameStatus.LOSE ); // if player has 21, // they cannot take more cards, and dealer plays if ( total == 21 ) { hitButton.Enabled = false; DealerPlay(); } // end if } // end method hitButton_Click // play the dealer's hand after the play chooses to stay private void stayButton_Click( object sender, EventArgs e ) { stayButton.Enabled = false; // disable Stay Button hitButton.Enabled = false; // display Hit Button dealButton.Enabled = true; // re-enable Deal Button DealerPlay(); // player chose to stay, so play the dealer's hand } // end method stayButton_Click } // end class BlackjackForm } // end namespace Blackjack /************************************************************************** * (C) Copyright 1992-2006 by Deitel & Associates, Inc. and * * Pearson Education, Inc. All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * **************************************************************************/