Please note I'm already using srand((unsigned) time(NULL));
to seed rand()
.
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#define NUM_SUITS 4
#define NUM_RANKS 13
bool in_hand[NUM_SUITS][NUM_RANKS] = {false};
bool newcard = {false};
int num_cards, rank, suit, totrank;
const char rank_code[] = {'A','2','3','4','5','6','7','8','9','T','J','Q','K',};
const char suit_code[] = {'C','D','H','S'};
int main_hand ()
{
suit = rand() % NUM_SUITS;
rank = rand() % NUM_RANKS;
if (!in_hand[suit][rank]) {
in_hand[suit][rank] = true;
num_cards--;
if (suit == 0){
printf("%c of Clubs \n", rank_code[rank]);
}
else if (suit == 1){
printf("%c of Diamonds \n", rank_code[rank]);
}
else if (suit == 2){
printf("%c of Hearts \n", rank_code[rank]);
}
else if (suit == 3){
printf("%c of Spades \n", rank_code[rank]);
}
}
}
int print_hand (suit)
{
}
int totrank_check (totrank)
{
if (totrank > 21) {
printf ("You lose!");
}
else if (totrank == 21) {
printf ("You win!");
}
}
int main()
{
bool stay = {false};
srand((unsigned) time(NULL));
totrank = 0;
num_cards = 2;
printf("Your hand: ");
while (num_cards > 0) {
main_hand();
totrank = totrank + (rank + 1);
}
printf("Total rank: %d\n", totrank);
totrank_check(totrank);
printf("\n");
while (totrank < 24 && stay == false) {
printf("Another card? 1. Yes 0. No\n");
scanf("%d", &newcard);
if(!newcard) {
main_hand();
}
totrank = totrank + (rank + 1);
totrank_check(totrank);
printf("Total rank: %d\n", totrank);
printf("Stay? 1. Yes 0. No\n");
scanf("%d", &stay);
}
return 0;
}
Basically it's a code that "simulates" a hand of blackjack.
It starts, rand()
chooses two numbers, the rank and the suit of the cards, that are set as true in a matrix so that they can't be chosen again in the same combination and then printed.
There's a check for the total rank of the cards (so that if it exceeds 21 you automatically lose) and then you are asked if you want another card or you want to stay.
Here's the error: if you choose that you want another cards, this new card will be the same as the last one. Basically, you get a Ace of Spades, the and Two of Diamonds, then you want another card and you get another Two of Diamonds. And the another, and another. If you remove the rank check in the second while you can see that the rank grows based on the rank of the last card.
Before, the printfs were in that print_hand() function, and you could see that you always got the same card, now I moved them in the main_hand() function because I tought that it might be the problem (it wasn't) and because having a separate function for the print was redundant. But you can see that technically, the if(!in_hand[suit][rank]) works, because, since the card is the same, it doesn't enter the if and it doesn't get printed.
I don't know what's causing this problem. Any idea? I tried moving srand() in the function just above rand() but nothing changed. It's strange, because it works fine the second time, but the third and on it doesn't. I feel like it's something obvious that I'm missing.
(I already asked this question before, but it was marked as duplicate by someone who didn't even bother to read it, so I didn't get any useful answers.)