-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiagonalMatch.java
84 lines (70 loc) · 2.26 KB
/
DiagonalMatch.java
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.ArrayList;
import java.util.List;
public class DiagonalMatch implements Matchable {
// CONSTRUCTOR
public DiagonalMatch() {}
// INTERFACE IMPLEMENTATION
public List<Tile> match(List<List<Tile>> gameBoard) {
int rows = gameBoard.size();
int columns = gameBoard.get(0).size();
List<Tile> matched = new ArrayList<>();
// Check matches along diagonals (top-left to bottom-right)
for (int i = 0; i < rows + columns - 1; i++) {
// new matched list for each diagonal
matched.clear();
int startRow = Math.max(0, i - columns + 1);
int startCol = Math.min(columns - 1, i);
for (int row = startRow, col = startCol; row < rows && col >= 0; row++, col--) {
Tile currentTile = gameBoard.get(row).get(col);
String currentDisplay = currentTile.getDisplay();
if (currentDisplay != null) {
if (matched.isEmpty() || currentDisplay.equals(matched.get(0).getDisplay())) {
// is the first non-empty Tile found OR matches tiles
matched.add(currentTile);
} else {
// reset matched
matched.clear();
matched.add(currentTile);
}
} else {
// display null? reset matched
matched.clear();
}
if (matched.size() >= 4) {
// found a match of 4 or more tiles
return matched;
}
}
}
// Check matches along diagonals (top-right to bottom-left)
for (int i = 0; i < rows + columns - 1; i++) {
// new matched list for each diagonal
matched.clear();
int startRow = Math.max(0, i - columns + 1);
int startCol = Math.min(columns - 1, i);
for (int row = startRow, col = columns - 1 - startCol; row < rows && col < columns; row++, col++) {
Tile currentTile = gameBoard.get(row).get(col);
String currentDisplay = currentTile.getDisplay();
if (currentDisplay != null) {
if (matched.isEmpty() || currentDisplay.equals(matched.get(0).getDisplay())) {
// is the first non-empty Tile found OR matches tiles
matched.add(currentTile);
} else {
// reset matched
matched.clear();
matched.add(currentTile);
}
} else {
// display null? reset matched
matched.clear();
}
if (matched.size() >= 4) {
// found a match of 4 or more tiles
return matched;
}
}
}
// no matches at all
return null;
}
}