#include <mathh>
#include <stdioh>
int compare(int num) /比较方法,num为输入的数/
int r=random()3; //随机获得一个0-2的整数
if(r==num) return 0; //返回0代表平手
if(r==0&&num==2) return -1; //返回-1代表输
//r=0 num=2时,是剪刀碰石头
if(r>num){
if(num==0&&r=2) return 1; //返回1代表赢 此为 石头碰剪刀
return -1; //此为布碰剪刀 或者石头碰布
}
return 1; //就是以上情况都没有碰到
}
int main(int arg,char srg[]){
int num=0; //用来记录输入的整数
int sum=0; //用来记录最终结果
printf("请输入0-2范围内三个整数:");
printf(“三次结果依次是:");
for(int i=0;i<3;i++)
{
scanf("%d",&num); //接收键盘输入
int result=compare(num); //进行判断
sum+=result; //将结果加和
printf("%s ",result==0"平":result==1"赢":"输"); //这里使用三目运算符是为了减少代码
}
printf("\n最终比赛结果: %s",sum==0"平":sum==1"赢":"输");
//这里使用三目运算符是为了减少代码
}
随机种子产生pc的随机出拳
srand(time(0));
int pc = rand()%3; //0, 1, 2 石头剪子布
屏幕输入自己的结果。
scanf(" %d", &var);
一个负责比较的代码块,很简单的逻辑处理。
一个最多执行三次的循环。
两个负责记录胜负次数的变量,针对单一角色,两胜或者两负,都会结束游戏。
找到一个,看看合适不。
如下:
enum p_r_s{
paper,rock,scissors,game,help,instructions,quit
}; /枚举变量,paper表示包袱,rock表示石头,scissors表示剪子/
#include <stdioh>
main()
{
enum p_r_s player,machine;
enum p_r_s selection_by_player(),selection_by_machine();
int win,lose,tie;
win=lose=tie=0;
instructions_for_the_player();
while((player=selection_by_player())!=quit)
switch(player){
case paper:
case rock:
case scissors:
machine=selection_by_machine();
if(player==machine){
++tie;
printf("\n a tie");
}
else if(you_won(player,machine)){
++win;
printf("\n you won");
}
else{
++lose;
printf("\n i won");
}
break;
case game:
game_status(win,lose,tie);
break;
case instructions:
instructions_for_the_player();
break;
case help:
help_for_the_player();
break;
}
game_status(win,lose,tie);
printf("\n\nBYE\n\n");
}
instructions_for_the_player()
{
printf("\n%s\n\n%s\n\n%s\n%s\n%s\n\n%s\n%s\n%s\n\n%s\n%s\n%s",
"PAPER,ROCK,SCISSORS",
"In this game",
"p is for paper,",
"r is for rock,",
"s is for scissors",
"Both the player and the machine will choose one",
"of p,r,or s If the two choices are the same,",
"then the game is a tie Otherwise:",
"\"paper covers the rock\" (a win for paper),",
"\"rock breaks the scissors\" (a win for rock),",
"\"scissors cut the paper\" (a win for scissors)");
printf("\n\n%s\n\n%s\n%s\n%s\n%s\n\n%s\n\n%s",
"There are other allowable inputs:",
"g for game status (the number of wins so far),",
"h for help,",
"i for instructions (reprin these instructions),",
"q for quit (to quit the game)",
"This game is played repeatedly until q is entered",
"Good luck!");
}
enum p_r_s selection_by_player()
{
char c;
enum p_r_s player;
printf("\n\ninput p,r,or s:");
while((c=getchar())==''||c=='\n'||c=='t');
;
switch(c){
case 'p':
player=paper;
break;
case 'r':
player=rock;
break;
case 's':
player=scissors;
break;
case 'g':
player=game;
break;
case 'i':
player=instructions;
break;
case 'q':
player=quit;
break;
default:
player=help;
}
return(player);
}
enum p_r_s selection_by_machine()
{
static int i;
i=++i%3;
return((i==0) paper:((i==1) rock:scissors));
}
you_won(player,machine)
enum p_r_s player,machine;
{
int victory;
if(player==paper)
victory=machine==rock;
else if(player==rock)
victory=machine==scissors;
else/player==scissors/
victory=machine==paper;
return(victory);
}
game_status(win,lose,tie)
{
printf("\nGAME STATUS");
printf("\n\n%7d%s\n%7d%s\n%7d%s\n%7d%s",
win,"games won by you",
lose,"games won by me",
tie,"game tied",
win+lose+tie,"games played:");
}
help_for_the_player()
{
printf("\n%s\n\n%s\n%s\n%s\n%s\n%s\n%s\n%s",
"the following characters can be used for input:",
" p for paper",
" r for rock",
" s for scissors",
" g to find out the game status",
" h to print this list",
" i to reprint the instructions for this game",
" q to quit this game");
}
#include<cstdio>
#include<cstdlib>
#include<ctime>
int main()
{
int a=0,b=0,i,c,h,wr;
printf("飞行棋,先输入格子数,之后每次输入1-3的数(上限100)\n");
printf("输入格子数:");
scanf("%d",&h);
a=a+h;b=b+h;
while(1)
{
printf("1=石头2=剪刀3=布\n");
printf(" \n \n \n");
printf("输入1~3\n");
printf("----------------------------------\n");
scanf("%d",&i);
srand(time(0));
c=rand()%3;
if(i==1&&c==2||i==2&&c==3||i==3&&c==1)
{a=a-1;
printf("你赢了,前进一步\n你还剩%d 电脑剩%d\n",a,b);
printf("--------------------------------\n");}
else if(c==1&&i==2||c==2&&i==3||c==3&&i==1)
{b=b-1;
printf("你输了,电脑前进\n你还剩%d 电脑剩%d\n",a,b);
printf("/--------------------------------/\n");}
else {printf("平局\n你还剩%d 电脑剩%d\n",a,b);
printf("--------------------------------\n");}
if(a==0||b==0)break;}
if(a==0)printf("you win\n");
else printf("you lose\n");
system("pause");
}
以上就是关于用c语言编写一个根据游戏规则输出判断结果全部的内容,包括:用c语言编写一个根据游戏规则输出判断结果、用C语言编写三局两胜的猜拳游戏,怎么编写、用c语言编写你和计算机玩猜拳游戏,五局三胜,在屏幕上显示每局情况和最终结果等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)