-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
239 lines (215 loc) · 6.2 KB
/
Board.java
File metadata and controls
239 lines (215 loc) · 6.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
public class Board
{
private final int BLANK = 0;
private final int X = 1;
private final int O = 2;
private int[][] _board;
private int[] _moveLocations;
private int N;
// True if X's turn, False if O's turn
private boolean _xTurn;
/**
* Generic Constructor, initializes a board with n by n blank spaces and sets the turn to X.
* @param n The dimension of the board will be n by n.
*/
public Board(int n)
{
N = n;
_board = new int[N][N];
_moveLocations = new int[N];
_xTurn = true;
}
/**
* Constructor for testing ony, board is passed in.
* @param board The array of integers representing the board.
*/
public Board(int[][] board)
{
N = board.length;
_board = board;
_moveLocations = new int[N];
}
/**
* Constructor for copying, board, moveLocations, and current turn are passed in.
* @param board The array of integers representing the board.
*/
public Board(int[][] board, int[] moveLocations, boolean turn)
{
N = board.length;
_board = board;
_moveLocations = moveLocations;
_xTurn = turn;
}
/**
* Creates a deep copy of this board instance.
* @return a new board that exactly copies the current one.
*/
public Board copy()
{
int[][] board = new int[N][N];
int[] moves = new int[N];
for (int x = 0; x < N; x++)
{
moves[x] = _moveLocations[x];
for (int y = 0; y < _moveLocations[x]; y++)
{
board[x][y] = _board[x][y];
}
}
return new Board(board, moves, _xTurn);
}
/**
* Make a move at specified location.
*
* Whether the turn is currently on X or O will be handled, simply provide the location for the move.
* @param move Integer value corresponding to the column that you will move into.
*/
public void makeMove(int move)
{
if (_xTurn && _moveLocations[move] < N)
{
_board[move][_moveLocations[move]] = X;
_xTurn = !_xTurn;
_moveLocations[move]++;
}
else if (!_xTurn && _moveLocations[move] < N)
{
_board[move][_moveLocations[move]] = O;
_xTurn = !_xTurn;
_moveLocations[move]++;
}
}
/**
* Return an integer value representing undecided, X wins, O wins, or catsgame
* @return 0 = undecided, 1 = X wins 2 = O wins, 3 = Catsgame
*/
public int getWinner()
{
if (nInALineWith4Possible(4 , X) > 0)
{
return X;
}
if (nInALineWith4Possible(4, O) > 0)
{
return O;
}
int blanks = 0;
for (int[] column : _board)
{
for (int space : column)
{
if (space == BLANK)
{
blanks++;
}
}
}
if (blanks == 0)
{
return 3;
}
else
{
return 0;
}
}
/**
* Check if a move is valid.
* @param move the integer value for the column you are playing in.
* @return true if and only if there is room left in that column for playing.
*/
public boolean isValidMove(int move)
{
return _moveLocations[move] < N;
}
/**
* Will return the number of times that n things appear in a line of 4 possible
* @param n Things in a line of 4, should be a value 2 -> 4 inclusive
* @param value 1 for Xs, 2 for 0s
* @return an integer representing the number of times that n things in a line of 4 possible occurred.
*/
public int nInALineWith4Possible(int n, int value)
{
int occurances = 0;
for (int i = 0; i < N; i++)
{
// horizontal
occurances += checkInARow(i,0,0,1,value,n);
// vertical
occurances += checkInARow(0,i,1,0,value,n);
// diagonal: up to right along left
occurances += checkInARow(0,i,1,1,value,n);
// diagonal: up to left along right
occurances += checkInARow(N-1,i,-1,1,value,n);
if (i != 0)
{
// diagonal: up to right along bottom
occurances += checkInARow(i,0,1,1,value,n);
// diagonal: up to left along bottom
occurances += checkInARow(i,0,-1,1,value,n);
}
}
return occurances;
}
private int checkInARow( int x, int y, int deltaX, int deltaY, int value, int n)
{
int occurances = 0;
int[] slide = {0,0,0,0};
while (x >= 0 && x < N && y >= 0 && y < N)
{
slide( slide, _board[x][y] );
occurances += checkSlide(n , value, slide);
x += deltaX;
y += deltaY;
}
return occurances;
}
/**
* Iterate the slider
* @param slide pointer to the slider.
* @param value the new value entering the slider.
*/
private void slide(int[] slide, int value)
{
for (int i = 0; i < slide.length - 1; i++)
{
slide[i] = slide[i + 1];
}
slide[slide.length - 1] = value;
}
/**
* Check a single instance of a slider for n or more occurrences of value
* @param n the quota of instances you are trying to hit
* @param value the value (X or O) that you are checking for
* @param slide an array of four consecutive values on the board
* @return 1 if quota is met, 0 if not met or an occurrence of the opposite of value appears.
*/
private int checkSlide(int n, int value, int[] slide)
{
int numValues = 0;
for (int i = 0; i < slide.length; i++)
{
if (slide[i] == value)
{
numValues++;
}
if (slide[i] == opposite(value))
{
return 0;
}
}
if (numValues >= n)
{
return 1;
}
return 0;
}
private int opposite(int value)
{
if (value == X)
{
return O;
}
return X;
}
}