C语言实现小猫钓鱼游戏

时间:2021-07-11 13:04:13

本文实例为大家分享了C语言实现小猫钓鱼游戏的具体代码,供大家参考,具体内容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#include<stdio.h>
#include<time.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
 
 
typedef struct
{
 int data[3600];
 int col[3600];
 int top;
} stack;
 
typedef struct
{
 int data[3600];
 int col[3600];
 int head;
 int tail;
} queue;
 
queue p1, p2;
stack table;
 
void printmenu();
void deal();
void printp1();
void printtable();
void printzzz();
void judge(int a, int player);
void game();
void ask();
 
int main()
{
 while(1)
 {
  char ch;
  printmenu();
  scanf("%c",&ch);
  switch(ch)
  {
  case '1':
   game();
   break;
 
  case '2':
   exit(0);
 
  default:
   system("cls");
   break;
  }
 }
 
 return 0;
}
 
void printmenu()
{
 printf("*****************************\n");
 printf("*       *\n");
 printf("*       *\n");
 printf("*   金钩钓鱼   *\n");
 printf("*       *\n");
 printf("*       *\n");
 printf("*       *\n");
 printf("*****************************\n");
 printf("\n\n\n");
 printf("游戏规则:\n");
 printf("每人各拿一半的牌,然后你放1张牌,\n");
 printf("他放一张牌,如跟上面的牌一样,\n");
 printf("即可把当中的牌全部吃掉,\n");
 printf("直至对手手上无牌为止。\n\n\n");
 printf("输入1开始游戏\n");
 printf("输入2退出程序\n");
}
 
void deal()
{
 p1.head = 1;
 p1.tail = 1;
 p2.head = 1;
 p2.tail = 1;
 table.top = 0;
 int i, card, book[60];
 srand((unsigned)time(0));
 memset(book, 0, sizeof(book));
 for(i = 1; i <= 27;)
 {
  card = rand()%55;
  if(card == 0)
   continue;
  else if(card >= 1 && card <= 13 && book[card] == 0)
  {
   p1.data[p1.tail] = card;
   p1.col[p1.tail] = 1;
   p1.tail++;
   book[card]=1;
   i++;
  }
  else if(card >= 14 && card <= 26 && book[card] == 0)
  {
   p1.data[p1.tail] = card - 13;
   p1.col[p1.tail] = 2;
   p1.tail++;
   book[card]=1;
   i++;
  }
  else if(card >= 27 && card <= 39 && book[card] == 0)
  {
   p1.data[p1.tail] = card - 26;
   p1.col[p1.tail] = 3;
   p1.tail++;
   book[card]=1;
   i++;
  }
  else if(card >= 40 && card <= 52 && book[card] == 0)
  {
   p1.data[p1.tail] = card - 39;
   p1.col[p1.tail] = 4;
   p1.tail++;
   book[card]=1;
   i++;
  }
  else if(card == 53)
  {
   p1.data[p1.tail] = card;
   p1.col[p1.tail] = 5;
   p1.tail++;
   book[card]=1;
   i++;
  }
  else if(card == 54)
  {
   p1.data[p1.tail] = card;
   p1.col[p1.tail] = 5;
   p1.tail++;
   book[card]=1;
   i++;
  }
 }
 for(i = 1; i <= 27;)
 {
  card = rand()%55;
  if(card == 0)
   continue;
  else if(card >= 1 && card <= 13 && book[card] == 0)
  {
   p2.data[p2.tail] = card;
   p2.col[p2.tail] = 1;
   p2.tail++;
   book[card]=1;
   i++;
  }
  else if(card >= 14 && card <= 26 && book[card] == 0)
  {
   p2.data[p2.tail] = card - 13;
   p2.col[p2.tail] = 2;
   p2.tail++;
   book[card]=1;
   i++;
  }
  else if(card >= 27 && card <= 39 && book[card] == 0)
  {
   p2.data[p2.tail] = card - 26;
   p2.col[p2.tail] = 3;
   p2.tail++;
   book[card]=1;
   i++;
  }
  else if(card >= 40 && card <= 52 && book[card] == 0)
  {
   p2.data[p2.tail] = card - 39;
   p2.col[p2.tail] = 4;
   p2.tail++;
   book[card]=1;
   i++;
  }
  else if(card == 53 && book[card] == 0)
  {
   p2.data[p2.tail] = card;
   p2.col[p2.tail] = 5;
   p2.tail++;
   book[card]=1;
   i++;
  }
  else if(card == 54 && book[card] == 0)
  {
   p2.data[p2.tail] = card;
   p2.col[p2.tail] = 5;
   p2.tail++;
   book[card]=1;
   i++;
  }
 }
 
 return;
}
 
void printp1()
{
 int i;
 printf("您的牌有: \n");
 for(i = p1.tail-1; i >= p1.head; i--)
 {
  if(p1.col[i] == 1)
  {
   printf("方片");
  }
  else if(p1.col[i] == 2)
  {
   printf("红桃");
  }
  else if(p1.col[i] == 3)
  {
   printf("黑桃");
  }
  else if(p1.col[i] == 4)
  {
   printf("梅花");
  }
  if(p1.data[i] == 11)
  {
   printf("J ");
  }
  else if(p1.data[i] == 12)
  {
   printf("Q ");
  }
  else if(p1.data[i] == 13)
  {
   printf("K ");
  }
  else if(p1.data[i] == 53)
  {
   printf("小王 ");
  }
  else if(p1.data[i] == 54)
  {
   printf("大王 ");
  }
  else
   printf("%d ", p1.data[i]);
 }
 printf("\n\n");
 return;
}
 
void printtable()
{
 int i;
 printf("桌上的牌有:\n");
 if(table.top == 0)
 {
  printf("(无)\n");
 }
 for(i = 1; i <= table.top; i++)
 {
  if(table.col[i] == 1)
  {
   printf("方片");
  }
  else if(table.col[i] == 2)
  {
   printf("红桃");
  }
  else if(table.col[i] == 3)
  {
   printf("黑桃");
  }
  else if(table.col[i] == 4)
  {
   printf("梅花");
  }
  if(table.data[i] == 11)
  {
   printf("J ");
  }
  else if(table.data[i] == 12)
  {
   printf("Q ");
  }
  else if(table.data[i] == 13)
  {
   printf("K ");
  }
  else if(table.data[i] == 53)
  {
   printf("小王 ");
  }
  else if(table.data[i] == 54)
  {
   printf("大王 ");
  }
  else
   printf("%d ", table.data[i]);
 }
 printf("\n\n\n");
 return;
}
 
void printzzz()
{
 system("cls");
 Sleep(400);
 printf("正 ");
 Sleep(400);
 printf("在 ");
 Sleep(400);
 printf("出 ");
 Sleep(400);
 printf("牌 ");
 Sleep(400);
 printf(". ");
 Sleep(400);
 printf(". ");
 Sleep(400);
 printf(". ");
 Sleep(800);
 judge(table.data[table.top], 1);
 system("cls");
 printf("玩家2 开始回合!!\n\n\n\n");
 printtable();
 table.top++;
 table.data[table.top] = p2.data[p2.head];
 table.col[table.top] = p2.col[p2.head];
 p2.head++;
 Sleep(600);
 printf("玩 ");
 Sleep(400);
 printf("家 ");
 Sleep(400);
 printf("2 ");
 Sleep(400);
 printf("正 ");
 Sleep(400);
 printf("在 ");
 Sleep(400);
 printf("出 ");
 Sleep(400);
 printf("牌 ");
 Sleep(400);
 printf(". ");
 Sleep(400);
 printf(". ");
 Sleep(400);
 printf(". ");
 Sleep(1000);
 judge(table.data[table.top], 2);
 return;
}
 
void judge(int a, int player)
{
 int i, j, sum = 2;
 if(player == 1)
 {
  for(i = table.top-1; i >= 1; i--, sum++)
  {
   if(a == table.data[i] || ((a == 53 || a == 54) && (table.data[i] == 53 || table.data[i] == 54)))
   {
    for(j = table.top; j >= i; j--)
    {
     p1.data[p1.tail] = table.data[table.top];
     p1.col[p1.tail] = table.col[table.top];
     p1.tail++;
     table.top--;
    }
    break;
   }
  }
 
  if(i != 0)
  {
   system("cls");
   Sleep(400);
   printf("玩 ");
   Sleep(400);
   printf("家 ");
   Sleep(400);
   printf("1 ");
   Sleep(400);
   printf("赢 ");
   Sleep(400);
   printf("了 ");
   Sleep(400);
   printf("%d ", sum);
   Sleep(400);
   printf("张");
   Sleep(400);
   printf("牌 ");
   Sleep(400);
   printf("! ");
   Sleep(400);
   printf("! ");
   Sleep(400);
   printf("! ");
   Sleep(2000);
   system("cls");
  }
 }
 else if(player == 2)
 {
  for(i = table.top-1; i >= 1; i--, sum++)
  {
   if(a == table.data[i] || ((a == 53 || a == 54) && (table.data[i] == 53 || table.data[i] == 54)))
   {
    for(j = table.top; j >= i; j--)
    {
     p2.data[p2.tail] = table.data[table.top];
     p2.col[p2.tail] = table.col[table.top];
     p2.tail++;
     table.top--;
    }
    break;
   }
  }
 
  if(i != 0)
  {
   system("cls");
   Sleep(400);
   printf("玩 ");
   Sleep(400);
   printf("家 ");
   Sleep(400);
   printf("2 ");
   Sleep(400);
   printf("赢 ");
   Sleep(400);
   printf("了 ");
   Sleep(400);
   printf("%d ", sum);
   Sleep(400);
   printf("张");
   Sleep(400);
   printf("牌 ");
   Sleep(400);
   printf("! ");
   Sleep(400);
   printf("! ");
   Sleep(400);
   printf("! ");
   Sleep(2000);
   system("cls");
  }
 }
 return;
}
 
void game()
{
 deal();
 getchar();
 
 while(p1.head != p1.tail && p2.head != p2.tail)
 {
  system("cls");
  int i;
  printf("玩家1 开始回合!!\n\n\n\n");
  printp1();
  printtable();
  printf("按回车来出牌\n");
  getchar();
  table.top++;
  table.data[table.top] = p1.data[p1.head];
  table.col[table.top] = p1.col[p1.head];
  p1.head++;
  printzzz();
 }
 
 if(p1.head == p1.tail)
 {
  printf("恭喜您取得了胜利!!!\n");
  printf("感谢使用本程序\n");
  system("pause");
 }
 else
 {
  printf("你没有牌了!");
  printf("不好意思,您输了。。。\n");
  printf("不要灰心,可以再来一局!!\n");
  system("pause");
 }
 return;
}
 
void ask()
{
 char ch;
 
 system("cls");
 printf("你确定要退出?\n");
 printf("(输入y确认对出)\n");
 printf("Y/N\n");
 scanf("%c", &ch);
 
 if(ch == 'y' || ch == 'Y')
 {
  exit(0);
 }
 else
 {
  return;
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_43038891/article/details/85227580