Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
zmx0142857
GitHub Repository: zmx0142857/mini-games
Path: blob/master/c/poker/poker.h
363 views
1
/*
2
Name: poker - 0.2
3
Author: zmx0142857
4
Description: simple card game program
5
6
Use id:
7
<back> 0
8
♥ A~K 1 ~ 13 heart
9
♦ A~K 14 ~ 26 diamond
10
♣ A~K 27 ~ 39 club
11
♠ A~K 40 ~ 52 spade
12
Joker: 53 ~ 54
13
14
Use margin:
15
16
╓──────┐
17
║A │
18
║♠ │
19
║ │
20
║ │
21
╙──────┘
22
*/
23
24
#ifndef Poker_h
25
#define Poker_h
26
27
#include <iostream>
28
#include "../codepack/random.h"
29
30
class Poker;
31
32
class Card
33
{
34
friend class Poker;
35
friend std::wostream &operator<<(std::wostream &, const Card &);
36
37
public:
38
enum Suit {NULL_SUIT=0, HEART, DIAMOND, CLUB, SPADE};
39
enum Rank {BACK=0, ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT,
40
NINE, TEN, JACK, QUEEN, KING, joker, JOKER};
41
42
// constructor, destructor
43
Card(int id=BACK, bool visible=true);
44
Card(Rank r, Suit s=HEART, bool visible=true);
45
~Card();
46
47
// operators
48
virtual bool operator==(const Card &) const;
49
bool operator!=(const Card &) const;
50
virtual bool operator<(const Card &) const;
51
bool operator>(const Card &) const;
52
bool operator<=(const Card &) const;
53
bool operator>=(const Card &) const;
54
55
// methods
56
int rank() const;
57
int suit() const;
58
bool is_facecard() const;
59
int count() const;
60
std::string abbr() const;
61
62
// test
63
static void test();
64
void dump() const;
65
66
private:
67
// functions
68
Card(const Card &); // class Card is uncopyable
69
Card &operator=(const Card &);
70
71
protected:
72
int index() const;
73
74
// data
75
int id; // constructor ensures 0 <= id <= 54
76
bool visible;
77
78
// static
79
static int cnt[55];
80
static const wchar_t img[16 * 9][11];
81
static const wchar_t img_suit[5];
82
};
83
84
//--------Constructors etc--------
85
86
Card::Card(int i, bool v): id(0 <= i && i <= 54 ? i : 0), visible(v)
87
{
88
++cnt[id];
89
}
90
91
Card::Card(Rank r, Suit s, bool v): id(0), visible(v)
92
{
93
if (r == BACK)
94
id = 0;
95
else if (r > 13)
96
id = 39 + r;
97
else
98
id = (s - HEART) * 13 + r;
99
++cnt[id];
100
}
101
102
Card::~Card()
103
{
104
--cnt[id];
105
}
106
107
//---------Operators-------------
108
109
bool Card::operator==(const Card &rhs) const
110
{
111
return id == rhs.id;
112
}
113
114
bool Card::operator!=(const Card &rhs) const
115
{
116
return !(*this == rhs);
117
}
118
119
bool Card::operator<(const Card &rhs) const
120
{
121
return rank() < rhs.rank()
122
|| (rank() == rhs.rank() && suit() < rhs.suit());
123
}
124
125
bool Card::operator>(const Card &rhs) const
126
{
127
return (rhs < *this);
128
}
129
130
bool Card::operator<=(const Card &rhs) const
131
{
132
return !(*this > rhs);
133
}
134
135
bool Card::operator>=(const Card &rhs) const
136
{
137
return !(*this < rhs);
138
}
139
140
std::wostream& operator<<(std::wostream &os, const Card &rhs)
141
{
142
const int begin = (rhs.visible ? 9 * rhs.rank() : 0);
143
for (int i = begin; i < begin + 9; ++i)
144
{
145
for (int j = 0; j < 11; ++j)
146
{
147
wchar_t tmp = Card::img[i][j];
148
os << (tmp != '$' ? tmp : rhs.img_suit[rhs.suit()]);
149
}
150
os << '\n';
151
}
152
return os;
153
}
154
155
//---------Methods--------------
156
157
int Card::rank() const
158
{
159
if (id == 0)
160
return BACK;
161
if (id == 53 || id == 54)
162
return id - 39;
163
return (id - 1) % 13 + 1;
164
}
165
166
int Card::suit() const
167
{
168
if (id == 0)
169
return NULL_SUIT;
170
if (id == 53)
171
return SPADE; // ♠
172
if (id == 54)
173
return HEART; // ♥
174
return (id - 1) / 13 + HEART;
175
}
176
177
int Card::count() const
178
{
179
return cnt[id];
180
}
181
182
std::string Card::abbr() const
183
{
184
if (!visible)
185
return "??";
186
static const char rank_abbr[] = "?A23456789TJQK";
187
static const char suit_abbr[] = "?HDCS";
188
int r = rank();
189
int s = suit();
190
if (r == BACK || s == NULL_SUIT)
191
return "??";
192
if (r == joker)
193
return "jo";
194
if (r == JOKER)
195
return "JO";
196
return std::string(1, rank_abbr[r]) + suit_abbr[s];
197
}
198
199
//--------------Test-------------------
200
201
void Card::test()
202
{
203
using namespace std;
204
cout << "---- test class Card ----\n";
205
Card c1; cout << "c1: "; c1.dump();
206
Card c2; cout << "c2: "; c2.dump();
207
cout << "c1: "; c1.dump();
208
{
209
Card c3; cout << "c3: "; c3.dump();
210
}
211
cout << "c1: "; c1.dump();
212
213
Card c4(Card::SIX, Card::HEART); cout << "c4: "; c4.dump();
214
Card c5(Card::JOKER); cout << "c5: "; c5.dump();
215
}
216
217
void Card::dump() const
218
{
219
std::cout << abbr() << " count: " << count() << '\n';
220
}
221
222
//--------Static Member------------
223
224
int Card::cnt[55] = {};
225
const wchar_t Card::img_suit[5] = {'*',L'♥',L'♦',L'♣',L'♠'};
226
const wchar_t Card::img[16*9][11] = { // 16 cards, height=9, width=11
227
228
{' ', '_', '_', '_', '_', '_', '_', '_', '_', '_', ' ' },
229
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
230
{'|', ' ', '@', '@', '@', '@', '@', '@', '@', ' ', '|' },
231
{'|', ' ', '@', '@', '@', '@', '@', '@', '@', ' ', '|' },
232
{'|', ' ', '@', '@', '@', '@', '@', '@', '@', ' ', '|' },
233
{'|', ' ', '@', '@', '@', '@', '@', '@', '@', ' ', '|' },
234
{'|', ' ', '@', '@', '@', '@', '@', '@', '@', ' ', '|' },
235
{'|', ' ', '@', '@', '@', '@', '@', '@', '@', ' ', '|' },
236
{'|', '_', '_', '_', '_', '_', '_', '_', '_', '_', '|' },
237
238
{' ', '_', '_', '_', '_', '_', '_', '_', '_', '_', ' ' },
239
{'|', 'A', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
240
{'|', '$', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
241
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
242
{'|', ' ', ' ', ' ', ' ', '$', ' ', ' ', ' ', ' ', '|' },
243
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
244
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
245
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '$', '|' },
246
{'|', '_', '_', '_', '_', '_', '_', '_', '_', 'A', '|' },
247
248
{' ', '_', '_', '_', '_', '_', '_', '_', '_', '_', ' ' },
249
{'|', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
250
{'|', '$', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
251
{'|', ' ', ' ', ' ', ' ', '$', ' ', ' ', ' ', ' ', '|' },
252
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
253
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
254
{'|', ' ', ' ', ' ', ' ', '$', ' ', ' ', ' ', ' ', '|' },
255
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '$', '|' },
256
{'|', '_', '_', '_', '_', '_', '_', '_', '_', '2', '|' },
257
258
{' ', '_', '_', '_', '_', '_', '_', '_', '_', '_', ' ' },
259
{'|', '3', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
260
{'|', '$', ' ', ' ', ' ', '$', ' ', ' ', ' ', ' ', '|' },
261
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
262
{'|', ' ', ' ', ' ', ' ', '$', ' ', ' ', ' ', ' ', '|' },
263
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
264
{'|', ' ', ' ', ' ', ' ', '$', ' ', ' ', ' ', ' ', '|' },
265
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '$', '|' },
266
{'|', '_', '_', '_', '_', '_', '_', '_', '_', '3', '|' },
267
268
{' ', '_', '_', '_', '_', '_', '_', '_', '_', '_', ' ' },
269
{'|', '4', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
270
{'|', '$', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
271
{'|', ' ', ' ', '$', ' ', ' ', ' ', '$', ' ', ' ', '|' },
272
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
273
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
274
{'|', ' ', ' ', '$', ' ', ' ', ' ', '$', ' ', ' ', '|' },
275
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '$', '|' },
276
{'|', '_', '_', '_', '_', '_', '_', '_', '_', '4', '|' },
277
278
{' ', '_', '_', '_', '_', '_', '_', '_', '_', '_', ' ' },
279
{'|', '5', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
280
{'|', '$', ' ', '$', ' ', ' ', ' ', '$', ' ', ' ', '|' },
281
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
282
{'|', ' ', ' ', ' ', ' ', '$', ' ', ' ', ' ', ' ', '|' },
283
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
284
{'|', ' ', ' ', '$', ' ', ' ', ' ', '$', ' ', ' ', '|' },
285
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '$', '|' },
286
{'|', '_', '_', '_', '_', '_', '_', '_', '_', '5', '|' },
287
288
{' ', '_', '_', '_', '_', '_', '_', '_', '_', '_', ' ' },
289
{'|', '6', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
290
{'|', '$', ' ', '$', ' ', ' ', ' ', '$', ' ', ' ', '|' },
291
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
292
{'|', ' ', ' ', '$', ' ', ' ', ' ', '$', ' ', ' ', '|' },
293
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
294
{'|', ' ', ' ', '$', ' ', ' ', ' ', '$', ' ', ' ', '|' },
295
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '$', '|' },
296
{'|', '_', '_', '_', '_', '_', '_', '_', '_', '6', '|' },
297
298
{' ', '_', '_', '_', '_', '_', '_', '_', '_', '_', ' ' },
299
{'|', '7', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
300
{'|', '$', ' ', '$', ' ', ' ', ' ', '$', ' ', ' ', '|' },
301
{'|', ' ', ' ', ' ', ' ', '$', ' ', ' ', ' ', ' ', '|' },
302
{'|', ' ', ' ', '$', ' ', ' ', ' ', '$', ' ', ' ', '|' },
303
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
304
{'|', ' ', ' ', '$', ' ', ' ', ' ', '$', ' ', ' ', '|' },
305
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '$', '|' },
306
{'|', '_', '_', '_', '_', '_', '_', '_', '_', '7', '|' },
307
308
{' ', '_', '_', '_', '_', '_', '_', '_', '_', '_', ' ' },
309
{'|', '8', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
310
{'|', '$', ' ', '$', ' ', ' ', ' ', '$', ' ', ' ', '|' },
311
{'|', ' ', ' ', ' ', ' ', '$', ' ', ' ', ' ', ' ', '|' },
312
{'|', ' ', ' ', '$', ' ', ' ', ' ', '$', ' ', ' ', '|' },
313
{'|', ' ', ' ', ' ', ' ', '$', ' ', ' ', ' ', ' ', '|' },
314
{'|', ' ', ' ', '$', ' ', ' ', ' ', '$', ' ', ' ', '|' },
315
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '$', '|' },
316
{'|', '_', '_', '_', '_', '_', '_', '_', '_', '8', '|' },
317
318
{' ', '_', '_', '_', '_', '_', '_', '_', '_', '_', ' ' },
319
{'|', '9', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
320
{'|', '$', ' ', '$', ' ', ' ', ' ', '$', ' ', ' ', '|' },
321
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
322
{'|', ' ', ' ', '$', ' ', ' ', ' ', '$', ' ', ' ', '|' },
323
{'|', ' ', ' ', ' ', ' ', '$', ' ', ' ', ' ', ' ', '|' },
324
{'|', ' ', ' ', '$', ' ', ' ', ' ', '$', ' ', ' ', '|' },
325
{'|', ' ', ' ', '$', ' ', ' ', ' ', '$', ' ', '$', '|' },
326
{'|', '_', '_', '_', '_', '_', '_', '_', '_', '9', '|' },
327
328
{' ', '_', '_', '_', '_', '_', '_', '_', '_', '_', ' ' },
329
{'|', '1', '0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
330
{'|', '$', ' ', '$', ' ', ' ', ' ', '$', ' ', ' ', '|' },
331
{'|', ' ', ' ', ' ', ' ', '$', ' ', ' ', ' ', ' ', '|' },
332
{'|', ' ', ' ', '$', ' ', ' ', ' ', '$', ' ', ' ', '|' },
333
{'|', ' ', ' ', '$', ' ', ' ', ' ', '$', ' ', ' ', '|' },
334
{'|', ' ', ' ', ' ', ' ', '$', ' ', ' ', ' ', ' ', '|' },
335
{'|', ' ', ' ', '$', ' ', ' ', ' ', '$', ' ', '$', '|' },
336
{'|', '_', '_', '_', '_', '_', '_', '_', '1', '0', '|' },
337
338
{' ', '_', '_', '_', '_', '_', '_', '_', '_', '_', ' ' },
339
{'|', 'J', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
340
{'|', '$', ' ', '|', ' ', '+', '+', ' ', ' ', ' ', '|' },
341
{'|', ' ', ' ', '|', '(', '\"',',', ')', ' ', ' ', '|' },
342
{'|', ' ', ' ', 'C', '/', ' ', ' ', '\\','7', ' ', '|' },
343
{'|', ' ', ' ', '/', '_', '_', '_', '_', '\\',' ', '|' },
344
{'|', ' ', ' ', '|', '|', ' ', ' ', '|', ' ', ' ', '|' },
345
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '$', '|' },
346
{'|', '_', '_', '_', '_', '_', '_', '_', '_', 'J', '|' },
347
348
{' ', '_', '_', '_', '_', '_', '_', '_', '_', '_', ' ' },
349
{'|', 'Q', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
350
{'|', '$', ' ', ' ', ' ', '@', '@', '@', ' ', ' ', '|' },
351
{'|', ' ', ' ', ' ', '(', '"', ',', '@', '@', ' ', '|' },
352
{'|', ' ', ' ', '*', '/', ' ', ' ', '\\','7', ' ', '|' },
353
{'|', ' ', ' ', '/', '_', '_', '_', '_', '\\',' ', '|' },
354
{'|', ' ', ' ', ' ', '|', ' ', ' ', '|', ' ', ' ', '|' },
355
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '$', '|' },
356
{'|', '_', '_', '_', '_', '_', '_', '_', '_', 'Q', '|' },
357
358
{' ', '_', '_', '_', '_', '_', '_', '_', '_', '_', ' ' },
359
{'|', 'K', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
360
{'|', '$', ' ', '|', ' ', 'w', 'w', ' ', ' ', ' ', '|' },
361
{'|', ' ', ' ', '|', '(', '"', ',', ')', ' ', ' ', '|' },
362
{'|', ' ', ' ', 'T', '/', ' ', ' ', '\\','7', ' ', '|' },
363
{'|', ' ', ' ', '/', '_', '_', '_', '_', '\\',' ', '|' },
364
{'|', ' ', ' ', ' ', '|', ' ', ' ', '|', ' ', ' ', '|' },
365
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '$', '|' },
366
{'|', '_', '_', '_', '_', '_', '_', '_', '_', 'K', '|' },
367
368
{' ', '_', '_', '_', '_', '_', '_', '_', '_', '_', ' ' },
369
{'|', 'J', 'o', 'k', 'e', 'r', ' ', ' ', ' ', ' ', '|' },
370
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
371
{'|', ' ', ' ', ' ', '\\','A', '/', ' ', ' ', ' ', '|' },
372
{'|', ' ', ' ', '_', '(', '&', ')', '$', ' ', ' ', '|' },
373
{'|', ' ', ' ', ' ', 'V', 'I', 'V', ' ', ' ', ' ', '|' },
374
{'|', ' ', ' ', ' ', '/', ' ', '\\',' ', ' ', ' ', '|' },
375
{'|', ' ', ' ', 'J', ' ', ' ', ' ', 'L', ' ', ' ', '|' },
376
{'|', '_', '_', '_', '_', '_', '_', '_', '_', '_', '|' },
377
378
{' ', '_', '_', '_', '_', '_', '_', '_', '_', '_', ' ' },
379
{'|', 'J', 'o', 'k', 'e', 'r', ' ', ' ', ' ', ' ', '|' },
380
{'|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
381
{'|', ' ', ' ', ' ', '\\','A', '/', ' ', ' ', ' ', '|' },
382
{'|', ' ', ' ', '_', '(', '&', ')', '$', ' ', ' ', '|' },
383
{'|', ' ', ' ', ' ', 'V', 'I', 'V', ' ', ' ', ' ', '|' },
384
{'|', ' ', ' ', ' ', '/', ' ', '\\',' ', ' ', ' ', '|' },
385
{'|', ' ', ' ', 'J', ' ', ' ', ' ', 'L', ' ', ' ', '|' },
386
{'|', '_', '_', '_', '_', '_', '_', '_', '_', '_', '|' },
387
388
};
389
390
class Deck
391
{
392
friend std::ostream &operator<<(std::ostream &, const Deck &);
393
public:
394
Deck(unsigned decks=1);
395
void shuffle() const;
396
static void test();
397
398
private:
399
Card **card; // array of pointers
400
const int SIZE;
401
};
402
403
//--------Constructor & Destructor----------
404
405
Deck::Deck(unsigned decks): SIZE(54 * decks)
406
{
407
card = new Card*[SIZE];
408
for (int i = 0; i < SIZE; ++i)
409
card[i] = new Card(i % 54 + 1);
410
}
411
412
//----------Method-----------
413
414
void Deck::shuffle() const
415
{
416
for (int i = 0; i < SIZE; ++i)
417
{
418
int j = Random::integer(SIZE);
419
std::swap(card[i], card[j]);
420
}
421
}
422
423
//----------Friend-----------
424
425
std::ostream &operator<<(std::ostream &os, const Deck &rhs)
426
{
427
for (int i = 0; i < rhs.SIZE; ++i)
428
os << rhs.card[i]->abbr() << (i % 13 == 12 ? "\n" : " ");
429
return os;
430
}
431
432
void Deck::test()
433
{
434
using namespace std;
435
cout << "---- test class Deck ----\n";
436
Deck deck;
437
cout << deck << endl;
438
deck.shuffle();
439
cout << deck << endl;
440
deck.shuffle();
441
cout << deck << endl;
442
}
443
444
#endif // Poker_h
445
446