-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTTT.java
More file actions
234 lines (208 loc) · 5.37 KB
/
TTT.java
File metadata and controls
234 lines (208 loc) · 5.37 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
227
228
229
230
231
232
233
234
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.util.*;
import java.util.Random;
public class TTT extends JFrame implements ActionListener {
public static void main(String[] args) {
// TODO Auto-generated method stub
TTT game = new TTT();
}
private enum GameStatus {
ZeroWon, CrossWon, Tie, Incomplete;
}
private boolean playerflag = players();
private boolean crossTurn = true;
public static final int BOARD_SIZE = 3;
public static final String CROSS = "X";
public static final String ZERO = "O";
private JButton[][] buttons = new JButton[BOARD_SIZE][BOARD_SIZE];
public TTT() {
super.setTitle("TIC TAC TOE");
super.setBackground(Color.GRAY);
GridLayout layout = new GridLayout(BOARD_SIZE, BOARD_SIZE);
super.setLayout(layout);
super.setSize(900, 900);
super.setResizable(false);
Font font = new Font("Serif", 1, 150);
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
JButton button = new JButton();
button.setFont(font);
button.addActionListener(this);
this.buttons[row][col] = button;
super.add(button);
}
}
super.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
JButton clickedbutton = (JButton) e.getSource();
clickedbutton.setForeground(Color.WHITE);
clickedbutton.setBackground(Color.BLACK);
makeMove(clickedbutton);
if (playerflag) {
cpuMove();
}
GameStatus gs = getGameStatus();
if (gs == GameStatus.Incomplete) {
return;
} else {
declareWinner(gs);
int Jchoice = JOptionPane.showConfirmDialog(this, "AGAIN ");
if (Jchoice == JOptionPane.YES_OPTION) {
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
JButton button = this.buttons[row][col];
button.setText("");
button.setBackground(Color.GRAY);
}
}
this.crossTurn = true;
} else {
super.dispose();
}
}
}
private void makeMove(JButton clickedbutton) {
if (clickedbutton.getText().length() > 0) {
JOptionPane.showMessageDialog(this, "hey! box is already filled.");
return;
} else if (crossTurn) {
clickedbutton.setText(CROSS);
} else {
clickedbutton.setText(ZERO);
}
crossTurn = !crossTurn;
}
public void cpuMove() {
int x = 0;
int y = 0;
do {
Random rand = new Random();
x = rand.nextInt(3);
y = rand.nextInt(3);
if (this.buttons[x][y].getText().equals("")) {
this.buttons[x][y].setText(ZERO);
this.buttons[x][y].setForeground(Color.WHITE);
this.buttons[x][y].setBackground(Color.BLACK);
crossTurn = true;
getGameStatus();
}
} while (crossTurn == false);
}
private GameStatus getGameStatus() {
String prev = "", nxt = "";
int row = 0, col = 0;
row = 0;
while (row < BOARD_SIZE) {
col = 0;
while (col < BOARD_SIZE - 1) {
prev = buttons[row][col].getText();
nxt = buttons[row][col + 1].getText();
if (!prev.equals(nxt) || prev.length() == 0) {
break;
}
col++;
}
if (col == BOARD_SIZE - 1) {
if (prev.equals(CROSS)) {
return GameStatus.CrossWon;
} else if (prev.equals(ZERO)) {
return GameStatus.ZeroWon;
}
}
row++;
}
col = 0;
while (col < BOARD_SIZE) {
row = 0;
while (row < BOARD_SIZE - 1) {
prev = buttons[row][col].getText();
nxt = buttons[row + 1][col].getText();
if (!prev.equals(nxt) || prev.length() == 0) {
break;
}
row++;
}
if (row == BOARD_SIZE - 1) {
if (prev.equals(CROSS)) {
return GameStatus.CrossWon;
} else if (prev.equals(ZERO)) {
return GameStatus.ZeroWon;
}
}
col++;
}
row = 0;
col = 0;
while (col < BOARD_SIZE - 1 && row < BOARD_SIZE - 1) {
prev = buttons[row][col].getText();
nxt = buttons[row + 1][col + 1].getText();
if (!prev.equals(nxt) || prev.length() == 0) {
break;
}
col++;
row++;
}
if (col == BOARD_SIZE - 1 && row == BOARD_SIZE - 1) {
if (prev.equals("X")) {
return GameStatus.CrossWon;
} else if (prev.equals("O")) {
return GameStatus.ZeroWon;
}
}
row = 0;
col = BOARD_SIZE - 1;
while (col > 0 && row < BOARD_SIZE - 1) {
prev = buttons[row][col].getText();
nxt = buttons[row + 1][col - 1].getText();
if (!prev.equals(nxt) || prev.length() == 0) {
break;
}
col--;
row++;
}
if (col == 0 && row == BOARD_SIZE - 1) {
if (prev.equals(CROSS)) {
return GameStatus.CrossWon;
} else if (prev.equals(ZERO)) {
return GameStatus.ZeroWon;
}
}
String text="";
for (row = 0; row < BOARD_SIZE; row++) {
for (col = 0; col < BOARD_SIZE; col++) {
text = buttons[row][col].getText();
if (text.length() == 0) {
return GameStatus.Incomplete;
}
}
}
return GameStatus.Tie;
}
private void declareWinner(GameStatus gs) {
if (gs == GameStatus.ZeroWon) {
JOptionPane.showMessageDialog(this, "Zero is winner");
}
if (gs == GameStatus.CrossWon) {
JOptionPane.showMessageDialog(this, "Cross is winner");
}
if (gs == GameStatus.Tie) {
JOptionPane.showMessageDialog(this, "TIE");
}
}
public boolean players() {
int playerchoice = JOptionPane.showConfirmDialog(this, "Do you want to play with computer ? ");
if (playerchoice == JOptionPane.YES_OPTION) {
this.playerflag = true;
} else {
this.playerflag = false;
}
return playerflag;
}
}