-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe.java
355 lines (328 loc) · 6.98 KB
/
TicTacToe.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
package tic_tac_toe;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class GameFrame extends JFrame implements MouseListener{
int p=-1;
int count=0;
int time = 10;
int[][] a = new int[3][3];
JLabel commentary,xTimer,oTimer,xTimerLabel,oTimerLabel;
JButton start;
JLabel[][] labels = new JLabel[3][3];
Timer xTimerObj,oTimerObj;
GameFrame()
{
commentary = new JLabel("GAME STARTS WITH O");
start = new JButton("START");
xTimerLabel = new JLabel("X");
oTimerLabel = new JLabel("O");
xTimer = new JLabel(time+"");
oTimer = new JLabel(time+"");
addMouseListener(this);
setTitle("Tic Tac Toe");
setLayout(null);
setSize(800,800);
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
oTimerObj = new Timer(1000,oGameTimer);
xTimerObj = new Timer(1000,xGameTimer);
start.addActionListener(ae->
{
oTimerObj.start();
start.setEnabled(false);
});
}
ActionListener xGameTimer = new ActionListener()
{
int timeRem=time;
public void actionPerformed(ActionEvent e)
{
if(timeRem == 0)
{
commentary.setText("PLAYER X TIME OUT!!! PLAYER O WON");
count = 9;
oTimerObj.stop();
xTimerObj.stop();
}
else
{
timeRem--;
xTimer.setText(timeRem+"");
}
}
};
ActionListener oGameTimer = new ActionListener()
{
int timeRem=time;
public void actionPerformed(ActionEvent e)
{
if(timeRem == 0)
{
commentary.setText("PLAYER O TIME OUT!!! PLAYER X WON");
count = 9;
oTimerObj.stop();
xTimerObj.stop();
}
else
{
timeRem--;
oTimer.setText(timeRem+"");
}
}
};
public void paint(Graphics g)
{
g.drawRect(100, 100, 600, 600);
g.drawLine(100,300,700,300);
g.drawLine(100, 500, 700, 500);
g.drawLine(300, 100, 300, 700);
g.drawLine(500, 100, 500, 700);
add(xTimer);
xTimer.setBounds(10,350,70,70);
xTimer.setFont(new Font("Arial",Font.PLAIN,25));
add(oTimer);
oTimer.setBounds(725,350,70,70);
oTimer.setFont(new Font("Arial",Font.PLAIN,25));
add(commentary);
commentary.setBounds(250,710,500,25);
commentary.setFont(new Font("Arial",Font.PLAIN,25));
this.add(start);
start.setBounds(325,40,150,25);
add(xTimerLabel);
xTimerLabel.setBounds(20,300,50,25);
xTimerLabel.setFont(new Font("Arial",Font.BOLD,25));
add(oTimerLabel);
oTimerLabel.setBounds(725,300,50,25);
oTimerLabel.setFont(new Font("Arial",Font.BOLD,25));
}
public void mousePressed(MouseEvent e) {
if(start.isEnabled()) return;
if(count == 0);
{
xTimerObj.start();
}
if(p==-1)
{
oTimerObj.stop();
xTimerObj.restart();
}
else
{
xTimerObj.stop();
oTimerObj.restart();
}
int x=e.getX();
int y =e.getY();
if(x>100 && x<700 && y>100 && y<700 && count<9)
{
JLabel label = getLabel();
count++;
if(a[0][0]==0 && x>100 && x<300 && y>100 && y<300)//square 1
{
a[0][0]=p;
add(label);
label.setBounds(150,150,100,100);
labels[0][0]=label;
}
else if(a[0][1]==0 && x>300 && x<500 && y>100 && y<300)//square 2
{
a[0][1]=p;
add(label);
label.setBounds(350,150,100,100);
labels[0][1]=label;
}
else if(a[0][2]==0 && x>500 && x<700 && y>100 && y<300)//square 3
{
a[0][2]=p;
add(label);
label.setBounds(550,150,100,100);
labels[0][2]=label;
}
else if(a[1][0]==0 && x>100 && x<300 && y>300 && y<500)//square 4
{
a[1][0]=p;
add(label);
label.setBounds(150,350,100,100);
labels[1][0]=label;
}
else if(a[2][0]==0 && x>100 && x<300 && y>500 && y<700)//square 7
{
a[2][0]=p;
add(label);
label.setBounds(150,550,100,100);
labels[2][0]=label;
}
else if(a[1][1]==0 && x>300 && x<500 && y>300 && y<500)//square 5
{
a[1][1]=p;
add(label);
label.setBounds(350,350,100,100);
labels[1][1]=label;
}
else if(a[2][1]==0 && x>300 && x<500 && y>500 && y<700)//square 8
{
a[2][1]=p;
add(label);
label.setBounds(350,550,100,100);
labels[2][1]=label;
}
else if(a[1][2]==0 && x>500 && x<700 && y>300 && y<500)//square 6
{
a[1][2] = p;
add(label);
label.setBounds(550,350,100,100);
labels[1][2]=label;
}
else if(a[2][2]==0 && x>500 && x<700 && y>500 && y<700)//square 9
{
a[2][2] = p;
add(label);
label.setBounds(550,550,100,100);
labels[2][2]=label;
}
p=-p;
if(checkWin()!=0)
{
switch(checkWin())
{
case 1:
for(int i=0;i<3;i++)
{
labels[0][i].setBackground(Color.green);
}
break;
case 2:
for(int i=0;i<3;i++)
{
labels[1][i].setBackground(Color.green);
}
break;
case 3:
for(int i=0;i<3;i++)
{
labels[2][i].setBackground(Color.green);
}
break;
case 4:
for(int i=0;i<3;i++)
{
labels[i][0].setBackground(Color.green);
}
break;
case 5:
for(int i=0;i<3;i++)
{
labels[i][1].setBackground(Color.green);
}
break;
case 6:
for(int i=0;i<3;i++)
{
labels[i][2].setBackground(Color.green);
}
break;
case 7:
for(int i=0;i<3;i++)
{
labels[i][i].setBackground(Color.green);
}
break;
case 8:
for(int i=0;i<3;i++)
{
labels[i][3-i-1].setBackground(Color.green);
}
break;
}
count = 9;
p=-p;
commentary.setText("PLAYER "+getPlayer()+" WON");
oTimerObj.stop();
xTimerObj.stop();
}
else if(count==9)
{
commentary.setText("MATCH IS DRAW");
xTimerObj.stop();
oTimerObj.stop();
}
else commentary.setText("PLAYER "+getPlayer()+" TURN");
}
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public String getPlayer()
{
return (p==-1)?"O":"X";
}
public JLabel getLabel()
{
String str;
JLabel l;
if(p == 1) str="X";
else str="O";
l = new JLabel(str);
l.setOpaque(true);
l.setFont(new Font("MV Boli",Font.PLAIN,100));
return l;
}
public int checkWin()
{
int i,j,c,tmp;
//checking horizontally
for(i=0;i<3;i++)
{
c=0;
tmp=a[i][0];
for(j=0;j<3;j++)
{
if(a[i][j]!=0 && a[i][j] == tmp) c++;
}
if(c == 3) return i+1;
}
//checking vertically
for(j=0;j<3;j++)
{
c=0;
tmp=a[0][j];
for(i=0;i<3;i++)
{
if(a[i][j] == tmp && a[i][j]!=0) c++;
}
if(c == 3) return j+4;
}
//checking top left to bottom right diagonal
c=0;
tmp=a[0][0];
for(i=0;i<3;i++)
{
if(a[i][i]==0) break;
if(a[i][i] == tmp) c++;
}
if(c==3) return 7;
//checking top right to bottom left
c=0;
tmp=a[0][2];
for(i=0;i<3;i++)
{
if(a[i][3-i-1]==0) break;
if(a[i][3-i-1] == tmp) c++;
}
if(c==3) return 8;
return 0;
}
}
public class TicTacToe
{
public static void main(String[] args) {
new GameFrame();
}
}