-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToePanel.java
More file actions
277 lines (271 loc) · 8.14 KB
/
TicTacToePanel.java
File metadata and controls
277 lines (271 loc) · 8.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
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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.util.Stack;
/**
* Represent the TicTacToe game Panel
* @author Acosta and Chen
* @version 1.5 4/8/14
*/
public class TicTacToePanel extends JPanel
{
private int turn = 1;//PlayerX's turn == 1, PlayerO's turn == 2
private int playerXWin = 0, playerOWin = 0;
private int gameCount = 1;
private Cell[][] cell = new Cell[3][3];
private JPanel boardPanel = new JPanel();//boardPanel is the 3*3 game board.
private JLabel statusLabel = new JLabel("PlayerX starts this time.");//statusLabel is the label showing current status.
private JLabel titleLabel = new JLabel("Tic Tac Toe Game 1");
private JLabel infoLabel= new JLabel("X:O = "+playerXWin+":"+playerOWin);
private JButton undo = new JButton("UNDO");
private Stack<Cell> stack = new Stack<Cell>();
/**
* Constructor, creates and implements all the components for display.
*/
public TicTacToePanel()
{
boardPanel.setLayout(new GridLayout(3,3,0,0));
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
boardPanel.add(cell[i][j] = new Cell(i,j));
boardPanel.setBorder(new LineBorder(Color.blue,1));
statusLabel.setBorder(new LineBorder(Color.red, 1));
statusLabel.setFont(new Font("Arial", Font.BOLD, 50));
titleLabel.setBorder(new LineBorder(Color.black, 1));
titleLabel.setFont(new Font("Times New Roman", Font.CENTER_BASELINE, 60));
infoLabel.setBorder(new LineBorder(Color.green, 1));
infoLabel.setFont(new Font("Arial", Font.PLAIN, 60));
undo.addActionListener(new UndoButtonListener());
undo.addKeyListener(new ControlZListener());
this.setLayout(new BorderLayout());
this.add(boardPanel, BorderLayout.CENTER);
this.add(statusLabel, BorderLayout.SOUTH);
this.add(titleLabel, BorderLayout.NORTH);
this.add(infoLabel, BorderLayout.EAST);
this.add(undo, BorderLayout.WEST);
}
/**
* Decide if the player just won for this turn
* @param symbol the symbol for the current player
* @return true if the player just won
*/
private boolean isWin(char symbol)
{
for(int i=0;i<3;i++)
if((cell[i][0].getSymbol()==symbol)
&&(cell[i][1].getSymbol()==symbol)&&(cell[i][2].getSymbol()==symbol))
return true;//if the player win horizontally
for(int j=0;j<3;j++)
if((cell[0][j].getSymbol()==symbol)
&&(cell[1][j].getSymbol()==symbol)&&(cell[2][j].getSymbol()==symbol))
return true;//if the player win vertically
if((cell[0][0].getSymbol()==symbol)
&&(cell[1][1].getSymbol()==symbol)&&(cell[2][2].getSymbol()==symbol))
return true;//if the player win cis-diagonally
if((cell[0][2].getSymbol()==symbol)
&&(cell[1][1].getSymbol()==symbol)&&(cell[2][0].getSymbol()==symbol))
return true;//if the player win trans-diagonally
return false;
}
/**
* Decide if the game is ended as a draw
* @return true if the game is ended as a draw
*/
private boolean isDraw()
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if(cell[i][j].getSymbol()==' ') return false;//if there is still empty cell
return true;
}
/**
* Reset the game by empty all the symbols on the board.
*/
private void gameOver()
{//Reset the game
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
cell[i][j].setSymbol(' ');
infoLabel.setText("X:O = "+playerXWin+":"+playerOWin);
stack.clear();
turn = (gameCount + 1) % 2 + 1;
if (turn == 1) statusLabel.setText("PlayerX starts this time.");
if (turn == 2) statusLabel.setText("PlayerO starts this time.");
titleLabel.setText("Tic Tac Toe Game "+gameCount);
}
/**
* Represent a cell in the 3*3 board.
* @author Acosta and Chen
* @version 1.3 4/5/14
*/
private class Cell extends JPanel
{
private int i, j;//row and column index of this cell
private char symbol;//'X', 'O', or ' '
/**
* Get the char symbol of this cell
* @return symbol ('X', 'O', or ' ')
*/
public char getSymbol()
{
return symbol;
}
/**
* Set the char symbol of this cell, push to the stack, and repaint the panel
* @param newSymbol the symbol to set
*/
public void setSymbol(char newSymbol)
{
symbol = newSymbol;
if ((symbol == 'X')||(symbol == 'O')) stack.push(this);
repaint();
}
/**
* Constructor, create the cell and add mouse listener.
* @param i Row index of this cell
* @param j Column index of this cell
*/
public Cell(int i, int j)
{
this.i = i; this.j = j; this.symbol = ' ';
setBorder(new LineBorder(Color.yellow, 1));
addMouseListener(new StepListener());
}
/**
* Paint the display of the panel.
*/
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(symbol == 'X')
{
g.drawLine(10, 10, getSize().width-10, getSize().height-10);
g.drawLine(getSize().width-10, 10, 10, getSize().height-10);
}
if(symbol == 'O')
{
g.drawOval(10, 10, getSize().width-20, getSize().height-20);
}
}
/**
* MouseListener for the cells of the board
* @author Acosta and Chen
* @version 1.2 4/4/14
*/
private class StepListener implements MouseListener
{
public void mousePressed(MouseEvent e)
{
if(symbol == ' ')//Each step must click an empty cell i.e. symbol is ' '
{
if(turn == 1)
{
setSymbol('X');
if(isWin('X'))
{
playerXWin++; gameCount++;
JOptionPane.showMessageDialog(null,"X won! "+playerXWin+":"+playerOWin,
"Message", JOptionPane.YES_NO_CANCEL_OPTION);
//statusLabel.setText("X won! Game Over");
gameOver();
}
else if(isDraw())
{
gameCount++;
JOptionPane.showMessageDialog(null,"Draw! "+playerXWin+":"+playerOWin,
"Message", JOptionPane.INFORMATION_MESSAGE);
//statusLabel.setText("Draw! Game Over");
gameOver();
}
else
{
turn = 2;
statusLabel.setText("O's turn");
}
}
else if(turn == 2)
{
setSymbol('O');
if(isWin('O'))
{
playerOWin++; gameCount++;
JOptionPane.showMessageDialog(null, "O won! "+playerXWin+":"+playerOWin
,"Message", JOptionPane.INFORMATION_MESSAGE);
//statusLabel.setText("O won! Game Over");
gameOver();
}
else if(isDraw())
{
gameCount++;
JOptionPane.showMessageDialog(null,"Draw! "+playerXWin+":"+playerOWin,
"Message", JOptionPane.INFORMATION_MESSAGE);
//statusLabel.setText("Draw! Game Over");
gameOver();
}
else
{
statusLabel.setText("X's turn");
turn = 1;
}
}
}
else
{//Show the user that this move is invalid
JOptionPane.showMessageDialog(null,"Not here!",
"Message", JOptionPane.INFORMATION_MESSAGE);
}
undo.requestFocus();
}
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
}
/**
* ActionListener for the Undo Button.
* @author Acosta and Chen
* @version 1.0 4/3/14
*/
private class UndoButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
undoOneStep();
}
}
/**
* KeyListener for the Control-Z.
* @author Acosta and Chen
* @version 1.0 4/5/14
*/
private class ControlZListener implements KeyListener
{
public void keyPressed(KeyEvent event)
{
if(event.isControlDown() == true)
if(event.getKeyCode() == KeyEvent.VK_Z)
undo.doClick();
}
public void keyReleased(KeyEvent event){}
public void keyTyped(KeyEvent event){}
}
/**
* Undo one step of the game when the game is not finished. Called by the undo button action.
*/
private void undoOneStep()
{
if (stack.size() > 0)
{
Cell oneStep = stack.pop();
cell[oneStep.i][oneStep.j].setSymbol(' ');
int temp = 0;
if (turn == 1) temp = 2;
else if (turn == 2) temp = 1;
turn = temp;
if (turn == 1) statusLabel.setText("X's turn");
if (turn == 2) statusLabel.setText("O's turn");
}
}
}