-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.java
More file actions
218 lines (193 loc) · 5.07 KB
/
TicTacToe.java
File metadata and controls
218 lines (193 loc) · 5.07 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
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class TicTacToe extends JFrame {
private JPanel contentPane;
JButton one = new JButton("");
JButton two = new JButton("");
JButton three = new JButton("");
JButton four = new JButton("");
JButton five = new JButton("");
JButton six = new JButton("");
JButton seven = new JButton("");
JButton eight = new JButton("");
JButton nine = new JButton("");
boolean text = true;
int count = 0;
GameOverSplash gos = new GameOverSplash();
/**
* Launch the application.
*/
public static void main(String[] args) {
TicTacToe frame = new TicTacToe();
frame.setVisible(true);
}
/**
* Create the frame.
*/
public TicTacToe() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 493, 358);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
one.setBounds(33, 39, 100, 30);
contentPane.add(one);
one.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
printxorzero(one);
}
});
two.setBounds(183, 39, 100, 30);
contentPane.add(two);
two.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
printxorzero(two);
}
});
three.setBounds(325, 39, 100, 30);
contentPane.add(three);
three.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
printxorzero(three);
}
});
four.setBounds(33, 137, 100, 30);
contentPane.add(four);
four.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
printxorzero(four);
}
});
five.setBounds(183, 137, 100, 30);
contentPane.add(five);
five.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
printxorzero(five);
}
});
six.setBounds(325, 137, 100, 30);
contentPane.add(six);
six.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
printxorzero(six);
}
});
seven.setBounds(33, 235, 100, 30);
contentPane.add(seven);
seven.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
printxorzero(seven);
}
});
eight.setBounds(183, 235, 100, 30);
contentPane.add(eight);
eight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
printxorzero(eight);
}
});
nine.setBounds(325, 235, 100, 30);
contentPane.add(nine);
nine.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
printxorzero(nine);
}
});
}
protected void printxorzero(JButton button) {
if (button.getText().trim().length() == 0) {
if (text == true) {
button.setText("x");
} else {
button.setText("0");
}
text = !text;
count = count + 1;
if (count >= 5) {
checker();
}
}
}
private void checker() {
String a1 = "one";
String a2 = "two";
String a3 = "three";
String a4 = "four";
String a5 = "five";
String a6 = "six";
String a7 = "seven";
String a8 = "eight";
String a9 = "nine";
if(one.getText().trim().length()!=0) {
a1=one.getText();
}
if(two.getText().trim().length()!=0) {
a2=two.getText();
}
if(three.getText().trim().length()!=0) {
a3=three.getText();
}
if(four.getText().trim().length()!=0) {
a4=four.getText();
}
if(five.getText().trim().length()!=0) {
a5=five.getText();
}
if(six.getText().trim().length()!=0) {
a6=six.getText();
}
if(seven.getText().trim().length()!=0) {
a7=seven.getText();
}
if(eight.getText().trim().length()!=0) {
a8=eight.getText();
}
if(nine.getText().trim().length()!=0) {
a9=nine.getText();
}
if (a1.equals(a2) && a1.equals(a3) && a2.equals(a3)) {
TicTacToe.this.setVisible(false);
TicTacToe.this.dispose();
gos.setVisible(true);
//gos.over(a1);
} if (a1.equals(a4) && a1.equals(a7) && a4.equals(a7)) {
TicTacToe.this.setVisible(false);
TicTacToe.this.dispose();
gos.setVisible(true);
//gos.over(a1);
} if (a1.equals(a5) && a5.equals(a9) && a9.equals(a1)) {
TicTacToe.this.setVisible(false);
TicTacToe.this.dispose();
gos.setVisible(true);
// gos.over(a1);
} if (a3.equals(a6) && a3.equals(a9) && a6.equals(a9)) {
TicTacToe.this.setVisible(false);
TicTacToe.this.dispose();
gos.setVisible(true);
//gos.over(a3);
} if (a3.equals(a5) && a3.equals(a7) && a5.equals(a7)) {
TicTacToe.this.setVisible(false);
TicTacToe.this.dispose();
gos.setVisible(true);
// gos.over(a3);
} if (a7.equals(a8) && a8.equals(a9) && a7.equals(a9)) {
TicTacToe.this.setVisible(false);
TicTacToe.this.dispose();
gos.setVisible(true);
//gos.over(a7);
}
if (count == 9) {
TicTacToe.this.setVisible(false);
TicTacToe.this.dispose();
gos.setVisible(true);
//gos.over(null);
}
}
}