0

Below I have an array that works but it is far too long and I am thinking there is a far easier way to generate the same result using a loop but I just can't get my head around it at the moment. The array is as follows:

var CardDeck = new Array();
CardDeck[0] = new C1();
CardDeck[1] = new C2();
CardDeck[2] = new C3();
CardDeck[3] = new C4();
CardDeck[4] = new C5();
CardDeck[5] = new C6();
CardDeck[6] = new C7();
CardDeck[7] = new C8();
CardDeck[8] = new C9();
CardDeck[9] = new C10();
CardDeck[10] = new C11();
CardDeck[11] = new C12();
CardDeck[12] = new C13();
CardDeck[13] = new D1();
CardDeck[14] = new D2();
CardDeck[15] = new D3();
CardDeck[16] = new D4();
CardDeck[17] = new D5();
CardDeck[18] = new D6();
CardDeck[19] = new D7();
CardDeck[20] = new D8();
CardDeck[21] = new D9();
CardDeck[22] = new D10();
CardDeck[23] = new D11();
CardDeck[24] = new D12();
CardDeck[25] = new D13();
CardDeck[26] = new H1();
CardDeck[27] = new H2();
CardDeck[28] = new H3();
CardDeck[29] = new H4();
CardDeck[30] = new H5();
CardDeck[31] = new H6();
CardDeck[32] = new H7();
CardDeck[33] = new H8();
CardDeck[34] = new H9();
CardDeck[35] = new H10();
CardDeck[36] = new H11();
CardDeck[37] = new H12();
CardDeck[38] = new H13();
CardDeck[39] = new S1();
CardDeck[40] = new S2();
CardDeck[41] = new S3();
CardDeck[42] = new S4();
CardDeck[43] = new S5();
CardDeck[44] = new S6();
CardDeck[45] = new S7();
CardDeck[46] = new S8();
CardDeck[47] = new S9();
CardDeck[48] = new S10();
CardDeck[49] = new S11();
CardDeck[50] = new S12();
CardDeck[51] = new S13();

Can someone tell me how to put that together into a loop that will generate the same results?

Thanks Ben

3
  • You created a brand new class for each individual card? Doesn't that seem a bit extensive? Perhaps you could create a single card class and allow it to have properties that elicited values relevant to a card deck (which would solve the problem you're having, incidentally). Commented Dec 15, 2009 at 4:24
  • Er...excessive, not extensive. Commented Dec 15, 2009 at 4:25
  • Thanks for your response Michael, how would you go about doing that? Can you give me some sample code. This is my first real attempt at AS3 and as you can probably already tell I have no idea what I am doing. Commented Dec 15, 2009 at 4:33

2 Answers 2

3

You could use getDefinitionByName to get references to the numbered card classes, something like this:

import flash.utils.getDefinitionByName;

var deck:Array = [];
var suits:Array = ['C', 'D', 'H', 'S'];
var card:Class;

for each(var suit:String in suits) {
    for(var i:int = 1; i <= 13; i++) {
        card = getDefinitionByName(suit + i) as Class;
        deck.push(new card());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

This code may not work as is (I'm a little rusty with AS3), but with some tweaking it should steer you in the right direction:

public class Card {
    var suit:String;
    var value:int;

    public Card(Suit:String, Value:int)
    {
        this.suit = Suit;
        this.value = Value;
    }
} 

public class Deck {
    var deck:Array = new Array();
    var suits:Array = new Array('C', 'D', 'H', 'S');

    for (var x = 0; x < 52; x++) {
        deck[x] = new Card(suits[x / 13], x % 13);
    }
}

Now I'm not positive about the Card declaration (I don't have a copy of Flash handy to check my syntax via compiling), but what the "deck[x] = new Card..." line is supposed to do is set the first 13 card suits to C, the second 13 to D, etc., and to set the values from 0 to 13 (meaning Ace=0, 2=1, 3=2, ..., K=12).

Hope that makes sense (and I hope you don't have to tweak it too much to get it to work for you).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.