发新话题
打印

C++做的猜拳游戏

C++做的猜拳游戏

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int random(int maxlim);
int judgewin(char h1,char h2);
void disphand(char h);

void main()
{
char man,computer;
char wantplay;

/*重置随机数序列*/
srand( (unsigned)time( NULL ) );

printf("-----------猜      拳-------------- ");
do
{
   /*你出拳*/
   while(1)
   {
     printf("您出什么拳?(1--石头 2--剪子 3--布):");
     man=getche();
     if(man<'1'||man>'3')
       printf("您出的不是拳! ");
     else
       break;
   }
   /*显示您出的拳*/
   printf(" 您出的是");
   disphand(man);
   printf(" ");

   /*电脑出拳*/
   computer=random(3) + '1';

   /*显示电脑出的拳*/
   printf("我出的是");
   disphand(computer);
   printf(" ");

   /*判断胜负*/
   switch(judgewin(man,computer))
   {
   case 0://平
     printf("不分胜负 ");
     break;
   case 1://您赢
     printf("唉! 我输了。 ");
     break;
   case -1://电脑赢
     printf("哈哈! 我赢了。 ");
     break;
   }

   printf("还玩吗?(Y/N)");
   wantplay=getche();
   printf(" ");
}while(wantplay=='y'||wantplay=='Y');

}

/*产生0到maxlim之间的随机数*/
int random(int maxlim)
{
float number;

number=((float)rand()/RAND_MAX)*maxlim;
return((int)number);
}

/*判断h1和h2的胜负*/
int judgewin(char h1,char h2)
{
if(h1==h2)
   return 0;//peace
else if( (h1=='1'&&h2=='2')   /* h1出石头,h2出剪子*/
||(h1=='2'&&h2=='3')   /*或者h1出剪子,h2出布*/
||(h1=='3'&&h2=='1') ) /*或者h1出布  ,h2出石头*/
   return 1;//h1 win
else
   return -1;//h2 win
}

/*根据h的值显示"石头"、"剪子"、"布"*/
void disphand(char h)
{
switch(h)
{
case '1':
   printf("石头");
   break;
case '2':
   printf("剪子");
   break;
case '3':
   printf("布");
   break;
}
}

c++

我正在学阿
会有用的
我爱你MJ~~~~~虽然你对我这么狠,不过,爱你,我可以忍受,可以忍受一辈子~


有意思啊`!~


                                                                                                                                                                                                         
发新话题