卡在剪纸摇滚游戏上 [英] Stuck on Paper-Scissors-Rock game
问题描述
虽然每次循环重复两次,并且CPU仅在为剪刀选择"S"时得分.另一个问题是,如何使它变得更高级以及在哪里添加该功能?我必须使用字符"r","s"和"p",而不是1,2,3来接受用户输入.不确定如何解决此问题
#include< stdlib.h>#include< time.h>int main(){字符播放器1;int播放器2;int userScore = 0,cpuScore = 0;player2 = rand()%3 +1;srand((int)time(NULL));int count = 0;while(count&=; = 10){printf("\ n对于纸张,输入p;对于岩石,输入r;对于剪刀,输入S:");scanf(%c",& player1);切换(玩家1){情况"P":if(player1 =='P'& player2 == 1){printf("Draw!");休息;} else if(player1 =='P'&&player2 == 2){userScore ++;printf(用户赢得了这个!");休息;} 别的 {cpuScore ++;printf("CPU赢得了这个!");休息;}情况"R":if(player1 =='R'&&player2 == 2){printf("Draw!");休息;} else if(player1 =='R'&&player2 == 3){userScore ++;printf(用户赢得了这个!");休息;} 别的 {cpuScore ++;printf("CPU赢得了这个!");休息;}案例"S":if(player1 =='S'&&player2 == 3){printf("Draw!");休息;} else if(player1 =='S'&&player2 == 1){userScore ++;休息;printf(用户赢得了这个!");} 别的 {cpuScore ++;printf("CPU赢得了这个!");休息;}默认 :printf("\ n无效输入");休息;}printf("\ nUser Score:%d",userScore);printf("\ nCPU分数:%d",cpuScore);数++;}if(userScore == cpuScore){printf("\ n抽奖游戏!");} else if(userScore> cpuScore){printf("\ n您赢了!");} 别的 {printf("\ nCPU获胜!");}返回0;}```
我建议您修复此问题,以使其不那么复杂.剪刀石头布的获胜者可以通过算法确定.
像like这样比较比较容易,并且使用整数值可以简化算术求解.同样从算术上来说,使用0,1,2而不是1,2,3更简单.因此,首先将用户输入转换为0,1,2:
#define INVALID_SELECTION sizeof(rps)静态const char rps [] = {'r','p','s'};int human = INVALID_SELECTION;while(人类== INVALID_SELECTION){printf("\ n对于岩石输入R,对于纸张输入P,或者对于剪刀输入S:");字符ch = 0;scanf(%c",& ch);while(ch!='\ n'&& getchar()!='\ n');for(人类= 0;人<INVALID_SELECTION&&tolower(ch)!= rps [人类];人类++){//没做什么}}
然后应确定计算机的播放方式:
srand((int)time(NULL));int计算机= rand()%3;
尽管请注意,您只需要调用一次 srand()
,所以如果将游戏置于循环中以重复播放,则 srand()
调用应该出现<在重复循环之前.
然后您可以按以下方式报告播放情况:
static const char * play_lookup [] = {"Rock","Paper","Scissors"};printf(人类扮演%s \ n",play_lookup [human]);printf(计算机播放了%s \ n",play_lookup [计算机]);
然后 human
和 computer
可以直接进行算术比较,从而:
int战斗=人机-计算机;if(战斗< 0)战斗+ = 3;开关(战斗){情况0:printf(绘制!\ n");休息 ;情况1:printf(人类获胜!\ n");休息 ;情况2:printf("Computer wins!\ n");休息 ;}
或(信用额为@ HAL9000):
int战斗=((人类-计算机)+ 3)%3;开关(战斗)...
将它们放在一起:
#include< stdio.h>#include< stdlib.h>#include< ctype.h>#include< time.h>int main(){//随机化srand((int)time(NULL));//无限重复播放为了(;;){#定义INVALID_SELECTION sizeof(rps)静态const char rps [] = {'r','p','s'};int human = INVALID_SELECTION;//虽然人类输入不是R,P,S,r,p或s之一...while(人类== INVALID_SELECTION){printf("\ n对于岩石输入R,对于纸张输入P,或者对于剪刀输入S:");字符ch = 0;scanf(%c",& ch);while(ch!='\ n'&& getchar()!='\ n');//将输入转换为0,1,2之一(分别用于R,P,S)for(人类= 0;人<INVALID_SELECTION&&tolower(ch)!= rps [人类];人类++){//没做什么}}//获得计算机的作用int计算机= rand()%3;//全文报告人和计算机的播放情况static const char * play_lookup [] = {"Rock","Paper","Scissors"};printf(人类扮演%s \ n",play_lookup [human]);printf(计算机播放了%s \ n",play_lookup [计算机]);//计算并报告结果int战斗=((人机)+ 3)%3;开关(战斗){情况0:printf(绘制!\ n");休息 ;情况1:printf(人类获胜!\ n");休息 ;情况2:printf("Computer wins!\ n");休息 ;}}返回0;}
示例输出:
为岩石输入R,为纸张输入P,或为剪刀输入S:r人类演奏的摇滚电脑玩摇滚画!输入R代表岩石,P代表纸张,或S代表剪刀:r人类演奏的摇滚电脑剪刀人赢了!输入R代表岩石,P代表纸张,或S代表剪刀:r人类演奏的摇滚电脑玩摇滚画!输入R代表岩石,P代表纸张,或S代表剪刀:r人类演奏的摇滚电脑剪刀人赢了!输入R代表岩石,P代表纸张,或S代表剪刀:r人类演奏的摇滚电脑打纸电脑胜出!输入R代表岩石,P代表纸张,或S代表剪刀:p人打纸电脑打纸画!输入R代表岩石,P代表纸张,或S代表剪刀:p人打纸电脑剪刀电脑胜出!输入R代表岩石,P代表纸张,或S代表剪刀:p人打纸电脑玩摇滚人赢了!输入R代表岩石,P代表纸张,或S代表剪刀:s人类玩剪刀电脑剪刀画!输入R代表岩石,P代表纸张,或S代表剪刀:s人类玩剪刀电脑打纸人赢了!输入R代表岩石,P代表纸张,或S代表剪刀:s人类玩剪刀电脑打纸人赢了!输入R代表岩石,P代表纸张,或S代表剪刀:s人类玩剪刀电脑剪刀画!输入R代表岩石,P代表纸张,或S代表剪刀:s人类玩剪刀电脑玩摇滚电脑胜出!输入R代表岩石,P代表纸张,或S代表剪刀:R人类演奏的摇滚电脑剪刀人赢了!输入R代表岩石,P代表纸张,或S代表剪刀:P人打纸电脑打纸画!输入R(代表岩石),P(代表纸张)或S(代表剪刀):S人类玩剪刀电脑打纸人赢了!输入R(代表岩石),P(代表纸张)或S(代表剪刀):xx输入R(代表岩石),P(代表纸张)或S(代表剪刀):yy为岩石输入R,为纸张输入P,或为剪刀输入S:zz输入R代表岩石,P代表纸张,或S代表剪刀:
While loop iterates two times every time, and CPU only scores point when choosing 'S' for scissors. Another question is, how do I make it toupper and where do I add that function? I have to use characters 'r','s', and 'p', instead of 1,2,3 to accept user input. Not sure how to proceed to fix this
#include <stdlib.h>
#include<time.h>
int main () {
char player1;
int player2;
int userScore = 0, cpuScore = 0;
player2 = rand ( ) % 3 + 1;
srand ((int) time (NULL));
int count = 0;
while(count <= 10) {
printf("\nEnter p for Paper, r for rock, or S for scissor: ");
scanf("%c", &player1);
switch(player1) {
case 'P' :
if(player1 == 'P' && player2 == 1) {
printf("Draw!");
break;
} else if(player1 == 'P' && player2 == 2) {
userScore++;
printf("User won this one!");
break;
} else {
cpuScore++;
printf("CPU won this one!");
break;
}
case 'R':
if(player1 == 'R' && player2 == 2) {
printf("Draw!");
break;
} else if(player1 == 'R' && player2 == 3) {
userScore++;
printf("User won this one!");
break;
} else {
cpuScore++;
printf("CPU won this one!");
break;
}
case 'S':
if(player1 == 'S' && player2 == 3) {
printf("Draw!");
break;
} else if(player1 == 'S' && player2 == 1) {
userScore++;
break;
printf("User won this one!");
} else {
cpuScore++;
printf("CPU won this one!");
break;
}
default :
printf("\nInvalid Input");
break;
}
printf("\nUser Score: %d", userScore);
printf("\nCPU Score: %d", cpuScore);
count++;
}
if(userScore == cpuScore) {
printf("\nDraw game!");
} else if(userScore > cpuScore) {
printf("\nYou win!");
} else {
printf("\nCPU wins!");
}
return 0;
} ```
I suggest you fix it my making it far less complicated. The winner of Rock-Paper-Scisors can be determined arithmetically.
It is simpler to compare like for like, and using integer values allows a simpler arithmetic solution. Also arithmetically it is simpler to use 0,1,2 rather than 1,2,3. So first transform the user input to 0,1,2:
#define INVALID_SELECTION sizeof(rps)
static const char rps[] = {'r', 'p', 's'} ;
int human = INVALID_SELECTION ;
while( human == INVALID_SELECTION )
{
printf("\nEnter R for Rock, P for Paper, or S for Scissors: ");
char ch = 0 ;
scanf("%c", &ch ) ;
while( ch != '\n' && getchar() != '\n' ) ;
for( human = 0;
human < INVALID_SELECTION && tolower(ch) != rps[human] ;
human++ )
{
// do nothing
}
}
The computer play should then be determined thus :
srand( (int)time(NULL) ) ;
int computer = rand() % 3 ;
though note that you need only call srand()
once, so if you put the game in a loop to repeat play, the srand()
call should appear before the repeat loop.
You can then report the play thus:
static const char* play_lookup[] = { "Rock", "Paper", "Scissors" } ;
printf( "Human played %s\n", play_lookup[human] ) ;
printf( "Computer played %s\n", play_lookup[computer] ) ;
Then human
and computer
are directly and arithmetically comparable such that:
int battle = human - computer ;
if( battle < 0 ) battle += 3 ;
switch( battle )
{
case 0 : printf( "Draw!\n" ) ; break ;
case 1 : printf( "Human wins!\n" ) ; break ;
case 2 : printf( "Computer wins!\n" ) ; break ;
}
or (credit to @HAL9000):
int battle = ((human - computer) + 3) % 3 ;
switch( battle )
...
Putting it all together:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
int main()
{
// Randomize
srand( (int)time(NULL) ) ;
// Repeat play indefinitely
for(;;)
{
#define INVALID_SELECTION sizeof(rps)
static const char rps[] = {'r', 'p', 's'} ;
int human = INVALID_SELECTION ;
// While human input is not one of R,P,S,r,p or s...
while( human == INVALID_SELECTION )
{
printf("\nEnter R for Rock, P for Paper, or S for Scissors: ");
char ch = 0 ;
scanf("%c", &ch ) ;
while( ch != '\n' && getchar() != '\n' ) ;
// Transform input to one of 0,1,2 (for R,P,S respectively)
for( human = 0;
human < INVALID_SELECTION && tolower(ch) != rps[human] ;
human++ )
{
// do nothing
}
}
// Get computer's play
int computer = rand() % 3 ;
// Report human and computer plays in full text
static const char* play_lookup[] = { "Rock", "Paper", "Scissors" } ;
printf( "Human played %s\n", play_lookup[human] ) ;
printf( "Computer played %s\n", play_lookup[computer] ) ;
// Calculate and report result
int battle = ((human - computer) + 3) % 3 ;
switch( battle )
{
case 0 : printf( "Draw!\n" ) ; break ;
case 1 : printf( "Human wins!\n" ) ; break ;
case 2 : printf( "Computer wins!\n" ) ; break ;
}
}
return 0;
}
Example output:
Enter R for Rock, P for Paper, or S for Scissors: r
Human played Rock
Computer played Rock
Draw!
Enter R for Rock, P for Paper, or S for Scissors: r
Human played Rock
Computer played Scissors
Human wins!
Enter R for Rock, P for Paper, or S for Scissors: r
Human played Rock
Computer played Rock
Draw!
Enter R for Rock, P for Paper, or S for Scissors: r
Human played Rock
Computer played Scissors
Human wins!
Enter R for Rock, P for Paper, or S for Scissors: r
Human played Rock
Computer played Paper
Computer wins!
Enter R for Rock, P for Paper, or S for Scissors: p
Human played Paper
Computer played Paper
Draw!
Enter R for Rock, P for Paper, or S for Scissors: p
Human played Paper
Computer played Scissors
Computer wins!
Enter R for Rock, P for Paper, or S for Scissors: p
Human played Paper
Computer played Rock
Human wins!
Enter R for Rock, P for Paper, or S for Scissors: s
Human played Scissors
Computer played Scissors
Draw!
Enter R for Rock, P for Paper, or S for Scissors: s
Human played Scissors
Computer played Paper
Human wins!
Enter R for Rock, P for Paper, or S for Scissors: s
Human played Scissors
Computer played Paper
Human wins!
Enter R for Rock, P for Paper, or S for Scissors: s
Human played Scissors
Computer played Scissors
Draw!
Enter R for Rock, P for Paper, or S for Scissors: s
Human played Scissors
Computer played Rock
Computer wins!
Enter R for Rock, P for Paper, or S for Scissors: R
Human played Rock
Computer played Scissors
Human wins!
Enter R for Rock, P for Paper, or S for Scissors: P
Human played Paper
Computer played Paper
Draw!
Enter R for Rock, P for Paper, or S for Scissors: S
Human played Scissors
Computer played Paper
Human wins!
Enter R for Rock, P for Paper, or S for Scissors: xx
Enter R for Rock, P for Paper, or S for Scissors: yy
Enter R for Rock, P for Paper, or S for Scissors: zz
Enter R for Rock, P for Paper, or S for Scissors:
这篇关于卡在剪纸摇滚游戏上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!