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 !
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?
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.
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.
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
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
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]));
}
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.
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.
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.