Shop More Submit  Join Login

Details

Closed to new replies
November 23, 2012
Link

Statistics

Replies: 6

C++ Card Deck Help

:iconroxxiwarhol:
RoxxiWarhol Featured By Owner Nov 23, 2012  Hobbyist General Artist
Hi there,

For my final project in c++ we have to basically write a program for the card game Lives. At the moment I'm having trouble just getting a card deck going, I wonder if anyone can help me? So far I have programmed this:

int main() {

int card[13];
int i,j,temp = 1;

srand(time (NULL));

for (i = 0; i < 13; i++){
card[i] = i + 1;
}

for (i = 0; i < 13; i++) {
j = (rand()%12) + 1;
card[i] = card[j];
card[j] = temp;

if(card[i] = 1)
cout << " A";
if(card[i] = 11)
cout << " J";
if(card[i] = 12)
cout << " Q";
if(card[i] = 13)
cout << " K";
else
cout << " " << card[i];
}

return 0;


The program is a quick exercise to generate 13 random cards in random order and to make sure the 1, 11, 12 ,13 come out as ace, jack, queen, king. When I run the program though it gives me a load of random cards (not 13) and all of them are jack kings queens etc.

Can anyone help me with this quick exercise so I can get onto writing the actual game? :) Would be greatly appreciated!

Kel
Reply

You can no longer comment on this thread as it was closed due to no activity for a month.

Devious Comments

:icondj0hybrid:
DJ0Hybrid Featured By Owner Nov 23, 2012  Hobbyist Writer
Inside the if statements in which you are trying to convert the numbers to the appropriate cards, you are actually assigning them.

So your code like this:
if(card[i] = 1)
cout << " A";

Should actually be this:
if(card[i] == 1) // We are comparing card[i] with 1 now, versus making card[i] hold the value 1.
cout << " A";
Reply
:iconroxxiwarhol:
RoxxiWarhol Featured By Owner Nov 26, 2012  Hobbyist General Artist
ooooh ok, ah yes i see it now. :) brilliant thanks a mil!! :D
Reply
:icontherealpadster:
theRealPadster Featured By Owner Nov 23, 2012  Hobbyist Interface Designer
I'd say use a struct to make a card variable type, so you can have a suit and a value (1-13).
Then make a deck array [52] and a second deck for shuffling purposes.
Then use a for loop to assign values to each of the cards.
Then shuffle the deck by randomly picking a card in the deck, and putting it in the second deck, and erasing it from the first deck. You can use a for loop for this as well.
Reply
:iconroxxiwarhol:
RoxxiWarhol Featured By Owner Nov 23, 2012  Hobbyist General Artist
oh thats a good method!! thanks ill give it a try think itll be a bit cleaner! :)
Reply
:icontherealpadster:
theRealPadster Featured By Owner Nov 23, 2012  Hobbyist Interface Designer
No problem :)
Reply
:iconroxxiwarhol:
RoxxiWarhol Featured By Owner Nov 23, 2012  Hobbyist General Artist
hahaha nevermind I think i got it! :) now just gotta see if I can stop it generating one card more than 4 times :D
Reply
Add a Comment: