Passing Arraylist
I am modifying a Blackjack game program I got from from one of my Deitel C# books. I want to use objects for the cards. There is a Windows forms program and a Web service program. The web service creates the deck of cards in one method. Then a second method returns a card object one at a time for the player and the dealer. A third method recevies a arraylist of card objects to tabulate totals from the Windows form program. The problem arises in the Windows form program. I am trying to pass a arraylist of card objects to the service. The windows form program won't compile because it says it can't convert an arraylist to an object array even though my method definition in the service declares the parameter as an arraylist. Any help would be appreciated. Stumped in Chicago.
Bruce ZelenkaPosted Sep 5, 2007, 6:17 AM
In addition to the ArrayLists used to insert objects for the dealer and player I defined a standard fixed object array. When I get ready to call the service to compute a total intstead
of trying to pass an ArrayList I convert the ArrayList. For example object[] dealtCards = dealersCards.ToArray(); int dealersTotal = dealer.GetHandValue( dealtCards ); So I pass the object array to the service. I get the total back OK. Now the programs work and they work correctly.
Bruce ZelenkaPosted Sep 4, 2007, 8:06 PM
Rohit SinghPosted Sep 4, 2007, 4:39 AM
You can't pass an arraylist back and forth a web service, it's not serializable.
I hope this makes things clear.
Bruce ZelenkaPosted Sep 4, 2007, 2:50 AM
// Exercise 22.5
// Blackjack game that uses the OOPBlackjack 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{
// object allowing communication with Web service private localhost.OOPBlackjackService dealer = new localhost.OOPBlackjackService(); // reference to Card class private localhost.Card card; // ArrayList of the dealer's cards private ArrayList dealersCards; // ArrayList of the player's cards private ArrayList 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 tieLOSE,
// player losesWIN,
// player winsBLACKJACK
// player has blackjack}
// end enum GameStatus public BlackjackForm(){
InitializeComponent();
}
// end constructor // sets up the game private void BlackjackForm_Load( object sender, EventArgs e ){
// allow session statedealer.CookieContainer =
new CookieContainer();cardBoxes =
new ArrayList();dealersCards =
new ArrayList();playersCards =
new ArrayList(); // put PictureBoxes into cardBoxescardBoxes.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 ){
card = dealer.DealCard();
// deal new carddealersCards.Add( card );
// add new card to dealers hand // update GUI to show new cardDisplayCard( currentDealerCard, card,
false );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 ); elseGameOver(
GameStatus.PUSH );}
// end method DealerPlay // displays card represented by card object in specified PictureBox public void DisplayCard( int card, localhost.Card cardObject, bool displaySwitch ){
// retrieve appropriate PictureBox from ArrayList PictureBox displayBox = ( PictureBox )( cardBoxes[ card ] ); // if displaySwitch equals false, // set displayBox to display back of card if ( !displaySwitch ){
displayBox.Image =
Image.FromFile( "blackjack_images/cardback.png" ); return;}
// end if // retrieve face value of card from card object int face = cardObject.Face; // retrieve the suit of the card from card object int suit = cardObject.Suit; // string representation of face used to form image file name string strFace = Convert.ToString( face ); char suitLetter; // suit letter used to form image file name // determine the suit letter of the card switch ( suit ){
case 0: // clubssuitLetter =
'c'; break; case 1: // diamondssuitLetter =
'd'; break; case 2: // heartssuitLetter =
'h'; break; default: // spadessuitLetter =
's'; break;}
// end switch // set displayBox to display appropriate imagedisplayBox.Image =
Image.FromFile( "blackjack_images/" + strFace + suitLetter + ".png" );}
// end method DisplayCard // displays all player cards and shows // appropriate game status message public void GameOver( GameStatus winner ){
int i = 0; // display all the dealer's cards foreach ( object tempCard in dealersCards ){
localhost.
Card card = tempCard as localhost.Card;DisplayCard( i, card,
true);i++;
}
// display appropriate status image if ( winner == GameStatus.PUSH ) // pushstatusPictureBox.Image =
Image.FromFile( "blackjack_images/tie.png" ); else if ( winner == GameStatus.LOSE ) // player losesstatusPictureBox.Image =
Image.FromFile( "blackjack_images/lose.png" ); else if ( winner == GameStatus.BLACKJACK ) // player has blackjackstatusPictureBox.Image =
Image.FromFile( "blackjack_images/blackjack.png" ); else // player winsstatusPictureBox.Image =
Image.FromFile( "blackjack_images/win.png" ); // display final totals for dealer and playerdealerTotalLabel.Text =
"Dealer: " + dealer.GetHandValue( dealersCards );playerTotalLabel.Text =
"Player: " + dealer.GetHandValue( playersCards ); // reset controls for new gamestayButton.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 ){
// clear card arraysdealersCards.Clear();
playersCards.Clear();;
// clear card images foreach ( PictureBox cardImage in cardBoxes )cardImage.Image =
null;statusPictureBox.Image =
null; // clear status imagedealerTotalLabel.Text =
""; // clear final total for dealerplayerTotalLabel.Text =
""; // clear final total for player // create a new, shuffled deck on the remote machinedealer.Shuffle();
// deal two cards to playercard = dealer.DealCard();
// deal a card to player's handplayersCards.Add( card );
// add first card to player's hand // update GUI to display new cardDisplayCard( 11, card,
true );card = dealer.DealCard();
// deal a second cardDisplayCard( 12, card,
true ); // update GUI to display new cardplayersCards.Add( card );
// add second card to player's hand // deal two cards to dealer, only display face of first cardcard = dealer.DealCard();
// deal a card to dealer's handdealersCards.Add( card );
// add first card to dealer's handDisplayCard( 0, card,
true ); // update GUI to display new cardcard = dealer.DealCard();
// deal a second carddealersCards.Add( card );
// add second card to dealer's handDisplayCard( 1, card,
false ); // update GUI to show face-down cardstayButton.Enabled =
true; // allow player to stayhitButton.Enabled =
true; // allow player to hitdealButton.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 winsGameOver(
GameStatus.LOSE ); else if ( playersTotal == 21 ) // player has blackjackGameOver(
GameStatus.BLACKJACK ); // next dealer card has index 2 in cardBoxescurrentDealerCard = 2;
// next player card has index 13 in cardBoxescurrentPlayerCard = 13;
}
// end method dealButton // deal another card to player private void hitButton_Click( object sender, EventArgs e ){
// get player another cardcard = dealer.DealCard();
// deal new cardplayersCards.Add( card );
// add new card to player's hand // update GUI to show new cardDisplayCard( currentPlayerCard, card,
true );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 ButtonhitButton.Enabled =
false; // display Hit ButtondealButton.Enabled =
true; // re-enable Deal ButtonDealerPlay();
// player chose to stay, so play the dealer's hand}
// end method stayButton_Click}
// end class BlackjackForm}
// end namespace BlackjackScott LyslePosted Sep 3, 2007, 11:31 PM
Bruce:
May we see the code showing how you are handling it in the client. An arraylist is not type safe and everything in it is cast to System.Object; how are you handing that?