c语言实现简单猜拳游戏

1 #include<stdio.h>
2 #include<stdlib.h> //rand()函数
3 #include<string.h>
4 #include<time.h>
5 #include<unistd.h> //system()函数,getpid()函数
6 #define SIZE 20
7 #define NAME “user1”
8 #define PASS “123456”
9 #define TRUE 1
10 #define FALSE 0
11 #define CONTINUE(X) ({printf("%s",X);getchar();})
12 #define MYFLUSH ({int ch = 0;while((ch = getchar()) != ‘\n’ && ch != EOF);})
13
14 /*
15 结构体玩家
16 /
17 typedef struct{
18 char name[SIZE];
19 char passwd[SIZE];
20 int tot;
21 int vic_tot;
22 int win; //表输赢
23 }player;
24
25 //玩家结构体的指针变量
26 player usr;
27
28 /

29 * 函数功能:创建一个玩家
30 * 函数参数:无
31 * 返回值 :成功-玩家堆内存地址,失败-返回NULL
32 */
33 player *create_player(void){
34 usr = (player )calloc(1,sizeof(player));
35 if( NULL == usr)
36 return NULL;
37 strcpy(usr->name,NAME);
38 strcpy(usr->passwd,PASS);
39 return usr;
40 }
41
42
43 /

44 * 函数功能:菜单
45 * 函数参数:无
46 * 返回值 :无
47 */
48 void menu(){
49 system(“clear”);
50 printf(" 欢迎来到猜拳游戏\n");
51 printf("=============================\n\n");
52 printf(“1.石头 2。剪子 3.布 0.退出\n\n”);
53 printf(“请出拳:”);
54 }
55
56 /

57 * 函数功能:产生随机数
58 * 函数参数:随机数种子
59 * 返回值 :随机数
60 /
61 int myrand(int num){
62 srand((unsigned int)time(NULL)+num);
63 return (rand()%3+1);//(0–2)+1==1–3
64 }
65
66
67 /

68 * 函数功能:界面载入
69 * 函数参数:无
70 * 返回值 :无
71 /
72 void load(){
73 int i = 0;
74 for(i = 2;i>=0;i–){
75 system(“clear”);
76 printf(“电脑出拳中:%d”,i);
77 fflush(stdout);
78 sleep(1);
79 }
80 printf("\n");
81 }
83 /

84 * 函数功能:控制函数
85 * 函数参数:无
86 * 返回值 :无
87 /
88 void out_win(int chose,int compu){
89 if(chose == 1) printf(“你出了石头!\n”);
90 else if(chose == 2) printf(“你出了剪子!\n”);
91 else if(chose == 3) printf(“你出了布!\n”);
92
93 if(compu == 1) printf(“电脑出了石头!\n”);
94 else if(compu == 2) printf(“电脑出了剪子!\n”);
95 else if(compu == 3) printf(“电脑出了布!\n”);
96 }
97
98 /

99 * 函数功能:控制函数
100 * 函数参数:无
101 * 返回值 :无
102 /
103 void control(){
104 //玩家出的拳
105 int chose = 0;
106 //电脑出的拳
107 int compu = 0;
108 while(TRUE){
109 do{
110 menu();
111 scanf("%d",&chose);
112 //MYFLUSH;
113 }while(chose > 3 && chose < 0);
114 if(chose == 0) return;
115 usr->tot++;
116 compu = myrand(getpid());
117 load();
118 printf(“出拳详情\n”);
119 int win = chose - compu;
120 out_win(chose,compu);
121 switch(win){
122 case -1:case 2:
123 printf(“恭喜你赢了\n”);
124 usr->vic_tot++;
125 CONTINUE(“按回车继续\n”);
126 MYFLUSH;
127 break;
128 case 0:
129 printf(“和了\n”);
130 CONTINUE(“按回车继续\n”);
131 MYFLUSH;
132 break;
133 default:
134 printf(“很遗憾,你输了\n”);
135 CONTINUE(“按回车继续\n”);
136 MYFLUSH;
137 break;
138 }
139 }
140 }
141
142 /

143 * 函数功能:胜利排行榜
144 * 函数参数:无
145 * 返回值 :无
146 */
147 void vict_display(){
148 printf("\n\n 排行榜\n");
149 printf("===\n");
150 printf("%-10s %-5s %-5s %-5s\n",“姓名”,“总局数”,“胜利数”,“胜率”);
151 printf("
\n");
152 double v=0.0;
153 if(usr->tot != 0){
154 v = (double)usr->vic_tot/usr->tot;
155 }
156 printf("%-10s %-5d %-5d %-5.2lf\n",usr->name,usr->tot,usr->vic_tot,v);
157 }
158
159 int main(){
160 usr = create_player();
161 if(usr == NULL) exit(1);
162 control();
163 vict_display();
164 return 0;
165 }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值