-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
331 lines (310 loc) · 12 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
namespace ReflexGame
{
class Program
{
static void Main(string[] args)
{
Game game = new();
game.Start();
}
}
class Game
{
private readonly static Dictionary<string, ConsoleKey> _keyMap =
new()
{
{ "Escape", ConsoleKey.Escape },
{ "Spacebar", ConsoleKey.Spacebar },
{ "Backspace", ConsoleKey.Backspace },
{ "Tab", ConsoleKey.Tab },
{ "RightArrow", ConsoleKey.RightArrow },
{ "LeftArrow", ConsoleKey.LeftArrow }
};
private readonly static string path = Environment.CurrentDirectory + @"\scores.txt";
private List<double> scores = [];
private List<string> sequence = [];
private Dictionary<string, ConsoleKey> randomMap = [];
public void Start()
{
ShowWelcomeMessage();
ShowInstructions();
Controls();
bool restartLoop = true;
while (restartLoop)
{
scores = LoadScores();
Random r = new();
GenerateMap(r);
Console.WriteLine("\n");
Console.WriteLine("WAIT FOR IT...\n");
int N = r.Next(1000, 10000);
Thread.Sleep(N);
GenerateSequence();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("LET'S GOOOO !!");
Console.ResetColor();
// start timer
var watch = System.Diagnostics.Stopwatch.StartNew();
// read space from use
List<string> enteredValues = [];
int count = 0;
while (count != 10)
{
if (Console.KeyAvailable)
{
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
if (keyInfo.Key == ConsoleKey.Q) // If Q is pressed, break the loop
{
restartLoop = false;
break;
}
else if (keyInfo.Key == ConsoleKey.Enter) // If Enter is pressed, restart the loop
{
restartLoop = true;
break;
}
else if (keyInfo.KeyChar == 'S' || keyInfo.KeyChar == 's')
{
ScoreBoard();
restartLoop = true;
break;
}
else if (keyInfo.KeyChar == 'C' || keyInfo.KeyChar == 'c')
{
ClearScores();
restartLoop = true;
break;
}
string enteredKey = keyInfo.Key.ToString();
enteredValues.Add(enteredKey);
count++;
// Console.WriteLine(enteredKey, sequence[count - 1]);
// Console.WriteLine(enteredKey.Equals(sequence[count - 1]));
if (enteredKey.Equals(sequence[count - 1]))
{
Console.ForegroundColor = ConsoleColor.Green;
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
}
Console.Write(enteredKey);
Console.Write(" ");
Console.ResetColor();
}
}
Console.WriteLine("\n");
if (!restartLoop)
{
break;
}
bool sequenceCorrect = sequence.SequenceEqual(enteredValues);
if (sequenceCorrect)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Correct Sequence.");
}
Console.ResetColor();
// stop timer
watch.Stop();
TimeSpan ts = watch.Elapsed;
// Format and display the TimeSpan value.
string elapsedTime = string.Format(
"{0:00}.{1:00}",
ts.Seconds,
ts.Milliseconds / 10
);
double parsedValue = Math.Round(double.Parse(elapsedTime), 2);
if (!sequenceCorrect)
{
if (!restartLoop)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("You lost. Try again!");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("Restarting... ");
ConsoleUtility c = new();
c.WriteProgress(0);
for (var i = 0; i <= 40; ++i)
{
c.WriteProgress(i, true);
Thread.Sleep(50);
}
Console.ResetColor();
}
}
else
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine($"You took {elapsedTime}s to complete.");
Console.ResetColor();
// handle empty txt file case
double highScore = double.MaxValue;
if (scores.Count > 0)
{
highScore = scores.Min();
}
Console.ForegroundColor = ConsoleColor.Yellow;
if (parsedValue > highScore)
{
Console.WriteLine(
"You couldn't beat the lowest SpeedTime: " + highScore + "s."
);
}
else
{
Console.WriteLine("Congrats! You have set a new SpeedTime.");
}
Console.ResetColor();
// Save score to text file
string txt = parsedValue + Environment.NewLine;
using StreamWriter sw = File.AppendText(path);
sw.WriteLine(txt);
}
}
End();
}
public static void ShowWelcomeMessage()
{
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine(
@"
WELCOME TO
____ __ _
| _ \ ___ / _| | _____ __
| |_) / _ \ |_| |/ _ \ \/ /
| _ < __/ _| | __/> <
|_| \_\___|_| |_|\___/_/\_\
"
);
Console.ResetColor();
}
public static void ShowInstructions()
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine(
"INSTRUCTIONS\n1. Press the keys in sequence.\n2. Sequence is always 10 keys long.\n3. Sequence always contains any of the 6 keys: ESCAPE, SPACE, RIGHTARROW, LEFTARROW, TAB & BACKSPACE.\n4. Game might randomly start anytime within 10 seconds from now.\n"
);
Console.ResetColor();
LoadingProgress();
Console.WriteLine("\n");
Console.ResetColor();
}
private static void LoadingProgress()
{
ConsoleUtility consoleUtility = new();
consoleUtility.WriteProgressBar(0);
for (var i = 0; i <= 100; ++i)
{
consoleUtility.WriteProgressBar(i, true);
Thread.Sleep(50);
}
}
private static void Controls()
{
Console.ForegroundColor = ConsoleColor.DarkBlue;
Console.WriteLine("----------------------------------");
Console.WriteLine("| CONTROLS |");
Console.WriteLine("----------------------------------");
Console.WriteLine("| Q - Quit Game |");
Console.WriteLine("| R - Restart Game |");
Console.WriteLine("| S - Show Scoreboard |");
Console.WriteLine("| C - Clear Scores |");
Console.WriteLine("|--------------------------------|");
Console.ResetColor();
}
private void ScoreBoard()
{
var top10 = scores.OrderBy(o => o).Take(10);
Console.WriteLine("----------------------------------");
Console.WriteLine("| TOP 10 SCOREBOARD |");
Console.WriteLine("----------------------------------");
foreach (var item in top10)
{
Console.WriteLine($"| {item} |");
}
Console.WriteLine("|--------------------------------|");
}
private void ClearScores()
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\nAre you sure you want to clear all Scores? (Y/N)");
char res = Console.ReadKey(true).KeyChar;
if (res == 'Y' || res == 'y')
{
Console.WriteLine("\nClearing Scores...\n");
File.WriteAllText(path, String.Empty);
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Scores cleared!\n");
Console.ResetColor();
}
}
private void GenerateSequence()
{
sequence.Clear();
Console.ForegroundColor = ConsoleColor.DarkMagenta;
foreach (var item in randomMap.Keys.Select((value, i) => new { i, value }))
{
string seqItem = item.value.Remove(item.value.Length - 1);
Console.Write(seqItem.ToUpper());
sequence.Add(seqItem);
if (item.i != randomMap.Keys.Count - 1)
{
Console.Write("--");
}
}
Console.WriteLine("\n");
Console.ResetColor();
}
private List<double> LoadScores()
{
List<double> scores = [];
if (!File.Exists(path))
{
File.WriteAllText(path, "0");
}
var lines = File.ReadAllLines(path);
for (var i = 0; i < lines.Length; i += 1)
{
if (lines[i] != "")
{
var line = double.Parse(lines[i]);
scores.Add(line);
}
}
return scores;
}
private void GenerateMap(Random r)
{
randomMap.Clear();
for (int i = 0; i < 10; i++)
{
int j = r.Next(0, 6);
randomMap.Add($"{_keyMap.Keys.ElementAt(j)}{i}", _keyMap.Values.ElementAt(j));
}
}
private static void End()
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(
@"
Thank you for playing Reflex! ^_^
_ _ _
| | | | | |
| |_| |__ __ _ _ __ | | ___ _ ___ _ _
| __| '_ \ / _` | '_ \| |/ / | | |/ _ \| | | |
| |_| | | | (_| | | | | <| |_| | (_) | |_| |
\__|_| |_|\__,_|_| |_|_|\_\\__, |\___/ \__,_|
__/ |
|___/
"
);
Console.ResetColor();
}
}
}