C语言实现自动发牌程序

时间:2022-12-07 10:00:45

C语言自动发牌程序,供大家参考,具体内容如下

一副扑克有52张牌,打桥牌时应将牌分给4个人。请设计一个程序完成自动发牌的工作。要求:黑桃用S (Spaces)表示,红桃用H (Hearts)表示,方块用D (Diamonds)表示,梅花用C (Clubs)表示。

分析:

要设置数组表现扑克牌
要设置数组表现玩家
要给扑克牌做特定标识,得到结果后玩家要知道自己手中黑桃有哪些、方块有哪些

初步想法:

设置4个字符数组保存4种梅花牌,设置4个字符数组表示4名玩家分配到的牌
每张牌随机发给4名玩家,当玩家的持牌数达到13,不再分配给该名玩家牌

代码展示:

?
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
void mycode_13()
{
 srand(unsigned(time(NULL)));
 /*全部牌*/
 char S[13] = { '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A' };
 char H[13] = { '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A' };
 char D[13] = { '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A' };
 char C[13] = { '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A' };
 
 /*4个玩家*/
 char player1[13], player2[13], player3[13], player4[13];
 int p1 = 0, p2 = 0, p3 = 0, p4 = 0;
 
 distribution(S, player1, player2, player3, player4, &p1, &p2, &p3, &p4);
 distribution(H, player1, player2, player3, player4, &p1, &p2, &p3, &p4);
 distribution(D, player1, player2, player3, player4, &p1, &p2, &p3, &p4);
 distribution(C, player1, player2, player3, player4, &p1, &p2, &p3, &p4);
 
 puts("运行结束");
 
 for (int i = 0; i < 13; i++)
 printf("%c ", player1[i]);
 putchar('\n');
 for (int i = 0; i < 13; i++)
 printf("%c ", player2[i]);
 putchar('\n');
 for (int i = 0; i < 13; i++)
 printf("%c ", player3[i]);
 putchar('\n');
 for (int i = 0; i < 13; i++)
 printf("%c ", player4[i]);
}
 
void distribution(char * S_H_D_C, char * player1, char * player2, char * player3, char * player4, int *p1, int *p2, int *p3, int *p4)
{
 static int h = 1;
 int r;
 int a = *p1, b = *p2, c = *p3, d = *p4;
 
 for (int i = 0; i < 13; i++)
 {
 r = (rand() % 4) + 1;
 while ((r == 1 && (*p1) == 13) || (r == 2 && (*p2) == 13) || (r == 3 && (*p3) == 13) || (r == 4 && (*p4) == 13))
  r = (rand() % 4) + 1;
 
 switch (r)
 {
 case 1:
  player1[(*p1)++] = S_H_D_C[i];
  break;
 case 2:
  player2[(*p2)++] = S_H_D_C[i];
  break;
 case 3:
  player3[(*p3)++] = S_H_D_C[i];
  break;
 case 4:
  player4[(*p4)++] = S_H_D_C[i];
  break;
 default:
  break;
 }
 }
 
 switch (h++)
 {
 case 1:
  printf("黑桃:\n");
  break;
 case 2:
  printf("红桃:\n");
  break;
 case 3:
  printf("方块:\n");
  break;
 case 4:
  printf("梅花:\n");
  break;
 }
 
 printf("Player1:");
 for (int i = a; i < (*p1); i++)
  printf("%c ", player1[i]);
 
 putchar('\n');
 
 printf("Player2:");
 for (int i = b; i < (*p2); i++)
  printf("%c ", player2[i]);
 
 putchar('\n');
 printf("Player3:");
 for (int i = c; i < (*p3); i++)
  printf("%c ", player3[i]);
 
 putchar('\n');
 
 printf("Player4:");
 for (int i = d; i < (*p4); i++)
  printf("%c ", player4[i]);
 
 putchar('\n');
}

以下代码保证了当某个人得到13张牌后不在得牌

?
1
2
3
r = (rand() % 4) + 1;
 while ((r == 1 && (*p1) == 13) || (r == 2 && (*p2) == 13) || (r == 3 && (*p3) == 13) || (r == 4 && (*p4) == 13))
  r = (rand() % 4) + 1;

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

原文链接:https://blog.csdn.net/weixin_43063069/article/details/90293134