-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplay.java
More file actions
277 lines (251 loc) · 8.27 KB
/
Display.java
File metadata and controls
277 lines (251 loc) · 8.27 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.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
* The Display class handles the graphics, draws the grid and pieces, and
* updates after every move by getting input from the mouse and keyboard.
*/
public class Display extends JPanel implements KeyListener, MouseListener {
/**
* Constants for determining grid and piece size.
*/
public static final int MARGIN = 35;
public static final int GRID_ROWS = 15;
public static final int PIECE_DIAMETER = 30;
public static final int END_MARGIN = MARGIN * GRID_ROWS;
/**
* Instance variables that keep track of game play.
*/
private Point currentLocation;
private ArrayList<Point> playerOneMoves;
private ArrayList<Point> playerTwoMoves;
private boolean isPlayerOneTurn;
private boolean hasPlayerOneWon;
private boolean hasPlayerTwoWon;
/**
* Class constructor initalizes this to receive input from mouse click and
* Enter key.
*/
public Display() {
this.addKeyListener(this);
this.addMouseListener(this);
this.setFocusable(true);
currentLocation = new Point();
playerOneMoves = new ArrayList<Point>();
playerTwoMoves = new ArrayList<Point>();
isPlayerOneTurn = true;
hasPlayerOneWon = false;
hasPlayerTwoWon = false;
}
/**
* Update the display every turn or after receiving input.
*
* @param g Graphics object to draw
*/
public void paint(Graphics g) {
super.paint(g);
drawGrid(g);
drawAllCircles(g);
}
/**
* Draw a 15x15 black grid using class constants.
*
* @param g Graphics object to draw lines
*/
private void drawGrid(Graphics g) {
g.setColor(Color.black);
for (int space = 0; space < GRID_ROWS * MARGIN; space += MARGIN) {
g.drawLine(MARGIN + space, MARGIN,
MARGIN + space, GRID_ROWS*MARGIN);
g.drawLine(MARGIN, MARGIN + space,
GRID_ROWS*MARGIN, MARGIN + space);
}
}
/**
* Draw all pieces for both players.
*
* Player 1 is white and moves first. Player 2 is black and moves second.
*
* @param g Graphics object to draw circles
*/
private void drawAllCircles(Graphics g) {
drawCircle(g, playerOneMoves, Color.WHITE);
drawCircle(g, playerTwoMoves, Color.BLACK);
}
/**
* Draw pieces at specified locations given by a list of Points.
*
* @param g Graphics object to draw circles
* @param moves list of Points containing locations where pieces should be
* @param col color of pieces
*/
private static void drawCircle(Graphics g, ArrayList<Point> moves, Color col) {
for (Point piece: moves) {
g.setColor(col);
g.fillOval(piece.getX() - PIECE_DIAMETER/2,
piece.getY() - PIECE_DIAMETER/2,
PIECE_DIAMETER,
PIECE_DIAMETER);
if (col != Color.BLACK) {
g.setColor(Color.BLACK); // black borders on white pieces
g.drawOval(piece.getX() - PIECE_DIAMETER/2,
piece.getY() - PIECE_DIAMETER/2,
PIECE_DIAMETER,
PIECE_DIAMETER);
}
}
}
/**
* Check for a win, 5 pieces in a row in any direction.
*/
private void checkWin() {
ArrayList<Point> moves = getlastPlayerMoves();
// begin with all valid adjacent locations
ArrayList<Point> adjacentPoints = currentLocation.getAdjacentAll();
for (Point adjPoint : adjacentPoints) {
if (moves.contains(adjPoint)) {
// look for more pieces in this direction
int i = adjacentPoints.indexOf(adjPoint);
int direction = Point.DIRECTIONS[i];
boolean isGameOver = checkInRow(moves, adjPoint, direction, 3);
if (isGameOver) {
setWinner();
}
}
}
}
/**
* Set hasPlayerOneWon or hasPlayerTwoWon to true. Assume game is over.
*/
private void setWinner() {
if (!isPlayerOneTurn) {
hasPlayerOneWon = true;
} else {
hasPlayerTwoWon = true;
}
}
/**
* Check for additional consecutive pieces and report win.
*
* @param moves list of Points containing locations where pieces should be
* @param loc starting location to check adjacent Points
* @param dir direction in which to look for adjacent pieces
* @param num number of times to check in a row
*/
private static boolean checkInRow(ArrayList<Point> moves, Point loc, int dir,
int num) {
Point nextLoc = loc.getAdjacent(dir);
while (moves.contains(nextLoc) && num > 0) {
nextLoc = nextLoc.getAdjacent(dir);
num--;
}
if (num == 0) {
return true;
} else {
return false;
}
}
/**
* Get list of moves of the player that went last.
*
* @return list of moves for the last player
*/
private ArrayList<Point> getlastPlayerMoves() {
if (!isPlayerOneTurn) {
return playerOneMoves;
} else {
return playerTwoMoves;
}
}
/**
* Set location from mouse click and make call to update display.
* Freeze the display if the game is over.
*
* @param e mouse click input
*/
@Override
public void mouseClicked(MouseEvent e) {
if (!hasPlayerOneWon && !hasPlayerTwoWon) {
Point newLocation = Point.convertFromPixel(e.getX(), e.getY());
if (isPlayerOneTurn) {
// if not previous move and not overlapping with player two
if (!playerOneMoves.contains(newLocation) &&
!playerTwoMoves.contains(newLocation)) {
playerOneMoves.add(newLocation);
isPlayerOneTurn = false;
}
} else { // player two
// if not previous move and not overlapping with player one
if (!playerTwoMoves.contains(newLocation) &&
!playerOneMoves.contains(newLocation)) {
playerTwoMoves.add(newLocation);
isPlayerOneTurn = true;
}
}
currentLocation = newLocation;
checkWin();
}
this.repaint();
showWinDialog();
}
/**
* Display a dialog saying which player won the game and reset game after
* clicking OK.
*/
private void showWinDialog() {
String winner = null;
if (hasPlayerOneWon) {
winner = "Player 1 won.";
} else if (hasPlayerTwoWon) {
winner = "Player 2 won.";
}
if (hasPlayerOneWon || hasPlayerTwoWon) {
JOptionPane.showMessageDialog(null, winner, "Gomoku",
JOptionPane.PLAIN_MESSAGE);
resetGame();
this.repaint();
}
}
/**
* Clear the board for a new game if Enter key is pressed.
*
* @param e key input
*/
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
resetGame();
this.repaint();
}
}
/**
* Reset all variables to default values.
*/
private void resetGame() {
currentLocation.setAll(0, 0);
playerOneMoves.clear();
playerTwoMoves.clear();
isPlayerOneTurn = true;
hasPlayerOneWon = false;
hasPlayerTwoWon = false;
}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void keyReleased(KeyEvent arg0) {}
@Override
public void keyTyped(KeyEvent arg0) {}
}