-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathInterfaceEngine.java
More file actions
299 lines (249 loc) · 10.2 KB
/
Copy pathInterfaceEngine.java
File metadata and controls
299 lines (249 loc) · 10.2 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
/*
Author: Jared Ostmeyer
Disclaimer: Feel free to use and modify the code. My only demand is that you do
not take credit for this work. You may not claim this code as your own!
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class InterfaceEngine extends Applet implements ActionListener, ComponentListener,
MouseListener, MouseMotionListener, Runnable, Game{
// the below constants determine intial properties of the window--------------------------------------------------
public final static Color EMPTY_COLOR = Color.WHITE;
public final static Color BOARD_COLOR = Color.ORANGE;
public final static Color HUMAN_COLOR = Color.RED;
public final static Color COMPUTER_COLOR = Color.BLACK;
public final static Color HIGHLIGHT_COLOR = Color.GREEN;
public final static int WINDOW_WIDTH = 400;
public final static int WINDOW_HEIGHT = 400;
public final static String WINDOW_NAME = "Java 4";
public final static int FRAME_RATE = 15;
public final int BUTTON_BORDER = 25;
public final static String TITLE = "Connnect Four",
COMPUTER_WINS = "The computer has won!",
HUMAN_WINS = "The human has won!",
BOARD_FULL = "The game has ended.",
NEW_GAME = "New Game",
SET_AI = "A.I. Difficulty",
ABOUT = "About",
DIALOG_ABOUT = "Connect Four was written by Jared Ostmeyer",
HUMAN_MOVE_FIRST = "Move first",
HUMAN_MOVE_SECOND = "Move second",
DIALOG_NEW_GAME = "Do you want to move first?",
DIALOG_SET_AI = "Choose the difficulty",
AI_EASY = "Easy",
AI_MEDIUM = "Medium",
AI_HARD = "Hard",
COMPUTER_TURN = "The computer is thinking.",
HUMAN_TURN = "Your turn human.";
public final static int AI_PLY_EASY = 3,
AI_PLY_MEDIUM = 6,
AI_PLY_HARD = 8;
public final static double AI_RAND_WEIGHT_EASY = 1.0,
AI_RAND_WEIGHT_MEDIUM = 0.5,
AI_RAND_WEIGHT_HARD = 0.1;
// the above constants determine intial properties of the window--------------------------------------------------
// the below variables are used only by InterfaceEngine-----------------------------------------------------------
Connect4 c4 = null;
public Color boardColors[][];
public int x_;
public Rectangle clientRect, boardRect;
public int spacing;
public int aI;
Button newGame, setAI, about;
// the below variables are used only by InterfaceEngine-----------------------------------------------------------
public void init(){
boardColors = new Color[8][8];
x_ = -1;
resetBoardColors();
aI = Game.AI_MEDIUM;
clientRect = new Rectangle();
boardRect = new Rectangle();
addMouseListener(this);
addMouseMotionListener(this);
addComponentListener(this);
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setBackground(BOARD_COLOR);
newGame = new Button(NEW_GAME);
setAI = new Button(SET_AI);
about = new Button(ABOUT);
add(newGame);
add(setAI);
add(about);
newGame.addActionListener(this);
setAI.addActionListener(this);
about.addActionListener(this);
newGame.setBounds(0, 0, 120, 30);
componentResized(null);
(new Thread(this)).start();
}
public void update(Graphics g){ // override or else there will be flickering!
paint(g);
}
public void paint(Graphics g){
Rectangle pieceRect;
for(int x = 0; x < 8; x++){
for(int y = 0; y < 8; y++){
pieceRect = getPieceRect(x, y);
g.setColor(boardColors[x][y]);
g.fillOval(pieceRect.x, pieceRect.y,
pieceRect.width, pieceRect.height);
}
pieceRect = getPieceRect(x, -1);
if(x == x_){
g.setColor(HIGHLIGHT_COLOR);
g.fillRect(pieceRect.x, boardRect.y - (3*spacing)/10,
pieceRect.width, spacing/18);
}
else{
g.setColor(BOARD_COLOR);
g.fillRect(pieceRect.x, boardRect.y - (3*spacing)/10,
pieceRect.width, spacing/18);
g.setColor(HIGHLIGHT_COLOR);
g.drawRect(pieceRect.x, boardRect.y - (3*spacing)/10,
pieceRect.width, spacing/18);
}
}
}
public void finalize(){
c4.terminate();
}
public void run(){
promptNewGame();
}
public synchronized void makeMove() throws InterruptedException{
showStatus(HUMAN_TURN);
wait();
int move = x_;
c4.dropPiece(move);
animateDrop(move, HUMAN_COLOR);
showStatus(COMPUTER_TURN);
}
public void status(int message) throws InterruptedException{
switch(message){
case Game.BOARD_IS_FULL:{
JOptionPane.showMessageDialog(null, BOARD_FULL, TITLE,
JOptionPane.INFORMATION_MESSAGE);
showStatus(BOARD_FULL);
}
break;
case Game.COMPUTER_IS_WINNER:{
JOptionPane.showMessageDialog(null, COMPUTER_WINS, TITLE,
JOptionPane.INFORMATION_MESSAGE);
showStatus(COMPUTER_WINS);
}
break;
case Game.HUMAN_IS_WINNER:{
JOptionPane.showMessageDialog(null, HUMAN_WINS, TITLE,
JOptionPane.INFORMATION_MESSAGE);
showStatus(HUMAN_WINS);
}
}
}
public void getMove(int move) throws InterruptedException{
animateDrop(move, COMPUTER_COLOR);
}
public void mouseMoved(MouseEvent e){
x_ = getColumn(e.getX());
repaint();
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){
x_ = -1;
repaint();
}
public void mousePressed(MouseEvent e){}
public synchronized void mouseReleased(MouseEvent e){
x_ = getColumn(e.getX());
notify();
}
public void mouseDragged(MouseEvent e){}
public void actionPerformed(ActionEvent e){
String arg = e.getActionCommand();
if(arg.equals(NEW_GAME))
promptNewGame();
else if(arg.equals(SET_AI)){
Object[] options = {AI_EASY, AI_MEDIUM, AI_HARD};
int n = JOptionPane.showOptionDialog(this, DIALOG_SET_AI, TITLE,
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options,
options[aI]);
if(n == 0)
aI = Game.AI_EASY;
else if(n == 1)
aI = Game.AI_MEDIUM;
else if(n == 2)
aI = Game.AI_HARD;
c4.setAI(aI);
}
else if(arg.equals(ABOUT)){
JOptionPane.showMessageDialog(null, DIALOG_ABOUT, TITLE,
JOptionPane.INFORMATION_MESSAGE);
}
}
public void componentResized(ComponentEvent e){
Dimension clientDim = getSize();
clientRect.setLocation(0, BUTTON_BORDER);
clientRect.setSize(clientDim.width, clientDim.height-BUTTON_BORDER);
int minDim = clientRect.width > clientRect.height ? clientRect.height : clientRect.width;
spacing = minDim/10;
int xOffSet = (clientRect.width-minDim)/2+spacing,
yOffSet = (clientRect.height-minDim)/2+spacing;
boardRect.setLocation(clientRect.x+xOffSet, clientRect.y+yOffSet);
boardRect.setSize(clientRect.width-2*xOffSet,
clientRect.height-2*yOffSet);
}
public void componentMoved(ComponentEvent e){}
public void componentShown(ComponentEvent e){}
public void componentHidden(ComponentEvent e){}
public Rectangle getPieceRect(int column, int row){
Rectangle pieceRect = new Rectangle();
pieceRect.setLocation(boardRect.x+spacing*column+spacing/20,
boardRect.y+boardRect.height-spacing*row-(19*spacing)/20);
pieceRect.setSize((9*spacing)/10, (9*spacing)/10);
return pieceRect;
}
public int getColumn(int xCoor){
if(xCoor < boardRect.x)
return -1;
return (xCoor-boardRect.x)/spacing;
}
public int getRow(int yCoor){
if(yCoor < boardRect.y)
return -1;
return (yCoor-boardRect.y)/spacing;
}
private void animateDrop(int x, Color col) throws InterruptedException{
int y = 7;
if(boardColors[x][y] != EMPTY_COLOR)
return;
for(; y >= 0; y--){
boardColors[x][y] = col;
repaint();
Thread.sleep((long)(1000/FRAME_RATE));
if(y > 0 && boardColors[x][y-1] == EMPTY_COLOR)
boardColors[x][y] = EMPTY_COLOR;
else
break;
}
}
public void resetBoardColors(){
for(int x = 0; x < 8; x++)
for(int y = 0; y < 8; y++)
boardColors[x][y] = EMPTY_COLOR;
}
public void promptNewGame(){
if(c4 != null)
c4.terminate();
Object[] options = {HUMAN_MOVE_FIRST, HUMAN_MOVE_SECOND};
int n = JOptionPane.showOptionDialog(this, DIALOG_NEW_GAME, TITLE,
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options,
options[0]);
if(n == 1) // human moves second
c4 = new Connect4(this, COMPUTER, aI);
else // human moves first
c4 = new Connect4(this, HUMAN, aI);
resetBoardColors();
}
}