用C语言做一个简单的石头剪子布游戏
求教,谢谢不是这个啦~没这么复杂~就只用到了IF,srand((unsigned)time(null));cpu=rand()%3;...
求教,谢谢
不是这个啦~没这么复杂~就只用到了IF,
srand((unsigned)time(null));
cpu=rand()%3; 展开
不是这个啦~没这么复杂~就只用到了IF,
srand((unsigned)time(null));
cpu=rand()%3; 展开
展开全部
#include <stdio.h>
#include <stdlib.h>
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,"次游戏总共");
}
#include <stdlib.h>
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,"次游戏总共");
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询