-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoultionsOhtersProject2MathGames.cpp
336 lines (304 loc) · 10.4 KB
/
SoultionsOhtersProject2MathGames.cpp
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
#include <iostream>
#include <cstdlib>
using namespace std;
enum enLevelQuestions {
Easy = 1,
Med = 2,
Hard = 3,
Mix = 4
};
enum enTypeQuestions {
Add = 1,
Sub = 2,
Mul = 3,
Div = 4,
OpMix = 5
};
struct stQuestions {
int Number[3] = { 0, 0 }; // تهيئة الأرقام
short NumberOfQuestions = 0;
enLevelQuestions LevelQuestions = Easy; // تهيئة إلى مستوى "Easy" كقيمة افتراضية
enTypeQuestions OpType = Add; // تهيئة إلى العملية "Add" كقيمة افتراضية
int correctRights = 0;
int AnswerPlayer = 0;
bool PassOrFeild = false;
// مُنشئ لتهيئة القيم بشكل افتراضي
stQuestions() : NumberOfQuestions(0), LevelQuestions(Easy), OpType(Add), correctRights(0), AnswerPlayer(0), PassOrFeild(false) {
Number[0] = 0;
Number[1] = 0;
}
};
struct stQuiz {
stQuestions Questions[100]; // مصفوفة الأسئلة
short NumberOfQuestions = 0;
enLevelQuestions LevelQuestions = Easy; // تهيئة إلى مستوى "Easy" كقيمة افتراضية
enTypeQuestions OpType = Add; // تهيئة إلى العملية "Add" كقيمة افتراضية
int AnswerRights = 0;
int AnswerWrong = 0;
bool finallyResult = true;
// مُنشئ لتهيئة القيم بشكل افتراضي
stQuiz() : NumberOfQuestions(0), LevelQuestions(Easy), OpType(Add), AnswerRights(0), AnswerWrong(0), finallyResult(true) {}
};
string GetOpTypeSymbol(enTypeQuestions OpType)
{
switch (OpType)
{
case Add:
return "+";
case Sub:
return "-";
case Mul:
return "x";
case Div:
return "/";
default:
return "Mix";
}
}
short RandomNumber(short From, short To)
{
return rand() % (To - From + 1) + From;
}
short HowManyQuestions()
{
short NumberQuestion;
cout << "How Many Questions want ?";
cin >> NumberQuestion;
return NumberQuestion;
}
int SimpleCalculator(int Number1, int Number2, enTypeQuestions OpType)
{
if (OpType == Div) {
if (Number2 == 0) {
cout << "Cannot divide by zero. Setting answer to 0 by default." << endl;
return 0;
}
return Number1 / Number2;
}
switch (OpType) {
case Add: return Number1 + Number2;
case Sub: return Number1 - Number2;
case Mul: return Number1 * Number2;
default: return 0;
}
}
string GetQuestionLevelText(enLevelQuestions QuestionLevel)
{
string arrQuestionLevelText[4] = { "Easy", "Med", "Hard", "Mix" };
return arrQuestionLevelText[QuestionLevel - 1];
}
enLevelQuestions AskQuestionsLevel()
{
short Level;
do
{
cout << "Enter Questions Level [1] Easy, [2] Med, [3] Hard, [4] Mix ?";
cin >> Level;
} while (Level < 1 || Level > 4);
return static_cast<enLevelQuestions>(Level);
}
enTypeQuestions TypeQuestions()
{
short Type;
do
{
cout << "Enter Operation type [1] Add, [2] Sub, [3] Mul, [4] Div, [5] Mix ?";
cin >> Type;
} while (Type < 1 || Type > 5);
return static_cast<enTypeQuestions>(Type);
}
int AnswerPlay() {
int Answer;
while (!(cin >> Answer)) {
cout << "Please enter a valid number: ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
return Answer;
}
void SetScreenColor(bool Right)
{
if (Right)
system("color 2F"); // turn screen to Green
else
{
system("color 4F"); // turn screen to Red
cout << "\a";
}
}
int ComplexCalculator(int Number1, int Number2, int Number3, enTypeQuestions OpType1, enTypeQuestions OpType2) {
int result = SimpleCalculator(Number1, Number2, OpType1);
return SimpleCalculator(result, Number3, OpType2);
}
stQuestions GenerateQuestionsAsk(enLevelQuestions LevelQuestions, enTypeQuestions TypeQuestions)
{
stQuestions Questions;
// إذا كان مستوى السؤال Mix، نختار مستوى عشوائي
if (LevelQuestions == Mix)
{
LevelQuestions = static_cast<enLevelQuestions>(RandomNumber(1, 3));
}
// إذا كان نوع السؤال OpMix، نختار عملية عشوائية
if (TypeQuestions == OpMix)
{
TypeQuestions = static_cast<enTypeQuestions>(RandomNumber(1, 4));
}
Questions.OpType = TypeQuestions;
// توليد الأرقام حسب مستوى الصعوبة
switch (LevelQuestions)
{
case Easy:
Questions.Number[0] = RandomNumber(1, 30);
Questions.Number[1] = RandomNumber(1, 30);
// في حالة Mix، نستخدم ComplexCalculator
if (Questions.OpType == OpMix)
{
Questions.Number[2] = RandomNumber(1, 30); // الرقم الثالث في حالة Mix
enTypeQuestions OpType1 = static_cast<enTypeQuestions>(RandomNumber(1, 4));
enTypeQuestions OpType2 = static_cast<enTypeQuestions>(RandomNumber(1, 4));
Questions.correctRights = ComplexCalculator(Questions.Number[0], Questions.Number[1], Questions.Number[2], OpType1, OpType2);
}
else
{
Questions.correctRights = SimpleCalculator(Questions.Number[0], Questions.Number[1], TypeQuestions);
}
return Questions;
case Med:
Questions.Number[0] = RandomNumber(30, 60);
Questions.Number[1] = RandomNumber(30, 60);
if (Questions.OpType == OpMix)
{
Questions.Number[2] = RandomNumber(30, 60);
enTypeQuestions OpType1 = static_cast<enTypeQuestions>(RandomNumber(1, 4));
enTypeQuestions OpType2 = static_cast<enTypeQuestions>(RandomNumber(1, 4));
Questions.correctRights = ComplexCalculator(Questions.Number[0], Questions.Number[1], Questions.Number[2], OpType1, OpType2);
}
else
{
Questions.correctRights = SimpleCalculator(Questions.Number[0], Questions.Number[1], TypeQuestions);
}
return Questions;
case Hard:
Questions.Number[0] = RandomNumber(60, 100);
Questions.Number[1] = RandomNumber(60, 100);
if (Questions.OpType == OpMix)
{
Questions.Number[2] = RandomNumber(60, 100);
enTypeQuestions OpType1 = static_cast<enTypeQuestions>(RandomNumber(1, 4));
enTypeQuestions OpType2 = static_cast<enTypeQuestions>(RandomNumber(1, 4));
Questions.correctRights = ComplexCalculator(Questions.Number[0], Questions.Number[1], Questions.Number[2], OpType1, OpType2);
}
else
{
Questions.correctRights = SimpleCalculator(Questions.Number[0], Questions.Number[1], TypeQuestions);
}
return Questions;
}
return Questions;
}
void GenerateQuestions(stQuiz& Quiz)
{
for (size_t i = 0; i < Quiz.NumberOfQuestions; i++)
{
Quiz.Questions[i] = GenerateQuestionsAsk(Quiz.LevelQuestions, Quiz.OpType);
}
}
void PrintQuestions(stQuiz Quiz, short Round)
{
cout << "\n";
cout << "Questions [" << Round + 1 << "/" << Quiz.NumberOfQuestions << endl;
cout << "\n\n";
cout << Quiz.Questions[Round].Number[0];
cout << "\n"
<< Quiz.Questions[Round].Number[1] << GetOpTypeSymbol(Quiz.Questions[Round].OpType) << endl;
cout << "________________" << endl;
}
void CorrectTheQuestionAnswer(stQuiz &Questions, short NumberQuestions)
{
if (Questions.Questions[NumberQuestions].AnswerPlayer != Questions.Questions[NumberQuestions].correctRights)
{
Questions.Questions[NumberQuestions].PassOrFeild = false;
Questions.AnswerWrong++;
cout << "Wrong Answer :-( \n";
cout << "The right answer is: ";
cout << Questions.Questions[NumberQuestions].correctRights;
cout << "\n";
}
else
{
Questions.Questions[NumberQuestions].PassOrFeild = true;
Questions.AnswerRights++;
cout << "Right Answer :-) \n";
}
cout << endl;
SetScreenColor(Questions.Questions[NumberQuestions].PassOrFeild);
}
void AskAndCorrectQuestionListAnswers(stQuiz& Quiz)
{
Quiz.NumberOfQuestions = Quiz.NumberOfQuestions;
for (short NumberQuestions = 0; NumberQuestions < Quiz.NumberOfQuestions; NumberQuestions++)
{
PrintQuestions(Quiz, NumberQuestions);
Quiz.Questions[NumberQuestions].AnswerPlayer = AnswerPlay();
CorrectTheQuestionAnswer(Quiz, NumberQuestions);
}
Quiz.finallyResult = (Quiz.AnswerRights >= Quiz.AnswerWrong);
}
string GetFinalResultsText(bool Pass)
{
if (Pass)
return "PASS :-)";
else
return "Fail :-(";
}
void PrintResultQuestions(stQuiz Quiz)
{
cout << "================ Final Results ================\n";
cout << "Number of Questions: " << Quiz.NumberOfQuestions << endl;
cout << "Questions Level : " << GetQuestionLevelText(Quiz.LevelQuestions) << endl;
cout << "Operation Type : " << GetOpTypeSymbol(Quiz.OpType) << endl;
cout << "Correct Answers : " << Quiz.AnswerRights << endl;
cout << "Incorrect Answers : " << Quiz.AnswerWrong << endl;
cout << "Final Result : " << GetFinalResultsText(Quiz.finallyResult) << endl;
cout << "================================================\n";
}
void ShowInstructions() {
cout << "Welcome to the Math Quiz Game!" << endl;
cout << "Choose the difficulty and type of operation, then answer each question." << endl;
cout << "Good luck!" << endl;
}
void PlayMathGame()
{
stQuiz Quiz;
ShowInstructions();
Quiz.NumberOfQuestions = HowManyQuestions();
Quiz.LevelQuestions = AskQuestionsLevel();
Quiz.OpType = TypeQuestions();
GenerateQuestions(Quiz);
AskAndCorrectQuestionListAnswers(Quiz);
PrintResultQuestions(Quiz);
}
void ResetScreen()
{
system("cls");
system("color 0F");
}
void StartGame()
{
char PlayAgain = 'Y';
do
{
ResetScreen();
PlayMathGame();
cout << endl
<< "Do you want to play again? Y/N? ";
cin >> PlayAgain;
} while (PlayAgain == 'Y' || PlayAgain == 'y');
}
int main()
{
// Seeds the random number generator in C++, called only once
srand((unsigned)time(NULL));
StartGame();
return 0;
}