-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackwordDiagonalCheck.java
More file actions
56 lines (48 loc) · 2.1 KB
/
Copy pathBackwordDiagonalCheck.java
File metadata and controls
56 lines (48 loc) · 2.1 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
/**
* BackwordDiagonalCheck checks user input for win condition across backword diagonal.
*/
public class BackwordDiagonalCheck extends WinCheckStrategy {
/**
* check method is to check for win condition across backword 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;
int[] topLeftNeighbour = board.getTopLeftNeighbour(row, column);
while (topLeftNeighbour != null && currentMatchCount <= matchLength) {
int topLeftRow = topLeftNeighbour[0];
int topLeftColumn = topLeftNeighbour[1];
char topLeftToken = board.getBoard()[topLeftRow][topLeftColumn].getToken();
if (playerToken == topLeftToken) {
currentMatchCount++;
topLeftNeighbour = board.getTopLeftNeighbour(topLeftRow, topLeftColumn);
continue;
} else
break;
}
int[] bottomRightNeighbour = board.getBottomRightNeighbour(row, column);
while (bottomRightNeighbour != null && currentMatchCount <= matchLength) {
int bottomRightRow = bottomRightNeighbour[0];
int bottomRightColumn = bottomRightNeighbour[1];
char bottomRightToken = board.getBoard()[bottomRightRow][bottomRightColumn].getToken();
if (playerToken == bottomRightToken) {
currentMatchCount++;
bottomRightNeighbour = board.getBottomRightNeighbour(bottomRightRow, bottomRightColumn);
continue;
} else
break;
}
if (currentMatchCount >= matchLength)
return true;
return false;
}
}