c 中的剪刀石头布游戏。有人可以告诉我错在哪里吗?
rock paper scissors game in c. Can someone tell where my fault is?
计算机总是给出相同的输入。
例如:example or example2
如示例所示,我总是选择石头,而计算机总是选择纸。我应该怎么做才能让电脑给出不同的结果?
可能是最后一个函数有问题,但我没找到。有人可以帮我吗?
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;
}
TL;DR 答案是这样的:
- 您正在将
input
和 computerInput
与 =
进行比较,而您应该使用 ==
- 你创建了一个名为
getRandomMove()
的函数,我怀疑你的意思是用来设置 computerInput
,但你实际上并没有在任何地方调用它,所以 computerInput
开始未初始化然后由于上述错误被意外分配给。
但是因为我很无聊,所以让我们来看看为什么它会这样:
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);
}
到目前为止还不错。您要求用户输入 1 到 3 之间的数字,如果他们不这样做,则重复输入请求。
while (input != computerInput && userPoint < 3 && computerPoint < 3)
{
呃哦。这是你的第一个错误:你在没有首先初始化它的情况下将 input
与 computerInput
进行比较。
您在程序开始时使用
行声明了 computerInput
int input, computerInput;
但是由于您没有给它一个值,computerInput
将包含任意内存数据。由于这个 ever 是 1、2 或 3 的可能性非常小,所以 input != computerInput
第一次可能不是真的。
case ROCK:
if (computerInput = 2)
此处您使用的是 赋值 运算符 (=
),而不是 equality 运算符 (==
).您没有将 computerInput
与 2
进行比较,而是 赋予它 2 的值。碰巧在 C 中,赋值语句将评估为您正在分配的值。
所以这归结为 if(2)
,它始终为真,因为它不是零。此时,你再给computerpoint加一,跳出switch语句。
对于您的第一个示例,您每次都输入了“1”。所以“1”不等于“2”(这是你在上面设置的computerInput
),所以循环又开始了。您再次进入 ROCK,所以发生了与上面完全相同的事情,导致计算机得到另一个分数。
这种情况每次都会发生,直到分数达到 3 并且计算机获胜。
我用你自己的代码“修复”了你的问题:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/**
* ROCK = 1
* PAPER = 2
* SCISSORS = 3
*/
// random move returns a random int from 1 - 3.
// I removed the local variable
int getRandomMove()
{
return 1 + rand() %3;
}
// main logic of the game
int main()
{
//Your enum with rock, paper, scissors defind as 1, 2, 3
enum Move {ROCK = 1, PAPER = 2, SCISSORS = 3} userInput = 0;
// sets a random seed for the rand()-function
srand(time(NULL));
// here you declared userInput another time
// int userInput is not needed, you already got it up here
int computerInput = 0, 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");
// add big while loop to avoid multiple loops:
// runs as long as no one reached the goal of 3 points
while (computerPoint < 3 && userPoint < 3)
{
// Test for valid input
// when user input is unvalid (not from 1 - 3), we ask to enter a number again
while (userInput < 1 || userInput > 3)
{
printf("\n");
printf("Your input must be 1, 2 or 3 \n");
printf("Input your move : ");
scanf("%d",&userInput);
printf("\n");
}
// You missed this in your initial program:
// The computer gets a random value (ROCK/PAPER/SCISSOR)
computerInput = getRandomMove();
// what if it's a draw...?
if (userInput == computerInput)
{
printf("It's draw. \n");
printf("You got %d point(s) and the computer has %d point(s)\n",userPoint, computerPoint);
userInput = 0;
computerInput = 0;
continue;
}
// I changed little besides comparing the computer input with the the different "Move" types
switch (userInput)
{
case ROCK:
if(computerInput == PAPER)
{
printf("You picked Rock. Computer picked paper. Computer got 1 point(s). \n");
computerPoint++;
}
else if(computerInput == SCISSORS)
{
printf("You picked rock. Computer picked scissors. You got 1 point(s). \n");
userPoint++;
}
break;
case PAPER:
if(computerInput == ROCK)
{
printf("You picked paper. Computer picked Rock. You got 1 point(s). \n");
userPoint++;
}
else if(computerInput == SCISSORS)
{
printf("You picked paper. Computer picked scissors. Computer got 1 point(s). \n");
computerPoint++;
}
break;
case SCISSORS:
if(computerInput == ROCK)
{
printf("You picked Scissors. Computer picked Rock. Computer got 1 point(s). \n");
computerPoint++;
}else if(computerInput == PAPER)
{
printf("You picked Scissors. Computer picked Paper. You got 1 point(s). \n");
userPoint++;
}
break;
default:
break;
}
// game ending condition (one player reached 3 points)
if (computerPoint == 3 || userPoint == 3)
{
// print out each players points
printf("You got %d points and the Computer got %d points\n", userPoint, computerPoint);
// computer won
if (computerPoint == 3)
{
printf("You lost :/\n");
}
// player won
else
{
printf("You WON!!!\n");
}
// if this is reached the next time we enter the loop while(computerPoint < 3 && userPoint < 3)
// isn't true anymore so we skip to the end of program (no break, continue whatever needed)
}
// set the values 0 for the next loop
userInput = 0;
computerInput = 0;
}
// waits for the user to enter "enter"
// else the console would close before you see the result!
system("pause");
}
首先,您的代码非常混乱,我对其进行了一些重组,使其更易于阅读和理解。我为大多数语句添加了注释以解释以下代码的作用。
让我们来看看这个
由于您的代码非常小,您可以复制 includes
以及主要功能。
首先我在开头设置了getRandomMove()
函数,所以我们不必单独声明它(再次,混乱的代码等)。
enum Move getRandomMove() { // we don't want to return an enum but an Integer
int computerInput; // here you define a variable to return it's value a few lines further, i deleted it
srand(time(NULL)); // this function should be called once, i put it in the main function
computerInput = 1 + rand() %3; // you store a random number in a variable to return it on line later hence we put the function call in the return
return computerInput;
}
我删除了一些代码,因为
int a = 0;
return a;
与return 0;
相同。
接下来我将 while
循环的数量减少到两个。
第一个重复,直到玩家达到 3 分。
我在计算机获取随机数的地方添加了一行,你错过了,所以计算机永远不会公平竞争:
computerInput = getRandomMove();
接下来我们检查他们是否平局,如果为真我们重置玩家 Moves
。
在循环结束时我添加了两行
userInput = 0;
computerInput = 0;
重置球员 Moves
,与平局相同的程序。
所有更改后,这是正确的工作和清除版本。非常感谢大家的帮助。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void){
enum Move {ROCK = 1, PAPER, SCISSORS}input;
srand(time(NULL));
int computerInput;
int computerPoint = 0, userPoint = 0;
enum Move getRandomMove()
{
return (enum Move)(1 + rand() % SCISSORS);
}
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 (computerPoint < 3 && userPoint < 3)
{
while(input < 1 || input > 3)
{
printf("Your input must be 1, 2 or 3 \n");
printf("Input your move : \n\n");
scanf("%d",&input);
}
computerInput = getRandomMove();
if(input == computerInput)
{
printf("\nIt's draw. \n");
printf("Your Point is %d and Computer's point is %d \n",userPoint, computerPoint);
printf("Input your move : ");
scanf("%d",&input);
continue;
}
switch (input)
{
case ROCK:
if(computerInput == PAPER){
printf("\nYou picked Rock. Computer picked Paper. Computer got 1 point(s). \n");
computerPoint++;
}
else if(computerInput == SCISSORS){
printf("\nYou picked Rock. Computer picked Scissors. You got 1 point(s). \n");
userPoint++;
}
break;
case PAPER:
if(computerInput == ROCK){
printf("\nYou picked Paper. Computer picked Rock. You got 1 point(s). \n");
userPoint++;
}else if(computerInput == SCISSORS){
printf("\nYou picked Paper. Computer picked Scissors. Computer got 1 point(s). \n");
computerPoint++;
}
break;
case SCISSORS:
if(computerInput == ROCK){
printf("\nYou picked Scissors. Computer picked Rock. Computer got 1 point(s). \n");
computerPoint++;
}else if(computerInput == PAPER){
printf("\nYou picked Scissors. Computer picked Paper. You got 1 point(s). \n");
userPoint++;
}
break;
}
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;
}
计算机总是给出相同的输入。
例如:example or example2
如示例所示,我总是选择石头,而计算机总是选择纸。我应该怎么做才能让电脑给出不同的结果?
可能是最后一个函数有问题,但我没找到。有人可以帮我吗?
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;
}
TL;DR 答案是这样的:
- 您正在将
input
和computerInput
与=
进行比较,而您应该使用==
- 你创建了一个名为
getRandomMove()
的函数,我怀疑你的意思是用来设置computerInput
,但你实际上并没有在任何地方调用它,所以computerInput
开始未初始化然后由于上述错误被意外分配给。
但是因为我很无聊,所以让我们来看看为什么它会这样:
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);
}
到目前为止还不错。您要求用户输入 1 到 3 之间的数字,如果他们不这样做,则重复输入请求。
while (input != computerInput && userPoint < 3 && computerPoint < 3)
{
呃哦。这是你的第一个错误:你在没有首先初始化它的情况下将 input
与 computerInput
进行比较。
您在程序开始时使用
行声明了computerInput
int input, computerInput;
但是由于您没有给它一个值,computerInput
将包含任意内存数据。由于这个 ever 是 1、2 或 3 的可能性非常小,所以 input != computerInput
第一次可能不是真的。
case ROCK:
if (computerInput = 2)
此处您使用的是 赋值 运算符 (=
),而不是 equality 运算符 (==
).您没有将 computerInput
与 2
进行比较,而是 赋予它 2 的值。碰巧在 C 中,赋值语句将评估为您正在分配的值。
所以这归结为 if(2)
,它始终为真,因为它不是零。此时,你再给computerpoint加一,跳出switch语句。
对于您的第一个示例,您每次都输入了“1”。所以“1”不等于“2”(这是你在上面设置的computerInput
),所以循环又开始了。您再次进入 ROCK,所以发生了与上面完全相同的事情,导致计算机得到另一个分数。
这种情况每次都会发生,直到分数达到 3 并且计算机获胜。
我用你自己的代码“修复”了你的问题:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/**
* ROCK = 1
* PAPER = 2
* SCISSORS = 3
*/
// random move returns a random int from 1 - 3.
// I removed the local variable
int getRandomMove()
{
return 1 + rand() %3;
}
// main logic of the game
int main()
{
//Your enum with rock, paper, scissors defind as 1, 2, 3
enum Move {ROCK = 1, PAPER = 2, SCISSORS = 3} userInput = 0;
// sets a random seed for the rand()-function
srand(time(NULL));
// here you declared userInput another time
// int userInput is not needed, you already got it up here
int computerInput = 0, 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");
// add big while loop to avoid multiple loops:
// runs as long as no one reached the goal of 3 points
while (computerPoint < 3 && userPoint < 3)
{
// Test for valid input
// when user input is unvalid (not from 1 - 3), we ask to enter a number again
while (userInput < 1 || userInput > 3)
{
printf("\n");
printf("Your input must be 1, 2 or 3 \n");
printf("Input your move : ");
scanf("%d",&userInput);
printf("\n");
}
// You missed this in your initial program:
// The computer gets a random value (ROCK/PAPER/SCISSOR)
computerInput = getRandomMove();
// what if it's a draw...?
if (userInput == computerInput)
{
printf("It's draw. \n");
printf("You got %d point(s) and the computer has %d point(s)\n",userPoint, computerPoint);
userInput = 0;
computerInput = 0;
continue;
}
// I changed little besides comparing the computer input with the the different "Move" types
switch (userInput)
{
case ROCK:
if(computerInput == PAPER)
{
printf("You picked Rock. Computer picked paper. Computer got 1 point(s). \n");
computerPoint++;
}
else if(computerInput == SCISSORS)
{
printf("You picked rock. Computer picked scissors. You got 1 point(s). \n");
userPoint++;
}
break;
case PAPER:
if(computerInput == ROCK)
{
printf("You picked paper. Computer picked Rock. You got 1 point(s). \n");
userPoint++;
}
else if(computerInput == SCISSORS)
{
printf("You picked paper. Computer picked scissors. Computer got 1 point(s). \n");
computerPoint++;
}
break;
case SCISSORS:
if(computerInput == ROCK)
{
printf("You picked Scissors. Computer picked Rock. Computer got 1 point(s). \n");
computerPoint++;
}else if(computerInput == PAPER)
{
printf("You picked Scissors. Computer picked Paper. You got 1 point(s). \n");
userPoint++;
}
break;
default:
break;
}
// game ending condition (one player reached 3 points)
if (computerPoint == 3 || userPoint == 3)
{
// print out each players points
printf("You got %d points and the Computer got %d points\n", userPoint, computerPoint);
// computer won
if (computerPoint == 3)
{
printf("You lost :/\n");
}
// player won
else
{
printf("You WON!!!\n");
}
// if this is reached the next time we enter the loop while(computerPoint < 3 && userPoint < 3)
// isn't true anymore so we skip to the end of program (no break, continue whatever needed)
}
// set the values 0 for the next loop
userInput = 0;
computerInput = 0;
}
// waits for the user to enter "enter"
// else the console would close before you see the result!
system("pause");
}
首先,您的代码非常混乱,我对其进行了一些重组,使其更易于阅读和理解。我为大多数语句添加了注释以解释以下代码的作用。
让我们来看看这个
由于您的代码非常小,您可以复制 includes
以及主要功能。
首先我在开头设置了getRandomMove()
函数,所以我们不必单独声明它(再次,混乱的代码等)。
enum Move getRandomMove() { // we don't want to return an enum but an Integer
int computerInput; // here you define a variable to return it's value a few lines further, i deleted it
srand(time(NULL)); // this function should be called once, i put it in the main function
computerInput = 1 + rand() %3; // you store a random number in a variable to return it on line later hence we put the function call in the return
return computerInput;
}
我删除了一些代码,因为
int a = 0;
return a;
与return 0;
相同。
接下来我将 while
循环的数量减少到两个。
第一个重复,直到玩家达到 3 分。
我在计算机获取随机数的地方添加了一行,你错过了,所以计算机永远不会公平竞争:
computerInput = getRandomMove();
接下来我们检查他们是否平局,如果为真我们重置玩家 Moves
。
在循环结束时我添加了两行
userInput = 0;
computerInput = 0;
重置球员 Moves
,与平局相同的程序。
所有更改后,这是正确的工作和清除版本。非常感谢大家的帮助。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void){
enum Move {ROCK = 1, PAPER, SCISSORS}input;
srand(time(NULL));
int computerInput;
int computerPoint = 0, userPoint = 0;
enum Move getRandomMove()
{
return (enum Move)(1 + rand() % SCISSORS);
}
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 (computerPoint < 3 && userPoint < 3)
{
while(input < 1 || input > 3)
{
printf("Your input must be 1, 2 or 3 \n");
printf("Input your move : \n\n");
scanf("%d",&input);
}
computerInput = getRandomMove();
if(input == computerInput)
{
printf("\nIt's draw. \n");
printf("Your Point is %d and Computer's point is %d \n",userPoint, computerPoint);
printf("Input your move : ");
scanf("%d",&input);
continue;
}
switch (input)
{
case ROCK:
if(computerInput == PAPER){
printf("\nYou picked Rock. Computer picked Paper. Computer got 1 point(s). \n");
computerPoint++;
}
else if(computerInput == SCISSORS){
printf("\nYou picked Rock. Computer picked Scissors. You got 1 point(s). \n");
userPoint++;
}
break;
case PAPER:
if(computerInput == ROCK){
printf("\nYou picked Paper. Computer picked Rock. You got 1 point(s). \n");
userPoint++;
}else if(computerInput == SCISSORS){
printf("\nYou picked Paper. Computer picked Scissors. Computer got 1 point(s). \n");
computerPoint++;
}
break;
case SCISSORS:
if(computerInput == ROCK){
printf("\nYou picked Scissors. Computer picked Rock. Computer got 1 point(s). \n");
computerPoint++;
}else if(computerInput == PAPER){
printf("\nYou picked Scissors. Computer picked Paper. You got 1 point(s). \n");
userPoint++;
}
break;
}
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;
}