-3

所以我正在做一个骰子游戏,你掷骰子数量,然后计算机掷出相同数量的骰子,总点数最高的玩家赢得这一轮。但是,我陷入了询问玩家是否想再次滚动的循环中。无论我输入什么,它都会再次滚动。我已经坚持了一段时间,所以任何帮助将不胜感激。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
    /* Easy dice game
    |
    |  The game consists of 7 rounds.
    |  In each round, the computer throws a die, 
    |  then the human throws a die.
    |  The winner of the round is the player who has the highest throw.
    |  In case of a tie, neither player wins.
    |  The winner of the game is the player who has won the most rounds.
    |
    */

    char input[132];   /* user input buffer */

int throwDie()
{
    static int initialized = 0;
    int num;

    if ( !initialized )
    {
        printf("Initializing Die!\n\n");
        srand( time(NULL) );
        initialized = 1;
    }
    num = rand()%6 + 1 ;
    return num;
}

/* Human turn
|
|  This might be mode made interesting in the future.
|
*/
int humanTurn()
{
    int toss;
    toss = throwDie();
    printf("Human throws a %d\n", toss );
    return toss;

}

/* Computer turn
|
|  This might be made more interesting in the future.
|
*/
int computerTurn()
{
    int toss;
    toss = throwDie();
    printf("Computer throws a %d\n", toss );
    return toss;
} 

int main(int argc, char *argv[])
{
    int round, humanWins=0, computerWins=0 ;
    int humanToss, computerToss;
    int i = 0, yesorno;
    const int numberOfRounds = 7;
    char ta=0;
    /* Play 13 Rounds */
    for ( round = 1; round<=numberOfRounds; round++ )
    {
        printf("\nRound %d\n\n", round );
        printf("Player's Turn: (hit enter)");
        gets( input ); /* pause for dramatic effect */
        humanToss = humanTurn();
        printf("Do you wish to throw again? [Y or N]");
        ta = getchar();


        while (i == 0)
        {
            if (yesorno = 'Y')
            {
                gets( input );
                humanToss = humanTurn();
                printf("Do you wish to throw again? [Y or N]");
                ta = getchar();

            }
            if(yesorno == 'N')
            {
                i++;
            }
        }




        printf("Computer's Turn: (hit enter)");

        gets( input ); /* pause for dramatic effect */
        computerToss = computerTurn();

        /* Determine Winner of the Round */
        if ( humanToss > computerToss )
        {
            humanWins++;
            printf("\tHuman wins the round.    human: %3d. computer: %3d\n",
                humanWins, computerWins );
        }
        else if ( computerToss > humanToss )
        {
            computerWins++;
            printf("\tComputer wins the round. human:%3d. computer: %3d\n",
                humanWins, computerWins );
        }
        else if ( computerToss == humanToss)
        {
            printf("\tTie.                     human:%3d. computer: %3d\n",
                humanWins, computerWins );
        }
    }

    /* Determine Winner to the Game */
    if ( humanWins > computerWins )
        printf("\n\nWINNER!! The human wins the game!\n");
    else if ( computerWins < humanWins )
        printf("\n\nThe computer wins the game!\n");
    else
        printf("\n\nTie Game!\n");

    printf("\n");
    system("pause");
    return 0;
}
4

3 回答 3

4

将您的程序更改为

if (yesorno == 'Y')

您正在分配而不是检查是。

于 2013-02-13T18:32:01.200 回答
1

你被困在这里:

while (i == 0)
{
  if (yesorno = 'Y')
  {
     gets( input );
     humanToss = humanTurn();
     printf("Do you wish to throw again? [Y or N]");
     ta = getchar();

  }
  if(yesorno == 'N')
  {
     i++;
  }
}

yesorno从先前的输入中获得了价值;然后你会收到新的输入,但变量yesorno是相同的——你只需设置变量ta So yesornois always 'Y'i总是 0 并且你处于无限的 while 循环中。

yesorno正如第二位评论者所说,编辑 并且您正在分配您的 if。但无论如何,如果你写==而不是=,你仍然会处于无限循环中。

于 2013-02-13T18:31:42.213 回答
0

这两行之间有很大的区别。
你能发现吗?

if (yesorno = 'Y')
if (yesorno == 'N')

此外,因为您正在检查 的值yesorno,所以您应该问自己:
“我在哪里设置这个值?我如何设置这个值?”

于 2013-02-13T23:49:54.400 回答