【发布时间】:2021-03-30 02:35:52
【问题描述】:
计算机总是给出相同的输入。
就像在示例中一样,我总是选择岩石,而计算机总是选择纸张。我应该怎么做才能让电脑给出不同的结果?
可能最后一个函数有问题,但我找不到。有人可以帮帮我吗?
enum Move {ROCK = 1, PAPER, SCISSORS}input;
int input, computerInput = 0;
int rock = 1;
int paper = 2;
int scissors = 3;
int computerPoint = 0, userPoint = 0;
printf(" ###### Welcome to Rock-Paper-Scissors Game ###### \n");
printf(" ### Rules: \n ### Press 1 for Rock \n ### Press 2 for Paper \n ### Press 3 for Scissors \n ### First one to reach 3 points will win. \n");
printf("Input your move : ");
scanf("%d", &input);
while(input < 1 || input > 3)
{
printf("Your input must be 1, 2 or 3 \n");
printf("Input your move : ");
scanf("%d",&input);
}
while(input == computerInput)
{
printf("It's draw. \n");
printf("Your Point is %d and Computer's point is %d \n",userPoint, computerPoint);
printf("Input your move : ");
scanf("%d",&input);
}
while(input != computerInput && userPoint < 3 && computerPoint < 3){
switch (input)
{
case ROCK:
if(computerInput == 2){
printf("You picked Rock. Computer picked Paper. Computer got 1 point(s). \n");
computerPoint++;
}
else if(computerInput == 3){
printf("You picked Rock. Computer picked Scissors. You got 1 point(s). \n");
userPoint++;
}
break;
case PAPER:
if(computerInput == 1){
printf("You picked Paper. Computer picked Rock. You got 1 point(s). \n");
userPoint++;
}else if(computerInput == 3){
printf("You picked Paper. Computer picked Scissors. Computer got 1 point(s). \n");
computerPoint++;
}
break;
case SCISSORS:
if(computerInput == 1){
printf("You picked Scissors. Computer picked Rock. Computer got 1 point(s). \n");
computerPoint++;
}else if(computerInput == 2){
printf("You picked Scissors. Computer picked Paper. You got 1 point(s). \n");
userPoint++;
}
break;
return 0;
}
if(computerPoint == 3){
printf("Your Point is %d and Computer's point is %d \n",userPoint, computerPoint);
printf("\n!!Computer won the game!!");
return 0;
}
if(userPoint == 3){
printf("Your Point is %d and Computer's point is %d \n",userPoint, computerPoint);
printf("\n!!You won the game!!");
return 0;
}
printf("Your Point is %d and Computer's point is %d \n",userPoint, computerPoint);
printf("Input your move : ");
scanf("%d",&input);
}
return 0;
enum Move getRandomMove(){
int computerInput;
srand(time(NULL));
computerInput = 1 + rand() %3;
return computerInput;
}
【问题讨论】:
-
阅读 Modern C、this C reference 以及您的 C 编译器 GCC 和调试器 GDB 的文档
-
您能否将显示的代码简化为您用作示例的一种情况并制作一个minimal reproducible example?给出您输入的确切输入、您期望的输出、解释原因以及您得到的输出将有很大帮助。见How to Ask。
-
关于switch部分的两个问题,但是如果其他部分有问题,我都发了,也许有人可以警告他们。如果你仍然说删除那些部分,我会删除它们。
-
if(input = computerInput)是错字吗?顺便说一句:由于重复三行说明位置、询问和下一步行动,代码不必要地臃肿。它们都可以(6 * 3 = 18 行)被放在switch代码块之外的那三行替换 -
您修改后的代码仍在使用
if(computerPoint = 3)之类的比较。你的意思是==?
标签: c function time enums srand