-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
232 lines (192 loc) · 6.92 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
using System.Diagnostics;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using math_game;
MathGameLogic mathGame = new();
Random random = new();
int firstNumber, secondNumber, userMenuSelection, score = 0;
bool gameOver = false;
DiffcultyLevel diffcultyLevel = DiffcultyLevel.Easy;
while(!gameOver)
{
userMenuSelection = GetUserMenuSelection(mathGame);
firstNumber = random.Next(1, 101);
secondNumber = random.Next(1, 101);
switch(userMenuSelection)
{
case 1:
score += await PerformOperation(mathGame, firstNumber, secondNumber, score, '+', diffcultyLevel);
break;
case 2:
score += await PerformOperation(mathGame, firstNumber, secondNumber, score, '-', diffcultyLevel);
break;
case 3:
score += await PerformOperation(mathGame, firstNumber, secondNumber, score, '*', diffcultyLevel);
break;
case 4:
while(firstNumber % secondNumber != 0)
{
firstNumber = random.Next(1, 101);
secondNumber = random.Next(1, 101);
}
score += await PerformOperation(mathGame, firstNumber, secondNumber, score, '/', diffcultyLevel);
break;
case 5:
int numberOfQuestions = 99;
Console.WriteLine("please enter the number of question you want to attempt.");
while(!int.TryParse(Console.ReadLine(),out numberOfQuestions))
{
Console.WriteLine("please enter the number of questions you want to attempt as an intger number");
}
while(numberOfQuestions > 0){
int randomOperation = random.Next(1, 5);
if(randomOperation == 1)
{
firstNumber = random.Next(1, 101);
secondNumber = random.Next(1, 101);
score += await PerformOperation(mathGame, firstNumber, secondNumber, score, '+', diffcultyLevel);
}
else if(randomOperation == 2)
{
firstNumber = random.Next(1, 101);
secondNumber = random.Next(1, 101);
score += await PerformOperation(mathGame, firstNumber, secondNumber, score, '-', diffcultyLevel);
}
else if(randomOperation == 3)
{
firstNumber = random.Next(1, 101);
secondNumber = random.Next(1, 101);
score += await PerformOperation(mathGame, firstNumber, secondNumber, score, '*', diffcultyLevel);
}
else
{
firstNumber = random.Next(1, 101);
secondNumber = random.Next(1, 101);
while(firstNumber % secondNumber != 0)
{
firstNumber = random.Next(1, 101);
secondNumber = random.Next(1, 101);
}
score += await PerformOperation(mathGame, firstNumber, secondNumber, score, '/', diffcultyLevel);
}
numberOfQuestions--;
}
break;
case 6:
Console.WriteLine("GAME HISTORY: \n");
foreach( var operation in mathGame.GameHistory)
{
Console.WriteLine($"{operation}");
}
break;
case 7:
diffcultyLevel = ChangeDifficulty();
DiffcultyLevel diffcultyEnum = (DiffcultyLevel)diffcultyLevel;
Enum.IsDefined(typeof(DiffcultyLevel), diffcultyEnum);
Console.WriteLine($"Your new difficulty level: {diffcultyLevel}");
break;
case 8:
gameOver = true;
Console.WriteLine($"Your final score is: {score}");
break;
}
}
static DiffcultyLevel ChangeDifficulty()
{
int userSelection = 0;
Console.WriteLine("please enter a difficulty level");
Console.WriteLine("1. Easy");
Console.WriteLine("2. Medium");
Console.WriteLine("3. Hard");
while(!int.TryParse(Console.ReadLine(), out userSelection) || (userSelection < 1 || userSelection > 3) )
{
Console.WriteLine("please enter a valid option 1-3");
}
return userSelection switch
{
1 => DiffcultyLevel.Easy,
2 => DiffcultyLevel.Medium,
3 => DiffcultyLevel.Hard,
_ => DiffcultyLevel.Easy,
};
}
static void DisplayMathGameQuestion(int firstNumber, int secondNumber, char operation)
{
Console.WriteLine($"{firstNumber} {operation} {secondNumber} = ??");
}
static int GetUserMenuSelection(MathGameLogic mathGame)
{
int selection = -1;
mathGame.ShowMenu();
while(selection < 1 || selection > 8)
{
while(!int.TryParse(Console.ReadLine(), out selection))
{
Console.WriteLine("Please enter a valid option 1-8");
}
if(!(selection >= 1 && selection <= 8))
{
Console.WriteLine("Please enter a valid option 1-8");
}
}
return selection;
}
static async Task<int?> GetUserResponse(DiffcultyLevel difficulty)
{
int response = 0;
int timeout = (int)difficulty;
Stopwatch stopwatch = new();
stopwatch.Start();
Task<string?> getUserInputTask = Task.Run(() => Console.ReadLine());
try
{
string? result = await Task.WhenAny(getUserInputTask, Task.Delay(timeout * 1000)) == getUserInputTask ? getUserInputTask.Result : null;
stopwatch.Stop();
if(result != null && int.TryParse(result, out response))
{
//Console.WriteLine($"Time taken to answer: {stopwatch.Elapsed.ToString(@"m\::ss\.fff")}");
Console.WriteLine($"Time taken to answer: {stopwatch.Elapsed.ToString(@"m\:ss\.fff")}");
return response;
}
else
{
throw new OperationCanceledException();
}
}
catch(OperationCanceledException)
{
Console.WriteLine("Time is up");
return null;
}
}
static int ValidateResult(int result, int? userRepose, int score)
{
if (result == userRepose)
{
Console.WriteLine("you answered correctly;\n you earned 5 points");
score += 5;
}
else
{
Console.WriteLine("Try again!");
Console.WriteLine($"Correct answer is: {result}");
}
return score;
}
static async Task<int> PerformOperation(MathGameLogic mathGame, int firstNumber, int secondNumber, int score, char operation, DiffcultyLevel diffculty)
{
int result;
int? userResponse;
DisplayMathGameQuestion(firstNumber, secondNumber, operation);
result = mathGame.MathOperation(firstNumber, secondNumber, operation);
userResponse = await GetUserResponse(diffculty);
score += ValidateResult(result, userResponse, score);
return score;
}
public enum DiffcultyLevel
{
Easy = 45,
Medium = 30,
Hard = 15
}