-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameBoard.java
More file actions
275 lines (251 loc) · 6.21 KB
/
GameBoard.java
File metadata and controls
275 lines (251 loc) · 6.21 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
import java.util.*;
public class GameBoard{
private int[][] grid;
public static final int DEFAULT_ROWS = 6;
public static final int DEFUALT_COLUMNS = 7;
public GameBoard(){
//this constructor makes the defualt game board
grid = new int[DEFAULT_ROWS][DEFUALT_COLUMNS];
for(int r = 0; r < grid.length; r++){
for(int c = 0; c < grid[0].length; c++){
grid[r][c] = 0;
}
}
}
public GameBoard(int[][] chips){
//this constructor effectively copies another game board
grid = new int[chips.length][chips[0].length];
for(int r = 0; r < chips.length; r++){
for(int c = 0; c < chips[0].length; c++){
grid[r][c] = chips[r][c];
}
}
}
public GameBoard(int rows, int cols){
grid = new int[rows][cols];
}
public int[][] getGridCopy(){
int[][] toRet = new int[grid.length][grid[0].length];
for(int r = 0; r < grid.length; r++){
for(int c = 0; c < grid[0].length; c++){
toRet[r][c] = grid[r][c];
}
}
return toRet;
}
public int getNumColumns(){
return grid[0].length;
}
public int getNumRows(){
return grid.length;
}
public void printBoard(){
//this method formats and prints the current game board
System.out.println(" ------------- ");
for(int r = 0; r < grid.length; r++){
System.out.print("|");
for(int c = 0; c < grid[0].length; c++){
if(c != 0){
System.out.print(" ");
}
if(grid[r][c] == 0){
System.out.print("_");
}
else if(grid[r][c] == 1){
System.out.print("O");
}
else if(grid[r][c] == 2){
System.out.print("X");
}
}
System.out.println("|");
}
System.out.println(" ------------- ");
}
public GameBoard copyBoard(){
GameBoard toRet = new GameBoard(this.grid);
return toRet;
}
public String getRowAsString(int row){
//this method creates a string of the desired row ex: "011012"
String theRow = "";
for(int i = 0; i < grid[row].length; i++){
theRow += grid[row][i];
}
return theRow;
}
public String getColumnAsString(int col){
String theCol = "";
for(int r = 0; r < grid.length; r++){
theCol += grid[r][col];
}
return theCol;
}
public String getUpDiagonalAsString(int r, int c){
if(r + c < grid[0].length-1){
r = r + c;
c = 0;
}
else{
int val = grid.length - r;
c = c - val + 1;
r = grid.length-1;
}
String theDiag = this.getUpDiagonalAsString(r,c,"");
return theDiag;
}
public String getUpDiagonalAsString(int r, int c, String s){
s += grid[r][c];
if(r == 0 || c == grid[0].length-1){
return s;
}
else{
s = this.getUpDiagonalAsString(r-1,c+1, s);
return s;
}
}
public String getDownDiagonalAsString(int r, int c){
if(r - c >= grid.length-grid[0].length){
int val = grid.length-1-r;
c = c + val;
r = grid.length-1;
}
else{
int val = grid[0].length - c;
c = grid[0].length - 1;
r = r + val;
}
String theDiag = this.getDownDiagonalAsString(r,c,"");
return theDiag;
}
public String getDownDiagonalAsString(int r, int c, String s){
s += grid[r][c];
if(r==0 || c == 0){
return s;
}
else{
s = this.getDownDiagonalAsString(r-1,c-1,s);
return s;
}
}
public int getWinner(){
int r = 0;
int c = 0;
String row;
String col;
String upDiag;
String downDiag;
while(r < grid.length){
row = this.getRowAsString(r);
col = this.getColumnAsString(c);
upDiag = this.getUpDiagonalAsString(r,c);
downDiag = this.getDownDiagonalAsString(r,c);
int[] rowArr = checkWinner(row);
int[] colArr = checkWinner(col);
int[] upDiagArr = checkWinner(upDiag);
int[] downDiagArr = checkWinner(downDiag);
if(rowArr[0] == 1){
return rowArr[1];
}
else if(colArr[0] == 1){
return colArr[1];
}
else if(upDiagArr[0] == 1){
return upDiagArr[1];
}
else if(downDiagArr[0] == 1){
return downDiagArr[1];
}
if(c == grid[0].length-1){
r++;
c = 0;
}
else {
c++;
}
}
return -1;
}
public int[] checkWinner(String s){
int[] toRet = new int[2];
if(s.indexOf("1111") >= 0){
toRet[0] = 1;
toRet[1] = 1;
}
else if(s.indexOf("2222") >= 0){
toRet[0] = 1;
toRet[1] = 2;
} else{
toRet[0] = -1;
toRet[1] = -1;
}
return toRet;
}
public void placeChipInColumn(int aPlayerNum, int col){
int row = getRowForCol(col);
changeSlot(row,col,aPlayerNum);
}
public void changeSlot(int r, int c, int val){
if(!(val == 0 || val == 1 || val == 2)){
System.out.println("Invalid Value");
return;
}
grid[r][c] = val;
}
public int getRowForCol(int c){
String col = this.getColumnAsString(c);
col = GameBoard.reverse(col);
int ind = col.indexOf("0");
if(ind == -1){
return -1;
}
else{
return grid.length - 1 - ind;
}
}
private static String reverse(String s){
String[] Char = new String[s.length()];
for(int i = 0; i < s.length(); i++){
Char[i] = s.substring(i,i+1);
}
String temp = "";
for(int i = 0; i < Char.length; i++){
temp += Char[Char.length-1-i];
}
return temp;
}
public boolean isColumnFull(int c){
if(getRowForCol(c) == -1){
return true;
}
return false;
}
public boolean isFull(){
int full = 0;
for(int c = 0; c < grid[0].length; c++){
if(this.isColumnFull(c)){
full++;
}
}
if(full == grid[0].length){return true;}
return false;
}
public String getValidColumns(){
String validCols = "";
for(int c = 0; c < grid[0].length; c++){
if(!this.isColumnFull(c)){
validCols += c + ", ";
}
}
return validCols.substring(0,validCols.length()-2);
}
public List getValidColumnsAsIntList(){
List<Integer> validCols= new ArrayList<Integer>();
for(int c = 0; c < grid[0].length; c++){
if(!this.isColumnFull(c)){
validCols.add(c);
}
}
return validCols;
}
}