2-D Array of cards

Pages: 12
Hi guys ,

i am new to c++ and have been reading two books for around 30hrs+ of study so far! I am actually really enjoying it .... which is good! I am looking for a little help with a small project i set myself ! It had been suggested as a very good program to start with before i get into harder things ! My question is as follows:
All i want to do is declared and initialize a 2-D array ( which will represent a 52 card deck of playing cards ) , i just want to know can i declared an array in a string data type because if i declared the array as char or int i cannot accurately state the cards value and suit ! ( does that make any sense - because char only allows a single character as in A, S, D, and will not a allow the value for example 3D,4S, ) whereas a string array would ? Any suggestions, all i plan on doing initially is declaring the array / initializing it and then outputting specific elements of that array !

Have you come across structs or classes yet?

If so, you could make a card class and give it two member variables - value and suit.

Yeah, you can declare an array of strings no problem if you prefer. Is there a specific reason you need your array to be 2D? Why not just a one dimensional array of 52?
I just figured it would be tidier ? like this :

string cards [4][13] = {{AD,KD,QD ..............

but yeh i think i may revise and got a one dimensional array .

hmmm .....
Just because something "makes sense" in 2D doesn't mean it "makes sense" to implement it that way!

You're idea is probably to give each "colour" a number (e.g. Spades = 0, Hearts = 1, Clubs = 2, Diamonds = 3) and then have card[type][number] look up the card. You can do the exact same with a 1D array: card[type*13+number].

For example:
King of Diamonds -> card[3][12] -> card[51], being the last card.
It's entirely up to you. That's the beauty of it: there's no one right answer.

I, personally, would go for an array of structs/classes, like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct card
{
   int value;
   char suit;
};

int main()
{
   card deck[52];  // An array of cards named deck of size 52.

   // other code

   return 0;
}


Initialising them would be a case of looping through and setting the values. You could make an initialise function. With a bit of thought, you could make a decent shuffle function too.

One thing I would say: probably not best to represent your picture cards as K,Q,J. If you think to comparisons, it's going to get ugly when you want to compare, say, a 7 with a king (how would you do if ( 7 > 'K')?). I would keep them numeric, so 11 = J, 12 = Q, 13 = K. It's going to make comparing them a whole lot easier. If you want the output to display K,Q and J, that's a lot easier to manipulate than working around comparisons.
i think the 2D array is not a bad idea.

You can use chars if you want:
now you only have to use a single char. because the array already definded the card type

1
2
3
4
5
6
7
8
9
10
#define Spades      0
#define Hearts       1
#define Diamonds  2
#define Clubs         3

char cards [4][13] = { { 2, 3, 4,  ... , K, A },
                       { 2, 3, 4,  ... , K, A },
                       { 2, 3, 4,  ... , K, A },
                       { 2, 3, 4,  ... , K, A } };


Now you can , for example , acces the 5 of Diamonds:

char c = cards [Diamonds][3];
Ugh, defines. What's the point of still having those four arrays? cards[i][5] is going to hold the same value regardless of i.

iHutch105's suggestion is actually the best, because it keeps track of actual Cards. You can shuffle his deck and still have information on the deck itself.
Agree. 2D array is a very simple way to do it. Makes sense. Reverse the array so card number is first.

1
2
3
4
5
6
7
char Card;

             Card = 5;
             Card++;
 
             char c = cards [Card][Diamonds];
Return 5 or Diamonds.
Last edited on
Having a one-dimensional array simplifies looping and comparison.

2D arrays will require double loops everywhere when you need to compare cards.
because it keeps track of actual Cards. You can shuffle his deck and still have information on the deck itself.


very true! convinced me ;)
Thanks for the replies ill post later tonight with what i come up with ! not sure which way im headed yet
Make it a function call where the numbers of card in the 2D array are first.

char GetCard(char a, char, b)
{
// Error check
if (a-- < 0)
{
a = 0;
}

c = cards[a][b];
return c;
}

d = GetCard(5, Diamonds);

Return 5 of Diamonds.

Simple.

Last edited on
You'll need to add a bit of error-checking, but that's just as simple with one dimension.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
card GetCard (int val, char suit)
{
   for (int i=0;i<52;i++)
   {
      if (val  == deck[i].value &&
          suit == deck[i].suit)
      {
          return deck[i];
      }
   } 
}


// Call
card desired_card = GetCard(5, 'D');
Last edited on
Below is what i have so far and i think it is looking ok ? I now need to define my four arrays and then have that number/suit appear before the actual number stored within the element im calling for! any help. ps it does not output at the moment needs tweaking

#include <iostream>
#include <string>
#include <cstdlib>//for srand and rand
#include <ctime>//for time

using namespace std;

// try and randomize an declared array of cards


int main ()
{
char cards [4][13] = {

{ '0','1','2','3','4','5','6','7','8','9','10','11','12',},
{ '0','1','2','3','4','5','6','7','8','9','10','11','12',},
{ '0','1','2','3','4','5','6','7','8','9','10','11','12',},
{ '0','1','2','3','4','5','6','7','8','9','10','11','12',},



};



cout << "The value of element [0,1] :" << cards[0][1]<<endl;

system ( "pause");



return 0;
}
Last edited on
Ok i think my next step is to define variables that are called globally before the array definition and initialization, they will then be passed as the output represented for that specific element of the array when it is called :

for example i ask for the [2][10] element to be output to the screen

cout<< " This is the value stored in elemt [2][10] element << cards[2][10] <<endl;
the part in bold i want to be a suit and face number ??? does that make sense , like a simple exchange of the element means this ....
I think it's a taste how you want to do it. Making a 1D array takes up 52 elements. Making a 2D array rakes up 52 element. For my point of view it make sense to use 2D array because you are asking with 2 constants like like Diamond(1) of King(12). The 2D array will return the value for that location without any loops. A = Cards[Diamond][King]. If the cards are defined as 1 to 9 and Jack, Queen, King and Ace and the same is done with Diamond... and so on no error check is needed.
Per
Hi guys and gals ,

Pleased as punch with my project so far ! It is pretty weak and rife with errors but for what its worth it is pretty good for ( Semi BRAG ) , all i wanted was to simulate visually the dealer showing a deck of 52 cards and then show them suffled face up on the table ( if you can visualise that - start of a 1 deck blackjack game !

I have don that part plus i am on the way to getting the menu down for the game !

Heres the code anyways : ( pretty much all my own except for one small section which i understand and to be honest felt no need in messing with )

#include <iostream>
#include <time.h>



using namespace std;

int card [52];// array of cards with fiftytwo elemets
int i,j,temp;// global variables

void main(void)

{
srand(time(NULL));// allows random number generation

for ( i=0;i<52;i++) // for loop which sets a variable , conditions, and increments
{

card[i] = i+1; // tell the array what element to start at in card and then assign that + 1

}
for (i=0;i<52;i++)

switch (card[i])
{
case 1 : printf(" Ad"); // this iteration will show the complete deck to the table
break;
case 2 : printf(" 2d");
break;
case 3 : printf(" 3d");
break;
case 4 : printf(" 4d");
break;
case 5 : printf(" 5d");
break;
case 6 : printf(" 6d");
break;
case 7 : printf(" 7d");
break;
case 8 : printf(" 8d");
break;
case 9 : printf(" 9d");
break;
case 10 : printf(" 10d");
break;
case 11 : printf(" Jd");
break;
case 12 : printf(" Qd");
break;
case 13 : printf(" Kd");
break;
case 14 : printf(" As");
break;
case 15 : printf(" 2s");
break;
case 16 : printf(" 3s");
break;
case 17 : printf(" 4s");
break;
case 18 : printf(" 5s");
break;
case 19 : printf(" 6s");
break;
case 20 : printf(" 7s");
break;
case 21 : printf(" 8s");
break;
case 22 : printf(" 9s");
break;
case 23 : printf(" 10s");
break;
case 24 : printf(" Js");
break;
case 25 : printf(" Qs");
break;
case 26 : printf(" Ks");
break;
case 27 : printf(" Ah");
break;
case 28 : printf(" 2h");
break;
case 29 : printf(" 3h");
break;
case 30 : printf(" 4h");
break;
case 31 : printf(" 5h");
break;
case 32 : printf(" 6h");
break;
case 33 : printf(" 7h");
break;
case 34 : printf(" 8h");
break;
case 35 : printf(" 9h");
break;
case 36 : printf(" 10h");
break;
case 37 : printf(" Jh");
break;
case 38 : printf(" Qh");
break;
case 39 : printf(" Kh");
break;
case 40 : printf(" Ac");
break;
case 41 : printf(" 2c");
break;
case 42 : printf(" 3c");
break;
case 43 : printf(" 4c");
break;
case 44 : printf(" 5c");
break;
case 45 : printf(" 6c");
break;
case 46 : printf(" 7c");
break;
case 47 : printf(" 8c");
break;
case 48 : printf(" 9c");
break;
case 49 : printf(" 10c");
break;
case 50 : printf(" Jc");
break;
case 51 : printf(" Qc");
break;
case 52 : printf(" Kc");
break;
default : printf(" %d",(card[i]));
}


for (i=0;i<13;i++)
{
j = (rand()%12) +1;
temp = card[i];
card [i] = card[j];
card[j] = temp;
}
printf( " \n\n___________________________________________\n\n");
for (i=0;i<52;i++)

switch (card[i])
{
case 1 : printf(" Ad");
break;
case 2 : printf(" 2d");
break;
case 3 : printf(" 3d");
break;
case 4 : printf(" 4d");
break;
case 5 : printf(" 5d");
break;
case 6 : printf(" 6d");
break;
case 7 : printf(" 7d");
break;
case 8 : printf(" 8d");
break;
case 9 : printf(" 9d");
break;
case 10 : printf(" 10d");
break;
case 11 : printf(" Jd");
break;
case 12 : printf(" Qd");
break;
case 13 : printf(" Kd");
break;
case 14 : printf(" As");
break;
case 15 : printf(" 2s");
break;
case 16 : printf(" 3s");
break;
case 17 : printf(" 4s");
break;
case 18 : printf(" 5s");
break;
case 19 : printf(" 6s");
break;
case 20 : printf(" 7s");
break;
case 21 : printf(" 8s");
break;
case 22 : printf(" 9s");
break;
case 23 : printf(" 10s");
break;
case 24 : printf(" Js");
break;
case 25 : printf(" Qs");
break;
case 26 : printf(" Ks");
break;
case 27 : printf(" Ah");
break;
case 28 : printf(" 2h");
break;
case 29 : printf(" 3h");
break;
case 30 : printf(" 4h");
break;
case 31 : printf(" 5h");
break;
case 32 : printf(" 6h");
break;
case 33 : printf(" 7h");
break;
case 34 : printf(" 8h");
break;
case 35 : printf(" 9h");
break;
case 36 : printf(" 10h");
break;
case 37 : printf(" Jh");
break;
case 38 : printf(" Qh");
break;
case 39 : printf(" Kh");
break;
case 40 : printf(" Ac");
break;
case 41 : printf(" 2c");
break;
case 42 : printf(" 3c");
break;
case 43 : printf(" 4c");
break;
case 44 : printf(" 5c");
break;
case 45 : printf(" 6c");
break;
case 46 : printf(" 7c");
break;
case 47 : printf(" 8c");
break;
case 48 : printf(" 9c");
break;
case 49 : printf(" 10c");
break;
case 50 : printf(" Jc");
break;
case 51 : printf(" Qc");
break;
case 52 : printf(" Kc");
break;
default : printf(" %d",(card[i]));
}
cin.get();

}

I will know be focusing on the game play section which im sure will be very difficult .....
Just to get this right. You wrote:

Pleased as punch with my project so far ! It is pretty weak and rife with errors but for what its worth it is pretty good for ( Semi BRAG ) , all i wanted was to simulate visually the dealer showing a deck of 52 cards and then show them suffled face up on the table ( if you can visualise that - start of a 1 deck blackjack game !

Is what you want to do is to pick a random card out of a deck and then print it out until you have picked all cards?

That is going to take a little time with 52 cards because srand(time(NULL) and Ptr = (rand() % 52) (random pointer to deck) need to get redone every time you get a card you already picked out.

This can be done in a for loop until all cards are picked.

You got my brain going on that one. I will try to make a program that do that and see how long time it takes.
Last edited on


If so, you could make a card class and give it two member variables - value and suit.



I would also add a numerical value variable, since J-K count at 10 in Black Jack, where as the Ace can count as 1 or 11, I believe. Been a long time since I played Black Jack.
@englishkid19

The way you have your shuffling, only the first 13 cards are. To shuffle all 52, use
1
2
3
4
5
6
7
for (i=0;i<52;i++)
	{
		j = (rand()%52)+1;
		temp = card[i];
		card [i] = card[j];
		card[j] = temp;
	}
or add another loop to give a thorough shuffle
1
2
3
4
5
6
7
8
9
10
for(int x=0;x<6;x++)
{
  for (i=0;i<52;i++)
  {
	j = (rand()%52)+1;
	temp = card[i];
	card [i] = card[j];
	card[j] = temp;
  }
}


@Perman

After the cards are shuffled, they would just be dealt one at a time until all 52 cards are used, so the srand would not be needed for that part of the game. Only need to keep track of each players hand.
Last edited on
Pages: 12