-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHorizontalWinCheck.java
More file actions
59 lines (50 loc) · 2.09 KB
/
Copy pathHorizontalWinCheck.java
File metadata and controls
59 lines (50 loc) · 2.09 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
/**
* HorizontalWinCheck checks user input for win condition across horizontal direction.
*/
public class HorizontalWinCheck extends WinCheckStrategy {
/**
* check method is to check for win condition across horizontal direction.
* 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.
int[] rightNeighbour = board.getRightNeighbour(row, column);
while (rightNeighbour != null && currentMatchCount <= matchLength) {
int rightRow = rightNeighbour[0];
int rightColumn = rightNeighbour[1];
char rightToken = board.getBoard()[rightRow][rightColumn].getToken();
if (playerToken == rightToken) {
currentMatchCount++;
rightNeighbour = board.getRightNeighbour(rightRow, rightColumn);
continue;
} else
break;
}
// Counts matching tokens to the left.
int[] leftNeighbour = board.getLeftNeighbour(row, column);
while (leftNeighbour != null && currentMatchCount <= matchLength) {
int leftRow = leftNeighbour[0];
int leftColumn = leftNeighbour[1];
char leftToken = board.getBoard()[leftRow][leftColumn].getToken();
if (playerToken == leftToken) {
currentMatchCount++;
leftNeighbour = board.getLeftNeighbour(leftRow, leftColumn);
continue;
} else
break;
}
if (currentMatchCount >= matchLength)
return true;
return this.winCheckStrategy.check(board);
}
}