-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathGame.cs
More file actions
239 lines (209 loc) · 8.67 KB
/
Copy pathMathGame.cs
File metadata and controls
239 lines (209 loc) · 8.67 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace GameProject
{
public enum Difficulty { Easy, Medium, Hard }
public class Levels
{
//main proberty
public int Min { get; }
public int Max { get; }
public int NumberOfQuestions { get; }
public char[] Operators { get; }
public static int correctAnswersInLevel=0;
Difficulty TypeOfQuestion;
public static int totalQuestionsPlayed = 0, correctAnswers = 0, incorrectAnswers = 0;
public static int additionCount = 0, subtractionCount = 0, multiplicationCount = 0, divisionCount = 0;
public Levels()
{
Operators = new char[4];
}
public Levels(int min,int max,int numberOfQuestions, char[] operators, Difficulty typeOfQuestion)
{
Min = min;
Max = max;
NumberOfQuestions = numberOfQuestions;
Operators = operators;
TypeOfQuestion=typeOfQuestion;
}
}
public class MathGame
{
#region Variables
Levels easy = new Levels(1, 10, 5, new char[] { '+', '-' }, Difficulty.Easy);
Levels medium= new Levels(1, 10, 6, new char[] { '+', '-', '*' }, Difficulty.Medium);
Levels hard = new Levels(1, 20, 7, new char[] { '+', '-', '*', '/' }, Difficulty.Hard);
Random rand = new Random();
#endregion
#region Methods
void StartWelcome()
{
Console.WriteLine("Welcome to the Math Game!");
Console.WriteLine("In this game, you'll practice addition, subtraction, multiplication, and division.");
Console.WriteLine("Let's begin!");
}
Levels StartLevel(Difficulty difficulty)
{
Console.Clear();
//welcome level statment
string WelcomeLevelStatment = difficulty == Difficulty.Easy ? "Welcome to the Easy Level!" :
difficulty == Difficulty.Medium ? "Welcome to the Medium Level!" :
"Welcome to the Hard Level!";
Console.WriteLine(WelcomeLevelStatment);
Console.WriteLine("Let's begin!");
//return current level
var levelDetails = difficulty == Difficulty.Easy ? easy :
difficulty == Difficulty.Medium ? medium :
hard;
return levelDetails;
}
double GetCorrectAnswear(char currentOperator,int firstNumber,int secondNumber)
{
double correctAnswer = currentOperator == '+' ? firstNumber + secondNumber :
currentOperator == '-' ? firstNumber - secondNumber :
currentOperator == '*' ? firstNumber * secondNumber :
firstNumber / (double)secondNumber;
return correctAnswer;
}
double GetUserInput(char currentOperator, int firstNumber, int secondNumber)
{
Console.Write($"\nWhat is {firstNumber} {currentOperator} {secondNumber} : ");
bool isConvert = double.TryParse(Console.ReadLine(), out double userAnswer);
while (!isConvert)
{
Console.WriteLine("Invalid input. Please enter a valid number:");
Console.Write($"\nWhat is {firstNumber} {currentOperator} {secondNumber} : ");
isConvert = double.TryParse(Console.ReadLine(), out userAnswer);
}
return userAnswer;
}
bool IsCorrectAnswear(double correctAnswer,double userAnswer)
{
Levels.totalQuestionsPlayed++;
if (userAnswer == correctAnswer)
{
Levels.correctAnswers++;
Levels.correctAnswersInLevel++;
Console.WriteLine("Correct!");
return true;
}
else
{
Levels.incorrectAnswers++;
Console.WriteLine($"Incorrect! The correct answer is {correctAnswer}.");
return false;
}
}
void UpdateStatistics(char currentOperator)
{
if (currentOperator == '+') Levels.additionCount++;
else if (currentOperator == '-') Levels.subtractionCount++;
else if (currentOperator == '*') Levels.multiplicationCount++;
else if (currentOperator == '/') Levels.divisionCount++;
}
//
void PlayLevel(Levels levelDetails)
{
for (int i = 0; i < levelDetails.NumberOfQuestions; i++)
{
#region Get Mext Question
//get random operator char
char currentOperator = levelDetails.Operators[rand.Next(levelDetails.Operators.Length)];
int firstNumber, secondNumber;
//get random firstNumber and random secondNumber
do
{
firstNumber = rand.Next(levelDetails.Min, levelDetails.Max + 1);
secondNumber = rand.Next(levelDetails.Min, levelDetails.Max + 1);
} while (currentOperator == '/' && secondNumber == 0);
#endregion
double correctAnswer = GetCorrectAnswear(currentOperator, firstNumber, secondNumber);
double userAnswer = GetUserInput(currentOperator, firstNumber, secondNumber);
IsCorrectAnswear(correctAnswer, userAnswer);
UpdateStatistics(currentOperator);
}
}
//
bool AskToPlayAgain()
{
string continueChoice;
while (true)
{
continueChoice = Console.ReadLine().ToLower();
if (continueChoice == "y" || continueChoice == "yes" || continueChoice == "n" || continueChoice == "no")
{
break;
}
Console.WriteLine("Invalid input. Please enter 'y' or 'n':");
}
return continueChoice == "y" || continueChoice == "yes";
}
Difficulty NextLevel(Difficulty difficulty)
{
if (difficulty == Difficulty.Easy)
difficulty = Difficulty.Medium;
else if (difficulty == Difficulty.Medium)
difficulty = Difficulty.Hard;
return difficulty;
}
//
bool EvaluatePerformance(int numberOfQuestions)
{
#region Calculate Level Percentage
bool playAgain = false;
double passPersantage = 0.5;
double userPersantage = (double)Levels.correctAnswersInLevel / numberOfQuestions;
#endregion
#region Sucess Case
if (userPersantage >= passPersantage)
{
Console.WriteLine($"Congratulations! You've passed this level with {userPersantage:P} correct answers.");
Console.Write("\nDo you want to move to the next level? (y/n): ");
playAgain= AskToPlayAgain();
}
#endregion
#region Failure Case
else
{
Console.WriteLine($"Sorry, you didn't pass this level. You needed at least {passPersantage:P} correct answers.");
playAgain = false;
}
#endregion
return playAgain;
}
//
void ShowStatistics()
{
Console.WriteLine("\nGame Statistics:");
Console.WriteLine($"Total Questions Played: {Levels.totalQuestionsPlayed}");
Console.WriteLine($"Correct Answers: {Levels.correctAnswers}");
Console.WriteLine($"Incorrect Answers: {Levels.incorrectAnswers}");
Console.WriteLine($"Addition Count: {Levels.additionCount}");
Console.WriteLine($"Subtraction Count: {Levels.subtractionCount}");
Console.WriteLine($"Multiplication Count: {Levels.multiplicationCount}");
Console.WriteLine($"Division Count: {Levels.divisionCount}");
}
#endregion
public void Start()
{
StartWelcome();
bool playAgain = true;
Difficulty oDifficulty = Difficulty.Easy;
#region Play Level
while (playAgain)
{
var levelDetails = StartLevel(oDifficulty);
PlayLevel(levelDetails);
playAgain = EvaluatePerformance(levelDetails.NumberOfQuestions);
if (playAgain)
oDifficulty = NextLevel(Difficulty.Easy);
}
#endregion
ShowStatistics();
}
}
}