-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe.java
222 lines (190 loc) · 5.91 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
package ai_lab_final;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.Random;
import java.applet.*;
public class TicTacToe extends JApplet implements ChangeListener, ActionListener {
private JSlider slider;
private JButton oButton, xButton;
private Board board;
private int lineThickness=4;
private Color oColor=Color.BLUE, xColor=Color.RED;
static final char BLANK=' ', O='O', X='X';
private char position[]={
BLANK, BLANK, BLANK,
BLANK, BLANK, BLANK,
BLANK, BLANK, BLANK};
private int wins=0, losses=0, draws=0;
public void init() {
JPanel topPanel=new JPanel();
topPanel.setLayout(new FlowLayout());
topPanel.add(new JLabel("Line Thickness:"));
topPanel.add(slider=new JSlider(SwingConstants.HORIZONTAL, 1, 20, 4));
slider.setMajorTickSpacing(1);
slider.setPaintTicks(true);
slider.addChangeListener(this);
topPanel.add(oButton=new JButton("O Color"));
topPanel.add(xButton=new JButton("X Color"));
oButton.addActionListener(this);
xButton.addActionListener(this);
add(topPanel, BorderLayout.NORTH);
add(board=new Board(), BorderLayout.CENTER);
setVisible(true);
}
// Change line thickness
public void stateChanged(ChangeEvent e) {
lineThickness = slider.getValue();
board.repaint();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==oButton) {
Color newColor = JColorChooser.showDialog(this, "Choose a new color for O", oColor);
if (newColor!=null)
oColor=newColor;
}
else if (e.getSource()==xButton) {
Color newColor = JColorChooser.showDialog(this, "Choose a new color for X", xColor);
if (newColor!=null)
xColor=newColor;
}
board.repaint();
}
private class Board extends JPanel implements MouseListener {
private Random random=new Random();
private int rows[][]={{0,2},{3,5},{6,8},{0,6},{1,7},{2,8},{0,8},{2,6}};
public Board() {
addMouseListener(this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int w=getWidth();
int h=getHeight();
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(Color.WHITE);
g2d.fill(new Rectangle2D.Double(0, 0, w, h));
g2d.setPaint(Color.BLACK);
g2d.setStroke(new BasicStroke(lineThickness));
g2d.draw(new Line2D.Double(0, h/3, w, h/3));
g2d.draw(new Line2D.Double(0, h*2/3, w, h*2/3));
g2d.draw(new Line2D.Double(w/3, 0, w/3, h));
g2d.draw(new Line2D.Double(w*2/3, 0, w*2/3, h));
for (int i=0; i<9; ++i) {
double xpos=(i%3+0.5)*w/3.0;
double ypos=(i/3+0.5)*h/3.0;
double xr=w/8.0;
double yr=h/8.0;
if (position[i]==O) {
g2d.setPaint(oColor);
g2d.draw(new Ellipse2D.Double(xpos-xr, ypos-yr, xr*2, yr*2));
}
else if (position[i]==X) {
g2d.setPaint(xColor);
g2d.draw(new Line2D.Double(xpos-xr, ypos-yr, xpos+xr, ypos+yr));
g2d.draw(new Line2D.Double(xpos-xr, ypos+yr, xpos+xr, ypos-yr));
}
}
}
public void mouseClicked(MouseEvent e) {
int xpos=e.getX()*3/getWidth();
int ypos=e.getY()*3/getHeight();
int pos=xpos+3*ypos;
if (pos>=0 && pos<9 && position[pos]==BLANK) {
position[pos]=O;
repaint();
putX();
repaint();
}
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
void putX() {
if (won(O))
newGame(O);
else if (isDraw())
newGame(BLANK);
else {
nextMove();
if (won(X))
newGame(X);
else if (isDraw())
newGame(BLANK);
}
}
boolean won(char player) {
for (int i=0; i<8; ++i)
if (testRow(player, rows[i][0], rows[i][1]))
return true;
return false;
}
boolean testRow(char player, int a, int b) {
return position[a]==player && position[b]==player
&& position[(a+b)/2]==player;
}
void nextMove() {
int r=findRow(X);
if (r<0)
r=findRow(O);
if (r<0) {
do
r=random.nextInt(9);
while (position[r]!=BLANK);
}
position[r]=X;
}
int findRow(char player) {
for (int i=0; i<8; ++i) {
int result=find1Row(player, rows[i][0], rows[i][1]);
if (result>=0)
return result;
}
return -1;
}
int find1Row(char player, int a, int b) {
int c=(a+b)/2;
if (position[a]==player && position[b]==player && position[c]==BLANK)
return c;
if (position[a]==player && position[c]==player && position[b]==BLANK)
return b;
if (position[b]==player && position[c]==player && position[a]==BLANK)
return a;
return -1;
}
boolean isDraw() {
for (int i=0; i<9; ++i)
if (position[i]==BLANK)
return false;
return true;
}
void newGame(char winner) {
repaint();
String result;
if (winner==O) {
++wins;
result = "You Win!";
}
else if (winner==X) {
++losses;
result = "I Win!";
}
else {
result = "Tie";
++draws;
}
if (JOptionPane.showConfirmDialog(null,
"You have "+wins+ " wins, "+losses+" losses, "+draws+" draws\n"
+"Play again?", result, JOptionPane.YES_NO_OPTION)
!=JOptionPane.YES_OPTION) {
wins=losses=draws=0;
}
for (int j=0; j<9; ++j)
position[j]=BLANK;
if ((wins+losses+draws)%2 == 1)
nextMove();
}
}
}