-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.java
More file actions
189 lines (167 loc) · 4.14 KB
/
TicTacToe.java
File metadata and controls
189 lines (167 loc) · 4.14 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
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class TicTacToe extends JFrame implements ActionListener {
public static int BOARD_SIZE = 3;
public static enum GameStatus {
Incomplete, X_wins, O_wins, Tie
}
private JButton[][] buttons = new JButton[BOARD_SIZE][BOARD_SIZE];
boolean X_Turn = true;
public TicTacToe() {
super.setTitle("TicTacToe");
super.setSize(800, 800);
GridLayout grid = new GridLayout(BOARD_SIZE, BOARD_SIZE);
super.setLayout(grid);
Font font = new Font("Comic Sans", 1, 150);
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
JButton button = new JButton("");
buttons[row][col] = button;
button.setFont(font);
button.addActionListener(this);
super.add(button);
}
}
super.setResizable(false);
super.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
JButton clickedButton = (JButton) e.getSource();
makeMove(clickedButton);
GameStatus gs = this.getGameStatus();
if (gs == GameStatus.Incomplete)
return;
declareWinner(gs);
int choice = JOptionPane.showConfirmDialog(this, "Do you want to restart the game?");
if (choice == JOptionPane.YES_OPTION) {
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
buttons[row][col].setText("");
}
}
X_Turn = true;
} else {
super.dispose();
}
}
private GameStatus getGameStatus() {
String text1 = "", text2 = "";
int row = 0, col = 0;
while (row < BOARD_SIZE) {
col = 0;
while (col < BOARD_SIZE - 1) {
text1 = buttons[row][col].getText();
text2 = buttons[row][col + 1].getText();
if (!text1.equals(text2) || text1.length() == 0) {
break;
}
col++;
}
if (col == BOARD_SIZE - 1) {
if (text1.equals("X")) {
return GameStatus.X_wins;
} else {
return GameStatus.O_wins;
}
}
row++;
}
// text inside cols
col = 0;
while (col < BOARD_SIZE) {
row = 0;
while (row < BOARD_SIZE - 1) {
text1 = buttons[row][col].getText();
text2 = buttons[row + 1][col].getText();
if (!text1.equals(text2) || text1.length() == 0) {
break;
}
row++;
}
if (row == BOARD_SIZE - 1) {
if (text1.equals("X")) {
return GameStatus.X_wins;
} else {
return GameStatus.O_wins;
}
}
col++;
}
// test in diagonal 1
row = 0;
col = 0;
while (row < BOARD_SIZE - 1 && col < BOARD_SIZE - 1) {
text1 = buttons[row][col].getText();
text2 = buttons[row + 1][col + 1].getText();
if (!text1.equals(text2) || text1.length() == 0) {
break;
}
row++;
col++;
}
if (row == BOARD_SIZE - 1) {
if (text1.equals("X")) {
return GameStatus.X_wins;
} else {
return GameStatus.O_wins;
}
}
// test in diagonal 2
row = BOARD_SIZE - 1;
col = 0;
while (row > 0 && col < BOARD_SIZE - 1) {
text1 = buttons[row][col].getText();
text2 = buttons[row - 1][col + 1].getText();
if (!text1.equals(text2) || text1.length() == 0) {
break;
}
row--;
col++;
}
if (row == 0) {
if (text1.equals("X")) {
return GameStatus.X_wins;
} else {
return GameStatus.O_wins;
}
}
String txt = "";
for (row = 0; row < BOARD_SIZE; row++) {
for (col = 0; col < BOARD_SIZE; col++) {
txt = buttons[row][col].getText();
if (txt.length() == 0) {
return GameStatus.Incomplete;
}
}
}
return GameStatus.Tie;
}
private void declareWinner(GameStatus gs) {
if (gs == GameStatus.X_wins) {
JOptionPane.showMessageDialog(this, "X Wins");
} else if (gs == GameStatus.O_wins) {
JOptionPane.showMessageDialog(this, "O Wins");
} else {
JOptionPane.showMessageDialog(this, "It's a tie !");
}
}
private void makeMove(JButton clickedButton) {
String btntext = clickedButton.getText();
if (btntext.length() > 0) {
JOptionPane.showMessageDialog(this, "Invalid Move");
} else {
if (X_Turn) {
clickedButton.setText("X");
} else {
clickedButton.setText("O");
}
X_Turn = !X_Turn;
}
}
}