-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model.java
480 lines (436 loc) · 18 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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
* Last Modified: November 3, 2020
* Author: Shalee (Shahrukh) Qureshi
* Description: This class creates the Model for the Game
*
* Constructor List:
* 1. Model()
*
* Method List:
* 1. public void start() = This method starts the process of prompting the player before they play the game
* 2. public String getPlayerSelection() = This method returns the selected game mode
* 3. public void outputToFile() = This method outputs the data to a file (output.txt)
* 4. public void setGUI(View view) = This method sets the GUI and initializes the button matrix
* 5. public void btnEvents(String command) = This method performs an action based on the action command
* 6. public void setButtonNum(int num) = This method sets the number to the JButton
* 7. public int getButtonNum() = This method gets the number to the JButton
* 8. public String getPlayerX() = This method gets playerX
* 9. public String getPlayerO() = This method gets playerO
* 10. public int getXCount() = This method gets the xCount
* 11. public void updateXWins() = This method updates Player X's wins
* 12. public void updateOWins() = This method updates Player O's wins
* 13. public int currentRound() = This method returns the current round
* 14. public int[] getIndex(int val) = This method gets the indices within a matrix when given a specific value
* 15. public int[] computer() = This method determines where the AI should place its letter
* 16. public boolean determineWinner() = This method determines who won the game
* 17. public void newRound() = This method will reset the score between the players when a new round starts
* 18. public void updateState() = This method invokes the updateView Method in the View Class
*
*/
// Import Statements
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class Model extends Object {
// Instance Variables
private View view;
private String playerX, playerO;
private int buttonNum, playerXWins, playerOWins;
private String playerSelection;
private JButton[][] buttons;
private int xCount = 0;
private int oCount = 0;
private int currentRound = 1;
private Players players = new Players();
/**
* Model Constructor
*/
public Model() {
super();
} // Model Constructor
/**
* This method starts the process of prompting the player before they play the
* game
*/
public void start() {
this.players.gameMode();
this.playerX = this.players.getPlayerX();
this.playerO = this.players.getPlayerO();
this.playerSelection = this.players.getPlayerSelection();
} // getPlayers Method
/**
* This method returns the selected game mode
*
* @return playerSelection
*/
public String getPlayerSelection() {
return this.playerSelection;
} // getPlayerSelection Method
/**
* This method outputs the data to a file (output.txt)
*/
public void outputToFile() {
String winner = "";
// Determining the overall winner
if (this.playerXWins > this.playerOWins) {
winner = this.playerX;
} else if (this.playerXWins == this.playerOWins) {
winner = "Tie";
} else {
winner = this.playerO;
}
// Prompting user, letting them know the game is over
if (winner != "Tie") {
JOptionPane.showMessageDialog(null, "Thank you for playing! " + this.playerX + " and " + this.playerO + "\n"
+ winner + " won the most rounds!\nYour scores have been updated in output.txt!");
} else {
JOptionPane.showMessageDialog(null, "Thank you for playing! " + this.playerX + " and " + this.playerO
+ "\nThere is no winner it is a Draw!\nYour scores have been updated in output.txt!");
}
try {
// Outputting the data to the file (output.txt)
FileWriter fileW = new FileWriter("output.txt", true);
PrintWriter output = new PrintWriter(fileW);
output.println("Player X: " + this.playerX);
output.println("Player X Wins: " + this.playerXWins);
output.println("Player O: " + this.playerO);
output.println("Player O Wins: " + this.playerOWins);
fileW.close();
output.close();
System.exit(0); // Terminating the program
} catch (IOException error) {
JOptionPane.showMessageDialog(null, error.getMessage());
}
} // outputToFile Method
/**
* This method sets the GUI and initializes the button matrix
*
* @param view
*/
public void setGUI(View view) {
this.view = view;
this.buttons = this.view.getButtons(); // Getting the 2D Buttons array
} // setGUI Method
/**
* This method performs an event based on the action command
*
* @param command
*/
public void btnEvents(String command) {
// If the user presses the Settings button the following will occur
if (command == "Exit Game") {
this.outputToFile(); // Calling the output to file method
}
// If the restart button was clicked the following will occur
else if (command == "Restart") {
Object[] options = { "Restart Game", "Restart Round" };
int selectedOption = JOptionPane.showOptionDialog(null,
"Do you want to restart the game or the current round?",
"TicTacToe 2.0 by Shahrukh (Shalee) Qureshi", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options, null);
// If the user wants to Restart the Game the following will occur
if (selectedOption == JOptionPane.OK_OPTION) {
// Resetting the Score
this.playerXWins = 0;
this.playerOWins = 0;
this.view.setLabelCurrentRound("Round: " + (this.currentRound = 1));
// Resetting the gameboard
this.newRound();
}
// If the user wants to reset the round the following will occur
else {
this.newRound();
}
}
// If the user presses the Settings button the following will occur
else if (command == "Settings") {
// Opening up the Settings Model and calling its view method
SettingsModel model = new SettingsModel(this);
new SettingsView(model);
}
// If the user presses the Move the AI button the following will occur
else {
if (this.view.getLblTurnText() == "Current Turn: AI") {
this.updateState();
}
}
} // btnEvents Method
/**
* This method sets the number to the JButton
*
* @param num
*/
public void setButtonNum(int num) {
this.buttonNum = num;
this.updateState();
} // setButtonNum Method
/**
* This method gets the number to the JButton
*
* @return the number
*/
public int getButtonNum() {
return this.buttonNum;
} // getButtonNum Method
/**
* This method gets playerX
*
* @return playerX
*/
public String getPlayerX() {
return this.playerX;
} // getPlayerX Method
/**
* THis method gets playerO
*
* @return playerO
*/
public String getPlayerO() {
return this.playerO;
} // getPlayerO Method
/**
* This method gets the xCount
*
* @return xCount
*/
public int getXCount() {
return this.xCount;
} // getXCount Method
/**
* This method updates Player X's wins
*
*/
public void updateXWins() {
this.playerXWins++;
} // updateXWins Method
/**
* This method updates Player O's wins
*
*/
public void updateOWins() {
this.playerOWins++;
} // updateOWins Method
/**
* This method returns the current round
*
* @return current round
*/
public int currentRound() {
// The current round is equal to the total number of wins between the players +
// 1 since the initial round is 1
this.currentRound = this.playerOWins + this.playerXWins + 1;
return this.currentRound;
} // currentRound Method
/**
* This method gets the indices within a matrix when given a specific value
*
* @param val
* @return 2 indices in the matrix
*/
public int[] getIndex(int val) {
int[] array = new int[2]; // Creating an array with length of 2
// Loop to traverse through the rows of the matrix
for (int i = 0; i < this.buttons.length; i++) {
// Loop to traverse through the individual columns of the matrix
for (int j = 0; j < this.buttons[i].length; j++) {
// For row 1 values
if (i == 0 && j < 3) {
// If the value is in row 1 the following will occur
if (((i + 1) + j) == val) {
// Passing the values for the row and column to the array
array[0] = i;
array[1] = j;
return array; // Returing the array
}
}
// For row 2 values
else if (i == 1 && j < 3) {
// If the value is in row 2 the following will occur
if (((i + 2) + j + 1) == val) {
// Passing the values for the row and column to the array
array[0] = i;
array[1] = j;
return array; // Returing the array
}
}
// For row 3 values
else if (i == 2 && j < 3) {
// If the value is in row 3 the following will occur
if (((i + 3) + j + 2) == val) {
// Passing the values for the row and column to the array
array[0] = i;
array[1] = j;
return array; // Returing the array
}
}
} // for loop
} // for loop
return array; // Return array
} // getIndex Method
/**
* This method determines where the AI should place its letter
*
* @return an array of 2 numbers with the row and col the letter should be
* placed
*/
public int[] computer() {
int[] array = new int[2]; // Array of size 2
// Loop to traverse the array (rows)
for (int i = 0; i < this.buttons.length; i++) {
// Loop to traverse the array (columns)
for (int j = 0; j < this.buttons[i].length; j++) {
// If the current button does not already hold a value or is not the center
// value the following will occur
if (this.buttons[i][j].getText() != "X" && this.buttons[i][j].getText() != "O"
&& this.buttons[i][j].getText() != "N") {
array[0] = i;
array[1] = j;
return array;
}
} // for loop
} // for loop
return array;
} // computer Method
/**
* This method determines who won the game
*
* @return true if someone wins and false if no one wins
*/
public boolean determineWinner() {
// Loop to traverse through the rows of the matrix
for (int i = 0; i < this.buttons.length; i++) {
// Loop to traverse through the individual columns of the matrix
for (int j = 0; j < this.buttons[i].length; j++) {
// If a given value is equal to X or O the following will occur
if (this.buttons[i][j].getText() == "X" || this.buttons[i][j].getText() == "O") {
// If the current column is not the final column the following will occur
if (j < this.buttons[i].length - 1) {
// Checking to see if adjacent values match
if (this.buttons[i][j].getText() == this.buttons[i][j + 1].getText()) {
// If its an X then we add 1 to the xCount
if (this.buttons[i][j].getText() == "X") {
this.xCount++;
}
// If its an O we add 1 to the oCount
else {
this.oCount++;
}
}
}
// If the current column is not the last column the following will occur
if (i < this.buttons.length - 1) {
// Checking to see if adjacent values match
if (this.buttons[i][j].getText() == this.buttons[i + 1][j].getText()) {
// If its an X then we add 1 to the xCount
if (this.buttons[i][j].getText() == "X") {
this.xCount++;
}
// If its an O we add 1 to the oCount
else {
this.oCount++;
}
}
}
// Check diagonally (only for indicies 0 and 2)
// Checking index 0 (col 0)
if (i == 0) {
if (j == 0) {
// Checking to see if adjacent values match
if (this.buttons[i][j].getText() == this.buttons[i + 1][j + 1].getText()) {
// If its an X then we add 1 to the xCount
if (this.buttons[i][j].getText() == "X") {
this.xCount++;
}
// If its an O we add 1 to the oCount
else {
this.oCount++;
}
}
} else {
// Checking to see if adjacent values match
if (this.buttons[i][j].getText() == this.buttons[i + 1][j - 1].getText()) {
// If its an X then we add 1 to the xCount
if (this.buttons[i][j].getText() == "X") {
this.xCount++;
}
// If its an O we add 1 to the oCount
else {
this.oCount++;
}
}
}
}
// Checking index 2 (col 3)
if (i == 2) {
if (j == 0) {
// Checking to see if adjacent values match
if (this.buttons[i][j].getText() == this.buttons[i - 1][j + 1].getText()) {
// If its an X then we add 1 to the xCount
if (this.buttons[i][j].getText() == "X") {
this.xCount++;
}
// If its an O we add 1 to the oCount
else {
this.oCount++;
}
}
} else {
// Checking to see if adjacent values match
if (this.buttons[i][j].getText() == this.buttons[i - 1][j - 1].getText()) {
// If its an X then we add 1 to the xCount
if (this.buttons[i][j].getText() == "X") {
this.xCount++;
}
// If its an O we add 1 to the oCount
else {
this.oCount++;
}
}
}
}
}
} // for loop
} // for loop
// Return statement
return this.xCount >= 3 || this.oCount >= 3;
} // determineWinner Method
/**
* This method will reset the score between the players when a new round starts
*/
public void newRound() {
this.xCount = 0;
this.oCount = 0;
/**
* This method resets the game board to its default values
*/
// Loop to traverse through the rows
for (int i = 0; i < this.buttons.length; i++) {
// Loop to traverse through the individual columns
for (int j = 0; j < this.buttons[i].length; j++) {
// If the current button isn't at index 1 and 1 the following will occur
if (this.buttons[i][j].getText() != "N") {
// If its row 1 values the following will occur
if (i == 0 && j < 3) {
this.buttons[i][j].setText("" + ((i + 1) + j));
}
// If its row 2 values the following will occur
else if (i == 1 && j < 3) {
this.buttons[i][j].setText("" + ((i + 2) + j + 1));
}
// If its row 3 values the following will occur
else if (i == 2 && j < 3) {
this.buttons[i][j].setText("" + ((i + 3) + j + 2));
}
}
} // for loop
} // for loop
} // newRound Method
/**
* This method invokes the updateView Method in the View Class
*/
public void updateState() {
this.view.updateView();
} // updateState Method
} // Model Class