给一手扑克牌分类

更新时间:2023-08-02 19:52:38 阅读: 评论:0

给⼀⼿扑克牌分类
最近看了《C语⾔程序设计程序现代⽅法》,⾥⾯⼀个问题挺有意思的。原本⾃⼰写了⼀个,但结构混乱,⽰例程序条理清晰,全局变量⽤的挺好,故⼜按照书上的程序敲了⼀遍。
每次读取⼀⼿五张牌,然后根据下列类别把⼿中的牌分类(列出的顺序依次是从最好类别到最坏类别):
Straight flush: 同花顺的牌(即顺序相连⼜都是同花⾊)
Four-of-a-kind: 四张相同的牌(四张牌级别相同)
Full hou: 三张花⾊相同和两张花⾊相同的牌(三张牌是同样的花⾊,⽽另外两张牌是同样的花⾊)
Flush: 同花⾊的牌(五张牌是同花⾊的)
Straight: 同顺序的牌(五张牌的级别顺序相连)
Three-of-a-kind: 三张相同的牌(三张牌级别相同)
Two pairs: 两对⼦
Pair: ⼀对(两张牌级别相同)
High card: 其它牌(任何其它情况的牌)
代码如下:
1 #include <stdbool.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4
5#define NUM_RANKS 13 //13种牌型
6#define NUM_SUITS 4
7#define NUM_CARDS 5  //发5张牌
8
9int num_in_rank[NUM_RANKS];
10int num_in_suit[NUM_SUITS];
11bool straight,flush,four,three;
12int pairs;
13
14void read_cards();
15void analyze_hand();
16void print_result();
17
清炒马蹄18int main()实习接收函
19 {
20for(;;)
21    {
22        read_cards();
23        analyze_hand();
24        print_result();
25    }
26return0;
27 }
28
29void read_cards()
30 {
31bool card_exists[NUM_RANKS][NUM_SUITS];
32char ch,rank_ch,suit_ch;
33int rank,suit;
34bool bad_card;
35int cards_read = 0;
36for(rank = 0; rank < NUM_RANKS; rank++)
37    {
38        num_in_rank[rank] = 0;
39for(suit = 0; suit < NUM_SUITS; suit++)
40        {
41            card_exists[rank][suit] = fal;
42        }
43    }
44
烤箱烤肠45for(suit = 0; suit <NUM_SUITS; suit++)
46    {
47        num_in_suit[suit] = 0;
48    }
49
50while(cards_read <NUM_CARDS)
51    {
52        bad_card = fal;
53
54          printf("Enter %d piece of card\n",cards_read+1);
55        rank_ch = getchar();
56switch(rank_ch)
57        {
58ca'0':
59            exit(EXIT_SUCCESS);
60ca'2':
61            rank = 0;
62break;
63ca'3':
64            rank = 1;
65break;
66ca'4':
67            rank = 2;
68break;
69ca'5':
70            rank = 3;
71break;
72ca'6':
73            rank = 4;
74break;
75ca'7':
76            rank = 5;
77break;
78ca'8':
79            rank = 6;
80break;
81ca'9':
82            rank = 7;
83break;
84ca't':
85ca'T':
86            rank = 8;dl是什么意思
87break;
88ca'j':
89ca'J':
90            rank = 9;
91break;
92ca'q':
93ca'Q':
94            rank = 10;
95break;
96ca'k':
97ca'K':
98            rank = 11;
99break;
100ca'a':
101ca'A':
102            rank = 12;
103break;
104default:
105            bad_card = true;
106        }
107
108        suit_ch = getchar();
109switch(suit_ch)
110        {
111ca'c':
112ca'C':
113            suit = 0;
114break;
115ca'd':
116ca'D':
117            suit = 0;
118break;
119ca'h':
120ca'H':
121            suit = 0;
122break;
123ca's':
124ca'S':
125            suit = 0;
126break;
127default:
128            bad_card = true;
129        }
130
131while((ch = getchar())!='\n')
132if(ch!='')
133                bad_card = true;
134
135if(bad_card)
136            printf("Bad card;Ignored.\n");
137el if(card_exists[rank][suit])
138        {
139            printf("Duplicated card;Ignored.\n"); 140        }
141el
142        {
143            num_in_rank[rank]++;
144            num_in_suit[suit]++;
145            card_exists[rank][suit] = true;
146            cards_read++;
147        }
148    }
149 }
150
151void analyze_hand()
152 {
153int num_conc = 0;
154int rank,suit;
155    straight = fal;
156    flush = fal;
157    four = fal;
158    three = fal;
159    pairs = 0;
160for(suit = 0; suit < NUM_SUITS; suit++)
161    {
162if(num_in_suit[suit] == NUM_CARDS)
163            flush = true;
164    }
165    rank = 0;
166while(num_in_rank[rank] == 0)
167        rank++;
168for(; rank < NUM_RANKS &&num_in_rank[rank] > 0 ; rank++) 169        num_conc++;
170if(num_conc == NUM_CARDS)
171    {
172        straight = true;
173return;
174    }
175
176for(rank = 0; rank <NUM_RANKS; rank++)
177    {
178if(num_in_rank[rank] == 4)
179            four = true;
180if(num_in_rank[rank] == 3)
181            three = true;
182if(num_in_rank[rank] == 2)
183            pairs++;
诗人于谦184    }
185 }
186
职业责任
猪血丸子的做法187void print_result()
188 {
189if(straight && flush)
190        printf("Straight flush\n");
191el if (four)
192        printf("Four of a kind\n");
193el if(three && pairs ==1)
194        printf("Full hou\n");
195el if(flush)
196        printf("Flush\n");
197el if(straight)
198        printf("Straight\n");
199el if(three)
200        printf("Three of a kind\n");
201el if(pairs == 2)
202        printf("Two pairs\n");
203el if(pairs ==1)
204        printf("Pair\n");
205el
206        printf("High card\n");
207    printf("\n");科技制作小发明
208 }

本文发布于:2023-08-02 19:52:38,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1126985.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:顺序   类别   程序   分类
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图