简体   繁体   English

骰子游戏程序

[英]dice game program

I'm writing a dice program where there are 7 rounds In each round the user gets to roll as many times as they desire, the sum is added up, and then the computer rolls the same number of dice and it's sum is added up. 我正在编写一个骰子程序,其中有7个回合,每回合,用户可以随意滚动多次,将总和相加,然后计算机滚动相同数量的骰子,并且将其总和相加。 The winner of the round is determined by the highest dice roll and whoever winds the most rounds wins the game. 该回合的获胜者由最高的骰子掷骰决定,谁绕得最多,就赢得比赛。 This is what I have so far but I can not get the loop which asks the user if they want to roll again to work properly so any help would be greatly appreciated. 这是到目前为止的内容,但是我无法获得询问用户是否要再次滚动以使其正常工作的循环,因此将不胜感激。

#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

int humanTurn()
{
int toss;
toss = throwDie();
printf("Human throws a %d\n", toss );
return toss;

}

// Computer turn

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;
const int numberOfRounds = 7;
char ta=0;
/* Play 7 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]");
 scanf("%s", ta);

while (ta = 'Y')
{

     if(ta = 'Y')
     {
     humanToss = humanTurn();
     printf("Do you wish to throw again? [Y or N]");
     scanf("%s", ta);
     }
     else
     {
       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;
  }
while (ta = 'Y')

should be while (ta == 'Y') 应该是while (ta == 'Y')

so should your if statements be == not = 所以你的if语句应该==不是=

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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