猜拳游戏

 1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<time.h>
  4 int com()                                                //电脑生成剪刀、石头、布
  5 {  int comput;
  6    srand((unsigned)time(NULL));  
  7    comput=rand()%3;    
  8    switch(comput)                                                              
  9    {
 10 
 11      case 0:printf("computer is scisscor\n");break; 
 12      case 1:printf("computer is rock\n");break;
 13      case 2:printf("computer is paper\n");break;
 14 
 15    }
 16    return comput;                                                         //返回电脑的选择(剪刀、石头、布)
 17 }
 18 
 19 int judge(int people,int pc)                                    // 胜负判断
 20 {
 21    int judge;
 22    judge=people-pc;
 23   if(judge==-2||judge==1)                                    // 玩家赢
 24    printf("\033[31mgamer win!\033[0m\n");
 25  else if(judge==0)                                                // 平局        
 26        printf("\033[31mthis is a goalless draw\033[0m\n");
 27  else                                                                     // 否则电脑赢
 28    printf("\033[31mcomputer win!\033[0m\n");   
 29   printf("\n");
 30 }
 31 
 32 void main()                                                                                   //主函数
 33 {
 34   int gamer,computer;                                                               

 35     while(1)

36  {
 37     printf("0->scissors\n");
 38     printf("1->rock\n");
 39     printf("2->paper\n");
 40     printf("3->exit\n");
 41     printf("\033[32minput your choice:\033[0m");    
 42     scanf("%d",&gamer);                                                     // 玩家选择
 43     getchar();
 44     switch(gamer)
 45   {
 46    case 0:
 47           printf("gamer is scissors\n");
 48           computer=com();
 49           judge(gamer,computer);
 50           break;
 51    case 1:
 52             printf("gamer is rock\n");
 53             computer=com();
 54             judge(gamer,computer);
 55             break;
 56    case 2:
 57            printf("gamer is paper\n");
 58            computer=com();
 59            judge(gamer,computer);
 60            break;
 61    case 3: exit(0);
 62    default: printf("choice error!!\n");
 63             printf("\n");
 64    }
 65  }
 66 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值