-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cs
220 lines (195 loc) · 6.08 KB
/
game.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Media;
using System.Security.RightsManagement;
using System.Text;
using System.Threading.Tasks;
namespace MathGame
{
public class game
{
private UInt16 gameType = 0;
private Int16 numQuestions = 10;
private int firstNumber;
private int secondNumber;
private Random rand;
private SoundPlayer buzzerSound, applause;
/* Game type guid
* 1 = addition
* 2 = subtraction
* 3 = multiplication
* 4 = division
*/
/// <summary>
/// constructor
/// </summary>
public game()
{
rand = new Random();
user.correctScore = 0;
user.incorrectScore = 0;
this.gameType = user.gameMode;
numQuestions = 10;
firstNumber = rand.Next(1, 11);
secondNumber = rand.Next(1, 11);
buzzerSound = new SoundPlayer("Buzzer.wav");
applause = new SoundPlayer("applause.wav");
setEquation();
}
/// <summary>
/// Keeps track of the time
/// </summary>
/// <returns>UInt64 of how many seconds has passed</returns>
public UInt64 updateTime()
{
if (numQuestions > 0)
{
user.TimeinSeconds++;
return user.TimeinSeconds;
}
return user.TimeinSeconds;
}
/// <summary>
/// generates the new equation but does not display it
/// </summary>
public void setEquation()
{
firstNumber = rand.Next(1, 11);
secondNumber = rand.Next(1, 11);
if (user.gameMode == 2 || user.gameMode == 4)
{
if (user.gameMode == 2)
{
bool moveOn = false;
do
{
int z = firstNumber - secondNumber;
if (z < 0)
{
moveOn = true;
firstNumber = rand.Next(1, 11);
secondNumber = rand.Next(1, 11);
}
else
{
moveOn = false;
}
} while (moveOn);
}
else
{
bool stop = false;
do
{
int r = firstNumber % secondNumber;
if (r != 0)
{
stop = true;
firstNumber = rand.Next(1, 11);
secondNumber = rand.Next(1, 11);
}
else
{
stop = false;
}
} while (stop);
}
}
}
/// <summary>
/// Returns a char representing the game mode
/// </summary>
/// <returns>Char that represents game mode</returns>
private char getGameTypeChar()
{
switch (user.gameMode)
{
case 1:
return '+';
case 2:
return '-';
case 3:
return 'x';
case 4:
return '/';
default:
return '~';
}
}
/// <summary>
/// Returns a string representing the question the user is being asked
/// </summary>
/// <returns>String</returns>
public String equasion()
{
return firstNumber.ToString() + " " + getGameTypeChar().ToString() + " " +
secondNumber.ToString() + " = ";
}
/// <summary>
/// checks the user's answer and anjusts grade accordingly
/// </summary>
/// <param name="userAns">int</param>
public void checkAnswer(int userAns)
{
int ans;
if (user.gameMode == 1)
{
ans = firstNumber + secondNumber;
}
else if (user.gameMode == 2)
{
ans = firstNumber - secondNumber;
}
else if (user.gameMode == 3)
{
ans = firstNumber * secondNumber;
}
else
{
ans = firstNumber / secondNumber;
}
if (userAns == ans)
{
applause.Play();
user.correctScore++;
}
else
{
buzzerSound.Play();
user.incorrectScore++;
}
numQuestions--;
if (numQuestions > 0)
{
setEquation();
}
}
#region Getters
/// <summary>
/// returns the number of points the user scored
/// </summary>
/// <returns>String</returns>
public String getCorrectScore()
{
return user.correctScore.ToString();
}
/// <summary>
/// returns the number of questions the user missed
/// </summary>
/// <returns>String</returns>
public String getIncorrectScore()
{
return user.incorrectScore.ToString();
}
/// <summary>
/// How many questions that still need to be answered
/// </summary>
/// <returns>int</returns>
public int getNumQuestions()
{
return numQuestions;
}
#endregion
}
}