-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
353 lines (289 loc) · 10.4 KB
/
MainWindow.xaml.cs
File metadata and controls
353 lines (289 loc) · 10.4 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
using System;
using System.Windows;
using System.Collections.ObjectModel;
using Blackjack_Simulator.Models;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace Blackjack_Simulator
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : INotifyPropertyChanged
{
private Shoe shoe = null;
private Dealer dealer = null;
private Player player = null;
private Round round = null;
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
//private ObservableCollection<String> playerValues = null;
public MainWindow()
{
InitializeComponent();
DataContext = this;
DataContext = this;
shoe = new Shoe(1);
dealer = new Dealer();
player = new Player();
this.DealerSide.ItemsSource = dealer.GetHand();
this.PlayerSide.ItemsSource = player.GetHand();
ConsoleAllocator.ShowConsoleWindow();
}
#region Game Logic
private void Deal()
{
DealButton.IsEnabled = false;
//dealer.Draw(new Card(1, CardSuit.Hearts));
dealer.Draw(shoe.Draw());
player.Draw(shoe.Draw());
//player.Draw(new Card(6, CardSuit.Hearts));
//dealer.Draw(new Card(13, CardSuit.Hearts));
dealer.Draw(shoe.Draw());
player.Draw(shoe.Draw());
// player.Draw(new Card(6, CardSuit.Clubs));
// tests: force dealer cards
//dealer.hand.Clear();
//dealer.Draw(new Card(1, CardSuit.Hearts));
//dealer.Draw(new Card(5, CardSuit.Hearts));
// ==========================================
//player.hand.Clear();
//player.Draw(new Card(10, CardSuit.Hearts));
//player.Draw(new Card(1, CardSuit.Hearts));
// pass initial hand
round = new Round(dealer, player);
round.CheckInitialBlackjack();
// test these canges
DealerPrompt.Text = dealer.handValue().ToString();
PlayerPrompt.Text = player.handValue().ToString();
EnableButtons();
if (player.HasPair())
{
SplitButton.IsEnabled = true;
}
if (!round.InProgress())
{
EndRound();
}
}
private void PlayerHit()
{
try
{
//player.Draw(shoe.Draw());
player.Draw(new Card(6, CardSuit.Spades));
//PlayerPrompt.Text = player.handValue().ToString();
// player did not split
if (!player.split)
{
PlayerPrompt.Text = player.handValue().ToString();
// check bust
if (player.busted)
{
round.SetResult(RoundResult.PlayerBust);
EndRound();
}
}
// player did split
else
{
//var currentHand = player.hands[player.focusedHandIndex];
var currentHand = player.currentHand();
// check which hand to focus
PlayerPrompt.Text = currentHand.handValueInt().ToString();
if (currentHand.busted)
{
// round.SetResult(RoundResult.PlayerBust);
// if current hand is th elast hand, end round
if(player.isLastHand())
{
Console.WriteLine("Player bust on last hand.");
// may need to change the condition up here
//EndRound();
EndPlayerTurn();
}
else
{
Console.WriteLine(string.Format("Player bust on hand {0}.", player.focusedHandIndex+1));
//round.SetResult(RoundResult.PlayerBust); uncesseary extra
player.endHand();
//current hand busted, hit for the next hand
PlayerHit();
}
}
}
if (player.HasPair())
{
this.SplitButton.IsEnabled = true;
}
else
{
this.SplitButton.IsEnabled = false;
}
}
catch (Exception ex)
{
var stop = ex.Message;
}
}
private void Stand()
{
// end turn if player didn't split
if (!player.split)
{
EndPlayerTurn();
}
// if they did split, check when hand they are standing on
else
{
// if focus in on the last hand, EndTurn()
if (player.focusedHandIndex != player.hands.Count - 1)
{
//player.focusedHandIndex++;
player.endHand();
PlayerHit();
}
else
{
EndPlayerTurn();
}
}
}
private void Split()
{
// create two hands for player
player.Split();
PlayerHit();
if (player.HasPair())
{
this.SplitButton.IsEnabled = true;
}
else
{
this.SplitButton.IsEnabled = false;
}
}
private void EndPlayerTurn()
{
// if player busted all hands, round is over. reveal dealer hand
if (player.BustedAllHands())
{
dealer.RevealCard();
round.CompareHands();
EndRound();
}
else
{
// if playe busted some hands, start deal turn but when comparing hands, set playe bust for busted hands.
// only compare valid player hands
//reveal dealer card
dealer.RevealCard();
// dealer stand on 17
// test this
while (dealer.underSeventeen())
{
dealer.Draw(shoe.Draw());
//dealer.Draw(new Card(1, CardSuit.Spades)); // ok
//dealer.Draw(new Card(2, CardSuit.Spades)); // ok
//dealer.Draw(new Card(3, CardSuit.Spades)); // ok
//dealer.Draw(new Card(4, CardSuit.Spades)); // ok
// dealer.Draw(new Card(5, CardSuit.Spades)); // ok after changing compareHands line 80 to 22 from 21
//dealer.Draw(new Card(6, CardSuit.Spades)); // ok
//dealer.Draw(new Card(7, CardSuit.Spades)); // ok
//dealer.Draw(new Card(8, CardSuit.Spades)); // ok
//dealer.Draw(new Card(9, CardSuit.Spades)); // ok
//dealer.Draw(new Card(10, CardSuit.Spades)); // ok
//dealer.Draw(new Card(11, CardSuit.Spades)); // ok
DealerPrompt.Text = dealer.handValue().ToString(); // can move this outside while loop
}
// dealer will either bust or it's time to compare hands
if (dealer.busted)
{
Console.WriteLine("EndPlayerTurn() dealer bust.");
DealerPrompt.Text = dealer.handValue().ToString();
//round.SetResult(RoundResult.DealerBust);
round.CompareHands();
EndRound();
}
else
{
Console.WriteLine("EndPlayerTurn() dealer no bust.");
DealerPrompt.Text = dealer.handValue().ToString();
round.CompareHands();
EndRound();
}
}
}
private void EndRound()
{
string results = "";
foreach(string result in round.GetWinner())
{
results += result + "\n";
}
GamePrompt.Text = results;
DisableButtons();
}
#endregion
#region cleanup
private void DisableButtons()
{
this.HitButton.IsEnabled = false;
this.Standbutton.IsEnabled = false;
this.DoubleButton.IsEnabled = false;
this.SplitButton.IsEnabled = false;
this.DealButton.IsEnabled = false;
}
private void EnableButtons()
{
this.HitButton.IsEnabled = true;
this.Standbutton.IsEnabled = true;
this.DoubleButton.IsEnabled = true;
this.DealButton.IsEnabled = true;
}
private void ClearTable()
{
GamePrompt.Text = "";
DealerPrompt.Text = "";
PlayerPrompt.Text = "";
dealer.Clear();
player.Clear();
round = null;
DisableButtons();
this.DealButton.IsEnabled = true;
}
#endregion
#region Button Events
private void Deal_Button_Click(object sender, RoutedEventArgs e)
{
Deal();
}
private void Hit_Button_Click(object sender, RoutedEventArgs e)
{
PlayerHit();
}
private void Double_Button_Click(object sender, RoutedEventArgs e)
{
PlayerHit();
EndPlayerTurn();
}
private void Stand_button_Click(object sender, RoutedEventArgs e)
{
Stand();
}
private void Split_Button_Click(object sender, RoutedEventArgs e)
{
Split();
}
private void Clear_Button_Click(object sender, RoutedEventArgs e)
{
ClearTable();
}
#endregion
}
}