Rand () returns the same value after the first time

First, 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, this is code that "mimics" the blackjack hand. It starts by rand()

picking two numbers, a rank and a card layout, that are set to be true in the matrix so that they cannot be picked again in the same combination, and then printed. There the overall rating of the cards is checked (so if it exceeds 21, you automatically lose), and then you are asked if you want another card or if you want to stay.

Here's the mistake: if you choose that you want other cards, this new card will be the same as the last one. Basically, you get Ace of Spades, Two and Diamonds, then you need another card and you get another Two of Diamonds. And the other and the other. If you remove the rank check in the second, until you see that the rank increases with the rank of the last card.

Earlier the printfs functions were in this function print_hand()

and you could see that you always have the same map, now I moved them to a function main_hand()

because I thought it might be a problem (it wasn't) and since having a separate function for printing was redundant. But you can see what technically works if(!in_hand[suit][rank])

, because since the card is the same, it doesn't type an if, and it doesn't print.

I don't know what is causing this problem. Any idea?

Note that I have already used srand((unsigned) time(NULL));

for the seed rand()

.

+3


source to share


1 answer


scanf("%d", &newcard);

and scanf("%d", &stay);

are a problem, since they are variables bool

, but the format specifier matters int

. Change in 2 places, for example:

// scanf("%d", &newcard);
int t;
scanf("%d", &t);
newcard = !!t;

      

The 3 functions return int

but do not return anything. Change to invalid functions.

// int main_hand() {
void main_hand() {    

      



There are other logical problems as well.
1) The code sometimes exists without knowing if it won / lost 2) @ Jongware's comment above is correct:

Finally: if the same random sequence still goes up, strip the code naked main srand time printf rand

and see if that works. time()

if it doesn't work, it always returns -1.
Or just add the following and validate srand()

with different values.

 int main(void) {
   unsigned now = (unsigned) time(NULL);
   printf("now = %u\n", now);
   srand(now);

      

+3


source







All Articles