-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectFourGame.java
More file actions
314 lines (280 loc) · 7.41 KB
/
ConnectFourGame.java
File metadata and controls
314 lines (280 loc) · 7.41 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
/*
*ConnectFour.java
*
*This is the main implementation for the connect four game
*To add your own AI to the file, change
*the class that is instantiated for p1 and p2.
*/
import javax.swing.*;
import java.awt.*;
public class ConnectFourGame extends JPanel
{
//board is an array of int, 0 for empty, 1 for black and 2 for red
private static final int EMPTY = 0;
private static final int BLACK = 1;
private static final int RED = 2;
private int[][] board;
private Player p1, p2;
private String winMsg = "";
private Color winColor=Color.BLACK;
//Stats variables
private boolean statsRound;
private int games;
private int[] wins = new int[3];
public ConnectFourGame()
{
//Setup your players here
/****************************************************/
p1 = new Example(BLACK);
p2 = new Human(RED);
/****************************************************/
board = new int[6][7];
for(int row = 0; row < 6; row++)
for(int col = 0; col < 7; col++)
board[row][col] = 0;
games = 1;
splashScreen();
}
public void splashScreen()
{
String input = JOptionPane.showInputDialog(null, "Enter number of games:");
games = Integer.parseInt(input);
statsRound = games > 1;
}
public void setupGUI()
{
JFrame jf = new JFrame("Connect Four");
Container cp = jf.getContentPane();
jf.setSize(500, 500);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLocationRelativeTo(null);
cp.add(this);
jf.setVisible(true);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
//Draw the names
Font f = new Font("Sans Serif", Font.BOLD, 20);
g.setFont(f);
g.setColor(Color.BLACK);
g.drawString(p1.getName(), 30,30);
g.setColor(Color.RED);
g.drawString(p2.getName(), width - 130, 30);
g.setColor(winColor);
g.drawString(winMsg, width/2-50, 75);
g.setColor(Color.YELLOW);
g.fillRect(0, 100, width, height-100);
//Draw the grid for C4
g.setColor(Color.BLACK);
int rowHeight = (height-100)/6;
for(int rows = 0; rows < 6; rows++)
{
g.drawLine(0, rows * rowHeight + 100, width,rows * rowHeight + 100);
}
int colWidth = (width)/7;
for(int cols = 0; cols<7; cols++)
{
g.drawLine(cols * colWidth, 100, cols*colWidth, height);
}
//paint the pieces on the board
for (int row = 0; row < 6; row++)
{
for(int col = 0; col < 7; col++)
{
if(board[row][col] == 0)
g.setColor(Color.WHITE);
else if(board[row][col] == 1)
g.setColor(Color.BLACK);
else
g.setColor(Color.RED);
g.fillOval(col * colWidth + 5,row * rowHeight + 100 + 5, colWidth - 10, rowHeight - 10);
}
}
}
public void drawBoard()
{
System.out.println("_______________");
for(int row = 0; row < 6; row++)
{
System.out.print("|");
for(int col = 0; col < 7; col++)
{
if(board[row][col] == 0)
System.out.print(" ");
else if(board[row][col] == 1)
System.out.print("B");
else
System.out.print("R");
System.out.print("|");
}
System.out.println();
}
}
public boolean playTurn(Player p)
{
int[][] tempBoard = new int[6][7];
for(int i = 0; i < 6; i++)
for(int j = 0; j < 7; j++)
tempBoard[i][j] = board[i][j];
int spot = p.play(tempBoard);
if(spot < 0 || spot > 6)
return false;
for(int row = 5; row >= 0; row--)
{
if(board[row][spot] == 0)
{
board[row][spot] = p.getColor();
return true;
}
}
return false;
}
public boolean checkWin()
{
//check 4 in a single row
for(int row = 0; row < board.length; row++)
{
for(int col = 0; col < board[row].length-3; col++)
{
if(board[row][col] != 0 && board[row][col] == board[row][col+1] &&board[row][col] == board[row][col+2]&& board[row][col] == board[row][col+3])
return true;
}
}
//check 4 in a single column
for(int col = 0; col < board[0].length; col++)
{
for (int row = board.length - 1; row >= 3; row--)
{
if(board[row][col] != 0 && board[row][col] == board[row-1][col] && board[row][col] == board[row-2][col] && board[row][col] == board[row-3][col])
{
return true;
}
}
}
//check for diag
for(int row = 0; row < board.length-3; row++)
{
for(int col = 0; col <board[0].length-3; col++)
{
if(board[row][col] != 0 && board[row][col] == board[row+1][col+1] && board[row][col] == board[row+2][col+2] && board[row][col] == board[row+3][col+3])
{
return true;
}
}
}
for(int row = 3; row < board.length; row++)
{
for(int col = 0; col <board[0].length-3; col++)
{
if(board[row][col] != 0 && board[row][col] == board[row-1][col+1] && board[row][col] == board[row-2][col+2] && board[row][col] == board[row-3][col+3])
{
return true;
}
}
}
return false;
}
public boolean checkTie()
{
for(int col = 0; col < 7; col++)
if(board[0][col] == 0)
return false;
return true;
}
public char game(Player plyr1, Player plyr2)
{
boolean playerOneTurn = true;
//drawBoard();
if(!statsRound)
setupGUI();
boolean validMove = true;
while(!checkWin() && !checkTie())
{
repaint();
if(playerOneTurn)
validMove = playTurn(plyr1);
else
validMove = playTurn(plyr2);
repaint();
//drawBoard();
try
{
if(!statsRound)
Thread.sleep(100);
}
catch (Exception e){};
if(validMove)
playerOneTurn = !playerOneTurn;
}
if(playerOneTurn && !checkTie()) // remember this has just been flipped so it is now opposite
{
winMsg = plyr2.getName() + " wins!!";
if(plyr2.getColor() == 1)
winColor = Color.BLACK;
else
winColor = Color.RED;
repaint();
return 2;
}
else if(!playerOneTurn && !checkTie())
{
winMsg = plyr1.getName() + " wins!!";
if(plyr1.getColor() == 1)
winColor = Color.BLACK;
else
winColor = Color.RED;
repaint();
return 1;
}
else
{
winMsg = "TIE";
winColor = Color.BLUE;
repaint();
return 0;
}
}
public void reset()
{
for(int row = 0; row < 6; row++)
for(int col = 0; col < 7; col++)
board[row][col] = 0;
}
public void gameLoop()
{
if(statsRound)
{
for(int g = 0; g < games; g++)
{
if(g < games/2)
wins[game(p1, p2)]++;
else
{
int winner = game(p2, p1);
if(winner == 1)
wins[2]++;
else if(winner == 2)
wins[1]++;
else
wins[0]++;
}
// drawBoard();
reset();
}
String msg = p1.getName() + ": " + wins[1];
msg += "\n" + p2.getName() + ": " + wins[2];
msg += "\n" + "Ties: " + wins[0];
JOptionPane.showMessageDialog(null, msg, "Stats Summary", JOptionPane.INFORMATION_MESSAGE);
}
else
game(p1, p2);
}
public static void main(String[] args)
{
ConnectFourGame cfg = new ConnectFourGame();
cfg.gameLoop();
}
}