首页 > 学院 > 开发设计 > 正文

猜拳

2019-11-17 05:46:25
字体:
来源:转载
供稿:网友
#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; }}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表