-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathProgram.cs
161 lines (157 loc) · 4.56 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
using System;
using System.Threading;
Exception? exception = null;
try
{
while (true)
{
int position = 0;
const int displacement = 10;
string L() => new(' ', displacement + position + 4);
string R() => new(' ', displacement - position + 4);
string Ground =
new string(' ', 2) +
new string('-', displacement + (15 - displacement) + 2) +
new string('=', displacement * 2 + 2) +
new string('-', displacement + (15 - displacement) + 2) +
new string(' ', 2);
bool frame_a = false;
Console.Clear();
Console.Write("""
Tug Of War
Out pull your opponent in a rope pulling
competition. Mash the [left]+[right] arrow
keys and/or the [A]+[D] keys to pull on the
rope. First player to pull the center of the
rope into their boundary wins.
Choose Your Opponent:
[1] Easy.......2 mashes per second
[2] Medium.....4 mashes per second
[3] Hard.......8 mashes per second
[4] Harder....16 mashes per second
[escape] give up
""");
int? requiredMash = null;
while (requiredMash is null)
{
Console.CursorVisible = false;
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.D1 or ConsoleKey.NumPad1: requiredMash = 02; break;
case ConsoleKey.D2 or ConsoleKey.NumPad2: requiredMash = 04; break;
case ConsoleKey.D3 or ConsoleKey.NumPad3: requiredMash = 08; break;
case ConsoleKey.D4 or ConsoleKey.NumPad4: requiredMash = 16; break;
case ConsoleKey.Escape: return;
}
}
Console.Clear();
int mash = 0;
int presses = 0;
int sleeps = 0;
ConsoleKey lastKey = default;
DateTime start = DateTime.Now;
while (true)
{
while (Console.KeyAvailable)
{
ConsoleKey key = Console.ReadKey(true).Key;
if (key is ConsoleKey.Escape)
{
return;
}
else if (lastKey is not default(ConsoleKey) &&
key is ConsoleKey.A or ConsoleKey.D or ConsoleKey.LeftArrow or ConsoleKey.RightArrow &&
key != lastKey)
{
presses++;
mash++;
lastKey = default;
}
else if (key is ConsoleKey.A or ConsoleKey.D or ConsoleKey.LeftArrow or ConsoleKey.RightArrow)
{
lastKey = key;
}
}
if (sleeps is 2)
{
position = mash < requiredMash.Value
? position + 1
: position - 1;
sleeps = 0;
mash = 0;
if (Math.Abs(position) >= displacement)
{
break;
}
}
Console.CursorVisible = false;
Console.SetCursorPosition(0, 0);
Console.WriteLine();
Console.WriteLine(" Tug Of War");
Console.WriteLine();
Console.Write(frame_a
?
$@"{L()}o o {R()}{"\n"}" +
$@"{L()}LL-------------+-------------JJ\{R()}{"\n"}" +
$@"{L()}\\ //{R()}{"\n"}" +
$@"{L()}| \ / |{R()}{"\n"}"
:
$@"{L()} o o{R()}{"\n"}" +
$@"{L()}/LL-------------+-------------JJ{R()}{"\n"}" +
$@"{L()}\\ //{R()}{"\n"}" +
$@"{L()}| \ / |{R()}{"\n"}");
Console.WriteLine(Ground);
Console.WriteLine();
Console.WriteLine(frame_a
? " *** Mash [A]+[D] or [Left]+[Right] ***"
: " ''' Mash [A]+[D] or [Left]+[Right] '''");
Thread.Sleep(500);
sleeps++;
frame_a = !frame_a;
}
bool win = position < 0;
double seconds = (DateTime.Now - start).TotalSeconds;
double average = presses / seconds;
Console.Clear();
Console.WriteLine();
Console.WriteLine(" Tug Of War");
Console.WriteLine();
Console.Write(win
?
$@"{L()}o {R()}{"\n"}" +
$@"{L()}LL------------+------. o___ {R()}{"\n"}" +
$@"{L()}\\ \// \\__{R()}{"\n"}" +
$@"{L()}| \ \_____\ {R()}{"\n"}"
:
$@"{L()} o{R()}{"\n"}" +
$@"{L()} ___o .------+------------JJ{R()}{"\n"}" +
$@"{L()}__// \\/ //{R()}{"\n"}" +
$@"{L()} /_____/ / |{R()}{"\n"}");
Console.WriteLine(Ground);
Console.WriteLine();
Console.WriteLine(" You " + (win ? "Win!" : "Lose!"));
Console.WriteLine($" Average: {average:0.##} mashes per second");
Console.WriteLine(" [enter] return to menu");
Console.WriteLine(" [escape] exit game");
bool enterPressed = false;
while (!enterPressed)
{
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.Enter: enterPressed = true; break;
case ConsoleKey.Escape: return;
}
}
}
}
catch (Exception e)
{
exception = e;
throw;
}
finally
{
Console.CursorVisible = true;
Console.Clear();
Console.WriteLine(exception?.ToString() ?? "Tug Of War was closed.");
}