-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFrmPlay.cs
378 lines (344 loc) · 11.1 KB
/
FrmPlay.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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace QuizGame
{
public partial class FrmPlay : Form
{
List<IdName> listCategory = new List<IdName>();
List<IdName> listDifficulty = new List<IdName>();
List<Question> listQuestion = new List<Question>();
List<Question> randomizedListQuestion = new List<Question>();
Question currentQuestion;
int score, count, totalTime;
int lifes = 3;
public FrmPlay()
{
InitializeComponent();
LoadGameData();
InitilizeData();
}
private void ListCategoryLoad()
{
listCategory.Clear();
listCategory.Add(new IdName(0, "History"));
listCategory.Add(new IdName(1, "Geography"));
listCategory.Add(new IdName(2, "Politics"));
cbxCategory.DataSource = listCategory;
cbxCategory.DisplayMember = "Name";
cbxCategory.ValueMember = "Id";
cbxCategory.SelectedIndex = 0;
}
private void ListDifficultyLoad()
{
listDifficulty.Clear();
listDifficulty.Add(new IdName(0, "Easy"));
listDifficulty.Add(new IdName(1, "Hard"));
cbxDifficulty.DataSource = listDifficulty;
cbxDifficulty.DisplayMember = "Name";
cbxDifficulty.ValueMember = "Id";
cbxDifficulty.SelectedIndex = 0;
}
private void QuestionsDeserealization()
{
string jsonString = "";
try
{
jsonString = System.IO.File.ReadAllText(Application.StartupPath + @"\QuizQuestions.json");
listQuestion = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Question>>(jsonString);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void ListQuestionLoad()
{
listQuestion.Clear();
QuestionsDeserealization();
randomizedListQuestion = RandomizeList(listQuestion);
}
private void LoadGameData()
{
ListCategoryLoad();
ListDifficultyLoad();
ListQuestionLoad();
}
private void InitilizeData()
{
score = 0;
count = 0;
EnableButtons(false);
currentQuestion = null;
}
private Question NextQuestion(int category, int difficulty)
{
int i, count = randomizedListQuestion.Count;
StartTimer();
for (i = 0; i < count; i++)
{
if (randomizedListQuestion[i].played == false && randomizedListQuestion[i].category == category && randomizedListQuestion[i].difficulty == difficulty)
{
randomizedListQuestion[i].played = true;
break;
}
}
ResetButtonsColor();
return (i < count) ? randomizedListQuestion[i] : null;
}
private void EnableButtons(Boolean boolean)
{
BtnA.Enabled = boolean;
BtnB.Enabled = boolean;
BtnC.Enabled = boolean;
BtnD.Enabled = boolean;
}
private bool CheckCorrectAnswer(int answer)
{
bool boolean;
if (answer == currentQuestion.correctAnswer)
{
score += (currentQuestion.difficulty + 1) * 10;
txtMessage.Text = "Well Done !!!";
boolean = true;
BtnNext.Enabled = true;
if (score == 1200)
{
PlayerWon();
}
}
else
{
txtMessage.Text = "Wrong Answer !!! " + "The correct answer is: " + (char)((int)'A' + currentQuestion.correctAnswer) + ". " +
currentQuestion.answers[currentQuestion.correctAnswer];
boolean = false;
BtnNext.Enabled = true;
lifes--;
CheckLifes();
if (lifes == 0)
{
PlayerLost();
}
}
EnableButtons(false);
ShowScore();
return boolean;
}
private void Play_Load(object sender, EventArgs e)
{
InitilizeData();
LoadGameData();
ShowQuestion();
}
private void ShowQuestion()
{
txtMessage.Text = "";
if (currentQuestion == null)
{
txtQuestion.Text = "";
BtnA.Text = "A";
BtnB.Text = "B";
BtnC.Text = "C";
BtnD.Text = "D";
}
else
{
RandomizeArray(currentQuestion.answers);
txtQuestion.Text = currentQuestion.question;
BtnA.Text = "A. " + currentQuestion.answers[0];
BtnB.Text = "B. " + currentQuestion.answers[1];
BtnC.Text = "C. " + currentQuestion.answers[2];
BtnD.Text = "D. " + currentQuestion.answers[3];
}
}
public void ResetButtonsColor()
{
BtnA.BackColor = Color.White;
BtnB.BackColor = Color.White;
BtnC.BackColor = Color.White;
BtnD.BackColor = Color.White;
}
public void CheckButton(Button button, bool boolean)
{
if (boolean == true)
{
button.BackColor = Color.Green;
}
else
{
button.BackColor = Color.Red;
}
}
public void CheckOtherButton(Button button, int answer)
{
if (answer == currentQuestion.correctAnswer)
{
button.BackColor = Color.Green;
}
}
public void ShowScore()
{
lblScore.Text = "Score: " + score.ToString();
}
private void BtnNext_Click(object sender, EventArgs e)
{
BtnNext.Enabled = false;
txtMessage.Text = "";
currentQuestion = NextQuestion((int)cbxCategory.SelectedValue, (int)cbxDifficulty.SelectedValue);
ShowQuestion();
if (currentQuestion == null)
{
txtQuestion.Text = "";
txtMessage.Text = "No more questions. Try another type of category or difficulty.";
BtnNext.Enabled = true;
}
else
{
count++;
lblCount.Text = "Count: " + count.ToString();
EnableButtons(true);
}
}
private void BtnA_Click(object sender, EventArgs e)
{
bool boolean;
timer.Enabled = false;
boolean = CheckCorrectAnswer(0);
CheckButton(BtnA, boolean);
CheckOtherButton(BtnB, 1);
CheckOtherButton(BtnC, 2);
CheckOtherButton(BtnD, 3);
}
private void BtnB_Click(object sender, EventArgs e)
{
bool boolean;
timer.Enabled = false;
boolean = CheckCorrectAnswer(1);
CheckButton(BtnB, boolean);
CheckOtherButton(BtnA, 0);
CheckOtherButton(BtnC, 2);
CheckOtherButton(BtnD, 3);
}
private void BtnC_Click(object sender, EventArgs e)
{
bool boolean;
timer.Enabled = false;
boolean = CheckCorrectAnswer(2);
CheckButton(BtnC, boolean);
CheckOtherButton(BtnA, 0);
CheckOtherButton(BtnB, 1);
CheckOtherButton(BtnD, 3);
}
private void BtnD_Click(object sender, EventArgs e)
{
bool boolean;
timer.Enabled = false;
boolean = CheckCorrectAnswer(3);
CheckButton(BtnD, boolean);
CheckOtherButton(BtnA, 0);
CheckOtherButton(BtnB, 1);
CheckOtherButton(BtnC, 2);
}
public List<T> RandomizeList<T>(List<T> list)
{
List<T> randomizedList = new List<T>();
Random rnd = new Random();
while (list.Count > 0)
{
int index = rnd.Next(0, list.Count);
randomizedList.Add(list[index]);
list.RemoveAt(index);
}
return randomizedList;
}
private void mainMenuToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
private void timer_Tick(object sender, EventArgs e)
{
lblTimer.ForeColor = Color.White;
totalTime--;
lblTimer.Text = "Timer: " + totalTime.ToString();
if (totalTime <= 5)
{
lblTimer.ForeColor = Color.Red;
}
if (totalTime <= 0)
{
BtnNext.Enabled = true;
timer.Enabled = false;
EnableButtons(false);
txtQuestion.Text = "";
txtMessage.Text = "Time is over!!! You lost this question. Click next to continue.";
lifes--;
CheckLifes();
if (lifes == 0)
{
PlayerLost();
}
}
}
public void RandomizeArray(string[] array)
{
Random rnd = new Random();
string corAnswer = currentQuestion.answers[currentQuestion.correctAnswer];
for (int i = 0; i < array.Length - 1; i++)
{
int j = i + rnd.Next(array.Length - i);
var tmp = array[j];
array[j] = array[i];
array[i] = tmp;
}
for (int i = 0; i < array.Length; i++)
{
if (corAnswer == array[i])
{
currentQuestion.correctAnswer = i;
}
}
}
public void StartTimer()
{
totalTime = 25;
timer.Enabled = true;
}
public void CheckLifes()
{
if (lifes == 2)
{
picBoxLife1.Hide();
}
else if (lifes == 1)
{
picBoxLife2.Hide();
}
else if (lifes == 0)
{
picBoxLife3.Hide();
}
}
public void PlayerLost()
{
txtQuestion.Text = "";
txtMessage.Text = "Game Over !!!";
EnableButtons(false);
BtnNext.Enabled = false;
timer.Enabled = false;
}
public void PlayerWon()
{
txtQuestion.Text = "";
txtMessage.Text = "Congratulations you win !!!";
EnableButtons(false);
BtnNext.Enabled = false;
timer.Enabled = false;
}
}
}