-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
411 lines (350 loc) · 11.9 KB
/
Copy pathProgram.cs
File metadata and controls
411 lines (350 loc) · 11.9 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
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
/*
V You need to create a game that consists of asking the player what's the result of a math question (i.e. 9 x 9 = ?), collecting the input and adding a point in case of a correct answer.
V A game needs to have at least 5 questions.
V The divisions should result on INTEGERS ONLY and dividends should go from 0 to 100. Example: Your app shouldn't present the division 7/2 to the user, since it doesn't result in an integer.
V Users should be presented with a menu to choose an operation
V You should record previous games in a List and there should be an option in the menu for the user to visualize a history of previous games.
You don't need to record results on a database. Once the program is closed the results will be deleted.
-------------------------------------------
V Levels of difficulty
V timer
V random game option (random operator)
- DRY principle
*/
//using System.Timers;
using System.Diagnostics;
Stopwatch timer = new Stopwatch();
int menuItems = 6;
string? readResult = null;
bool keepPlaying = false;
Random random = new Random();
//variable to save previous games
List<string> gameHistory = new List<string>(); //to save various operations done during all games played
List<double> timeHistory = new List<double>();//to save times for each game played
List<int> pointsHistory = new List<int>(); //saves points per game played
List<int> questionsPerGame = new List<int>(); //saves questions per game
do
{
int points = 0;
int range = 0;
int questions = 5;
int selection = 0;
int difficulty = 0;
bool validInputs1 = false;
bool validInputs2 = false;
bool randomGame = false;
//get input for menu selection and difficulty
do
{
Console.WriteLine("Choose a minigame (input number):");
Console.WriteLine("1 - Addition");
Console.WriteLine("2 - Subtraction");
Console.WriteLine("3 - Multiplication");
Console.WriteLine("4 - Division");
Console.WriteLine("5 - History");
Console.WriteLine("6 - Randomized operators game");
Console.WriteLine();
readResult = Console.ReadLine();
if (readResult != null && int.TryParse(readResult.Trim(), out selection) && selection > 0 && selection <= menuItems)
{
Console.WriteLine($"You selected {selection}");
validInputs1 = true;
}
else
{
Console.WriteLine("Invalid Input during menu selection.\n");
validInputs1 = false;
continue;
}
if (selection <= 4 || selection == 6)
{
Console.WriteLine("\nChoose a level of difficulty from 1 to 3:");
readResult = Console.ReadLine();
if (readResult != null && int.TryParse(readResult.Trim(), out difficulty) && difficulty > 0 && difficulty < 4)
{
Console.WriteLine($"You selected diffuclty {difficulty}");
(range, questions) = rangeSetting(range, difficulty, questions);
validInputs2 = true;
}
else
{
Console.WriteLine("Invalid Input during difficulty selection.\n");
validInputs2 = false;
}
}
else
{
validInputs2 = true;
}
} while (validInputs1 != true || validInputs2 != true);
//start game, generates random numbers based on difficulty selection and handles points
if (selection <= 4 || selection == 6)
{
timer.Start();
if (selection == 6)
randomGame = true;
points = mathGame(points, selection, difficulty, questions, range, randomGame);
timer.Stop();
Console.WriteLine($"You took {timer.Elapsed.TotalSeconds:F} seconds");
//save data for history
timeHistory.Add(timer.Elapsed.TotalSeconds);
timer.Reset(); //reset timer after saving it
pointsHistory.Add(points);
questionsPerGame.Add(questions);
}
//print game history, leaves a space every 5
else if (selection == 5)
{
if (gameHistory.Count == 0)
{
Console.WriteLine("No games played yet!");
}
else
{
Console.WriteLine("Here is the history of the calculations you made: \n");
int counter = 0;
int fullGameData = 0;
int questionsInThisGame = questionsPerGame[fullGameData];
foreach (string calculation in gameHistory)
{
counter++;
Console.WriteLine(calculation);
if (counter == questionsInThisGame)
{
Console.WriteLine($"You did {pointsHistory[fullGameData]} points out of {questionsInThisGame}");
Console.WriteLine($"And took: {timeHistory[fullGameData]:F} seconds");
Console.WriteLine("");
fullGameData++;
counter = 0;
if (fullGameData < questionsPerGame.Count)
questionsInThisGame = questionsPerGame[fullGameData];
}
}
}
}
Console.WriteLine("wanna do something else? Press anything to keep playing, write \"EXIT\" to exit");
readResult = Console.ReadLine();
//check user input for possible extra game - on invalid input or "n" closes the app
if (readResult != null && readResult.ToLower().Trim() == "exit")
{
keepPlaying = false;
}
else
{
keepPlaying = true;
}
} while (keepPlaying == true);
Console.WriteLine("Thanks for playing!");
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//game logic, redirects code based on the menu selection using switch case
int mathGame(int points, int selection, int difficulty, int questions, int range, bool randomGame)
{
string operation = "";
int totalQuestions = questions;
do
{
if (randomGame == true)
{
selection = random.Next(1, 5);
}
bool rightAnswer = false;
switch (selection)
{
case 1:
operation = "+";
break;
case 2:
operation = "-";
break;
case 3:
operation = "*";
break;
case 4:
operation = "/";
break;
}
rightAnswer = Calculation(range, operation);
if (rightAnswer)
points++;
Console.WriteLine($"You made {points} points out of {totalQuestions} \n");
questions--;
} while (questions > 0);
return points;
}
//handles additions, checks if user input is equal to result
bool Calculation(int range, string operation)
{
int n1 = random.Next(0, range);
int n2 = random.Next(0, range);
int givenResult = 0;
if (operation == "-")
{
if (n2 > n1)
{
int temp = n1;
n1 = n2;
n2 = temp;
}
}
else if (operation == "/")
{
n2 = random.Next(1, 15);
n1 = n2 * random.Next(1, range / n2);
}
int result = operation switch
{
"+" => n1 + n2,
"-" => n1 - n2,
"*" => n1 * n2,
"/" => n1 / n2,
_ => 0
};
Console.WriteLine(n1 + operation + n2);
readResult = Console.ReadLine();
if (readResult != null && int.TryParse(readResult, out givenResult) && givenResult == result)
{
Console.WriteLine("You gave the rigt answer!");
gameHistory.Add($"{n1} {operation} {n2} = {result} - result given: {givenResult} - right");
return true;
}
else if (readResult != null && int.TryParse(readResult, out givenResult) && givenResult != result)
{
Console.WriteLine("You gave a wrong answer!");
gameHistory.Add($"{n1} {operation} {n2} = {result} - result given: {givenResult} - wrong");
return false;
}
else
{
Console.WriteLine("You didn't even give a number!");
gameHistory.Add($"{n1} {operation} {n2} = {result} - result given: // - false");
return false;
}
}
//sets range based on difficulty chosen by user
(int, int) rangeSetting(int range, int difficulty, int questions)
{
(range, questions) = difficulty switch
{
1 => (21, 5),
2 => (51, 7),
3 => (101, 10),
_ => (21, 5)
};
return (range, questions);
}
/*
//sets range based on difficulty chosen by user
(int, int) rangeSetting(int range, int difficulty, int questions)
{
(if (difficulty == 1)
{
range = 21;
questions = 5;
}
else if (difficulty == 2)
{
range = 51;
questions = 7;
}
else if (difficulty == 3)
{
range = 101;
questions = 10;
})
return (range, questions);
}
//handles Subtractions, checks if user input is equal to result
bool Subtraction(int range)
{
int n1 = random.Next(0, range);
int n2 = random.Next(0, range);
int result = 0;
//switches numbers in case n2 is bigger than n1, making sure n1 is always the bigger one
if (n2 > n1)
{
int temp = n1;
n1 = n2;
n2 = temp;
}
previousNumbers[currentSlot, 0] = n1;
previousNumbers[currentSlot, 1] = n2;
Console.WriteLine(n1 + "-" + n2);
readResult = Console.ReadLine();
if (readResult != null && int.TryParse(readResult, out result) && result == n1 - n2)
{
Console.WriteLine("You gave the rigt answer!");
previousResultGiven[currentSlot] = readResult;
return true;
}
else if (readResult != null && int.TryParse(readResult, out result) && result != n1 - n2)
{
Console.WriteLine("You gave a wrong answer!");
previousResultGiven[currentSlot] = readResult;
return false;
}
else
{
Console.WriteLine("You didn't even give a number!");
previousResultGiven[currentSlot] = "//";
return false;
}
}
//handles Multiplications, checks if user input is equal to result
bool Multiplication(int range)
{
int n1 = random.Next(0, range);
int n2 = random.Next(0, range);
int result = 0;
previousNumbers[currentSlot, 0] = n1;
previousNumbers[currentSlot, 1] = n2;
Console.WriteLine(n1 + "*" + n2);
readResult = Console.ReadLine();
if (readResult != null && int.TryParse(readResult, out result) && result == n1 * n2)
{
Console.WriteLine("You gave the rigt answer!");
previousResultGiven[currentSlot] = readResult;
return true;
}
else if (readResult != null && int.TryParse(readResult, out result) && result != n1 * n2)
{
Console.WriteLine("You gave a wrong answer!");
previousResultGiven[currentSlot] = readResult;
return false;
}
else
{
Console.WriteLine("You didn't even give a number!");
previousResultGiven[currentSlot] = "//";
return false;
}
}
//handles divisions, checks if user input is equal to result
bool Division(int range)
{
int n2 = random.Next(1, range);
int n1 = n2 * random.Next(1, range / n2);
int result = 0;
previousNumbers[currentSlot, 0] = n1;
previousNumbers[currentSlot, 1] = n2;
Console.WriteLine(n1 + "/" + n2);
readResult = Console.ReadLine();
if (readResult != null && int.TryParse(readResult, out result) && result == n1 / n2)
{
Console.WriteLine("You gave the rigt answer!");
previousResultGiven[currentSlot] = readResult;
return true;
}
else if (readResult != null && int.TryParse(readResult, out result) && result != n1 / n2)
{
Console.WriteLine("You gave a wrong answer!");
previousResultGiven[currentSlot] = readResult;
return false;
}
else
{
Console.WriteLine("You didn't even give a number!");
previousResultGiven[currentSlot] = "//";
return false;
}
}
*/