-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.java
365 lines (308 loc) · 8.76 KB
/
Game.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
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
// Aaryan Prakash
// 10/24/18
// Game.java
// Holds the game state and calls methods for various components
// to change the UI.
import javax.swing.Timer;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Game
{
// used to tell which token to move (name changed for ease in reading)
private boolean BLUE_TOKEN = true;
private boolean RED_TOKEN = false;
private final int CORRECT = 0; // represents a correct answer
private final int ALTERNATE = 1; // represents alternate answer
private final int WRONG = 2; // represents wrong answer
private Board board; // current board that is used
private Alerts alertBox; // alert box that is being used
private boolean gameEnded; // tells if game has ended
private int redPos; // red player's position
private int bluePos; // blue player's position
private int roll; // the roll the player did
private Timer redTurnWait; // waits for 1/2 second and does red turn
private String questionFileName; // name of the file with questions
private Scanner input; // scanner that scans through
private int numQuestions; // stores number of questions
private Question[] questions; // stores question to show to user
private boolean[] questionsDone; // stores which questions have been asked/answered already
// used to go back out of the panel or create new game
private GamePanel gamePanel; // variable for the GamePanel this is controlling
private double compWrongProb; // probability computer gets a question wrong
// easy = 0.5, medium = 0.33, hard = 0.25, impossible = 0
// sets intial state of game and creates components
public Game(GamePanel gamePanelIn)
{
gamePanel = gamePanelIn;
roll = 0;
redPos = 1;
bluePos = 1;
gameEnded = false;
board = new Board();
alertBox = new Alerts();
questionFileName = new String("questions/questions.txt");
input = null;
openQuestionFile();
openQuestions();
redTurnWait = new Timer(1250, new TimerListener()); // adjust number for time
compWrongProb = 0.33; // set default to medium difficulty
}
// receives roll from dice, tells token to move forward, and adds message to alertBox
public void getDiceValue(int rollIn)
{
roll = rollIn;
if (!gameEnded)
{
// creates the alert to send to alertBox
String alert = new String();
alert = "You rolled a " + roll;
if (roll == 6)
{
alert += "!";
roll += (int)(Math.random() * 6 + 1); // adds an extra move for player
}
else
{
alert += ".";
}
alertBox.addAlert(alert);
}
openRandQuestion();
}
// this is called by the question class to move the blue token forward
public void moveBlueForward(int correctness)
{
String alert = new String();
if (correctness == CORRECT)
{
board.moveForward(BLUE_TOKEN, bluePos, roll);
bluePos = Math.min(bluePos + roll, 100);
alertBox.addAlert("You moved " + roll + " forward!");
}
else if (correctness == ALTERNATE)
{
board.moveForward(BLUE_TOKEN, bluePos, roll - 1);
bluePos = Math.min(bluePos + roll - 1, 100);
alertBox.addAlert("You moved " + (roll - 1) + " forward.");
}
else
{
alertBox.addAlert("You lost your turn.");
}
// adds extra move if player rolled a 6
if (roll == 5 || roll == 6)
{
System.out.println("hi");
int extraMove = (int)(Math.random() * 6 + 1);
alertBox.addAlert("Because you rolled a 5 or 6, you get to move " + extraMove + " extra.");
board.moveForward(BLUE_TOKEN, bluePos, extraMove);
bluePos = Math.min(bluePos + extraMove, 100);
}
checkWin();
redTurnWait.start();
}
// this does a turn for the red player
public void redTurn()
{
redTurnWait.stop();
int redRoll = gamePanel.getDice().rollComputer();
// creates the alert to send to alertBox
String alert = "The computer rolled a " + redRoll;
if (redRoll == 6)
{
alert += "!";
}
else
{
alert += ".";
}
alertBox.addAlert(alert);
double mistakeProb = Math.random();
if (mistakeProb > compWrongProb)
{
board.moveForward(RED_TOKEN, redPos, redRoll);
redPos = Math.min(redPos + redRoll, 100);
alert = new String("The computer moves " + redRoll + " forward.");
}
else
{
alert = new String("The computer lost their turn");
}
alertBox.addAlert(alert);
checkWin();
}
// checks if anyone has one yet
public void checkWin()
{
if (bluePos == 100)
{
alertBox.addAlert("You won the game!");
gameEnded = true;
}
else if (redPos == 100)
{
alertBox.addAlert("The computer won the game!");
gameEnded = true;
}
}
// opens up a random, unanswered question for the user to answer
public void openRandQuestion()
{
int questionChecked = 0;
while (questionChecked != -1 && questionChecked < numQuestions)
{
int pos = (int)(Math.random() * numQuestions);
if (!questionsDone[pos])
{
questions[pos].createFrame();
questionsDone[pos] = true;
questionChecked = -2; // makes it so ++ makes it = -1
}
questionChecked++;
}
if (questionChecked == numQuestions)
{
RunOutQuestion roq = new RunOutQuestion(gamePanel);
roq.createFrame();
}
}
// opens up questions and adds to questions array
public void openQuestions()
{
numQuestions = input.nextInt();
questions = new Question[numQuestions];
questionsDone = new boolean[numQuestions];
int questionsAdded = 0;
while (questionsAdded < numQuestions)
{
int code = input.nextInt();
input.nextLine(); // flushes to next line
String question = input.nextLine();
String type = input.next();
if (type.equals("radio"))
{
questions[questionsAdded] = openRadioQuestion(code, question);
}
else
{
questions[questionsAdded] = openSliderQuestion(code, question);
}
questionsAdded++;
}
}
public Question openRadioQuestion(int code, String question)
{
int numChoices = input.nextInt();
input.nextLine(); // flushes to next line
int choicesAdded = 0;
AnswerChoice[] choices = new AnswerChoice[numChoices];
input.next(); // skips the word that identifies it is correct
int numCorrect = input.nextInt();
input.nextLine(); // flushes to next line
while (numCorrect > 0)
{
String name = input.nextLine();
String reason = input.nextLine();
choices[choicesAdded] = new AnswerChoice(name, CORRECT, reason);
choicesAdded++;
numCorrect--;
}
input.next(); // skips alternate identifier
int numAlt = input.nextInt();
input.nextLine(); // flushes to next line
while (numAlt > 0)
{
String name = input.nextLine();
String reason = input.nextLine();
choices[choicesAdded] = new AnswerChoice(name, ALTERNATE, reason);
choicesAdded++;
numAlt--;
}
input.next(); // skips wrong identifier
int numWrong = input.nextInt();
input.nextLine(); // flushes to next line
while (numWrong > 0)
{
String name = input.nextLine();
String reason = input.nextLine();
choices[choicesAdded] = new AnswerChoice(name, WRONG, reason);
choicesAdded++;
numWrong--;
}
Question theQuestion = new RadioQuestion(this, code, question, choices);
return theQuestion;
}
public Question openSliderQuestion(int code, String question)
{
int[] minMax = new int[2];
minMax[0] = input.nextInt();
minMax[1] = input.nextInt();
int ans = input.nextInt();
input.nextLine(); // flushes to next line
Tolerance[] tolerances = new Tolerance[3];
for (int i = 0; i < 3; i++)
{
input.nextLine(); // skips over correct identifier
int range = input.nextInt();
input.nextLine(); // flushes to next line
String reason = input.nextLine();
Tolerance tol = new Tolerance(range, i, reason);
tolerances[i] = tol;
}
Question theQuestion = new SliderQuestion(this, code, question,
minMax, ans, tolerances);
return theQuestion;
}
// opens scanner to read question file
public void openQuestionFile()
{
File outFile = new File(questionFileName);
try
{
input = new Scanner(outFile);
}
catch (FileNotFoundException e)
{
System.err.printf("Could not open %s.\n", questionFileName);
e.printStackTrace();
System.exit(1);
}
}
// returns Board instance
public Board getBoard()
{
return board;
}
// returns Alerts instance
public Alerts getAlertBox()
{
return alertBox;
}
// resets alertBox and board to original state (empty, at pos 1)
public void reset()
{
board.reset(bluePos, redPos);
bluePos = 1;
redPos = 1;
gameEnded = false;
questionsDone = new boolean[numQuestions];
alertBox.resetAlerts();
}
// sets the value of compWrongProb
public void setCompWrongProb(double prob)
{
compWrongProb = prob;
}
// class for timer
class TimerListener implements ActionListener
{
// does a red move after 1 second
public void actionPerformed(ActionEvent evt)
{
redTurn();
}
}
}