forked from jnguye14/MathGame
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModel.java
105 lines (87 loc) · 3.02 KB
/
Model.java
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
/*** Chris Jeffery ***/
import java.applet.AudioClip;
import java.util.Random;
import javax.swing.Timer;
import java.io.*;
public class Model {
protected int product;
protected int numNeeded = 3;
protected Random rnd;
// Each String corresponds to one of GamePanel's JLabels
protected String str, prb, time, msg;
protected StopWatch watch;
protected Timer timer;
protected AudioClip correctSFX, accessSFX, wrongSFX;
protected FileWriter outputStream = null; // should I use BufferedWriter or PrintStream instead?
Model()
{
rnd = new Random();
// start timer & watch
watch = new StopWatch();
generateProb();
gameReplay();
try {
// create file for output
outputStream = new FileWriter("YourRecord.txt"); // added
outputStream.write("User Name: _____\r\n\r\n");
// instead of using "\r\n" probably System.getProperty("line.separator") would be better...
} catch (IOException x)
{ System.out.println("There was an error"); }
}
// Generate Random Problems
protected void generateProb()
{
int num1 = rnd.nextInt(11); // numbers 0 to 10
int num2 = rnd.nextInt(11); // numbers 0 to 10
product = num1*num2;
prb = "<html><font face=\"Comic Sans MS\" color =\"BLUE\" size=\"6\"><b>"
+ "What is " + num1 + " x " + num2 + "?"
+ "</b></font></html>";
}
protected void answerCorrect()
{
try {
String output = "Problems Left: " + numNeeded
+ "\r\nElapsed Time: " + watch.getElapsedTime() + "\r\n\r\n";
outputStream.write(output);
} catch (IOException x)
{ System.out.println("Could Not Write"); }
correctSFX.play();
numNeeded--;
str = "<html>You need to get <font color =\"GREEN\">"
+ numNeeded
+ "</font> more right before you get internet.</html>";
msg = "Correct!";
generateProb();
}
protected void answerWrong()
{
wrongSFX.play();
msg = "<html><font color =\"RED\">"
+ "Your answer is wrong. Please double check your math."
+ "</font></html>";
}
protected void answerInvalid()
{
wrongSFX.play();
msg = "<html><font color =\"RED\">Please enter only digits.</font></html>";
}
protected void gameReplay()
{
generateProb();
str = "<html>You need to get <font color =\"GREEN\">"
+ numNeeded
+ "</font> problems right to get internet.</html>";
msg = "";
time = "<html>Elapsed Time: <font color =\"GREEN\">0</font> Seconds</html>";
}
// ABSOLUTELY MUST CLOSE BEFORE CLOSING APPLICATION
protected void close()
{
try {
outputStream.write("End of File");
outputStream.close();
} catch (IOException x)
{ System.out.println("Nothing to Close"); }
}
}