forked from troglobit/snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcards.cpp
160 lines (131 loc) · 3.04 KB
/
cards.cpp
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
/*
** CARDS.CPP - Playing card classes
**
** public domain by Bob Stout
*/
#include <stddef.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "cards.hpp"
const char *suits[] = {"Diamonds", "Clubs", "Hearts", "Spades"};
const char *cards[] = {"Ace", "Deuce", "Trey", "4", "5", "6", "7", "8", "9",
"10", "Jack", "Queen", "King"};
inline card::card(void)
{
rank_ = Rank_Error;
suit_ = Suit_Error;
}
inline card::card(cardSuit s, cardRank r)
{
rank_ = r;
suit_ = s;
}
inline cardRank card::rank(void)
{
return rank_;
}
inline cardSuit card::suit(void)
{
return suit_;
}
inline char * card::rankText(void)
{
return (char *)cards[(int)rank_ - 1];
}
inline char * card::suitText(void)
{
return (char *)suits[(int)suit_];
}
inline void card::set_rank(cardRank r)
{
rank_ = r;
}
inline void card::set_suit(cardSuit s)
{
suit_ = s;
}
inline void card::set_card(cardSuit s, cardRank r)
{
rank_ = r;
suit_ = s;
}
inline void card::get_card(cardSuit &s, cardRank &r)
{
r = rank_;
s = suit_;
}
deck::deck(void)
{
int n = 0;
for (int s = int(Diamond); s <= int(Spade); ++s)
{
for (int c = int(Ace); c <= int(King); ++c)
{
deck::card_[n].set_rank(cardRank(c));
deck::card_[n].set_suit(cardSuit(s));
++n;
}
}
top = 0;
}
inline void deck::deal(class card &c)
{
if (top < Deck_Size)
c = deck::card_[top++];
}
inline int deck::cards_left(void)
{
return int(Deck_Size - top);
}
void deck::shuffle(void)
{
int used[Deck_Size], posn = 0;
srand((unsigned)time(NULL) | 1);
memset(used, 0, Deck_Size * sizeof(int));
for (int s = int(Diamond); s <= int(Spade); ++s)
{
for (int c = int(Ace); c <= int(King); ++c)
{
posn = (posn + rand()) % Deck_Size;
while (used[posn])
posn = ++posn % Deck_Size;
deck::card_[posn].set_rank(cardRank(c));
deck::card_[posn].set_suit(cardSuit(s));
used[posn] = -1;
}
}
top = 0;
}
#ifdef TEST
#include <iostream.h>
void showem(class deck &d);
main()
{
class deck d;
cout << "Deck initially:" << endl << endl;
showem(d);
d.shuffle();
cout << endl << "After shuffling:" << endl << endl;
showem(d);
return EXIT_SUCCESS;
}
void showem(class deck &d)
{
for (int i = 0; i <= Deck_Size; ++i)
{
class card card_;
if (0 < d.cards_left())
{
d.deal(card_);
cout << "Card #";
cout.width(2);
cout.setf(ios::left, ios::adjustfield);
cout << (i + 1) << " - " <<
card_.rankText() << " of " <<
card_.suitText() << endl;
}
else cout << "No cards left" << endl;
}
}
#endif // TEST