-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDots.java
More file actions
395 lines (304 loc) · 11.7 KB
/
Copy pathDots.java
File metadata and controls
395 lines (304 loc) · 11.7 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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
//hiiiiiiiiiiiiiii tiffany here making changes
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Dots extends JPanel implements MouseMotionListener, MouseListener {
JButton restart;
JFrame frame;
JButton exit;
JPanel p1;
public int DOT_NUMBER=0; // The number of dots on each side of the square game board
public static final int DOT_GAP=40; // The space between each dot
public static final int DOT_SIZE=8; // The length of the sides of the square dot
public static final int PLAYER_ONE=1;
public static final int PLAYER_TWO=2;
public static final Color PLAYER_ONE_COLOR=Color.ORANGE; // The color of player1's boxes
public static final Color PLAYER_TWO_COLOR=Color.GREEN; // The color of player2's boxes
private ConnectionSprite[] horizontalConnections; // Array for all the ConnectionSprites that horizontally connect dots
private ConnectionSprite[] verticalConnections; // Array for all the ConnectionSprites that vertically connect dots
private BoxSprite[] boxes; // Array for all the BoxSprites
private Sprite[] dots; // Array for all the dots
private Dimension dim; // Window dimensions
private int clickx; // Holds the x coordinate of mouse click
private int clicky; // Holds the y coordinate of mouse click
private int mousex; // Holds the x coordinate of the mouse location
private int mousey; // Holds the y coordinate of the mouse location
private int centerx; // x coordinate of the center of the gameboard
private int centery; // y coordinate of the center of the gameborad
private int side; // Length of the sides of the square gameboard
private int space; // Length of 1 dot + 1 connection
private int activePlayer; // Holds the current player
public Dots() {
frame=new JFrame("Dots & Boxes");
frame.setSize(600, 650);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addMouseListener(this);
addMouseMotionListener(this);
restart= new JButton("Reset");
exit=new JButton("Exit");
p1=new JPanel();
p1.add(exit);
p1.add(restart);
setSize(600, 600);
frame.add(this,BorderLayout.CENTER);
frame.add(p1,BorderLayout.SOUTH);
restart.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
NewGame();
repaint();
}
});
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
NewGame();
}
private void loadProperties() {
// Initialize fields
clickx=0;
clicky=0;
mousex=0;
mousey=0;
dim=getSize();
centerx=dim.width/2;
centery=(dim.height - 100) /2;
side=DOT_NUMBER * DOT_SIZE + (DOT_NUMBER - 1) * DOT_GAP; // There is one less connection than dot per side
space=DOT_SIZE + DOT_GAP;
}
private void loadDots() {
dots=new Sprite[DOT_NUMBER * DOT_NUMBER];
for(int rows=0; rows<DOT_NUMBER; rows++) {
for(int cols=0; cols<DOT_NUMBER; cols++) {
Sprite dot=new Sprite();
dot.width=DOT_SIZE;
dot.height=DOT_SIZE;
dot.x=centerx - side/2 + cols * space;
dot.y=centery - side/2 + rows * space;
dot.shape.addPoint(-DOT_SIZE/2, -DOT_SIZE/2);
dot.shape.addPoint(-DOT_SIZE/2, DOT_SIZE/2);
dot.shape.addPoint(DOT_SIZE/2, DOT_SIZE/2);
dot.shape.addPoint(DOT_SIZE/2, -DOT_SIZE/2);
int index=rows * DOT_NUMBER + cols;
dots[index]=dot;
}
}
}
private void loadConnections() {
horizontalConnections=new ConnectionSprite[(DOT_NUMBER-1) * DOT_NUMBER];
verticalConnections=new ConnectionSprite[(DOT_NUMBER-1) * DOT_NUMBER];
for(int i=0; i<horizontalConnections.length; i++) {
int colsx=i % (DOT_NUMBER-1);
int rowsx=i / (DOT_NUMBER-1);
int horx=centerx - side / 2 + DOT_SIZE + colsx * space;
int hory=centery - side / 2 + rowsx * space;
horizontalConnections[i]=ConnectionSprite.createConnection(ConnectionSprite.HORZ_CONN, horx, hory);
int colsy=i % DOT_NUMBER;
int rowsy=i / DOT_NUMBER;
int vertx=centerx - side / 2 + colsy * space;
int verty=centery - side / 2 + DOT_SIZE + rowsy * space;
verticalConnections[i]=ConnectionSprite.createConnection(ConnectionSprite.VERT_CONN, vertx, verty);
}
}
private void loadBoxes() {
boxes=new BoxSprite[(DOT_NUMBER-1) * (DOT_NUMBER-1)];
for(int i=0; i<boxes.length; i++) {
int cols=i % (DOT_NUMBER-1);
int rows=i / (DOT_NUMBER-1);
int boxx=centerx - side / 2 + DOT_SIZE + cols * space;
int boxy=centery - side / 2 + DOT_SIZE + rows * space;
ConnectionSprite[] horConn=new ConnectionSprite[2];
horConn[0]=horizontalConnections[i];
horConn[1]=horizontalConnections[i + (DOT_NUMBER - 1)];
ConnectionSprite[] verConn=new ConnectionSprite[2]; // This only works if the verticalConnections were put into the array rows then columns
verConn[0]=verticalConnections[i + rows];
verConn[1]=verticalConnections[i + rows + 1];
boxes[i]=BoxSprite.createBox(boxx, boxy, horConn, verConn);
}
}
private void NewGame(){
String dot = JOptionPane.showInputDialog( "Enter Number of dots in a row/column (3-8)" );
if (dot==null)
System.exit(0);
DOT_NUMBER = Integer.parseInt(dot);
loadProperties();
loadDots();
startGame();
}
private void startGame() {
activePlayer=(double)Math.random()*1 >0.5?PLAYER_ONE:PLAYER_TWO;
loadConnections();
loadBoxes();
}
private ConnectionSprite getConnection(int x, int y) {
// Get the connection that encloses point (x, y) or return null if there isn't one
for(int i=0; i<horizontalConnections.length; i++) {
if(horizontalConnections[i].containsPoint(x, y)) {
return horizontalConnections[i];
}
}
for(int i=0; i<verticalConnections.length; i++) {
if(verticalConnections[i].containsPoint(x, y)) {
return verticalConnections[i];
}
}
return null;
}
private boolean[] getBoxStatus() {
boolean[] status=new boolean[boxes.length];
for(int i=0; i<status.length; i++) {
status[i]=boxes[i].isBoxed();
}
return status;
}
private boolean makeConnection(ConnectionSprite connection) {
boolean newBox=false;
boolean[] boxStatusBeforeConnection=getBoxStatus(); // The two boolean arrays are used to see if a new box was created after the connection was made
connection.connectionMade=true;
boolean[] boxStatusAfterConnection=getBoxStatus();
for(int i=0; i<boxes.length; i++) {
if(boxStatusAfterConnection[i]!=boxStatusBeforeConnection[i]) {
newBox=true;
boxes[i].player=activePlayer;
}
}
if(!newBox) { // Allow the current player to go again if he made a box
if(activePlayer==PLAYER_ONE)
activePlayer=PLAYER_TWO;
else
activePlayer=PLAYER_ONE;
}
checkForGameOver();
return newBox;
}
private int[] calculateScores() {
int[] scores={0, 0};
for(int i=0; i<boxes.length; i++) {
if(boxes[i].isBoxed() && boxes[i].player!=0) {
scores[boxes[i].player - 1]++;
}
}
return scores;
}
private void checkForGameOver() {
int[] scores=calculateScores();
if((scores[0] + scores[1])==((DOT_NUMBER - 1) * (DOT_NUMBER - 1))) {
JOptionPane.showMessageDialog(this, "Player1: " + scores[0] + "\nPlayer2: " + scores[1], "Game Over", JOptionPane.PLAIN_MESSAGE);
NewGame();
repaint();
}
}
private void handleClick() {
ConnectionSprite connection=getConnection(clickx, clicky);
if(connection==null)
return;
if(!connection.connectionMade) {
makeConnection(connection);
}
repaint();
}
public void mouseMoved(MouseEvent event) {
mousex=event.getX();
mousey=event.getY();
// System.out.println(mousex+":"+mousey);
repaint();
}
public void mouseDragged(MouseEvent event) {
mouseMoved(event);
}
public void mouseClicked(MouseEvent event) {
clickx=event.getX();
clicky=event.getY();
handleClick();
}
public void mouseEntered(MouseEvent event) {
}
public void mouseExited(MouseEvent event) {
}
public void mousePressed(MouseEvent event) {
}
public void mouseReleased(MouseEvent event) {
}
private void paintBackground(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, dim.width, dim.height-100);
}
private void paintDots(Graphics g) {
for(int i=0; i<dots.length; i++) {
dots[i].render(g);
}
}
private void paintConnections(Graphics g) {
for(int i=0; i<horizontalConnections.length; i++) {
if(!horizontalConnections[i].connectionMade) {
if(horizontalConnections[i].containsPoint(mousex, mousey)) {
horizontalConnections[i].color=Color.RED;
} else {
horizontalConnections[i].color=Color.WHITE;
}
} else {
horizontalConnections[i].color=Color.BLUE;
}
horizontalConnections[i].render(g);
}
for(int i=0; i<verticalConnections.length; i++) {
if(!verticalConnections[i].connectionMade) {
if(verticalConnections[i].containsPoint(mousex, mousey)) {
verticalConnections[i].color=Color.RED;
} else {
verticalConnections[i].color=Color.WHITE;
}
} else {
verticalConnections[i].color=Color.BLUE;
}
verticalConnections[i].render(g);
}
}
public void paintBoxes(Graphics g) {
for(int i=0; i<boxes.length; i++) {
if(boxes[i].isBoxed()) {
if(boxes[i].player==PLAYER_ONE) {
boxes[i].color=PLAYER_ONE_COLOR;
} else if(boxes[i].player==PLAYER_TWO) {
boxes[i].color=PLAYER_TWO_COLOR;
}
} else {
boxes[i].color=Color.WHITE;
}
boxes[i].render(g);
}
}
public void paintStatus(Graphics g) {
int[] scores=calculateScores();
String status="It is player" + activePlayer + "'s turn";
String status2="Player 1: " + scores[0];
String status3="Player 2: " + scores[1];
g.setColor(Color.BLACK);
g.drawString(status, 10, dim.height-80);
g.fillRect(0, dim.height-100, dim.width, 2);
g.setColor(PLAYER_ONE_COLOR);
g.drawString(status2, 10, dim.height-65);
g.setColor(PLAYER_TWO_COLOR);
g.drawString(status3, 10, dim.height-50);
}
public void paint(Graphics g) {
Image bufferImage=createImage(dim.width, dim.height);
Graphics bufferGraphics=bufferImage.getGraphics();
paintBackground(bufferGraphics);
paintDots(bufferGraphics);
paintConnections(bufferGraphics);
paintBoxes(bufferGraphics);
paintStatus(bufferGraphics);
g.drawImage(bufferImage, 0, 0, null);
}
public void update(Graphics g) {
paint(g);
}
public static void main(String[] args) {
new Dots();
}
}