-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBoard.java
More file actions
211 lines (183 loc) · 7.34 KB
/
Copy pathBoard.java
File metadata and controls
211 lines (183 loc) · 7.34 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
/**
* The playing board for Connect Four
*
* @author Sean DeZurik
*/
public class Board {
/** 2-D array of pieces to go into the board */
private Piece[][] gamePieces;
/** How many rows on the board */
private int numRows;
/** How many columns on the board */
private int numColumns;
/** Array tracking for each column which row will get the next piece */
private int[] lowestAvailableRow;
/**
* Constructor for Board
*
* @param numRows rows on the board
* @param numColumns columns on the board
*/
public Board(int numRows, int numColumns) {
gamePieces = new Piece[numRows][numColumns];
for (int i = 0; i < gamePieces.length; i++) {
for (int j = 0; j < gamePieces[i].length; j++) {
gamePieces[i][j] = new Piece();
}
}
this.numRows = numRows;
this.numColumns = numColumns;
lowestAvailableRow = new int[numColumns];
for (int i = 0; i < numColumns; i++) {
lowestAvailableRow[i] = numRows - 1;
}
}
/**
* Piece being dropped into the board. Set the characteristics and
* location for the piece.
*
* @param column which column the piece is dropped into
* @param color the color of the playing piece
* @return Piece the Piece object just dropped in
*/
public Piece dropPiece(int column, String color) {
int row = getRow(column);
if (row >= 0) {
gamePieces[row][column].setColor(color);
gamePieces[row][column].setFilled(true);
gamePieces[row][column].setRow(row);
gamePieces[row][column].setColumn(column);
pieceAdded(column);
return gamePieces[row][column];
} else {
return null;
}
}
/**
* Get row piece will occupy in a column
*
* @param column the column where the piece is dropped
* @return int the number of the row where the piece will occupy
*/
private int getRow(int column) {
return lowestAvailableRow[column];
}
/**
* Modify the next available row because a piece just occupied the
* previously available row
*
* @param column the column where the last piece was dropped into
*/
private void pieceAdded(int column) {
lowestAvailableRow[column]--;
}
/**
* Determine the maximum number of consecutive pieces of a given
* color. It only needs to check the vicinity of the piece just
* dropped into the game to see if it exceeds the previous max value.
*
* @param justDropped the Piece just dropped into the game
* @return an int with the max number of consecutive pieces
*/
public int determineMaxConsecutive(Piece justDropped) {
int maxHorizontal = getMaxHorizontal(justDropped);
int maxVertical = getMaxVertical(justDropped);
int maxDiagonal = getMaxDiagonal(justDropped);
int max = maxHorizontal;
if (max < maxVertical) {
max = maxVertical;
}
if (max < maxDiagonal) {
max = maxDiagonal;
}
return max;
}
/**
* Find max consecutive pieces in left-right direction
*
* @param justDropped the Piece just dropped in
* @return int max number of consecutive pieces left to right
*/
private int getMaxHorizontal(Piece justDropped) {
String color = justDropped.getColor();
int row = justDropped.getRow();
int column = justDropped.getColumn();
int count = 1; // Count the piece just dropped
int reverseCount = -1;
// Look to the right
while (column + count < numColumns && gamePieces[row][column + count].getFilled() && gamePieces[row][column + count].getColor() == justDropped.getColor()) {
count++;
}
// Look to the left
while (column + reverseCount >= 0 && gamePieces[row][column + reverseCount].getFilled() && gamePieces[row][column + reverseCount].getColor() == justDropped.getColor()) {
reverseCount--;
}
// Add count to right (includes just played piece) to the absolute value of the count to the left.
// But, we must subtract one because we end up counting the just played piece twice
return count + Math.abs(reverseCount) - 1;
}
/**
* Find max number of consecutive pieces up and down
*
* @param justDropped the Piece just dropped into the game
* @return an int with the max number of pieces up and down
*/
private int getMaxVertical(Piece justDropped) {
String color = justDropped.getColor();
int row = justDropped.getRow();
int column = justDropped.getColumn();
int count = 1; // Count the piece just dropped
int reverseCount = -1;
// Look down
while (row + count < numRows && gamePieces[row + count][column].getFilled() && gamePieces[row + count][column].getColor() == justDropped.getColor()) {
count++;
}
// Look up
while (row + reverseCount >= 0 && gamePieces[row + reverseCount][column].getFilled() && gamePieces[row + reverseCount][column].getColor() == justDropped.getColor()) {
reverseCount--;
}
return count + Math.abs(reverseCount) - 1;
}
/**
* Find max number of consecutive pieces in diagonal directions
*
* @param justDropped the Piece just dropped into the game
* @return an int with the max number of consecutive diagonal pieces
*/
private int getMaxDiagonal(Piece justDropped) {
String color = justDropped.getColor();
int row = justDropped.getRow();
int column = justDropped.getColumn();
int count = 1; // Count the piece just dropped
int reverseCount = -1;
int down = 1;
int left = -1;
int up = -1;
int right = 1;
// Look down and right
while (column + count < numColumns && row + count < numRows && gamePieces[row + count][column + count].getFilled() && gamePieces[row + count][column + count].getColor() == justDropped.getColor()) {
count++;
}
// Look up and left
while (column + reverseCount >= 0 && row + reverseCount >= 0 && gamePieces[row + reverseCount][column + reverseCount].getFilled() && gamePieces[row + reverseCount][column + reverseCount].getColor() == justDropped.getColor()) {
reverseCount--;
}
// Look down and left
while (row + down < numRows && column + left >= 0 && gamePieces[row + down][column + left].getFilled() && gamePieces[row + down][column + left].getColor() == justDropped.getColor()) {
down++;
left--;
}
// Look up and right
while (row + up >= 0 && column + right < numColumns && gamePieces[row + up][column + right].getFilled() && gamePieces[row + up][column + right].getColor() == justDropped.getColor()) {
right++;
up--;
}
int backSlash = count + Math.abs(reverseCount) - 1;
int forwardSlash = down + right - 1;
if (backSlash > forwardSlash) {
return backSlash;
} else {
return forwardSlash;
}
}
}