-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPokerMachineForm.cs
More file actions
440 lines (360 loc) · 13.2 KB
/
PokerMachineForm.cs
File metadata and controls
440 lines (360 loc) · 13.2 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
using System.Drawing.Drawing2D;
using WMPLib;
namespace PokerMachine
{
public partial class PokerMachineForm : Form
{
private int balance = 100;
private int currentBet = 1;
private bool[] holdFlags = new bool[5];
private List<Card> currentHand = new List<Card>();
private Deck deck = new Deck();
private WindowsMediaPlayer mediaPlayer = new WindowsMediaPlayer();
private bool isFirstTurn = true;
private readonly Dictionary<string, int> leftPaytableData = new Dictionary<string, int>
{
{ "Royal Flush", 250 },
{ "Straight Flush", 50 },
{ "Four of a Kind", 25 },
{ "Full House", 9 },
{ "Flush", 6 }
};
private readonly Dictionary<string, int> rightPaytableData = new Dictionary<string, int>
{
{ "Straight", 4 },
{ "Three of a Kind", 3 },
{ "Two Pair", 2 },
{ "Jacks or Better", 1 }
};
public PokerMachineForm()
{
InitializeComponent();
PopulatePaytablePanels();
lblBalance.Text = $"Balance: ${balance}";
lblBet.Text = $"Bet: ${currentBet}";
}
private Image LoadCardImage(string rank, string suit, int width, int height)
{
string imagePath = $"Images/Cards/{rank}_{suit}.png";
Image originalImage = Image.FromFile(imagePath);
return new Bitmap(originalImage, new Size(width, height));
}
private List<Card> DealCards(Deck deck)
{
List<Card> hand = new List<Card>();
for (int i = 0; i < 5; i++)
{
hand.Add(deck.Draw());
}
return hand;
}
private void DisplayHand(List<Card> hand)
{
pictureBox1.Image = LoadCardImage(hand[0].Rank, hand[0].Suit, pictureBox1.Width, pictureBox1.Height);
pictureBox2.Image = LoadCardImage(hand[1].Rank, hand[1].Suit, pictureBox2.Width, pictureBox2.Height);
pictureBox3.Image = LoadCardImage(hand[2].Rank, hand[2].Suit, pictureBox3.Width, pictureBox3.Height);
pictureBox4.Image = LoadCardImage(hand[3].Rank, hand[3].Suit, pictureBox4.Width, pictureBox4.Height);
pictureBox5.Image = LoadCardImage(hand[4].Rank, hand[4].Suit, pictureBox5.Width, pictureBox5.Height);
}
private void btnDeal_Click(object sender, EventArgs e)
{
mediaPlayer.URL = "Sounds/deal.mp3";
mediaPlayer.controls.play();
lblResult.Text = "Good luck!";
if (isFirstTurn)
{
if (currentBet > balance)
{
MessageBox.Show("You don't have enough credits to place this bet.", "Insufficient Credits", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
balance -= currentBet;
lblBalance.Text = $"Balance: ${balance}";
deck = new Deck();
deck.Shuffle();
currentHand = DealCards(deck);
DisplayHand(currentHand);
isFirstTurn = false;
btnDeal.Text = "Draw";
}
else
{
for (int i = 0; i < currentHand.Count; i++)
{
if (!holdFlags[i])
{
currentHand[i] = deck.Draw();
}
}
DisplayHand(currentHand);
string result = EvaluateHand(currentHand);
int payout = CalculatePayout(result, currentBet);
if (payout > 0)
{
mediaPlayer.URL = "Sounds/win.mp3";
mediaPlayer.controls.play();
lblResult.Text = $"{result}! You won ${payout}.";
}
else
{
lblResult.Text = $"{result}. Better luck next time!";
}
balance += payout;
lblBalance.Text = $"Balance: ${balance}";
isFirstTurn = true;
btnDeal.Text = "Deal";
Array.Fill(holdFlags, false);
ResetHoldButtons();
}
}
private string EvaluateHand(List<Card> hand)
{
if (IsRoyalFlush(hand)) return "Royal Flush";
if (IsStraightFlush(hand)) return "Straight Flush";
if (IsFourOfAKind(hand)) return "Four of a Kind";
if (IsFullHouse(hand)) return "Full House";
if (IsFlush(hand)) return "Flush";
if (IsStraight(hand)) return "Straight";
if (IsThreeOfAKind(hand)) return "Three of a Kind";
if (IsTwoPair(hand)) return "Two Pair";
if (IsJacksOrBetter(hand)) return "Jacks or Better";
return "No Winning Hand";
}
private int CalculatePayout(string handType, int betAmount)
{
if (leftPaytableData.ContainsKey(handType))
{
return leftPaytableData[handType] * betAmount;
}
else if (rightPaytableData.ContainsKey(handType))
{
return rightPaytableData[handType] * betAmount;
}
return 0;
}
private void btnHold1_Click(object sender, EventArgs e)
{
ToggleHold(0, btnHold1);
}
private void btnHold2_Click(object sender, EventArgs e)
{
ToggleHold(1, btnHold2);
}
private void btnHold3_Click(object sender, EventArgs e)
{
ToggleHold(2, btnHold3);
}
private void btnHold4_Click(object sender, EventArgs e)
{
ToggleHold(3, btnHold4);
}
private void btnHold5_Click(object sender, EventArgs e)
{
ToggleHold(4, btnHold5);
}
private void ToggleHold(int index, Button button)
{
if (index < 0 || index >= holdFlags.Length)
{
MessageBox.Show("Invalid hold index!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
holdFlags[index] = !holdFlags[index];
if (holdFlags[index])
{
button.BackColor = Color.LightGreen;
button.Text = "Held";
}
else
{
button.BackColor = SystemColors.Control;
button.Text = "Hold";
}
}
private void UpdateHoldButtonAppearance(Button button, bool isHeld)
{
if (isHeld)
{
button.BackColor = Color.LightGreen;
button.Text = "Held";
}
else
{
button.BackColor = Color.DarkGray;
button.Text = "Hold";
}
}
private void ResetHoldButtons()
{
UpdateHoldButtonAppearance(btnHold1, false);
UpdateHoldButtonAppearance(btnHold2, false);
UpdateHoldButtonAppearance(btnHold3, false);
UpdateHoldButtonAppearance(btnHold4, false);
UpdateHoldButtonAppearance(btnHold5, false);
}
private void btnPaytable_Click(object sender, EventArgs e)
{
string paytableText = "Paytable:\n\n";
foreach (var entry in leftPaytableData)
{
paytableText += $"{entry.Key}: {entry.Value}x\n";
}
paytableText += "\n";
foreach (var entry in rightPaytableData)
{
paytableText += $"{entry.Key}: {entry.Value}x\n";
}
MessageBox.Show(paytableText, "Paytable", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
// Paytable
private bool IsRoyalFlush(List<Card> hand)
{
return IsStraight(hand) && IsFlush(hand) && hand.Any(c => c.Rank == "ace");
}
private bool IsStraightFlush(List<Card> hand)
{
return IsStraight(hand) && IsFlush(hand);
}
private bool IsFourOfAKind(List<Card> hand)
{
return hand.GroupBy(c => c.Rank).Any(g => g.Count() == 4);
}
private bool IsFullHouse(List<Card> hand)
{
var groups = hand.GroupBy(c => c.Rank).ToList();
return groups.Count == 2 && (groups[0].Count() == 3 || groups[1].Count() == 3);
}
private bool IsFlush(List<Card> hand)
{
return hand.All(c => c.Suit == hand[0].Suit);
}
private bool IsStraight(List<Card> hand)
{
var ranks = hand.Select(c => GetRankValue(c.Rank)).OrderBy(v => v).ToList();
for (int i = 1; i < ranks.Count; i++)
{
if (ranks[i] != ranks[i - 1] + 1)
{
return false;
}
}
return true;
}
private int GetRankValue(string rank)
{
return rank switch
{
"2" => 2,
"3" => 3,
"4" => 4,
"5" => 5,
"6" => 6,
"7" => 7,
"8" => 8,
"9" => 9,
"10" => 10,
"j" => 11,
"q" => 12,
"k" => 13,
"ace" => 14,
_ => 0
};
}
private bool IsThreeOfAKind(List<Card> hand)
{
return hand.GroupBy(c => c.Rank).Any(g => g.Count() == 3);
}
private bool IsTwoPair(List<Card> hand)
{
return hand.GroupBy(c => c.Rank).Count(g => g.Count() == 2) == 2;
}
private bool IsJacksOrBetter(List<Card> hand)
{
var highCards = new HashSet<string> { "j", "q", "k", "ace" };
return hand.GroupBy(c => c.Rank).Any(g => g.Count() == 2 && highCards.Contains(g.Key));
}
//protected override void OnPaintBackground(PaintEventArgs e)
//{
// using (LinearGradientBrush gradientBrush = new LinearGradientBrush(
// this.ClientRectangle,
// Color.Gray, // Top color
// Color.DimGray, // Bottom color
// LinearGradientMode.Vertical))
// {
// e.Graphics.FillRectangle(gradientBrush, this.ClientRectangle);
// }
//}
private void pictureBox7_Click(object sender, EventArgs e)
{
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
currentBet = (int)numericUpDownBet.Value;
lblBet.Text = $"Bet: ${currentBet}";
}
private void PopulatePaytablePanels()
{
panelLeftPaytable.Controls.Clear();
panelExtraLeft.Controls.Clear();
panelRightPaytable.Controls.Clear();
panelExtraRight.Controls.Clear();
int row = 0;
foreach (var entry in leftPaytableData)
{
var handTypeLabel = new Label
{
Text = entry.Key,
AutoSize = true,
ForeColor = Color.Gold,
Font = new Font("Arial", 13, FontStyle.Bold),
Location = new Point(10, row * 20)
};
panelLeftPaytable.Controls.Add(handTypeLabel);
var payoutLabel = new Label
{
Text = entry.Value.ToString(),
AutoSize = true,
ForeColor = Color.Gold,
Font = new Font("Arial", 13, FontStyle.Bold),
Location = new Point(10, row * 20)
};
panelExtraLeft.Controls.Add(payoutLabel);
row++;
}
row = 0;
foreach (var entry in rightPaytableData)
{
var handTypeLabel = new Label
{
Text = entry.Key,
AutoSize = true,
ForeColor = Color.Gold,
Font = new Font("Arial", 13, FontStyle.Bold),
Location = new Point(10, row * 20)
};
panelRightPaytable.Controls.Add(handTypeLabel);
var payoutLabel = new Label
{
Text = entry.Value.ToString(),
AutoSize = true,
ForeColor = Color.Gold,
Font = new Font("Arial", 13, FontStyle.Bold),
Location = new Point(10, row * 20)
};
panelExtraRight.Controls.Add(payoutLabel);
row++;
}
}
private void btnReset_Click(object sender, EventArgs e)
{
balance = 100;
lblBalance.Text = $"Balance: ${balance}";
MessageBox.Show("Credits have been reset to $100!", "Reset Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);
isFirstTurn = true;
lblResult.Text = string.Empty;
Array.Fill(holdFlags, false);
ResetHoldButtons();
btnDeal.Text = "Deal";
}
}
}