-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGamePlay.java
More file actions
331 lines (277 loc) · 11.2 KB
/
GamePlay.java
File metadata and controls
331 lines (277 loc) · 11.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
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
package connectfour;
import javax.swing.JOptionPane;
public class GamePlay {
//Declare constants
final int ROWS = 6;
final int colums = 7;
final int winscore = 4;
final int filled = ROWS * colums;
public String p1=JOptionPane.showInputDialog(null,"enter 1st your name::","Player 1");
public String p2=JOptionPane.showInputDialog(null,"enter 2nd your name::","player2");
//Declare private variables
public Disc player1, player2, tempDisc;
public Matrix game;
//GamePlay method to run only for a New Game
public GamePlay () {
//Create the player objects (no colour yet)
player1 = new Disc();
player2 = new Disc();
game = new Matrix();
//Ask player1 to pick a colour
int colourSelection = playerColour(1);
//Assign colours as per player1's choice
assignColour(player1, player2, colourSelection);
//Create a new game and give player1 a disc
tempDisc = player1;
}
public void startGame() {
//Declare variables
int input = 0;
int row = 0;
//Gameplay
do {
String output = displayGrid(game, player1, player2, tempDisc);
String title = "Welcome to the Connect Four game";
String []options = {" EXIT", " 1 " , "2", "3", "4", "5", "6", "7"};
int reply = JOptionPane.showOptionDialog(null, output, title, JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
input = reply;
if (input == 0){
JOptionPane.showMessageDialog(null, "Thank you for playing, see you soon!");
break;
}//end if
//check if the column is full
if (checkColumnOverload(row)){
tempDisc = changePlayer(player1, player2, tempDisc); //change player
}
//add disc to selected square
row = addDiscToSquare(game, tempDisc, input);
//change player
tempDisc = changePlayer(player1, player2, tempDisc);
//display the grid
} while (!checkHorizontally(game) && !checkVertically(game, input) && !checkDiagonally(game, input, row)&& !checkIfGridIsFull(game));
//Conditions to show relevant message according to the result
if (checkHorizontally(game)){
JOptionPane.showMessageDialog(null, "YOU WIN! \nYOU CONNECTED FOUR IN ROW " + row);
showWinner(player1, player2, tempDisc);
displayGrid2(game); //display the winning grid
}//end if
else if(checkVertically(game, input)){
JOptionPane.showMessageDialog(null, "YOU WIN! \nYOU CONNECTED FOUR VERTICALLY IN COLUMN " + input);
showWinner(player1, player2, tempDisc);
displayGrid2(game); //display the winning grid
}//end else if
else if(checkDiagonally(game, input, row)){
JOptionPane.showMessageDialog(null, "YOU WIN! \nYOU CONNECTED FOUR DIAGONALLY");
showWinner(player1, player2, tempDisc);
displayGrid2(game); //display the winning grid
}//end else if
}
//Method for Player 1 to select his colour [returns 0-(RED) or 1-(YELLOW)]
public int playerColour(int playerNumber) {
String []options = {"RED", "YELLOW"};
String message = p1 + ", please select your colour.";
int reply = JOptionPane.showOptionDialog(null, message, "Colour selection", JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
return reply;
}//end playerColour
//Method to assignColour to players
public void assignColour(Disc player1, Disc player2, int colourSelection) {
if (colourSelection == 0) {
player1.setColour(" |____ (R)_____|");
player2.setColour(" |____ (Y)_____|");
JOptionPane.showMessageDialog(null, p1+" takes the RED colour.\n"+p2+" takes the YELLOW colour");
}//end if
else {
player1.setColour(" |____ (Y)_____|");
player2.setColour(" |____ (R)_____|");
JOptionPane.showMessageDialog(null,p1+" takes the YELLOW colour.\n"+p2+" takes the RED colour");
}//end else
}//end assignColour method
//Method to access and print grid in OptionPane
public String displayGrid(Matrix grid, Disc player1, Disc player2, Disc tempDisc) {
String output;
if (tempDisc == player1 ){
output = p1+", it's your turn.\n\n";
}
else {
output = p2+", it's your turn.\n\n";
}
for ( Row tempRow : grid.theGrid) {
String rowOutput = ""; //the output of each row
for (Box tempSquare : tempRow.theRow)
{
rowOutput = rowOutput + " " + tempSquare.toString();
}//end inner for loop
output = output + rowOutput + "\n" + "\n";
;
}//end outer for loop
return output;
}//end showGridinConsole method
//Method to display the winning grid
public String displayGrid2(Matrix grid) {
String output = "GAME OVER\n\n";
for ( Row tempRow : grid.theGrid) {
String rowOutput = ""; //the output of each row
for (Box tempSquare : tempRow.theRow) {
rowOutput = rowOutput + " " + tempSquare.toString();
}//end inner for loop
output = output + rowOutput + "\n" + "\n";
}//end outer for loop
JOptionPane.showMessageDialog(null, output);
return output;
}//end showGridinConsole method
//Access the grid and add player's disc to desired column
public int addDiscToSquare (Matrix grid, Disc theDisc, int colNum) {
int rowNum = 0;
for (int row = 1;row<=ROWS;row++){
for ( Row tempRow : grid.theGrid) {
for (Box tempSquare : tempRow.theRow) {
if (tempSquare.getNum() == colNum && tempRow.getNumber() == row && tempSquare.getEmpty()) {
tempSquare.addDisc(theDisc);
rowNum = row;
return rowNum;
}//end if
}//end inner for loop
}//end outer for loop3
}// end row iteration loop
return rowNum;
}//end addDiscToSquare method
//check Column method to check if the column has already 6 discs
public boolean checkColumnOverload (int row) {
if (row == ROWS) {
String output = "This column is full. Please select another column";
JOptionPane.showMessageDialog(null, output);
return true;
}//end if
return false;
}//end checkColumn method
//method to check connect four in a row
public boolean checkHorizontally (Matrix grid) {
boolean fourInARow = false;
for ( Row tempRow : grid.theGrid) {
String item = "";
int score = 1;
for (Box tempSquare : tempRow.theRow) {
if (tempSquare.toString() == item && !tempSquare.getEmpty()) {
score++;
}
else if (tempSquare.toString() != item || tempSquare.getEmpty()){
score = 1;
}
item = tempSquare.toString();
if (score == winscore) {
fourInARow = true;
}//end if
}//End loop through squares
}//end loop through rows
return fourInARow;
}//end checkRow
//method to check connect four in a column
public boolean checkVertically(Matrix grid, int colNum) {
boolean fourInACol = false;
int score = 0;
String item = ""; //temporary variable to hold the tempSquare
for (int row = 1;row<=ROWS;row++){
for ( Row tempRow : grid.theGrid) {
for (Box tempSquare : tempRow.theRow) {
if (tempSquare.getNum() == colNum && tempRow.getNumber() == row && !tempSquare.getEmpty()) {
if (tempSquare.toString() == item) {
score++;
}//end if
else {
score = 1;
}//end else
item = tempSquare.toString(); //change to the next tempSquare
}//end if
}//end inner for loop
}//end outer for loop
};
if (score == winscore) {
fourInACol = true;
}//end if
return fourInACol;
}//end checkVertically
//method to check four diagonally
public boolean checkDiagonally (Matrix grid, int colNum, int rowNum) {
Boolean fourInaDiagonal = false;
String item = ""; //variable to keep the played colour
for ( Row tempRow : grid.theGrid) {
for (Box tempSquare : tempRow.theRow) {
if (tempSquare.getNum() == colNum && tempRow.getNumber() == rowNum && !tempSquare.getEmpty()) {
item = tempSquare.toString(); //assign to item the played colour
}//end if
}//end inner for loop
}//end outer for loop
// CHECK AXIS (/)
for (int counter1 = winscore-1, counter2 = 0;counter1>=0 && counter2< winscore;counter1--, counter2++){
int score45degrees = 0; //score for axis (/)
for (int row=rowNum-counter1, col=colNum-counter1;row<=rowNum + counter2 && col<=colNum + counter2;row++, col++) {
for ( Row tempRow : grid.theGrid) {
for (Box tempSquare : tempRow.theRow) {
if (tempRow.getNumber() == row && tempSquare.getNum() == col && (tempSquare.toString() == null ? item == null : tempSquare.toString().equals(item))) {
score45degrees++;
}//end if
if (score45degrees == winscore) {
fourInaDiagonal = true;
return fourInaDiagonal;
}//end if
}//end Box loop
}//end Row loop
}//end loop
}//end loop
// CHECK AXIS (\)
for (int counter1 = winscore-1, counter2 = 0;counter1>=0 && counter2< winscore;counter1--, counter2++){
int score135degrees = 0; //score for axis (\)
for (int row=rowNum + counter1, col=colNum-counter1;row>=rowNum - counter2 && col<=colNum + counter2;row--, col++) {
for ( Row tempRow : grid.theGrid) {
for (Box tempSquare : tempRow.theRow) {
if (tempRow.getNumber() == row && tempSquare.getNum() == col && tempSquare.toString() == item) {
score135degrees++;
}//end if
if (score135degrees == winscore) {
fourInaDiagonal = true;
return fourInaDiagonal;
}//end if
}//end Box loop
}//end Row loop
}//end loop
}//end loop
return fourInaDiagonal;
}
//method to check if the grid is full
public boolean checkIfGridIsFull (Matrix grid) {
boolean full = false;
int gridCounter = 0;
for (Row tempRow: grid.theGrid) {
for (Box tempSquare : tempRow.theRow) {
if (tempSquare.getEmpty() == false) {
gridCounter++;
//System.out.println(gridCounter);
}//end if
}//end inner loop
}//end outer loop
if (gridCounter == filled) {
JOptionPane.showMessageDialog(null, "THE GRID IS FULL - IT'S A DRAW!");
full = true;
}
return full;
}//end checkIfGridIsFull
//method to change players after they play
public Disc changePlayer(Disc player1, Disc player2, Disc tempDisc) {
if (tempDisc == player1) {
tempDisc = player2;
} else {
tempDisc = player1;
}
return tempDisc;
}
//method to show winner
public void showWinner(Disc player1, Disc player2, Disc tempDisc){
if (tempDisc == player1){
JOptionPane.showMessageDialog(null, p2+", YOU WIN.");
}//end if
else {
JOptionPane.showMessageDialog(null, p1+", YOU WIN.");
}//end else
}//end showWinner
}