-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathGame.cpp
More file actions
288 lines (258 loc) · 7.15 KB
/
MathGame.cpp
File metadata and controls
288 lines (258 loc) · 7.15 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
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
enum enQuestionLevel{Easy=1,Medium=2,Hard=3,Mix=4};
enum enOperationType{Add=1,Sub=2,Mult=3,Div=4,MixOp=5};
struct stQuestion
{
int Number1 = 0;
int Number2 = 0;
enOperationType OperatioType;
enQuestionLevel QuestionLevel;
int CorrectAnswer = 0;
int PlayerAnswer = 0;
bool AnswerResult = false;
};
struct stQuiz
{
stQuestion QuestionsList[100];
short NumberOfQuestions;
enQuestionLevel QuestionLevel;
enOperationType OpType;
short NumberOfWrongAnswers=0;
short NumberOfRightAnswers=0;
bool isPass = false;
};
string GetOpType(enOperationType OpType)
{
switch (OpType)
{
case enOperationType::Add:
return "+";
case enOperationType::Div:
return "/";
case enOperationType::Mult:
return "*";
case enOperationType::Sub:
return "-";
default:
return "Mix";
}
}
void SetScreenColor(bool Right)
{
if (Right)
system("color 2F"); // Green for correct answers.
else
{
system("color 4F"); // Red for incorrect answers.
cout << "\a"; // Plays an alert sound.
}
}
int RandomNumber(int from, int to)
{
int randum = rand() % (to - from + 1) + from;
return randum;
}
short ReadHowManyQuestions()
{
short NumberOfQuestions;
do
{
cout << "How many questions do you want to answer? \n";
cin >> NumberOfQuestions;
} while (NumberOfQuestions < 1 || NumberOfQuestions>10);
return NumberOfQuestions;
}
string GetQuestionLevelText(enQuestionLevel QuestionLevel)
{
string arrQuestionLevelText[4] = { "Easy","Medium","Hard","Mix" };
return arrQuestionLevelText[QuestionLevel - 1];
}
enQuestionLevel ReadQuestionsLevel()
{
short QuestionLevel = 0;
do
{
cout << " Enter Q uestion Level Easy[1], Medium[2], Hard[3], Mix[4] \n";
cin >> QuestionLevel;
} while (QuestionLevel < 1 || QuestionLevel>4);
return (enQuestionLevel)QuestionLevel;
}
enOperationType ReadOperationType()
{
short OperationType = 0;
do
{
cout << "Enter Operation Type Add[1], Sub[2], Mult[3], Div[4], MixOp[5] ? \n";
cin >> OperationType;
} while (OperationType < 1 || OperationType>5);
return (enOperationType)OperationType;
}
enOperationType GetRandomOperationType()
{
int Op = RandomNumber(1, 4);
return (enOperationType)Op;
}
int SimpleCalculator(int Number1, int Number2, enOperationType OpType)
{
switch (OpType)
{
case enOperationType::Add:
return Number1 + Number2;
case enOperationType::Sub:
return Number1 - Number2;
case enOperationType::Div:
return Number1 / Number2;
case enOperationType::Mult:
return Number1 * Number2;
default:
return Number1 + Number2;
}
}
stQuestion GenerateQuestion(enQuestionLevel QuestionLevel,enOperationType OpType)
{
stQuestion Question;
if (QuestionLevel == enQuestionLevel::Mix)
{
QuestionLevel = (enQuestionLevel)RandomNumber(1, 3);
}
if (OpType == enOperationType::MixOp)
{
OpType=(enOperationType)RandomNumber(1,4);
}
Question.OperatioType = OpType;
switch (QuestionLevel)
{
case enQuestionLevel::Easy:
Question.Number1 = RandomNumber(1, 10);
Question.Number2 = RandomNumber(1, 10);
Question.CorrectAnswer = SimpleCalculator(Question.Number1, Question.Number2, Question.OperatioType);
Question.QuestionLevel = QuestionLevel;
return Question;
case enQuestionLevel::Medium:
Question.Number1 = RandomNumber(10, 50);
Question.Number2 = RandomNumber(10, 50);
Question.CorrectAnswer = SimpleCalculator(Question.Number1, Question.Number2, Question.OperatioType);
Question.QuestionLevel = QuestionLevel;
return Question;
case enQuestionLevel::Hard:
Question.Number1 = RandomNumber(50, 100);
Question.Number2 = RandomNumber(50, 100);
Question.CorrectAnswer = SimpleCalculator(Question.Number1, Question.Number2, Question.OperatioType);
Question.QuestionLevel = QuestionLevel;
return Question;
}
return Question;
}
void GenerateQuizzQuestions(stQuiz& Quiz)
{
for (short Question = 0; Question <= Quiz.NumberOfQuestions; Question++)
{
Quiz.QuestionsList[Question] = GenerateQuestion(Quiz.QuestionLevel, Quiz.OpType);
}
}
int ReadQuestionAnswer()
{
int Answer = 0;
cin >> Answer;
return Answer;
}
void PrintQuestions(stQuiz &Quiz, short QuestionNumber)
{
cout << "\n";
cout << "Question [" << QuestionNumber + 1 << " / " << Quiz.NumberOfQuestions << " ] \n\n";
cout << Quiz.QuestionsList[QuestionNumber].Number1 << endl;
cout << Quiz.QuestionsList[QuestionNumber].Number2 << " \n";
cout << GetOpType(Quiz.QuestionsList[QuestionNumber].OperatioType);
cout << "\n_______________" << endl;
}
void CorrectTheQuestionAnswer(stQuiz& Quiz,short QuestionNumber)
{
if (Quiz.QuestionsList[QuestionNumber].PlayerAnswer != Quiz.QuestionsList[QuestionNumber].CorrectAnswer)
{
Quiz.QuestionsList[QuestionNumber].AnswerResult = false;
Quiz.NumberOfWrongAnswers++;
cout << "Wrong Answer :(\n";
cout << "The Wright Answer is: ";
cout<<Quiz.QuestionsList[QuestionNumber].CorrectAnswer;
cout << "\n";
}
else
{
Quiz.QuestionsList[QuestionNumber].AnswerResult = true;
Quiz.NumberOfRightAnswers++;
cout << "Right Answer :))\n";
}
cout << endl;
SetScreenColor(Quiz.QuestionsList[QuestionNumber].AnswerResult);
}
void AskAndCorrectQuestionsListAnswers(stQuiz& Quiz)
{
for (short QuestionNumber = 0; QuestionNumber < Quiz.NumberOfQuestions; QuestionNumber++)
{
PrintQuestions(Quiz,QuestionNumber);
Quiz.QuestionsList[QuestionNumber].PlayerAnswer = ReadQuestionAnswer();
CorrectTheQuestionAnswer(Quiz, QuestionNumber);
}
Quiz.isPass = (Quiz.NumberOfRightAnswers >= Quiz.NumberOfWrongAnswers);
/*
if (Quiz.NumberOfRightAnswers >= Quiz.NumberOfWrongAnswers)
Quiz.isPass = true;
else
Quiz.isPass = false;
*/
}
string GetFinalResultText(bool Pass)
{
if (Pass)
return "PASS :))";
else
return "FAIL :((";
}
void PrintQuizResult(stQuiz Quiz)
{
cout<<"\n";
cout << "_____________________________________\n\n";
cout << "Final Result is: " << GetFinalResultText(Quiz.isPass) ;
cout << "_______________________________\n";
cout << "Number Of Questions: " << Quiz.NumberOfQuestions << endl;
cout << "Question Level : " << GetQuestionLevelText(Quiz.QuestionLevel) << endl;
cout << "OpType: " << GetOpType(Quiz.OpType) << endl;
cout << "Number Of right Answers: " << Quiz.NumberOfRightAnswers << endl;
cout << " Number Of Wrong Answers: " << Quiz.NumberOfWrongAnswers << endl;
cout << "______________________________________________\n";
}
void PlayGameMath()
{
stQuiz Quizz;
Quizz.NumberOfQuestions = ReadHowManyQuestions();
Quizz.QuestionLevel = ReadQuestionsLevel();
Quizz.OpType = ReadOperationType();
GenerateQuizzQuestions(Quizz);
AskAndCorrectQuestionsListAnswers(Quizz);
PrintQuizResult(Quizz);
}
void ResetScreen()
{
system("cls");
system("color 0F");
}
void StartGame()
{
char Playagain = 'Y';
do
{
ResetScreen();
PlayGameMath();
cout << endl << "DO You Want To Play Again N/Y: " << endl;
cin >> Playagain;
} while (Playagain == 'Y' || Playagain == 'y');
}
int main()
{
srand((unsigned)time(NULL));
StartGame();
return 0;
}