-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForwardDiagonalCheck.java
More file actions
58 lines (50 loc) · 2.25 KB
/
Copy pathForwardDiagonalCheck.java
File metadata and controls
58 lines (50 loc) · 2.25 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
/**
* ForwardDiagonalCheck checks user input for win condition across forward diagonal.
*/
public class ForwardDiagonalCheck extends WinCheckStrategy {
/**
* check method is to check for win condition across forward diagonal.
* Logic is to check first in right direction and count the match and then
* move to left direction and increment count for the matching token.
*
* @param board the model to be validated for win condition.
*/
@Override
boolean check(Board board) {
int row = board.lastFilledRow;
int column = board.lastFilledColumn;
Player player = board.getCurrentPlayer();
char playerToken = player.getToken().getToken();
int matchLength = board.matchLength;
int currentMatchCount = 1;
// Counts matching tokens to the right of the diagonal.
int[] topRightNeighbour = board.getTopRightNeighbour(row, column);
while (topRightNeighbour != null && currentMatchCount <= matchLength) {
int topRightRow = topRightNeighbour[0];
int topRightColumn = topRightNeighbour[1];
char topRightToken = board.getBoard()[topRightRow][topRightColumn].getToken();
if (playerToken == topRightToken) {
currentMatchCount++;
topRightNeighbour = board.getTopRightNeighbour(topRightRow, topRightColumn);
continue;
} else
break;
}
// Counts matching tokens to the left of the diagonal.
int[] bottomLeftNeighbour = board.getBottomLeftNeighbour(row, column);
while (bottomLeftNeighbour != null && currentMatchCount <= matchLength) {
int bottomLeftRow = bottomLeftNeighbour[0];
int bottomLeftColumn = bottomLeftNeighbour[1];
char bottomLeftToken = board.getBoard()[bottomLeftRow][bottomLeftColumn].getToken();
if (playerToken == bottomLeftToken) {
currentMatchCount++;
bottomLeftNeighbour = board.getBottomLeftNeighbour(bottomLeftRow, bottomLeftColumn);
continue;
} else
break;
}
if (currentMatchCount >= matchLength)
return true;
return this.winCheckStrategy.check(board);
}
}