-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOXOController.java
More file actions
363 lines (335 loc) · 12.5 KB
/
Copy pathOXOController.java
File metadata and controls
363 lines (335 loc) · 12.5 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
import OXOExceptions.*;
import java.lang.reflect.Array;
class OXOController
{
OXOModel gameModel;
public int curPlayerNumber = 0;
private int gameCounter = 0;
public int rowNumber;
public int colNumber;
private int colCounter;
private int rowCounter;
private final char[] NumberValueLowerLetters = {'a','b','c','d','e','f','g','h','i'};
private final char[] NumberValueUpperLetters = {'A','B','C','D','E','F','G','H','I'};
public OXOController(OXOModel model)
{
gameModel = model;
whoseTurnIsItNext();
}
public void handleIncomingCommand(String command) throws OXOMoveException
{
lengthNotRight(command);
setInputFromPlayer(command);
characterNotRight(command);
cellDoesNotExist();
cellIsAlreadyTaken();
gameModel.setCellOwner(gameModel.getCurrentRow(), gameModel.getCurrentCol(), gameModel.getCurrentPlayer());
gameCounter++;
checkForWinner();
isItADraw();
whoseTurnIsItNext();
}
public void setInputFromPlayer(String command)
{
//Sets the input from user into an array, and takes array[0] as Row, and array[1] as Column
char[] inputArray = new char[command.length()];
for (int i = 0; i < command.length(); i++) {
inputArray[i] = command.charAt(i);
}
rowNumber = command.charAt(0);
colNumber = command.charAt(1);
int arraySize = NumberValueLowerLetters.length;
char charRow = Array.getChar(inputArray, 0);
char charCol = Array.getChar(inputArray, 1);
//Changes Row and Column to an int with the same indexing as an array to make it easier to work
// with (eg, Row A => 0, Col 1 => 0.)
gameModel.setCurrentCol((Character.getNumericValue(charCol) - 1));
for (int i = 0; i < arraySize; i++){
if((charRow==NumberValueLowerLetters[i]) || (charRow==NumberValueUpperLetters[i])){
gameModel.setCurrentRow(i);
}
}
}
public void whoseTurnIsItNext(){
OXOPlayer curOXOPlayer = gameModel.getPlayerByNumber(curPlayerNumber);
gameModel.setCurrentPlayer(curOXOPlayer);
// Sets Players back to 0 once it's reached the end.
if ((gameModel.getNumberOfPlayers() -1 )== curPlayerNumber){
curPlayerNumber = 0;
}
else {
curPlayerNumber++;
}
}
public boolean checkForWinner(){
if ((WinByRow()) || (WinByCol()) || (diagSetValueStr()) || diagSetValueRev()){
gameModel.setWinner(gameModel.getCurrentPlayer());
}
return false;
}
public boolean WinByCol(){
rowCounter = 0;
int nextRow = rowCounter + 1;
int winCheck = 1;
while (winCheck < gameModel.getWinThreshold()) {
if (nextRow == gameModel.getNumberOfRows()) {
return false;
}
// Prevents a false win => eg a line of empty columns
if ((!RowSet(rowCounter, gameModel.getCurrentCol())) || (!RowSet(nextRow, gameModel.getCurrentCol()))) {
rowCounter++;
nextRow++;
winCheck = 1;
}
else {
if ((gameModel.getCellOwner(rowCounter, gameModel.getCurrentCol())) ==
(gameModel.getCellOwner(nextRow, gameModel.getCurrentCol()))) {
winCheck++;
}
else{
winCheck = 1;
}
rowCounter++;
nextRow++;
}
}
if (winCheck == gameModel.getWinThreshold()){
return true;
}
return false;
}
public boolean WinByRow(){
colCounter = 0;
int nextCol = colCounter + 1;
int winCheck = 1;
while (winCheck < gameModel.getWinThreshold()) {
if (nextCol == gameModel.getNumberOfColumns()) {
return false;
}
// Prevents a false win => eg a line of empty rows
if ((!RowSet(gameModel.getCurrentRow(), colCounter)) || (!RowSet(gameModel.getCurrentRow(), nextCol))) {
colCounter++;
nextCol++;
winCheck = 1;
}
else {
// If the cells have the same cell owner, continues checking
if ((gameModel.getCellOwner(gameModel.getCurrentRow(), colCounter)) ==
(gameModel.getCellOwner(gameModel.getCurrentRow(), nextCol))) {
winCheck++;
}
else {
winCheck = 1;
}
colCounter++;
nextCol++;
}
}
if (winCheck == gameModel.getWinThreshold()){
return true;
}
return false;
}
public boolean RowSet(int rowNumber, int colNumber){
int numberOfPlayers = gameModel.getNumberOfPlayers();
int initPlayer = 0;
//Checks the row is owned by any of the players.
while (initPlayer < numberOfPlayers){
if ((gameModel.getCellOwner(rowNumber, colNumber)) == (gameModel.getPlayerByNumber(initPlayer))){
return true;
}
initPlayer++;
}
return false;
}
public boolean diagSetValueStr(){
int initCol = gameModel.getNumberOfColumns() - gameModel.getWinThreshold();
int initRow = 0;
int nextRow = initRow + 1;
while (initCol != 0){
int nextCol = initCol + 1;
if (WinByStrDig(initCol, initRow, nextRow, nextCol)){
return true;
}
initCol = initCol - 1;
initRow = 0;
}
initCol = 0;
initRow = gameModel.getNumberOfRows() - gameModel.getWinThreshold();
int nextCol = initCol + 1;
while (initRow != 0){
nextRow = initRow + 1;
if (WinByStrDig(initCol, initRow, nextRow, nextCol)){
return true;
}
initRow = initRow - 1;
initCol = 0;
}
initCol = 0;
initRow = 0;
nextRow = initRow + 1;
nextCol = initCol + 1;
if (WinByStrDig(initCol, initRow, nextRow, nextCol)){
return true;
}
return false;
}
public boolean WinByStrDig(int initCol, int initRow, int nextRow, int nextCol){
int winCheck = 1;
while (winCheck < gameModel.getWinThreshold()) {
if ((nextCol == gameModel.getNumberOfColumns()) || (nextRow == gameModel.getNumberOfRows())){
return false;
}
// Prevents a false win => eg a line of empty cells
if (!RowSet(initRow, initCol)){
initRow++;
initCol++;
nextRow++;
nextCol++;
winCheck = 1;
}
else {
// Compares the initial cells + the ones next to see if they are the same
if ((gameModel.getCellOwner(initRow, initCol)) == (gameModel.getCellOwner(nextRow, nextCol))) {
winCheck++;
}
initRow++;
initCol++;
nextRow++;
nextCol++;
}
}
//It's a win if by the end the column & row count is the same as Win threshold
if (winCheck == gameModel.getWinThreshold()){
return true;
}
return false;
}
public boolean diagSetValueRev(){
int initRow = gameModel.getWinThreshold() - 1;
int initCol = 0;
while (initRow != gameModel.getNumberOfRows()){
int nextRow = initRow - 1;
int nextCol = initCol + 1;
if (WinByRevDig(initCol, initRow, nextRow, nextCol)){
return true;
}
initRow = initRow + 1;
initCol = 0;
}
initCol = 1;
initRow = gameModel.getNumberOfRows() - 1;
int nextCol = initCol + 1;
while (nextCol != gameModel.getNumberOfColumns()){
int nextRow = initRow - 1;
if (WinByRevDig(initCol, initRow, nextRow, nextCol)){
return true;
}
initRow = gameModel.getNumberOfRows() - 1;
initCol = initCol + 1;
nextCol = initCol + 1;
}
return false;
}
public boolean WinByRevDig(int initCol, int initRow, int nextRow, int nextCol){
int winCheck = 1;
while (winCheck < gameModel.getWinThreshold()) {
if ((nextRow < 0) || (nextCol == gameModel.getNumberOfColumns())){
return false;
}
// Prevents a false win => eg a line of empty cells
if (!RowSet(initRow, initCol)){
initRow--;
nextRow--;
initCol++;
nextCol++;
}
else {
// Compares the initial cells + the ones next to see if they are the same
if ((gameModel.getCellOwner(initRow, initCol)) == (gameModel.getCellOwner(nextRow, nextCol))) {
winCheck++;
}
initRow--;
nextRow--;
initCol++;
nextCol++;
}
}
//It's a win if by the end the column count is the same as Win threshold (since reverse diag)
if (winCheck == gameModel.getWinThreshold()){
return true;
}
return false;
}
public void isItADraw(){
int boardSize = (gameModel.getNumberOfRows()) * (gameModel.getNumberOfColumns());
if (gameCounter == boardSize){
gameModel.setGameDrawn();
}
}
public void cellIsAlreadyTaken() throws CellAlreadyTakenException {
if (RowSet(gameModel.getCurrentRow(), gameModel.getCurrentCol())){
throw new CellAlreadyTakenException(rowNumber, colNumber);
}
}
public boolean cellDoesNotExist() throws CellDoesNotExistException{
int totalRows = gameModel.getNumberOfRows();
int totalCols = gameModel.getNumberOfColumns();
if ((gameModel.getCurrentRow() >= totalRows) || (gameModel.getCurrentRow() < 0) ||
(gameModel.getCurrentCol() >= totalCols) || (gameModel.getCurrentCol() < 0)){
if (!outsideCellRange()){
throw new CellDoesNotExistException(rowNumber, colNumber);
}
}
return true;
}
public boolean outsideCellRange() throws OutsideCellRangeException {
if ((gameModel.getCurrentRow() >= gameModel.getNumberOfRows()) || (gameModel.getCurrentCol() >= gameModel.getNumberOfColumns())){
throw new OutsideCellRangeException(rowNumber, colNumber);
}
return false;
}
//INVALID IDENTIFIER - Either the length is too long or short
public boolean lengthNotRight(String command) throws InvalidIdentifierLengthException{
if (command.length() != 2){
int length = command.length();
int invalid = 0;
throw new InvalidIdentifierLengthException(invalid, length);
}
return true;
}
//INVALID CHARACTER - the character is not recognised
public boolean characterNotRight(String command) throws InvalidIdentifierCharacterException {
if ((!rowNotRight(command)) || (!colNotRight(command))){
throw new InvalidIdentifierCharacterException(rowNumber, colNumber);
}
return true;
}
public boolean colNotRight(String command){
char[] inputArray = new char[command.length()];
for (int i = 0; i < command.length(); i++) {
inputArray[i] = command.charAt(i);
}
int col = (Character.getNumericValue(inputArray[1]));
int j = 0;
while(j < 10){
if (j == col){
return true;
}
j++;
}
return false;
}
public boolean rowNotRight(String command){
char[] inputArray = new char[command.length()];
for (int i = 0; i < command.length(); i++) {
inputArray[i] = command.charAt(i);
}
for (int i = 0; i < NumberValueLowerLetters.length; i++){
if ((NumberValueLowerLetters[i] == inputArray[0]) || (NumberValueUpperLetters[i] == inputArray[0])){
return true;
}
}
return false;
}
}