-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProject2MathGames.cpp
311 lines (294 loc) · 6.98 KB
/
Project2MathGames.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
#include <iostream>
#include <cstdlib>
using namespace std;
enum enGameLevelChoice
{
Easy = 1,
Med = 2,
Hard = 3,
Mix = 4
};
enum enOperationType
{
Add = 1,
Sub = 2,
Mul = 3,
Div = 4,
mix = 5
};
struct stQuestionsInfo
{
short RoundNumber = 0;
short ArrayTwoNumber[2] ;
enGameLevelChoice PlayChoiceLevel ;
enOperationType OpType;
string PrintRightsOrWrong;
short StorageResult;
string PassOrFailed;
};
struct stRustleGame
{
string PassOrField;
short NumberOfQuotations = 0;
string QuestionsLevel;
string OperationType;
short NumberOfRights;
short numberOfWrong;
};
void PrintRightsOfWrong(bool Answer , stQuestionsInfo PrintAnswer)
{
if (Answer)
{
cout << "Answer is Rights :-) " << endl;
}
else
{
cout << "Answer is Wrong " << endl;
cout << "The Rights Answer is : " << PrintAnswer.StorageResult << endl;
}
}
string PrintOpType(enOperationType OpType)
{
string ArrAnswer[4] = { "+" , "-" , "x" , "/"};
return ArrAnswer[OpType - 1];
}
string PrintChoice(enGameLevelChoice Choice)
{
string ArrAnswer[4] = { "Easy" , "Med" , "Hard" , "Mix" };
return ArrAnswer[Choice - 1];
}
string Operation(enOperationType Op)
{
string ArrAnswer[5] = { "Add" , "Sub" , "Mul" , "Div" , "Mix"};
return ArrAnswer[Op - 1];
}
short QuestionsNumberWants()
{
short Answer = 0;
cout << "How Many Questions do you want to Answer ?";
cin >> Answer;
return Answer;
}
enGameLevelChoice ReadQuestionsLevel()
{
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<enGameLevelChoice>(Level);
}
enOperationType 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<enOperationType>(Type);
}
void SetWinnerScreenColor(bool Answer)
{
switch (Answer)
{
case true:
system("color 2F");
break;
default:
system("color 4F");
cout << "\a";
break;
}
}
short RandomNumber(short From, short To)
{
return rand() % (To - From + 1) + From;
}
enOperationType caseMix(stQuestionsInfo& OpType)
{
enOperationType Mixes = static_cast<enOperationType>(RandomNumber(1, 4));
switch (Mixes)
{
case Add:
OpType.StorageResult = OpType.ArrayTwoNumber[0] + OpType.ArrayTwoNumber[1];
return Add;
break ;
case Sub:
OpType.StorageResult = OpType.ArrayTwoNumber[0] - OpType.ArrayTwoNumber[1];
return Sub;
break;
case Mul:
OpType.StorageResult = OpType.ArrayTwoNumber[0] * OpType.ArrayTwoNumber[1];
return Mul;
break;
case Div:
OpType.StorageResult = OpType.ArrayTwoNumber[0] / OpType.ArrayTwoNumber[1];
return Div;
break;
}
}
short FillNumbersomeLevel(enGameLevelChoice Level)
{
short level;
switch (Level)
{
case Easy:
return level = RandomNumber(1, 30);
break;
case Med:
return level = RandomNumber(30, 60);
break;
case Hard:
return level = RandomNumber(60,100);
break;
case Mix:
return level = RandomNumber(1, 100);
break;
}
}
enOperationType SelectOpType(stQuestionsInfo &OpType)
{
switch (OpType.OpType)
{
case Add:
OpType.StorageResult = OpType.ArrayTwoNumber[0] + OpType.ArrayTwoNumber[1];
return Add;
break;
case Sub :
OpType.StorageResult = OpType.ArrayTwoNumber[0] - OpType.ArrayTwoNumber[1];
return Sub;
break;
case Mul :
OpType.StorageResult = OpType.ArrayTwoNumber[0] * OpType.ArrayTwoNumber[1];
return Mul;
break;
case Div :
OpType.StorageResult = OpType.ArrayTwoNumber[0] / OpType.ArrayTwoNumber[1];
return Div;
break;
case mix :
return caseMix(OpType);
break;
}
}
bool CheckResult(stQuestionsInfo result)
{
short readInput;
cin >> readInput;
cout << "\n";
if (readInput == result.StorageResult)
{
return true;
}
return false;
}
void PrintQuestions(stQuestionsInfo ShowQuestions)
{
cout << ShowQuestions.ArrayTwoNumber[0] << endl;
cout << ShowQuestions.ArrayTwoNumber[1] << " " << PrintOpType(SelectOpType(ShowQuestions)) << endl;;
cout << "_______________" << endl;
}
void FillArray(stQuestionsInfo &Numbers)
{
for (size_t i = 0; i < 2; i++)
{
Numbers.ArrayTwoNumber[i] = FillNumbersomeLevel(Numbers.PlayChoiceLevel);
}
}
string PrintPassOrFailed(short NumberPass , short NumberFailed)
{
if (NumberPass > NumberFailed)
{
return "Pass :-)";
}
else
return "Failed :-(";
}
bool returnTrueetc(string Pass)
{
if (Pass == "Pass :-)")
{
return true;
}
else {
return false;
}
}
stRustleGame fillResultGame(short NumbersRights , short NumberWrong , short NumberQuestion , enGameLevelChoice Choice , enOperationType Type )
{
stRustleGame Fill;
Fill.PassOrField = PrintPassOrFailed(NumbersRights, NumberWrong);
Fill.NumberOfQuotations = NumberQuestion;
Fill.QuestionsLevel = PrintChoice(Choice);
Fill.OperationType = Operation(Type);
Fill.NumberOfRights = NumbersRights;
Fill.numberOfWrong = NumberWrong;
return Fill;
}
stRustleGame PlayGame(short HowManyQuestions)
{
short NumberRights{}, NumberWrong{};
bool TrueOrFalse;
stQuestionsInfo Question;
Question.PlayChoiceLevel = ReadQuestionsLevel();
Question.OpType = TypeQuestions();
for ( short QuestionsRound = 1; QuestionsRound <= HowManyQuestions; QuestionsRound++)
{
cout << "Questions [" << QuestionsRound << "/" << HowManyQuestions << "]" << "\n\n";
Question.RoundNumber = QuestionsRound;
FillArray(Question);
PrintQuestions(Question);
SelectOpType(Question);
TrueOrFalse = (CheckResult(Question));
SetWinnerScreenColor(TrueOrFalse);
PrintRightsOfWrong(TrueOrFalse , Question);
if (TrueOrFalse == true)
{
NumberRights++;
}
else
{
NumberWrong++;
}
}
return fillResultGame(NumberRights, NumberWrong, HowManyQuestions,Question.PlayChoiceLevel, Question.OpType);
}
void PrintResultFinally(stRustleGame Result)
{
cout << "_________________________________" << endl;
cout << "Finally result is " << Result.PassOrField << endl;
cout << "_________________________________" << endl;
cout << endl;
cout << "Number Of Questions : " << Result.NumberOfQuotations << endl;
cout << "Question Level : " << Result.QuestionsLevel << endl;
cout << "OpType : " << Result.OperationType << endl;
cout << "Number Of Rights Answer : " << Result.NumberOfRights << endl;
cout << "Number Of Wrong Answer : " << Result.numberOfWrong << endl;
bool Color = returnTrueetc(Result.PassOrField);
SetWinnerScreenColor(Color);
}
void ResetScreen()
{
system("cls");
system("color 0F");
}
void StartGame()
{
char PlayAgain = 'Y';
do
{
ResetScreen();
stRustleGame GameResult = PlayGame(QuestionsNumberWants());
PrintResultFinally(GameResult);
cout << "Do you want Play Again Y/N ? ";
cin >> PlayAgain;
} while (PlayAgain == 'Y' || PlayAgain == 'y');
}
int main()
{
srand((unsigned)time(NULL));
StartGame();
return 0;
}