-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
370 lines (339 loc) · 9.95 KB
/
Main.java
File metadata and controls
370 lines (339 loc) · 9.95 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
import java.util.*;
import javax.swing.JFrame;
import java.awt.*;
public class Main {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
int width = 7;
int height = 7;
int bottomR = 6;
int turns = 0;
int lastPlay = 10;
int player = 0;
//initializes the frame for the graphics.
JFrame frame = new JFrame("CONNECT4PALOOZA - Dhruv Rawat");
Canvas canvas = new Drawing();
System.out.println("Welcome to CONNECT4PALOOZA! The best game of Connect 4 on the internet!");
System.out.println();
//array in which all information is stored
char[][] board = new char[width][height];
//initializes all indices in array
for(int i = 0; width > i; i++) {
for(int j = 0; height > j; j++) {
board[i][j] = '_';
}
}
//prints the board
print(board,frame, canvas, lastPlay, width, height);
boolean continueGame = true;
player = 1;
while(continueGame) {
if(player > 2) {
player = 1;
}
place(board, width, height, bottomR, player); //this method gets the input from the player and places a piece down in the appropriate spot.
print(board, frame, canvas, lastPlay, width, height);
turns++;
if(!playerCheck(board, width, height, player)) { //if the current player has won, then game is over
lastPlay = player; //sets this so program knows the current playerhas won
continueGame = false;
}
if(turns == 49) { //if all spots on the board are filled, but there is no winner, then game terminates
continueGame = false;
}
player++;
}
//determines winner based on who played the last play
if(lastPlay == 10) {
lastPlay = 0;
print(board, frame, canvas, lastPlay, width, height);
System.out.println("It is a draw!");
}
else if(lastPlay == 1) {
print(board, frame, canvas, lastPlay, width, height);
System.out.println("Player 1 wins!");
}
else {
print(board, frame, canvas, lastPlay, width, height);
System.out.println("Player 2 wins!");
}
}
public static boolean inputCheck(String playerInput) { //checks if input is valid
boolean valid = false;
int ACSII = 0;
if(playerInput.length() == 1) { //checks if the length is only one character
ACSII = (int) playerInput.charAt(0);
if(ACSII >= 49 && ACSII <= 55) { //checks if ACSII value of character matches accepted values of 1 to 5
valid = true; //states condition as true so loop will terminate
}
}
return valid;
}
//parameters w and h will only be used if game is text-based. Board, frame and canvas will only be used if graphics are used
public static void print(char[][] board, JFrame frame, Canvas canvas, int lastPlay, int w, int h) {
/*Part 1: this code is used for the text-based version of the game.*/
for (int i = 0; w > i; i += 1) {
for (int j = 0; h > j; j += 1) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
System.out.println("1 2 3 4 5 6 7");
System.out.println();
/*Part 2: this code is used for the graphics version of the game
((Drawing) canvas).addArray(board, lastPlay);//sends array to the Drawing class file
canvas.setSize(702, 860); //sizes the screen
frame.add(canvas);
frame.pack();
frame.setVisible(true); //sets screen as visible
*/
}
public static char[][] place(char[][] board, int w, int h, int bottomR, int p){
int counter = 1;
int inputCounter = 0;
int trial = 0;
if(p == 1) {//distinguishes between the player that is playing
System.out.println("Player 1 turn. Input a number between 1 and 7.");
}
else {
System.out.println("Player 2 turn. Input a number between 1 and 7.");
}
boolean inputValid = false;
String userInput = "";
boolean turnNotDone = true;
while(turnNotDone) {
if(trial == 0 || trial > 1 && counter == 0) {//ensures that inputs is only taken when necessary
trial++; //after every trial for a player, the value increases
inputValid = false;
inputCounter = 0;
while(inputValid == false) {//while the input is not valid
if(inputCounter > 0) { //if input was incorrect
System.out.println("Invalid input. Please input an integer between 1 and 7.");
}
userInput = input.next();
inputValid = inputCheck(userInput); //checks if input is valid
inputCounter++;
}
}
int c = Integer.valueOf(userInput) - 1; //converts string input into an integer
//finds appropriate spot on grid and places player's counter
if(counter < h) {
if(board[bottomR][c] == '_') {//if the bottom row is empty
if(p == 1) { //checks which player is playing
board[bottomR][c] = 'X';
turnNotDone = false;
}
else {
board[bottomR][c] = 'O';
turnNotDone = false;
}
}
else if(board[bottomR][c] == 'X' || board[bottomR][c] == 'O'){ //if the bottom row is full
if(board[bottomR - counter][c] == '_'){ //increments up one row until an empty spot is found
if(p == 1) {
board[bottomR - counter][c] = 'X';
turnNotDone = false;
}
else {
board[bottomR - counter][c] = 'O';
turnNotDone = false;
}
}
}
}
counter++; //with every failed attempt to find an empty spot in the row, the counter increases
if(counter == w) { //if column is full, the player is prompted to input another value
System.out.println("Please select a different column. The one you selected is full");
counter = 0;
trial++;
}
}
return board;
}
public static boolean playerCheck(char[][] b, int w, int h, int p) {
boolean result = true;
//checks all possible winning conditions and see ig they are present
if(!verticalCheck(b,w,h, p) || !horizontalCheck(b,w,h, p) || !forwardDiagonalCheck(b,w,h, p) || !backwardDiagonalCheck(b,w,h, p)) {
result = false;
}
return result;
}
public static boolean verticalCheck(char[][] board, int w, int h, int p) {
boolean result = true;
int counter = 0;
//goes through the board vertically
for(int i = 0; w > i; i += 1){
for(int j = 0; h > j; j += 1){
if(p == 1) {//distinguishes between the player playing
if(board[j][i] == 'X'){
counter++;
if(counter == 4){
result = false;
}
}
else {
counter = 0;
}
}
else {
if(board[j][i] == 'O'){
counter++;
if(counter == 4) {
result = false;
}
}
else {
counter = 0;
}
}
}
}
return result;
}
public static boolean horizontalCheck(char[][] board, int w, int h, int p) {
boolean result = true;
int counter = 0;
//goes through board horizontally
for(int i = 0; w > i; i += 1){
for(int j = 0; h > j; j += 1){
if(p == 1) {//distinguishes between player playing
if(board[i][j] == 'X'){ //if it finds an X, add 1 to counter
counter++;
if(counter == 4){
result = false;
}
}
else{
counter = 0; // if next piece is not an X, set counter to 0
}
}
else {
if(board[i][j] == 'O'){ //if it finds an X, add 1 to counter
counter++;
if(counter == 4){
result = false;
}
}
else{
counter = 0; // if next piece is not an X, set counter to 0
}
}
}
}
return result;
}
public static boolean forwardDiagonalCheck(char[][] board, int w, int h, int p) {
boolean result = true;
int counter = 0;
int checkColumn = 1;
int checkRow = 1;
boolean check = false;
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
if(p == 1) {//distinguishes between player playing
if(board[i][j] == 'X') {
counter++;
check = true;
while(check) { //checks to see if there is a possible winning combination
if(board[i + checkColumn][j + checkRow] == 'X') { //checks boxes diagonal
counter++;
if(counter == 4) { //if four consecutive matching pieces found, loop terminates
result = false;
check = false;
}
}
else { //if diagonal piece does not match, the loop terminates
check = false;
}
//increments these values to ensure that a row diagonal to the current one is searched next
checkColumn++;
checkRow++;
}
}
}
else {
if(board[i][j] == 'O') {
counter++;
check = true;
while(check) {
if(board[i + checkColumn][j + checkRow] == 'O') {
counter++;
if(counter == 4) {
result = false;
check = false;
}
}
else {
check = false;
}
checkColumn++;
checkRow++;
}
}
}
counter = 0;
checkColumn = 1;
checkRow = 1;
}
}
return result;
}
public static boolean backwardDiagonalCheck(char[][] board, int w, int h, int p) {
boolean result = true;
int counter = 0;
int checkColumn = 1;
int checkRow = 1;
boolean check = false;
for(int i = 0; i < 4; i++) {
for(int j = h-1; j > 2; j--) {
if(p == 1) { //distinguishes between player playing
if(board[i][j] == 'X') {
counter++;
check = true;
while(check) {
if(board[i + checkColumn][j - checkRow] == 'X') {
counter++;
if(counter == 4) {
result = false;
check = false;
}
}
else {
check = false;
}
checkColumn++;
checkRow++;
}
counter = 0;
checkColumn = 1;
checkRow = 1;
}
}
else {
if(board[i][j] == 'O') {
counter++;
check = true;
while(check) {
if(board[i + checkColumn][j - checkRow] == 'O') {
counter++;
if(counter == 4) {
result = false;
check = false;
}
}
else {
check = false;
}
checkColumn++;
checkRow++;
}
counter = 0;
checkColumn = 1;
checkRow = 1;
}
}
}
}
return result;
}
}