用C语言做一个简单的石头剪子布游戏

2024-11-28 23:43:06
推荐回答(2个)
回答(1):

#include
#include
enum games{cloth,hammer,scissors,game,quit};
enum games select();
enum games mach();
int won(enum games player, enum games machine);
void result(int win, int lose, int tie);

void main()
{
enum games player, machine;
int win,lose, tie;
win=lose=tie=0;

printf("\n%s\n%s\n%s\n%s\n%s","c 代表布","h 代表锤子","s 代表剪刀","g 代表结果","其他键退出");
while((player=select())!=quit)
switch(player){
case cloth:
case hammer:
case scissors:
machine=mach();
if(player==machine){
++tie;
printf("\n平局");
}
else if(won(player, machine)){
++win;
printf("\n你赢");
}
else{
++lose;
printf("\n电脑赢");
}
break;
case game:
result(win,lose,tie);
break;
}
result(win,lose,tie);
printf("\n再见,欢迎下次再来!\n");
}
enum games select()
{
char c;
enum games player;
printf("\n请按键选择: ");
while((c=getchar())==''||c=='\n'||c=='\t');
switch(c){
case 'c': player=cloth;
break;
case 'h': player=hammer;
break;
case 's': player=scissors;
break;
case 'g': player=game;
break;
default: player=quit;
break;
}
return(player);
}
enum games mach()
{
static int i=0;
i=rand()%3;
return((i==0)?cloth:((i==1)?hammer:scissors));
}
int won(enum games player, enum games machine)
{
int victory;
if(player==cloth)
victory=machine==hammer;
else if(player==hammer)
victory=machine==scissors;
else victory=machine==cloth;
return(victory);
}
void result(int win,int lose, int tie)
{
printf("\n游戏状况:");
printf("\n%-7d%s\n%-7d%s\n%-7d%s\n%-7d%s",win,"次你赢",lose,"次电脑赢", tie,"次平局",win+lose+tie,"次游戏总共");
}

回答(2):

就是A>B
B>C
C>A
如果其他可能就不能比较。