怎么用C语言编出猜拳游戏
2个回答
展开全部
挺有意思的,我用vc++6.0试了试,似乎可以
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void main()
{int a,b,c,d,e,f=0,g=0,h=0,num=0;
float i,j,k=0;
int caiquan1(int x);
int caiquan2(int x);
int shengfu1(int x,int y);
int shengfu2(int x,int y);
srand((int)time(0));
printf("1=剪刀\t2=锤子\t3=布\t0=结束\n");
printf("请输入\n");
while(1)
{
scanf("%f",&i);
a=(int)i;
if(a!=0&&a!=1&&a!=2&&a!=3)
{printf("输入错误,请重新输入\n");continue;}
if(a==0) break;
system("cls");
printf("1=剪刀\t2=锤子\t3=布\t0=退出\n");
printf("请输入\n");
b=1+rand()%99;
j=(a-1)*50;
printf("你出:");caiquan1(j);
c=caiquan2(j);
printf("\n");
printf("电脑出:");caiquan1(b);
d=caiquan2(b);
printf("\n");
num++;
shengfu1(c,d);
e=shengfu2(c,d);
if(e==1) f++;
if(e==2) g++;
if(e==3) h++;}
system("cls");
printf("赢%d\n",f);
printf("平%d\n",g);
printf("负%d\n",h);
printf("胜率%f\n",((float)f/num));
}
int caiquan1(int x)
{if(x<34) printf("剪刀");
else if(x>66) printf("布");
else printf("锤子");
return(0);}
int caiquan2(int x)
{int y;
if(x<34) y=1;
else if(x>66) y=3;
else y=2;
return(y);}
int shengfu1(int x,int y)
{if(x==1&&y==3) printf("赢");
else if(x==2&&y==1) printf("赢");
else if(x==3&&y==2) printf("赢");
else if(x==y) printf("平");
else printf("负");
return(0);}
int shengfu2(int x,int y)
{int k;
if(x==1&&y==3) k=1;
else if(x==2&&y==1) k=1;
else if(x==3&&y==2) k=1;
else if(x==y) k=2;
else k=3;
return(k);}
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void main()
{int a,b,c,d,e,f=0,g=0,h=0,num=0;
float i,j,k=0;
int caiquan1(int x);
int caiquan2(int x);
int shengfu1(int x,int y);
int shengfu2(int x,int y);
srand((int)time(0));
printf("1=剪刀\t2=锤子\t3=布\t0=结束\n");
printf("请输入\n");
while(1)
{
scanf("%f",&i);
a=(int)i;
if(a!=0&&a!=1&&a!=2&&a!=3)
{printf("输入错误,请重新输入\n");continue;}
if(a==0) break;
system("cls");
printf("1=剪刀\t2=锤子\t3=布\t0=退出\n");
printf("请输入\n");
b=1+rand()%99;
j=(a-1)*50;
printf("你出:");caiquan1(j);
c=caiquan2(j);
printf("\n");
printf("电脑出:");caiquan1(b);
d=caiquan2(b);
printf("\n");
num++;
shengfu1(c,d);
e=shengfu2(c,d);
if(e==1) f++;
if(e==2) g++;
if(e==3) h++;}
system("cls");
printf("赢%d\n",f);
printf("平%d\n",g);
printf("负%d\n",h);
printf("胜率%f\n",((float)f/num));
}
int caiquan1(int x)
{if(x<34) printf("剪刀");
else if(x>66) printf("布");
else printf("锤子");
return(0);}
int caiquan2(int x)
{int y;
if(x<34) y=1;
else if(x>66) y=3;
else y=2;
return(y);}
int shengfu1(int x,int y)
{if(x==1&&y==3) printf("赢");
else if(x==2&&y==1) printf("赢");
else if(x==3&&y==2) printf("赢");
else if(x==y) printf("平");
else printf("负");
return(0);}
int shengfu2(int x,int y)
{int k;
if(x==1&&y==3) k=1;
else if(x==2&&y==1) k=1;
else if(x==3&&y==2) k=1;
else if(x==y) k=2;
else k=3;
return(k);}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询