本文实例为大家分享了C语言实现随机发扑克牌的具体代码,供大家参考,具体内容如下
算法如下:
1、将1-10作为方块、2-20作为梅花,3-30作为红桃,4-40作为黑桃,JQK以及大小王分别排序共54张存放在一维数组中。
3、3个人用一个二维数组来存放三人的牌。
2、主要分为打散(乱序),分配,显示三个操作
打散即洗牌:通过rand以及srand函数来获得,为了避免相同的序列,使用标准时间来作为序列种子。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
void shuffle( int *cards, int lenth) { int temp, i, index; time_t t; srand ((unsigned int )(&t)); for (i = 0; i < lenth - 1; i++) //保证每一次交换都是和剩下的数进行交换 { index = rand () % (lenth - i) + i; if (index != i) { temp = cards[i]; cards[i] = cards[index]; cards[index] = temp; } } } |
分配:将乱序的卡组分别依次排入一个二维数组中
1
2
3
4
5
6
7
8
9
10
11
12
|
void deal( int (*everycards)[18], int * cards) { int i, j; for (i = 0; i < 3; i++) { for (j = 0; j < 18; j++) { everycards[i][j] = cards[i * 18 + j]; } } } |
显示:数字卡用迭代的方法显示,而JQK则用switch方法实现
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
|
void display( int (*everycards)[18], char *name1, char *name2, char *name3) { int i, j; //方块、梅花、红桃、黑桃 putchar ( '\n' ); printf ( "分配卡牌如下:\n" ); for (i = 0; i < 3; i++) { switch (i) { case 0: printf ( "%s手上的牌是: " , name1); break ; case 1: printf ( "%s手上的牌是: " , name2); break ; case 2: printf ( "%s手上的牌是: " , name3); break ; } for (j = 0; j < 18; j++) { if (everycards[i][j] >= 1 && everycards[i][j] <= 10) { printf ( "方%d " , everycards[i][j]); } else if (everycards[i][j] >= 11 && everycards[i][j] <= 20) { printf ( "梅%d " , everycards[i][j] - 10); } else if (everycards[i][j] >= 21 && everycards[i][j] <= 30) { printf ( "红%d " , everycards[i][j] - 20); } else if (everycards[i][j] >= 31 && everycards[i][j] <= 40) { printf ( "黑%d " , everycards[i][j] - 30); } else { switch (everycards[i][j]) { case 41: printf ( "方J " ); break ; case 42: printf ( "方Q " ); break ; case 43: printf ( "方K " ); break ; case 44: printf ( "梅J " ); break ; case 45: printf ( "梅Q " ); break ; case 46: printf ( "梅K " ); break ; case 47: printf ( "红J " ); break ; case 48: printf ( "红Q " ); break ; case 49: printf ( "红K " ); break ; case 50: printf ( "黑J " ); break ; case 51: printf ( "黑Q " ); break ; case 52: printf ( "黑K " ); break ; case 53: printf ( "小王 " ); break ; case 54: printf ( "大王 " ); break ; } } } putchar ( '\n' ); putchar ( '\n' ); } } |
具体实现如下
主函数下方所示(由于是vs2019所以scanf用的是微软自带的scanf_s):
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
|
int main( void ) { char name1[MAX], name2[MAX], name3[MAX]; int cards[54], everycards[3][18], lenth, i; char YorN; //初始化扑克牌 for ( int count1 = 0; count1 < 54; count1++) { cards[count1] = count1 + 1; } lenth = sizeof (cards) / sizeof (cards[0]); printf ( "请输入1号玩家的名字:" ); scanf_s( "%s" , name1, MAX); int b1 = getchar (); printf ( "请输入2号玩家的名字:" ); scanf_s( "%s" , name2, MAX); int b2 = getchar (); printf ( "请输入3号玩家的名字:" ); scanf_s( "%s" , name3, MAX); int b3 = getchar (); putchar ( '\n' ); printf ( "方块 = 方,梅花 = 梅,红桃 = 红, 黑桃 = 黑\n" ); //先做一次洗牌 shuffle(cards, lenth); deal(everycards, cards); display(everycards, name1, name2, name3); //寻问是否进行再次洗牌 while (1) { printf ( "\n请选择是否重新洗牌(Y/N):" ); scanf_s( "%c" , &YorN); int b4 = getchar (); // if (YorN == 'Y' ) { shuffle(cards, lenth); deal(everycards, cards); display(everycards, name1, name2, name3); } else if (YorN == 'N' ) { break ; } } return 0; } |
学生党,初学习编程,第一次发博客,望各位大佬指正错误和不足。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/o_Charlie_o/article/details/105505623