-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectFour.java
More file actions
162 lines (131 loc) · 4.17 KB
/
ConnectFour.java
File metadata and controls
162 lines (131 loc) · 4.17 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
import java.io.*;
import java.util.*;
/**
* Connect 4 logic works
* Try to redo with graphical interface
* check CS lecture for that
*
* Author: Pranav Tiwari
* Time: Wed. Jun 16, 2021 4:37 PM CST
*
* Description: Program for playing connect 4 with 2 players. Please follow the prompts.
* 1 refers to a piece from player 1
* 2 refers to a piece from player 2
*/
public class ConnectFour {
public static void main(String args[]){
int[][] board = new int[6][7];
int red=1;
int yellow=2;
Scanner input = new Scanner(System.in);
boolean play=true;
display(board);
while(play){
System.out.println("Player 1, pick the row you want to drop your piece in");
int placeR = input.nextInt();
for(int x=0; x<board[0].length; x++){
if(board[x][placeR]==0) {
board[x][placeR] = 1;
break;
}
}
play=showBoard(board);
if(play==false)
break;
System.out.println("Player 2, pick the row you want to drop your piece in");
int placeY = input.nextInt();
for(int x=0; x<board[0].length; x++){
if(board[x][placeY]==0) {
board[x][placeY] = 2;
break;
}
}
play=showBoard(board);
}
}
public static boolean showBoard(int [][]board){
int winner=done(board);
display(board);
if(winner==1) {
System.out.print("Player 1 has won! Player 2 is vanquished.");
return false;
}
else if(winner==2){
System.out.print("Player 2 has won! Player 1 is vanquished.");
return false;
}
return true;
}
public static void display(int[][] board){ //prints out graphical display of board
System.out.print("\n\n\n");
for(int x=board.length-1; x>=0; x--){
System.out.print("|");
for(int y=0; y<board[0].length; y++){
System.out.print(board[x][y]+" ");
}
System.out.print("|\n");
}
System.out.println("----------------");
System.out.print(" 0 1 2 3 4 5 6 \n");
}
public static int done(int[][]board){
for(int x=0; x<board.length; x++){
for(int y=0; y<board[0].length; y++){
if(board[x][y]==1||board[x][y]==2){
if(check(board, x, y))
return board[x][y];
}
}
}
return 0;
}
private static boolean check(int[][]board, int xCoord, int yCoord){
int checker = board[xCoord][yCoord];
int inRow = 0;
int inColumn =0;
int inDiaLtR =0;
int inDiaRtL =0;
for(int x=0; x<board[1].length; x++) { //search row
if(board[xCoord][x]==checker)
inRow++;
else
inRow=0;
if(inRow==4)
return true;
}
for(int x=0; x<board.length; x++) { //search column
if(board[x][yCoord]==checker)
inColumn++;
else
inColumn=0;
if(inColumn==4)
return true;
}
for(int x=0; x<board.length; x++) { //search diagonal Left to Right
int tempC = xCoord+x;
int tempCY = yCoord+x;
if(tempC<board.length && tempCY<board[0].length) {
if (board[tempC][tempCY] == checker)
inDiaLtR++;
else
inDiaLtR=0;
}
if(inDiaLtR==4)
return true;
}
//does not work
for(int x=0; x<board.length; x++) { //search diagonal Right to Left
int tempC = xCoord-x;
int tempCY = yCoord+x;
if(tempC>=0 && tempCY>=0) {
if (board[tempC][tempCY] == checker)
inDiaRtL++;
else
inDiaRtL=0;
}
if(inDiaRtL==4)
return true;
}
return false;
}
}