-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTICTACTOE.java
More file actions
226 lines (206 loc) · 6.51 KB
/
TICTACTOE.java
File metadata and controls
226 lines (206 loc) · 6.51 KB
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
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Dimension;
import javax.swing.JOptionPane;
/** TicTacToe game using basic GUI elements. Tracks the scores of both
* players allowing for continous play.
* Jeremy Okeyo || 5/25/2019
*
*/
public class TICTACTOE extends JFrame implements ActionListener {
public JButton[][] cells;
public JButton exit;
public JButton replay;
public JButton cell;
private String player = PLAYER_X;
private GridBagLayout gridBag = new GridBagLayout();
private GridBagConstraints area;
private JOptionPane stat;
private static final int SIZE = 3;
private static final String PLAYER_X = ("X");
private static final String PLAYER_O = ("O");
private static final String EMPTY_CELL = ("-");
private int topAlign;
private int leftAlign;
private final int DIMEN = 100;
private static int X_win;
private static int O_win;
private int[] score = new int[8]; //Keeps track of the game's play.
//Corresponds with all possible winning plays
private int noPlays;
private final int MAXPLAYS = 8;
/**
Contructs a new TicTacToe board with all its rules.
*/
public TICTACTOE() {
window();
}
/**
* Sets up the TicTacToe board
*/
private void window() {
setLayout(gridBag);
setLocation(500, 100);
setPreferredSize(new Dimension(700, 700));
boardLayout();
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
/**
* Sets up the TicTacToe board
*/
private void boardLayout() {
cells = new JButton[SIZE][SIZE];
setTitle("TIC TAC TOE");
for (int i = 0; i < SIZE; i++) {
leftAlign = 0;
for (int j = 0; j < SIZE; j++) {
cell = new JButton(EMPTY_CELL);
cells[i][j] = cell;
cells[i][j].setPreferredSize(new Dimension(100, 100));
area = new GridBagConstraints();
area.gridheight = area.REMAINDER;
area.gridwidth = area.REMAINDER;
area.gridheight = DIMEN;
area.gridwidth = DIMEN;
area.gridx = leftAlign;
area.gridy = topAlign;
add(cells[i][j], area);
leftAlign += DIMEN;
cells[i][j].addActionListener(this);
}
topAlign += DIMEN;
}
}
/**
* Registers the clicks of the user on the buttons.
* @param action The button press.
*/
@Override
public void actionPerformed(ActionEvent action) {
Object but = action.getSource();
for (int i = 0; i < cells.length; i++) {
for (int j = 0; j < cells[0].length; j++) {
if (cells[i][j].equals(but) && cells[i][j].getText() == EMPTY_CELL) {
cells[i][j].setText(player);
gamePlay(player, j, i);
gameOver();
player = nextPlayer(player);
noPlays++;
}
}
}
}
/**
* Proceeds to the next player depending on the current player. Helper to
* actionPerformed()
* @param player the current player
* @return the next player
*/
private String nextPlayer(String player) {
if (player.equals(PLAYER_X)) {
return PLAYER_O;
}
else {
return PLAYER_X;
}
}
/**
* Tests whether any of the players created a winning trio. Helper to
* isGameOver()
* @param player the current player
* @row the row of the button pressed
* @col the column of the button pressed
*
*/
private void gamePlay(String player, int row, int col) {
int point = -1;
if (player.equals(PLAYER_X)) {
point = 1;
}
score[col] += point;
score[3 + row] += point;
if (row == col) {
score[6] += point;
}
if ((2 - col) == row) {
score[7] += point;
}
}
/**
* Tests whether the game is over.
* @return whether the game is over.
*/
private boolean isGameOver() {
boolean gameOver = false;
for (int i = 0; i < score.length; i++) {
if (score[i] == 3) {
gameOver = true;
break;
}
else if (score[i] == -3) {
gameOver = true;
break;
}
else if (noPlays == MAXPLAYS) {
gameOver = true;
break;
}
else {
gameOver = false;
}
}
return gameOver;
}
/**
* Creates a dialogue box that prompts the user if the game is over.
*/
private void gameOver() {
boolean gameOver = isGameOver();
if (gameOver) {
Object[] options = {"Play Again?", "Exit"};
if (player.equals(PLAYER_X)) {
String dialog = "Player X has won. Score: X " + X_win + " O " + O_win;
X_win++;
Object obj = dialog;
stat = new JOptionPane();
int choice = stat.showOptionDialog(null, obj, "Select to advance",
JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE,
null, options, null);
option(choice);
}
else if (player.equals(PLAYER_O)) {
String dialog = "Player O has won. Score: X " + X_win + " O " + O_win;
O_win++;
Object obj = dialog;
stat = new JOptionPane();
int choice = stat.showOptionDialog(null, obj, "Select to advance",
JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE,
null, options, null);
option(choice);
}
}
}
/**
* Provides funtionality to the dialog boxes buttons.
@param choice The choice selected by the user
*/
public void option(int choice) {
if (choice == 1) {
dispose();
}
else {
dispose();
TICTACTOE game = new TICTACTOE();
}
}
//Used to test the program
public static void main(String[] args) {
TICTACTOE c = new TICTACTOE();
}
}