-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleBoard.java
More file actions
84 lines (73 loc) · 2.58 KB
/
Copy pathSimpleBoard.java
File metadata and controls
84 lines (73 loc) · 2.58 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
import java.util.List;
/**
* SimpleBoard is a concrete class inheriting application state and validation logic.
*/
public class SimpleBoard extends Board {
private WinCheckStrategy winCheckStrategy;
/**
* Constructor to create SimpleBoard object
*
* @param row is the total number of rows in board
* @param column is the total number of columns in board
* @param players is the total number of players in the game
* @param matchLength is the matching length for win condition
*/
public SimpleBoard(int row, int column, List<Player> players, int matchLength) {
super(row, column, players, matchLength);
this.winCheckStrategy = new HorizontalWinCheck();
WinCheckStrategy verticalWinCheck = new VerticalWinCheck();
WinCheckStrategy forwardDiagonalCheck = new ForwardDiagonalCheck();
WinCheckStrategy backwordDiagonalCheck = new BackwordDiagonalCheck();
winCheckStrategy.setWinCheckStrategy(verticalWinCheck);
verticalWinCheck.setWinCheckStrategy(forwardDiagonalCheck);
forwardDiagonalCheck.setWinCheckStrategy(backwordDiagonalCheck);
}
/**
* init method is to initialize board with default Token.
*
* @param defaultToken is the default token to initialize board
*/
@Override
void init(Token defaultToken) {
for (int i = 0; i < this.rows; i++) {
for (int j = 0; j < this.columns; j++) {
this.board[i][j] = defaultToken;
}
}
}
/**
* getState method return the state of Game i.e. INIT, IN_PROGRESS, WON, LOST, DRAW.
* @return Game state
*/
@Override
State getState() {
if (this.tokenFillCount >= rows * columns) {
this.currentState = State.DRAW;
return State.DRAW;
}
if (this.winCheckStrategy.check(this)) {
this.currentState = State.WON;
return State.WON;
}
return State.IN_PROGRESS;
}
/**
* setToken method updates model for player input
*
* @param column is the column input by player
* @return true if update is successfull else false.
*/
@Override
boolean setToken(int column) {
int row = this.getColumnEmptyCell(column);
if (row == -1) {
return false;
} else {
this.board[row][column] = this.getCurrentPlayer().getToken();
this.lastFilledRow = row;
this.lastFilledColumn = column;
this.tokenFillCount++;
return true;
}
}
}