简体   繁体   中英

Dice game in C for Fifty

I am having to write a C program to roll dice for two people. After the winner is determined, I need it to loop back around and ask to play again. If q is entered, the program will end. If the user presses enter the program will continue and ask who plays first and then determine the winner again. After the program runs once, and I enter 1 or 2 it just keeps displaying congratulation player1or2 you won. It never enters back into my while loop (playerTotal1 < WinScore). I have initialized playerTotal and Winscore = 0 again inside the while loop for while( end.= q) but it does not change anything.

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

int main(void)
{

int dice1, dice2;
srand((unsigned)time(NULL));
int player1Total = 0, player2Total = 0, playerTurn;
int currentSum;
int WinScore = 50;
char end = ' ';

printf("Welcome to Dice Game Fifty. The first player who reaches %d wins!\n", WinScore);

while (end != 'q' && end != 'Q')
{
    player1Total = 0; player2Total = 0; 
    printf("Choose who rolls first, player1 or player2 (1 or 2): ");
    scanf_s("%d", &playerTurn);

    while (playerTurn != 1 && playerTurn != 2  )
    {
        printf("Only 2 players play this game...\n");
        printf("Choose the first player to roll (1 or 2): ");
        scanf_s("%d", &playerTurn);
    }

    while (player1Total < WinScore && player2Total < WinScore)
    {
        dice1 = rand() % 6 + 1;
        dice2 = rand() % 6 + 1;

        currentSum = 0;

        if (dice1 == dice2)
        {
            if (dice1 == 6)
                currentSum = 25;
            else if (dice1 == 3)
            {
                currentSum = 0;

                if (playerTurn == 1)
                    player1Total = 0;
                else
                    player2Total = 0;
            }
            else
                currentSum = 5;
        }

        if (playerTurn == 1)
            player1Total += currentSum;
        else
            player2Total += currentSum;

        printf("Player %d: You rolled (%d, %d), and so your round score is: %d, and your final score as of now is: ", playerTurn, dice1, dice2, currentSum);

        if (playerTurn == 1)
            printf("%d\n", player1Total);
        else
            printf("%d\n", player2Total);

        if (playerTurn == 1)
            playerTurn = 2;
        else
            playerTurn = 1;
    }
    
    if (playerTurn == 1)
        printf("Congratulations to Player2. You WON.\n");
    else
        printf("Congratulations to Player1. You WON.\n");
    
    printf("Enter q to quit or enter to continue: "); 
    scanf_s("%s", &end);
}
}

The method scanf_s does not work with empty inputs, so if you just press enter it will try to read another line (it's not that the loop is not executed again, but that your code never returns from scanf_s). You should try other methods such as fgets or getch. For a char variable, I think getch suits you better (getch is Windows-specific but as you are using scanf_s I don't think that will be a problem).

printf("Enter q to quit or any other key to continue\n");
end = getch();

You have to add a new line in the printf due to the way getch works. I also changed the message to "any other key" because in your loop you only check for 'q' or 'Q'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM