-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMagicEightBallModel.java
190 lines (154 loc) · 5.08 KB
/
MagicEightBallModel.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
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
/** MagicEightBallModel class
* This is where the methods for deciding the output will be created
* Date Created: 11/04/2020
* Created by: P. Anmol
*/
import java.io.*;
public class MagicEightBallModel extends Object
{
//Variable Declarations
private MagicEightBallGUI gameView; //The view for the game
private String result; //This will hold the String output
private String extraResult; //This will hold the extra result, for any non-yes/no questions
public int rounds = 0; //This will count how many rounds or questions have been asked
private boolean roundsDone = true; //This will check if the rounds reached the limit or not(limit is 10)
/** Creates a default game with 10 tokens and question player going first
*/
public MagicEightBallModel()
{
super();
}
/** Sets the view for the game
* @param currentGUI The View used to display the game
*/
public void setGUI(MagicEightBallGUI currentGUI)
{
this.gameView = currentGUI;
}
//This method wil return the "result" variable that hold the string
//value of the output, or the answer to the question
public String resultReturned()
{
return this.result;
}
//return the number of rounds
public int getRounds()
{
return this.rounds;
}
//return the boolean value of roundsDone
public boolean getRoundsDone()
{
return this.roundsDone;
}
/** Determines the output or answer to the user's question
* @param randomAnswer this will determine which result or ouput String to use
*/
public void questionAnswered(int randomAnswer)
{
//if the number of rounds is less then 10
//then keep executing the code inside the
//if block
if(rounds <= 9)
{
//based on the random integer chosen from 1-3
//a case that represents that specific integer
//will be executed
switch(randomAnswer)
{
case 1:
this.result = "Yes";
break;
case 2:
this.result = "No";
break;
case 3:
this.result = "Maybe";
break;
}
rounds++; //Increase the number of rounds by 1 each time
}
//sets boolean value to false if number of rounds
//is greater then 10
else
{
this.roundsDone = false;
System.out.println(roundsDone);
}
//Run the update view method that calls the update
//method in the View
this.updateView();
}
//This method is for the buttons
//@param whichButton This will allow the controller to choose which code to execute in this method
public void optionsButtons(String whichButton)
{
//if the button is the new game button
//then execute this if block
if(whichButton == "New Game")
{
this.rounds = 0; //set the rounds value to 0
this.gameView.question.setEnabled(true); //enable the JTextField again
this.gameView.question.setText(""); //Set the text to nothing
this.gameView.questionAnswer.setText("");//set the text in the output to nothing
this.roundsDone = true; //the number of rounds is less then 10
}
//If the button is the end game button
//then execute this block
else if(whichButton == "End Game")
{
//create a new output file
PrintWriter outputFile = getPrintWriter("output.txt");
outputFile.print("hwy");
}
}
//this method is used by the View when the user
//enters a question that doesnt look like a yes/no question
//it functions the same way as the questionAnswered(int) method
//but serves a different purpose
public String extraWords(int num)
{
switch(num)
{
case 1:
extraResult = "a lot";
break;
case 2:
extraResult = "a little";
break;
case 3:
extraResult = "I don't know, ask the internet";
break;
case 4:
extraResult = "This time you must ask the INTERNET";
break;
}
return extraResult;
}
//this method is copied from the prompt class
//and modified so it doesnt prompt
public static PrintWriter getPrintWriter(String prompt)
{
String fileName = prompt;
//Opens the output file
File outputFile = new File(fileName);
try
{
//constructs & returns a PrintWriter object connected to the output file
return new PrintWriter(outputFile);
}
catch (FileNotFoundException ex)
{
//Error occured inform user
System.out.println(ex.getMessage());
System.out.println("in" + System.getProperty("user.dir"));
System.exit(1);
}
return null;
}
/** Updates the view in the GUI*/
public void updateView()
{
gameView.update();
}
}