-
Notifications
You must be signed in to change notification settings - Fork 3
/
poker.cpp
241 lines (228 loc) · 8.82 KB
/
poker.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//Author: PlanckBit
//MIT License
//Copyright (c) 2019 PlanckBit
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <time.h>
#include "cards.h"
#include "classify.h"
//Used for classify the hand
void classify_hand(classify, int [], unsigned int [], int, unsigned int);
//Displays the classification of a hand when it first occurs
void display(classify poker_hand, unsigned int);
int main() {
deckOfCards deck; //create my object deck and invoke constructor
classify poker_hand; //object created for class classify
deck.shuffle(); //call member function and shuffle cards
srand((unsigned) time(NULL)); //this will produce a different sequence of
// of random cards everytime the program
// is executed
char enter;
int test_deck; //this will store the return value of deck.deckEmpty()
std::cout << std::endl << std::endl << std::endl;
std::cout << " " << "****************************************" << std::endl
<< " " << "* THE POKER GAME RANDOM GENERATOR WILL *" << std::endl
<< " " << "* GENERATE 50000 POKER HANDS, AND ONLY *" << std::endl
<< " " << "* DISPLAY THE FIRST CLASSIFICATION OF *" << std::endl
<< " " << "* OF EACH HAND AT THE TIME IT OCCURS. *" << std::endl
<< " " << "* IT WILL THEN DISPLAY HOW MANY TIMES *" << std::endl
<< " " << "* EACH CLASSIFICATION OCCUR. *" << std::endl
<< " " << "****************************************" << std::endl << std::endl << std::endl;
std::cout << " " << "TO BEGIN THE RANDOM POKER GAME GENERATOR HIT ANY KEY " << std::endl
<< " " << " AND THEN ENTER OR HIT CTRL Z: ";
while(std::cin >> enter) {
std::cout << std::endl << std::endl;
const int number=9; //use for the number of classifications
unsigned int counter=0; //use for finding out the first time a classification hand occurs
int found[number]={0}; //use to determine first time a classification
// happens, and when it happens the element is set to
// 1, which means it can not be printed out again.
unsigned int occur[number]={0}; //this will be use to keep count of all the different
// types of classifications for each hand.
//Geenerate the number of random hands
for(unsigned int d=0; d<50000; d++){
//Will generate one card at a time, for a total of 5 cards per hand
for(int i=0; i<5; i++) {
test_deck = deck.deckEmpty(); //testing to make sure deck not empty
//if this is true then my deck is empty. so we replace all 52 cards
if(test_deck==1) {
deck.shuffle();
}
playingCard card=deck.deal(); //initialize constructor, by dealing a card
//add a card to the hand as it is dealt
poker_hand.insert_card(card.theRank(), card.theSuit(), i);
}
poker_hand.sort_hand(); //sort my poker hand by calling memeber funtion
++counter; //increment my counter, to indicate first hand
//Classify each hand
classify_hand(poker_hand, found, occur, number, counter);
}
std::cout << std::endl << std::endl;
//Print out how many occurrance of classifications
// occur for each hand
for(int k=0; k<9; k++) {
std::cout << std::endl
<< " ";
std::cout << std::setw(5)
<< std::setiosflags(std::ios::right);
switch(k) {
case 0:
std::cout << occur[k] << " " << "PLAIN"; break;
case 1:
std::cout << occur[k] << " " << "ONE_PAIR"; break;
case 2:
std::cout << occur[k] << " " << "TWO_PAIR"; break;
case 3:
std::cout << occur[k] << " " << "THREE_OF_A_KIND"; break;
case 4:
std::cout << occur[k] << " " << "STRAIGHT"; break;
case 5:
std::cout << occur[k] << " " << "FLUSH"; break;
case 6:
std::cout << occur[k] << " " << "FULL_HOUSE"; break;
case 7:
std::cout << occur[k] << " " << "FOUR_OF_A_KIND"; break;
case 8:
std::cout << occur[k] << " " << "STRAIGHT_FLUSH"; break;
}
}
std::cout << std::endl << std::endl << std::endl;
std::cout << " " << "TO PLAY THE RANDOM POKER GAME GENERATOR AGAIN HIT ANY KEY " << std::endl
<< " " << " AND THEN ENTER OR HIT CTRL Z: ";
}
return 0;
}
//Will use the inspectors in class classify and will
// determine the classification of each hand.
void classify_hand(classify poker_hand, int found[], unsigned int occur[], int call, unsigned int counter) {
int i=0;
i=call-1;
switch(call) {
case 9:
if(poker_hand.straight_flush()==1) {
occur[i]++;
if(found[i]==0) {
display(poker_hand, counter);
std::cout << " " << "STRAIGHT_FLUSH" << std::endl;
found[i]=1;
}
}
else {
classify_hand(poker_hand, found, occur, --call, counter);
}
break;
case 8:
if(poker_hand.four_kind()==1) {
occur[i]++;
if(found[i]==0) {
display(poker_hand, counter);
std::cout << " " << "FOUR_OF_A_KIND" << std::endl;
found[i]=1;
}
}
else {
classify_hand(poker_hand, found, occur, --call, counter);
}
break;
case 7:
if(poker_hand.full_house()==1) {
occur[i]++;
if(found[i]==0) {
display(poker_hand, counter);
std::cout << " " << "FULL_HOUSE" << std::endl;
found[i]=1;
}
}
else {
classify_hand(poker_hand, found, occur, --call, counter);
}
break;
case 6:
if(poker_hand.flush()==1) {
occur[i]++;
if(found[i]==0) {
display(poker_hand, counter);
std::cout << " " << "FLUSH" << std::endl;
found[i]=1;
}
}
else {
classify_hand(poker_hand, found, occur, --call, counter);
}
break;
case 5:
if(poker_hand.straight()==1) {
occur[i]++;
if(found[i]==0) {
display(poker_hand, counter);
std::cout << " " << "STRAIGHT" << std::endl;
found[i]=1;
}
}
else {
classify_hand(poker_hand, found, occur, --call, counter);
}
break;
case 4:
if(poker_hand.three_kind()==1) {
occur[i]++;
if(found[i]==0) {
display(poker_hand, counter);
std::cout << " " << "THREE_OF_A_KIND" << std::endl;
found[i]=1;
}
}
else {
classify_hand(poker_hand, found, occur, --call, counter);
}
break;
case 3:
if(poker_hand.two_pair()==1) {
occur[i]++;
if(found[i]==0) {
display(poker_hand, counter);
std::cout << " " << "TWO_PAIR" << std::endl;
found[i]=1;
}
}
else {
classify_hand(poker_hand, found, occur, --call, counter);
}
break;
case 2:
if(poker_hand.one_pair()==1) {
occur[i]++;
if(found[i]==0) {
display(poker_hand, counter );
std::cout << " " << "ONE_PAIR" << std::endl;
found[i]=1;
}
}
else {
classify_hand(poker_hand, found, occur, --call, counter);
}
break;
case 1:
if(poker_hand.plain()==1) {
occur[i]++;
if(found[i]==0) {
display(poker_hand, counter);
std::cout << " " << "PLAIN" << std::endl;
found[i]=1;
}
}
else {
break;
}
} //End switch
}
//Display function which will display the hand the first time
// it occurs
void display(classify poker_hand, unsigned int counter) {
std::cout << " ";
std::cout << std::setw(5)
<< std::setiosflags(std::ios::right)
<< counter <<": ";
poker_hand.print(); //calls member function of class classify and prints the hand
}