骰子游戏在C为50

骰子游戏在C为50,c,while-loop,C,While Loop,我不得不写一个C程序来为两个人掷骰子。在确定胜利者后,我需要它来循环并要求再次比赛。如果输入q,程序将结束。如果用户按enter键,程序将继续,询问谁先玩,然后再次确定赢家。程序运行一次后,我输入1或2,它会一直显示祝贺玩家1或2你赢了。它永远不会回到我的while循环(playerTotal1

我不得不写一个C程序来为两个人掷骰子。在确定胜利者后,我需要它来循环并要求再次比赛。如果输入q,程序将结束。如果用户按enter键,程序将继续,询问谁先玩,然后再次确定赢家。程序运行一次后,我输入1或2,它会一直显示祝贺玩家1或2你赢了。它永远不会回到我的while循环(playerTotal1
#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);
}
}
#包括
#包括
#包括
内部主(空)
{
智力骰子1,骰子2;
srand((无符号)时间(NULL));
int player1Total=0,player2Total=0,playerTurn;
int currentSum;
int WinScore=50;
char end='';
printf(“欢迎使用骰子游戏50。第一个达到%d的玩家获胜!\n”,WinScore);
while(end!='q'&&end!='q')
{
player1Total=0;player2Total=0;
printf(“选择谁先掷骰子,player1或player2(1或2):”;
扫描(“%d”和“playerTurn”);
while(playerTurn!=1&&playerTurn!=2)
{
printf(“只有2名玩家玩此游戏…\n”);
printf(“选择第一个掷骰的玩家(1或2):”;
扫描(“%d”和“playerTurn”);
}
while(player1Total
方法scanf\u s不适用于空输入,因此如果只按enter键,它将尝试读取另一行(不是循环不再执行,而是代码永远不会从scanf\u s返回)。您应该尝试其他方法,例如fgets或getch。对于char变量,我认为getch更适合您(getch是特定于Windows的,但由于您使用scanf_,我认为这不会有问题)


由于getch的工作方式,您必须在printf中添加新行。我还将消息更改为“任意其他键”,因为在循环中只检查“q”或“q”。

提示:远离
dice1
dice2
等变量。使用数组。对于玩家数量任意的游戏来说,这是双倍的。您最不希望看到的是变量,如
player902
scanf_s(“%s”,&end)我认为您想使用%c读取单个字符。。。不是%s
end
是一个
char
。您在scanf中使用了错误的格式说明符。尝试
scanf_u2;(“%c”,&end)当我进行此更改时,它会在While(end==q | | end==q)循环中抛出一个异常。什么样的异常?我已经在修改后运行了您的代码,它唯一一次抛出异常是在使用原始扫描时(由于问题,JHBonarius对%s进行了注释,该%s应该是%c),而对于getch,扫描不应该在那里。你能提供更新的代码吗?
printf("Enter q to quit or any other key to continue\n");
end = getch();