Recipe 4.10. Simulating Playing Cards


Problem

You want to use ActionScript to deal cards for a card game using a standard 52-card deck (without Jokers).

Solution

Use the custom Cards class.

Discussion

Playing cards requires a greater degree of sophistication than, say, rolling a couple dice. Therefore, to work with playing cards within your Flash applications, you should use a custom Cards class.

The Cards class is in the ascb.play package, and therefore you should be sure to import the class before you try to use it in your code.

import ascb.play.Cards; 


You can create a new Cards object using the constructor as follows:

var cards:Cards = new Cards(  );

By default, a Cards object creates a standard deck of 52 playing cards. Next you need to deal the cards by using the deal( ) method. The deal( ) method returns an array of CardHand objects. You should specify at least one parameter when calling the deal( ) method; the number of hands to deal.

// Deal four hands. var hands:Array = cards.deal(4);

By default, the deal( ) method deals every card in the deck (except when 52 is not evenly divisible by the number of hands). Some card games, such as Euchre, require fewer cards in each hand with some cards remaining in the deck. You can, therefore, specify a second, optional parameter that determines the number of cards per hand:

// Deal four hands with five cards each. var hands:Array = cards.deal(4, 5);

Each CardHand object is an array of Card objects. The CardHand class also provides an interface to draw and discard cards from the deck from which the hand was originally dealt. You can use the discard( ) method by specifying a list of card indices as parameters. The cards then are removed from the hand and added back to the bottom of the deck:

// Discard the cards with indices 0 and 4 from the CardHand object  // stored in the first element of the aHands array. hands[0].discard(0, 4);

Conversely, you can use the draw( ) method to draw cards from the top of the original deck. The draw( ) method draws one card by default if no parameters are specified. If you want to draw more than one card at a time, you can specify the number of cards as a parameter:

// Draw one card from the top of the deck, and add it to the  // hand stored in the first element of the hands array. hands[0].draw(  ); // Draw four cards from the top of the deck, and add them to  // the hand stored in the fourth element of the aHands array. hands[3].draw(4);

You can use the length property of a CardHand object to retrieve the number of cards in a hand, and you can use the getCardAt( ) method to retrieve a card at a specified index.

As mentioned, each CardHand is composed of Card objects. Card objects, in turn, have four properties: value, name, suit, and display. The value property returns a numeric value from 0 to 12, where 0 is a two card and 12 is an Ace. The name property returns the name of the card, such as 2, 10, Q, or A. The suit property returns clubs, diamonds, hearts, or spades. The display property returns the name and suit values joined with a space. The following example code illustrates use of the Cards class:

package {   import flash.display.Sprite;   import ascb.play.Cards;   import flash.util.trace;      public class CardExample extends Sprite {     public function CardExample(  ) {       var cards:Cards = new Cards(  );       var hands:Array = cards.deal(4, 10);       var i:uint;       var j:uint;       for(i = 0; i < hands.length; i++) {         trace("hand " + i);         for(j = 0; j < hands[i].length; j++) {           trace(hands[i].getCardAt(j));         }       }     }      } }                                             




ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net