Managing Cards

- Alyce Alyce Jul 11, 2011
Managing Cards | Arrays! | Single or Double? | Creating the Array | Filling the Array | Retrieving the Info

Arrays!


Whether you are creating a card game with images from a DLL, with bitmap images, or a text game, you'll need a way to keep track of the status of the cards. Arrays provide a perfect way to do this.

Single or Double?


Consider using a double-dimensioned array, since it is the most flexible. The first dimension holds the index of the card and the second dimension holds information such as it's x location, y location, face-up/face-down status, disable/enabled status, in play/removed from play status, suit, color, value, etc.

Creating the Array


An array with two dimensions must be DIMmed. If you are using a 52-card deck, the first dimension will be 52. The second dimension size depends upon the game and its pertinent information. Let's create an example.

 
dim deck$(52,6)
'deck$(x,1) : rank, A,2,3...J,Q,K
'deck$(x,2) : suit, hearts, spades, diamonds, clubs
'deck$(x,3) : side - face UP or face DOWN
'deck$(x,4) : in play Y or N
'deck$(x,5) : x location of upper left corner
'deck$(x,6) : y location of upper left corner

Filling the Array

We can use loops or nested loops to fill the array at the start of the game. Tge following code creates a string for the rank values. It then fills the array in a loop, using the word$() function on the string. It ends with a print routine so that we can check our work.

dim deck$(52,6)
value$ = "A 2 3 4 5 6 7 8 9 10 J Q K "
 
for m = 1 to 4
    rank$=rank$ + value$
next
 
for i = 1 to 52
    deck$(i,1) = word$(rank$,i)
next
 
'check to see how it is filled:
for j = 1 to 52
    print deck$(j,1)
next

You'd fill your arrays according to the specifications of the game under construction.

Retrieving the Info


You can retrieve the info in the same way it was printed in the example above.

idx=32 'array index to query
cardRank$ = deck$(idx,1)


Managing Cards | Arrays! | Single or Double? | Creating the Array | Filling the Array | Retrieving the Info