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 }