-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathProgram.cs
505 lines (432 loc) · 14 KB
/
Program.cs
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
using System;
using System.Globalization;
using System.Linq;
Exception? exception = null;
const string welcome = """
See the README for instructions if needed.
Press [enter] to begin...
""";
const string rollDice = """
Press [space] to roll the dice...
""";
const string rollDiceSmall = """
Press [space] to roll the dice...
""";
const string blank = """
""";
const string chooseDice2 = """
Choose dice to re-roll. You have 2 re-rolls
remaining. Use [left & right & up arrow keys]
to select dice, and [enter] to confirm.
""";
const string chooseDice1 = """
Choose dice to re-roll. You have 1 re-rolls
remaining. Use [left & right & up arrow keys]
to select dice, and [enter] to confirm.
""";
const string selectScore = """
Select the item on the score card to use these
dice rolls for. Use [up & down arrow keys] to
select, and [enter] to confirm.
""";
const string selectScoreInvalid = """
Invalid selection. Each score item may only be
used once. Press [enter] to continue...
""";
const string yahtzeeBonus = """
You got a Yahtzee Bonus! It has
been marked on your scorecard.
Press [enter] to continue...
""";
const string upperBonusSuccess = """
You scored at least 63 in the upper section of
the score sheet. You get the Aces-Sices Bonus.
Press [enter] to continue...
""";
const string upperBonusFail = """
You did not score at least 63 in the upper
section of the score sheet. You do not get the
Aces-Sices Bonus. Press [enter] to continue...
""";
const string gameComplete = """
Game complete. Your total score has been
calculated on your scorecard.
Play again [enter] or quit [escape]?
""";
const int minWidth = 50;
const int minHeight = 32;
int[] dice = [1, 2, 3, 4, 5];
bool[] locked = new bool[dice.Length];
int diceSelection = 0;
int scoreSelection = 0;
int?[] scores = new int?[16];
bool escape = false;
try
{
if (OperatingSystem.IsWindows())
{
try
{
int width = Console.WindowWidth;
int height = Console.WindowHeight;
if (width < minWidth || height < minHeight)
{
Console.SetWindowSize(minWidth, minHeight);
Console.SetBufferSize(minWidth, minHeight);
}
}
catch
{
// Is mayonaise and instrument?
}
}
Restart:
Array.Fill(scores, null);
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
Console.Clear();
Render(false, false, false);
Console.Write(welcome);
PressToContinue(ConsoleKey.Enter);
while (!escape && scores.Contains(null))
{
PlayRound();
if (escape) return;
if (IsYahtzee() && scores[12] > 0)
{
scores[14] = scores[14] is null ? 100 : scores[14] + 100;
Render(false, false, false);
Console.Write(yahtzeeBonus);
PressToContinue(ConsoleKey.Enter);
if (escape) return;
}
ScoreSelection();
}
if (escape) return;
Render(false, false, false);
Console.Write(gameComplete);
PlayAgainCheck:
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.Enter: goto Restart;
case ConsoleKey.Escape: escape = true; break;
default: goto PlayAgainCheck;
}
}
catch (Exception e)
{
exception = e;
throw;
}
finally
{
Console.ResetColor();
Console.Clear();
Console.WriteLine(exception?.ToString() ?? "Yahtzee was closed.");
}
void Render(bool selectingDice, bool selectingScore, bool successiveRoll)
{
Console.CursorVisible = false;
EnsureConsoleSize();
if (escape) return;
Console.CursorVisible = false;
Console.SetCursorPosition(0, 0);
Console.WriteLine();
Console.WriteLine(" Yahtzee");
Console.WriteLine($" ╒════════ Score Sheet ════════╕");
Console.WriteLine($" ├─────── Upper Section ───────┤");
RenderSelectableScoreSheetLine(selectingScore, 00, "Aces...............");
RenderSelectableScoreSheetLine(selectingScore, 01, "Twos...............");
RenderSelectableScoreSheetLine(selectingScore, 02, "Threes.............");
RenderSelectableScoreSheetLine(selectingScore, 03, "Fours..............");
RenderSelectableScoreSheetLine(selectingScore, 04, "Fives..............");
RenderSelectableScoreSheetLine(selectingScore, 05, "Sixes..............");
Console.WriteLine($" │ Aces-Sixes Bonus...[{RenderScore(scores[06])}] │");
Console.WriteLine($" ├─────── Lower Section ───────┤");
RenderSelectableScoreSheetLine(selectingScore, 07, "3 of a Kind........");
RenderSelectableScoreSheetLine(selectingScore, 08, "4 of a Kind........");
RenderSelectableScoreSheetLine(selectingScore, 09, "Full House.........");
RenderSelectableScoreSheetLine(selectingScore, 10, "Small Straight.....");
RenderSelectableScoreSheetLine(selectingScore, 11, "Large Straight.....");
RenderSelectableScoreSheetLine(selectingScore, 12, "Yahtzee............");
RenderSelectableScoreSheetLine(selectingScore, 13, "Chance.............");
Console.WriteLine($" │ Yahtzee Bonus......[{RenderScore(scores[14])}] │");
Console.WriteLine($" │ Total..............[{RenderScore(scores[15])}] │");
Console.WriteLine($" └─────────────────────────────┘");
Console.WriteLine(" ╔═══╗╔═══╗╔═══╗╔═══╗╔═══╗");
Console.Write(" Dice: ");
for (int i = 0; i < dice.Length; i++)
{
Console.Write($"║ {dice[i].ToString(CultureInfo.InvariantCulture)} ║");
}
Console.WriteLine();
Console.WriteLine(" ╚═══╝╚═══╝╚═══╝╚═══╝╚═══╝");
if (selectingDice)
{
Console.Write(" ");
for (int i = 0; i < dice.Length; i++)
{
Console.Write(!locked[i] ? "^roll" : " ");
}
Console.WriteLine();
if (!successiveRoll)
{
Console.Write(" ");
Console.ForegroundColor = ConsoleColor.Yellow;
for (int i = 0; i < dice.Length; i++)
{
Console.Write(i == diceSelection ? "^^^^^" : " ");
}
Console.ForegroundColor = ConsoleColor.White;
}
}
}
void RenderSelectableScoreSheetLine(bool selectingScore, int index, string line)
{
Console.Write($" │ ");
if (selectingScore && scoreSelection == index)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write('>');
}
else
{
Console.Write(" ");
}
Console.Write($" {line}[{RenderScore(scores[index])}]");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine($" │");
}
string RenderScore(int? score) =>
score switch
{
null => " ",
< 10 => " " + score.Value.ToString(CultureInfo.InvariantCulture),
< 100 => " " + score.Value.ToString(CultureInfo.InvariantCulture),
< 1000 => " " + score.Value.ToString(CultureInfo.InvariantCulture),
_ => score.Value.ToString(CultureInfo.InvariantCulture),
};
void RollDice(bool selectingDice)
{
TimeSpan rollTime = TimeSpan.FromSeconds(1.5);
DateTime start = DateTime.Now;
while (DateTime.Now - start < rollTime)
{
while (Console.KeyAvailable)
{
if (Console.ReadKey(true).Key is ConsoleKey.Escape)
{
escape = true;
return;
}
}
for (int i = 0; i < dice.Length; i++)
{
if (!locked[i])
{
dice[i] = Random.Shared.Next(1, 7);
}
}
Render(selectingDice, false, false);
Console.Write(blank);
}
}
void PlayRound()
{
Array.Fill(locked, false);
Render(false, false, false);
Console.Write(rollDice);
PressToContinue(ConsoleKey.Spacebar);
if (escape) return;
RollDice(false);
if (escape) return;
Array.Fill(locked, true);
DiceSelection(chooseDice2);
if (!locked.Contains(false) || escape) return;
Render(true, false, true);
Console.Write(rollDiceSmall);
PressToContinue(ConsoleKey.Spacebar);
if (escape) return;
RollDice(false);
if (escape) return;
Array.Fill(locked, true);
DiceSelection(chooseDice1);
if (!locked.Contains(false) || escape) return;
Render(true, false, true);
Console.Write(rollDiceSmall);
PressToContinue(ConsoleKey.Spacebar);
if (escape) return;
RollDice(false);
if (escape) return;
}
void DiceSelection(string message)
{
diceSelection = 0;
GetInput:
Render(true, false, false);
Console.Write(message);
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.LeftArrow: diceSelection = Math.Max(diceSelection - 1, 0); goto GetInput;
case ConsoleKey.RightArrow: diceSelection = Math.Min(diceSelection + 1, dice.Length - 1); goto GetInput;
case ConsoleKey.UpArrow: locked[diceSelection] = !locked[diceSelection]; goto GetInput;
case ConsoleKey.Enter: break;
case ConsoleKey.Escape: escape = true; return;
default: goto GetInput;
}
}
void ScoreSelection()
{
scoreSelection = 0;
GetInput:
Render(false, true, false);
Console.Write(selectScore);
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.UpArrow:
scoreSelection = Math.Max(scoreSelection - 1, 0);
if (scoreSelection is 06) scoreSelection -= 1;
goto GetInput;
case ConsoleKey.DownArrow:
scoreSelection = Math.Min(scoreSelection + 1, 13);
if (scoreSelection is 06) scoreSelection += 1;
goto GetInput;
case ConsoleKey.Enter:
if (scores[scoreSelection] is null)
{
scores[scoreSelection] = scoreSelection switch
{
00 => dice.Count(v => v is 1),
01 => dice.Count(v => v is 2) * 2,
02 => dice.Count(v => v is 3) * 3,
03 => dice.Count(v => v is 4) * 4,
04 => dice.Count(v => v is 5) * 5,
05 => dice.Count(v => v is 6) * 6,
07 => Is3OfAKind() ? dice.Sum() : 0,
08 => Is4OfAKind() ? dice.Sum() : 0,
09 => IsFullHouse() ? 25 : 0,
10 => IsSmallStraight() ? 30 : 0,
11 => IsLargeStraight() ? 40 : 0,
12 => IsYahtzee() ? 50 : 0,
13 => dice.Sum(),
_ => throw new Exception("invalid score selection"),
};
if (scores[06] is null && !scores[..06].Contains(null))
{
scores[06] = scores[..06].Sum() >= 63 ? 35 : 0;
Render(false, false, false);
Console.Write(scores[06] is 0 ? upperBonusFail : upperBonusSuccess);
PressToContinue(ConsoleKey.Enter);
if (escape) return;
}
if (scores[14] is null && !scores[..14].Contains(null))
{
scores[14] = 0;
}
if (!scores[..15].Contains(null))
{
scores[15] = scores.Sum();
}
break;
}
else
{
Render(false, true, false);
Console.Write(selectScoreInvalid);
PressToContinue(ConsoleKey.Enter);
if (escape) return;
goto GetInput;
}
case ConsoleKey.Escape:
escape = true;
return;
default:
goto GetInput;
}
}
void PressToContinue(ConsoleKey key)
{
GetInput:
ConsoleKey input = Console.ReadKey(true).Key;
if (input is ConsoleKey.Escape)
{
escape = true;
return;
}
if (input != key)
{
goto GetInput;
}
}
bool IsFullHouse()
{
int[] values = new int[6];
for (int i = 0; i < dice.Length; i++)
{
values[dice[i] - 1]++;
}
return values.Contains(2) && values.Contains(3);
}
bool Is3OfAKind() => GetMaxMatches() >= 3;
bool Is4OfAKind() => GetMaxMatches() >= 4;
bool IsYahtzee() => GetMaxMatches() is 5;
int GetMaxMatches()
{
int[] values = new int[6];
for (int i = 0; i < dice.Length; i++)
{
values[dice[i] - 1]++;
}
return values.Max();
}
bool IsSmallStraight() => GetMaxDiceInARow() >= 4;
bool IsLargeStraight() => GetMaxDiceInARow() >= 5;
int GetMaxDiceInARow()
{
int[] values = new int[6];
for (int i = 0; i < dice.Length; i++)
{
values[dice[i] - 1]++;
}
int maxInARow = 0;
int inARow = 0;
for (int i = 0; i < values.Length; i++)
{
if (values[i] > 0)
{
inARow++;
}
else
{
maxInARow = Math.Max(maxInARow, inARow);
inARow = 0;
}
}
return Math.Max(maxInARow, inARow);
}
void EnsureConsoleSize()
{
int width = Console.WindowWidth;
int height = Console.WindowHeight;
while (!escape && (width < minWidth || height < minHeight))
{
Console.Clear();
Console.WriteLine("Increase console size and press [enter]...");
bool enter = false;
while (!escape && !enter)
{
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.Enter:
enter = true;
break;
case ConsoleKey.Escape:
escape = true;
break;
}
}
width = Console.WindowWidth;
height = Console.WindowHeight;
Console.Clear();
}
}