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;
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!
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.
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?
Kel