user2901345
user2901345

Reputation: 9

Creating a game to guess if the next number will be higher of lower in c

So I am just learning to code now. This is a program running in a terminal on linux.

The game should generate a number between 2-14(a card deck) and let the user guess if the next number will be higher or lower.

The problem I am encountering is that the game works properly but for the game evaluates the numbers from the previous turn. If I get 6 on the first turn, 7 on the second turn and 3 on the third turn and I guessed that the number on the third turn would be higher it will evaluate as a correct guess because the 7 is higher than the 6, not taking the 3 into account as it should.

Also I have a problem with the code creating a new random number when there is incorrect input. It should just keep the same number until the user enters a valid choice.

Example output:

The current card is a 9. Will the next number be higher(1) or lower(2)? 2

You have guessed incorrectly. Your current score is -1! The current number is 9. Will the next number be higher(1) or lower(2)? 2

The cards are the same. Your current score is -1! The current number is 3 Will the next number be higher(1) or lower(2)?

Here is my entire code

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() 
{ 
    /*declare variables*/ 
    int pastCard; 
    int currentCard; 
    int score; 
    int userChoice; 
    int playGame = 1; 


    /*set up random*/ 
    int range; 
    srand(time(NULL)); 
    range = (13 - 1) + 1; 

    pastCard = rand() % range + 2; 
    currentCard = rand() % range + 2; 

    while (playGame == 1) 
    { 
        /*change the current card to the past card before creating new current card*/   
        pastCard = currentCard; 

        /*generate a random int for card*/ 

        currentCard = rand() % range + 2; 

        if (currentCard < 11) 
        { 
            printf("The current card is a %d.\n", currentCard); 
        } 
        else if (currentCard == 11) 
        { 
            printf("The current card is a jack.\n", currentCard); 
        } 
        else if (currentCard == 12) 
        { 
            printf("The current card is a queen.\n", currentCard); 
        } 
        else if (currentCard == 13) 
        { 
            printf("The current card is a king.\n", currentCard); 
        } 
        else if (currentCard == 14) 
        { 
            printf("The current card is an ace.\n", currentCard); 
        } 

        printf("Will the next card be higher(1) or lower(2)? (press 0 to quit)\n"); 
        scanf("%d", &userChoice); 
        printf("\n"); 


        if (userChoice == 1) 
        { 
            if (currentCard > pastCard) 
            { 
                score++; 

                printf("You have guessed correctly.\n"); 
                printf("Your current score is %d!\n", score); 
            } 
            else if (currentCard < pastCard) 
            { 
                score--; 

                printf("You have guessed incorrectly.\n"); 
                printf("Your current score is %d!\n", score); 
            } 
            else if (currentCard == pastCard) 
            { 
                printf("The cards are the same.\n"); 
                printf("Your current score is %d!\n", score); 
            } 
        } 
        else if (userChoice == 2) 
        { 
            if (currentCard < pastCard) 
            { 
                score++; 

                printf("You have guessed correctly.\n"); 
                printf("Your current score is %d!\n", score); 
            } 
            else if (currentCard > pastCard) 
            { 
                score--; 

                printf("You have guessed incorrectly.\n"); 
                printf("Your current score is %d!\n", score); 
            } 
            else if (currentCard == pastCard) 
            { 
                printf("The cards are the same.\n"); 
                printf("Your current score is %d!\n", score); 
            } 
        } 
        else if (userChoice == 0) 
        { 
            playGame = 0; 
            printf("Final score: %d\n", score); 

            score = 0; 

            printf("Play again? (press 1 for yes, 0 for no)\n"); 
            scanf("%d", &playGame); 
            printf("\n"); 
        } 
        else 
        { 
             printf("Please enter a valid choice.\n"); 
        } 

    } 

    return 0; 
}

Please help! This has been frustrating me very much!

Upvotes: 0

Views: 1104

Answers (2)

Crowman
Crowman

Reputation: 25918

You can fix your primary problem by changing:

printf("The current number is a %d.\n", currentCard);

to:

printf("The current number is a %d.\n", pastCard);

in all places, since as you've written it, currentCard is the one they're trying to guess, not the one you want to show them. You'd improve your program by renaming pastCard to currentCard, and currentCard to nextCard, throughout.

Or, with your updated code, change to this:

if (pastCard < 11) {
    printf("The current card is a %d.\n", pastCard); 
} else if (pastCard == 11) {
    printf("The current card is a jack.\n", pastCard); 
} else if (pastCard == 12) {
    printf("The current card is a queen.\n", pastCard); 
} else if (pastCard == 13) {
    printf("The current card is a king.\n", pastCard); 
} else if (pastCard == 14) {
    printf("The current card is an ace.\n", pastCard); 
}

Upvotes: 1

Chris Olsen
Chris Olsen

Reputation: 3511

You've got a logic error with keeping track of the cards. You're showing the user the wrong card. You want to show the current card, wait for input, and THEN choose a new card.

Also, when validating the user input, you can do a loop something like:

while user input not valid
    prompt for input
    scan input
    if input not valid then
         print error message
end while

Only after this loop should you go on to generate the new card and do the logic checks.

Upvotes: 0

Related Questions