-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chip Masters - Texas Hold'em.cpp
305 lines (266 loc) · 10.4 KB
/
Chip Masters - Texas Hold'em.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <map>
#include <set>
#include <iomanip>
// Card structure
struct Card {
std::string suit;
std::string value;
};
// Player structure
struct Player {
std::string name;
int chips;
std::vector<Card> hand;
bool folded = false;
bool allIn = false;
};
// Generate a deck of cards
std::vector<Card> generateDeck() {
std::vector<Card> deck;
std::vector<std::string> suits = {"Hearts", "Diamonds", "Clubs", "Spades"};
std::vector<std::string> values = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
for (const auto& suit : suits) {
for (const auto& value : values) {
deck.push_back({suit, value});
}
}
return deck;
}
// Shuffle the deck
void shuffleDeck(std::vector<Card>& deck) {
std::srand(unsigned(std::time(0)));
std::random_shuffle(deck.begin(), deck.end());
}
// Deal cards to players
std::vector<Card> dealCards(std::vector<Card>& deck, int numCards) {
std::vector<Card> hand;
for (int i = 0; i < numCards; ++i) {
hand.push_back(deck.back());
deck.pop_back();
}
return hand;
}
// Print cards
void printCards(const std::vector<Card>& cards) {
for (const auto& card : cards) {
std::cout << card.value << " of " << card.suit << "\n";
}
}
// Hand evaluation function
int evaluateHand(const std::vector<Card>& hand, const std::vector<Card>& communityCards) {
std::map<std::string, int> valueCount;
std::map<std::string, int> suitCount;
std::set<std::string> values;
for (const auto& card : hand) {
valueCount[card.value]++;
suitCount[card.suit]++;
values.insert(card.value);
}
for (const auto& card : communityCards) {
valueCount[card.value]++;
suitCount[card.suit]++;
values.insert(card.value);
}
bool flush = std::any_of(suitCount.begin(), suitCount.end(), [](const auto& entry) { return entry.second >= 5; });
bool straight = false;
std::vector<std::string> order = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
for (size_t i = 0; i <= order.size() - 5; ++i) {
if (values.count(order[i]) && values.count(order[i + 1]) && values.count(order[i + 2]) &&
values.count(order[i + 3]) && values.count(order[i + 4])) {
straight = true;
break;
}
}
if (flush && straight) return 8;
if (std::any_of(valueCount.begin(), valueCount.end(), [](const auto& entry) { return entry.second == 4; })) return 7;
if (std::any_of(valueCount.begin(), valueCount.end(), [](const auto& entry) { return entry.second == 3; }) &&
std::any_of(valueCount.begin(), valueCount.end(), [](const auto& entry) { return entry.second == 2; })) return 6;
if (flush) return 5;
if (straight) return 4;
if (std::any_of(valueCount.begin(), valueCount.end(), [](const auto& entry) { return entry.second == 3; })) return 3;
if (std::count_if(valueCount.begin(), valueCount.end(), [](const auto& entry) { return entry.second == 2; }) >= 2) return 2;
if (std::any_of(valueCount.begin(), valueCount.end(), [](const auto& entry) { return entry.second == 2; })) return 1;
return 0;
}
// Get the best hand description
std::string getHandDescription(int handValue) {
switch (handValue) {
case 8: return "Straight Flush";
case 7: return "Four of a Kind";
case 6: return "Full House";
case 5: return "Flush";
case 4: return "Straight";
case 3: return "Three of a Kind";
case 2: return "Two Pair";
case 1: return "One Pair";
case 0: return "High Card";
default: return "Unknown";
}
}
// Player action (betting)
void playerAction(Player& player, int& pot, int& currentBet, int minBet) {
if (player.folded || player.allIn) {
return;
}
std::cout << player.name << "'s turn. Chips: " << player.chips << "\n";
std::cout << "Current bet to call: " << currentBet << " chips\n";
std::cout << "1. Call\n2. Raise\n3. Fold\n4. Check\n5. All-in\n";
int action;
std::cin >> action;
switch (action) {
case 1: // Call
if (currentBet <= player.chips) {
player.chips -= currentBet;
pot += currentBet;
std::cout << player.name << " calls " << currentBet << " chips.\n";
} else {
pot += player.chips;
currentBet += player.chips;
player.chips = 0;
player.allIn = true;
std::cout << player.name << " goes all-in with " << currentBet << " chips.\n";
}
break;
case 2: // Raise
std::cout << "Enter raise amount (min raise " << minBet << " chips): ";
int raise;
std::cin >> raise;
if (raise >= minBet && raise <= player.chips) {
player.chips -= raise;
pot += raise;
currentBet += raise;
std::cout << player.name << " raises to " << currentBet << " chips.\n";
} else {
std::cout << "Invalid raise amount. Please choose again.\n";
playerAction(player, pot, currentBet, minBet);
}
break;
case 3: // Fold
player.folded = true;
std::cout << player.name << " folds.\n";
break;
case 4: // Check
if (currentBet == 0) {
std::cout << player.name << " checks.\n";
} else {
std::cout << "You cannot check. Please choose another action.\n";
playerAction(player, pot, currentBet, minBet);
}
break;
case 5: // All-in
pot += player.chips;
currentBet += player.chips;
player.chips = 0;
player.allIn = true;
std::cout << player.name << " goes all-in with " << currentBet << " chips.\n";
break;
default:
std::cout << "Invalid action. Please choose again.\n";
playerAction(player, pot, currentBet, minBet);
}
}
// Print community cards
void printCommunityCards(const std::vector<Card>& communityCards, int numCards) {
std::cout << "\nCommunity cards:\n";
for (int i = 0; i < numCards; ++i) {
std::cout << communityCards[i].value << " of " << communityCards[i].suit << "\n";
}
}
// Check if the game is over
bool isGameOver(const Player& player1, const Player& dealer) {
return player1.chips == 0 || dealer.chips == 0;
}
// Display the winner
void displayWinner(const Player& player1, const Player& dealer) {
if (player1.chips > dealer.chips) {
std::cout << "\n" << player1.name << " wins the game with " << player1.chips << " chips!\n";
} else if (dealer.chips > player1.chips) {
std::cout << "\n" << dealer.name << " wins the game with " << dealer.chips << " chips!\n";
} else {
std::cout << "\nIt's a tie! Both players have " << player1.chips << " chips.\n";
}
}
// Main game loop
int main() {
char restart;
do {
Player player1 = {"Player 1", 1000, {}};
Player dealer = {"Dealer", 1000, {}};
int smallBlind = 10;
int bigBlind = 20;
bool player1SmallBlind = true;
while (!isGameOver(player1, dealer)) {
std::vector<Card> deck = generateDeck();
shuffleDeck(deck);
player1.hand = dealCards(deck, 2);
dealer.hand = dealCards(deck, 2);
std::vector<Card> communityCards = dealCards(deck, 5);
std::cout << "\nPlayer's hand:\n";
printCards(player1.hand);
int pot = 0;
int currentBet = 0;
int minRaise = bigBlind;
// Blinds
if (player1SmallBlind) {
player1.chips -= smallBlind;
dealer.chips -= bigBlind;
pot += smallBlind + bigBlind;
currentBet = bigBlind;
} else {
dealer.chips -= smallBlind;
player1.chips -= bigBlind;
pot += smallBlind + bigBlind;
currentBet = bigBlind;
}
// Pre-flop betting round
playerAction(player1, pot, currentBet, minRaise);
playerAction(dealer, pot, currentBet, minRaise);
// Flop
printCommunityCards(communityCards, 3);
playerAction(player1, pot, currentBet, minRaise);
playerAction(dealer, pot, currentBet, minRaise);
// Turn
printCommunityCards(communityCards, 4);
playerAction(player1, pot, currentBet, minRaise);
playerAction(dealer, pot, currentBet, minRaise);
// River
printCommunityCards(communityCards, 5);
playerAction(player1, pot, currentBet, minRaise);
playerAction(dealer, pot, currentBet, minRaise);
// Hand evaluation
int playerScore = evaluateHand(player1.hand, communityCards);
int dealerScore = evaluateHand(dealer.hand, communityCards);
std::cout << "\nPlayer's best hand: " << getHandDescription(playerScore) << "\n";
std::cout << "Dealer's best hand: " << getHandDescription(dealerScore) << "\n";
if (playerScore > dealerScore) {
std::cout << "Player wins the pot of " << pot << " chips!\n";
player1.chips += pot;
} else if (playerScore < dealerScore) {
std::cout << "Dealer wins the pot of " << pot << " chips!\n";
dealer.chips += pot;
} else {
std::cout << "It's a tie! Pot is split.\n";
player1.chips += pot / 2;
dealer.chips += pot / 2;
}
std::cout << "\nPlayer's chips: " << player1.chips << "\n";
std::cout << "Dealer's chips: " << dealer.chips << "\n";
// Reset players for next round
player1.folded = false;
player1.allIn = false;
dealer.folded = false;
dealer.allIn = false;
// Rotate blinds
player1SmallBlind = !player1SmallBlind;
}
displayWinner(player1, dealer);
std::cout << "\nDo you want to restart the game? (y/n): ";
std::cin >> restart;
} while (restart == 'y' || restart == 'Y');
return 0;
}