-
Notifications
You must be signed in to change notification settings - Fork 126
/
Program.cs
494 lines (471 loc) · 12.1 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
bool closeRequested = false;
State state = State.Main;
Stopwatch stopwatch = new();
TimeSpan framerate = TimeSpan.FromSeconds(1d / 60d);
bool direction = default;
bool playerGoesFirst = default;
int x = 0;
int y = 0;
int x_max = 38;
int y_max = 14;
List<((int X, int Y)? Position, bool Player)> darts = new();
int computer_x = default;
int computer_y = default;
int computer_skip = default;
try
{
Console.CursorVisible = false;
Console.OutputEncoding = Encoding.UTF8;
while (!closeRequested)
{
Render();
Update();
}
}
finally
{
Console.CursorVisible = true;
Console.Clear();
Console.WriteLine("Darts was closed.");
}
void Update()
{
switch (state)
{
case State.Main:
PressEnterToContinue();
if (closeRequested)
{
return;
}
playerGoesFirst = Random.Shared.Next(0, 2) % 2 is 0;
state = State.ConfirmRandomTurnOrder;
Console.Clear();
break;
case State.ConfirmRandomTurnOrder:
PressEnterToContinue();
if (closeRequested)
{
return;
}
state = playerGoesFirst
? State.PlayerHorizontal
: State.ComputerHorizontal;
direction = true;
x = 0;
if (!playerGoesFirst)
{
computer_x = Random.Shared.Next(x_max + 1);
computer_skip = Random.Shared.Next(1, 4);
}
stopwatch.Restart();
Console.Clear();
break;
case State.ConfirmPlayerThrow:
PressEnterToContinue();
if (closeRequested)
{
return;
}
if (darts.Count >= 10)
{
state = State.ConfirmGameEnd;
Console.Clear();
break;
}
state = State.ComputerHorizontal;
direction = true;
x = 0;
computer_x = Random.Shared.Next(x_max + 1);
computer_skip = Random.Shared.Next(1, 4);
stopwatch.Restart();
Console.Clear();
break;
case State.ConfirmComputerThrow:
PressEnterToContinue();
if (closeRequested)
{
return;
}
if (darts.Count >= 10)
{
state = State.ConfirmGameEnd;
Console.Clear();
break;
}
state = State.PlayerHorizontal;
direction = true;
x = 0;
stopwatch.Restart();
Console.Clear();
break;
case State.PlayerHorizontal or State.ComputerHorizontal:
if (KeyPressed() && state is State.PlayerHorizontal)
{
if (closeRequested)
{
return;
}
state = State.PlayerVertical;
direction = true;
y = 0;
stopwatch.Restart();
Console.Clear();
break;
}
if (closeRequested)
{
return;
}
if (direction)
{
x++;
}
else
{
x--;
}
if (state is State.ComputerHorizontal && x == computer_x)
{
computer_skip--;
if (computer_skip < 0)
{
state = State.ComputerVertical;
direction = true;
y = 0;
stopwatch.Restart();
computer_y = Random.Shared.Next(y_max + 1);
computer_skip = Random.Shared.Next(1, 4);
}
}
if (x <= 0 || x >= x_max)
{
if (x < 0) x = 0;
if (x > x_max) x = x_max;
direction = !direction;
}
ControlFrameRate();
break;
case State.PlayerVertical or State.ComputerVertical:
if (KeyPressed() && state is State.PlayerVertical)
{
if (closeRequested)
{
return;
}
state = State.ConfirmPlayerThrow;
(int X, int Y)? position = (x, y);
for (int i = 0; i < darts.Count; i++)
{
if (darts[i].Position == (x, y))
{
darts[i] = (null, darts[i].Player);
position = null;
}
}
darts.Add(new(position, true));
Console.Clear();
break;
}
if (closeRequested)
{
return;
}
if (direction)
{
y++;
}
else
{
y--;
}
if (state is State.ComputerVertical && y == computer_y)
{
computer_skip--;
if (computer_skip < 0)
{
state = State.ConfirmComputerThrow;
(int X, int Y)? position = (x, y);
for (int i = 0; i < darts.Count; i++)
{
if (darts[i].Position == (x, y))
{
darts[i] = (null, darts[i].Player);
position = null;
}
}
darts.Add(new(position, false));
Console.Clear();
break;
}
}
if (y <= 0 || y >= y_max)
{
if (y < 0) y = 0;
if (y > y_max) y = y_max;
direction = !direction;
}
ControlFrameRate();
break;
case State.ConfirmGameEnd:
PressEnterToContinue();
if (closeRequested)
{
return;
}
state = State.Main;
darts = new();
break;
default:
throw new NotImplementedException();
}
}
void ControlFrameRate()
{
TimeSpan elapsed = stopwatch.Elapsed;
if (framerate > elapsed)
{
Thread.Sleep(framerate - elapsed);
}
stopwatch.Restart();
}
void PressEnterToContinue()
{
while (true)
{
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.Enter: return;
case ConsoleKey.Escape: closeRequested = true; return;
}
}
}
bool KeyPressed()
{
bool keyPressed = false;
while (Console.KeyAvailable)
{
keyPressed = true;
if (Console.ReadKey(true).Key is ConsoleKey.Escape)
{
closeRequested = true;
}
}
return keyPressed;
}
void Render()
{
var render = new StringBuilder();
if (state is State.Main)
{
StringBuilder output = new();
output.AppendLine();
output.AppendLine(" Darts");
output.AppendLine();
output.AppendLine(" Welcome to Darts. In this game you and the computer will");
output.AppendLine(" throw darts at a dart board in attempts to get the most ");
output.AppendLine(" points. If your dart lands on a line, it will round down");
output.AppendLine(" amongst all the regions it is touching. If your dart lands");
output.AppendLine(" on another dart it will knock both darts off the board so");
output.AppendLine(" they will each be worth 0 points. You and the computer each");
output.AppendLine(" get to throw 5 darts.");
output.AppendLine();
output.AppendLine(" Your darts: ○");
output.AppendLine(" Computer's darts: ●");
output.AppendLine();
output.AppendLine(" Press [escape] at any time to close the game.");
output.AppendLine();
output.Append(" Press [enter] to begin...");
Console.Clear();
Console.Write(output);
return;
}
string[] board =
[
"╔═══════╤═══════╤═══════╤═══════╤═══════╗",
"║ │ │ │ │ ║",
"║ 1 │ 2 │ 3 │ 2 │ 1 ║",
"║ ┌┴┐ ┌─┴─┐ ┌─┴─┐ ┌┴┐ ║",
"╟──────┤6├────┤ 5 ├───┤ 5 ├────┤6├──────╢",
"║ └┬┘ └─┬─┘ └─┬─┘ └┬┘ ║",
"║ 2 │ 3 │ 4 │ 3 │ 2 ║",
"║ │ │ ┌─┐ │ │ ║",
"╟───────┼───────┼──┤9├──┼───────┼───────╢",
"║ │ │ └─┘ │ │ ║",
"║ 2 │ 3 │ 4 │ 3 │ 2 ║",
"║ ┌┴┐ ┌─┴─┐ ┌─┴─┐ ┌┴┐ ║",
"╟──────┤6├────┤ 5 ├───┤ 5 ├────┤6├──────╢",
"║ └┬┘ └─┬─┘ └─┬─┘ └┬┘ ║",
"║ 1 │ 2 │ 3 │ 2 │ 1 ║",
"║ │ │ │ │ ║",
"╚═══════╧═══════╧═══════╧═══════╧═══════╝",
];
for (int i = 0; i < board.Length; i++)
{
for (int j = 0; j < board[i].Length; j++)
{
foreach (var dart in darts)
{
if (dart.Position == (j - 1, i - 1))
{
render.Append(dart.Player ? '○' : '●');
goto DartRendered;
}
}
render.Append(board[i][j]);
DartRendered:
continue;
}
if (state is State.PlayerHorizontal or State.PlayerVertical or State.ComputerHorizontal or State.ComputerVertical or State.ConfirmPlayerThrow or State.ConfirmComputerThrow)
{
render.Append(' ');
if (i - 1 == y && state is not State.PlayerHorizontal and not State.ComputerHorizontal)
{
render.Append("│██│");
}
else
{
render.Append(i is 0 ? "┌──┐" : i == board.Length - 1 ? "└──┘" : "│ │");
}
}
render.AppendLine();
}
if (state is State.PlayerHorizontal or State.PlayerVertical or State.ComputerHorizontal or State.ComputerVertical or State.ConfirmPlayerThrow or State.ConfirmComputerThrow)
{
render.AppendLine("┌───────────────────────────────────────┐");
for (int j = 0; j <= x_max + 2; j++)
{
render.Append(
j - 1 == x ? '█' :
j is 0 ? '│' :
j == x_max + 2 ? '│' :
' ');
}
render.AppendLine();
render.AppendLine("└───────────────────────────────────────┘");
}
if (state is State.PlayerHorizontal or State.PlayerVertical)
{
render.AppendLine();
render.AppendLine(" It is your turn.");
render.Append(" Press any key to aim your ○ dart... ");
}
if (state is State.ComputerHorizontal or State.ComputerVertical)
{
render.AppendLine();
render.Append(" Computer's turn. Wait for it to throw it's ● dart.");
}
if (state is State.ConfirmRandomTurnOrder)
{
render.AppendLine();
render.AppendLine(" You and the computer flip a coin and decide that ");
render.AppendLine($" {(playerGoesFirst ? "you" : "the computer")} will go first.");
render.AppendLine();
render.Append(" Press [enter] to continue...");
}
if (state is State.ConfirmPlayerThrow)
{
render.AppendLine();
render.AppendLine(" You threw a dart.");
if (darts[^1].Position is null)
{
render.AppendLine();
render.AppendLine(" Dart collision! Both darts fell off the board.");
}
render.AppendLine();
render.Append(" Press [enter] to continue...");
}
if (state is State.ConfirmComputerThrow)
{
render.AppendLine();
render.AppendLine(" Computer threw a dart.");
if (darts[^1].Position is null)
{
render.AppendLine();
render.AppendLine(" Dart collision! Both darts fell off the board.");
}
render.AppendLine();
render.Append(" Press [enter] to continue...");
}
if (state is State.ConfirmGameEnd)
{
var (playerScore, computerScore) = CalculateScores();
render.AppendLine();
render.AppendLine(" Game Complete! Final Scores...");
render.AppendLine($" Your Score: {playerScore}");
render.AppendLine($" Computer's Score: {computerScore}");
render.AppendLine();
if (playerScore > computerScore)
{
render.AppendLine(" You Win!");
}
else if (playerScore < computerScore)
{
render.AppendLine(" You Lose!");
}
else
{
render.AppendLine(" Draw!");
}
render.AppendLine();
render.Append(" Press [enter] to return to the main screen...");
}
Console.CursorVisible = false;
Console.SetCursorPosition(0, 0);
Console.Write(render);
}
(int PlayerScore, int ComputerScore) CalculateScores()
{
string[] scoreBoard =
[
"111111112222222233333332222222211111111",
"111111112222222233333332222222211111111",
"111111112222222233333332222222211111111",
"111111162222225553333355522222261111111",
"222222223333333344444443333333322222222",
"222222223333333344444443333333322222222",
"222222223333333344444443333333322222222",
"222222223333333344494443333333322222222",
"222222223333333344444443333333322222222",
"222222223333333344444443333333322222222",
"222222223333333344444443333333322222222",
"111111162222225553333355522222261111111",
"111111112222222233333332222222211111111",
"111111112222222233333332222222211111111",
"111111112222222233333332222222211111111",
];
int playerScore = 0;
int computerScore = 0;
foreach (var dart in darts)
{
if (dart.Position.HasValue)
{
if (dart.Player)
{
playerScore += scoreBoard[dart.Position.Value.Y][dart.Position.Value.X] - '0';
}
else
{
computerScore += scoreBoard[dart.Position.Value.Y][dart.Position.Value.X] - '0';
}
}
}
return (playerScore, computerScore);
}
enum State
{
Main,
ConfirmRandomTurnOrder,
PlayerHorizontal,
PlayerVertical,
ConfirmPlayerThrow,
ComputerHorizontal,
ComputerVertical,
ConfirmComputerThrow,
ConfirmGameEnd,
}