-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCheckWin.java
More file actions
74 lines (72 loc) · 2.29 KB
/
Copy pathCheckWin.java
File metadata and controls
74 lines (72 loc) · 2.29 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
public class CheckWin {
public static void main(String[] args) {
char[][] board = {{'R', 'R', 'R', 'R'}, {'Y', 'R', 'R', 'Y'}, {'R', 'Y', 'Y', 'R'}, {'Y', 'R', 'R', 'Y'}};
System.out.println(game_over(board, 0, 2));
}
public static char game_over(char[][] tboard, int x, int y) {
int countConsecutive = 0;
char color = ' ';
int i, a;
for (i = x - 3; i <= x + 3; i++) { // Horizontal
if (i >= 0 && i < tboard.length && countConsecutive < 4)
if (tboard[i][y] == color) countConsecutive++;
else if (tboard[i][y] == 'R' || tboard[i][y] == 'Y') {
color = tboard[i][y];
countConsecutive = 1;
}
else color = ' ';
else if (countConsecutive == 4)
return color;
if (countConsecutive == 4)
return color;
countConsecutive = 0;
color = ' ';
}
for (a = y - 3; a <= y + 3; a++) { // Vertical
if (a >= 0 && a < tboard.length && countConsecutive < 4)
if (tboard[x][a] == color) countConsecutive++;
else if (tboard[x][a] == 'R' || tboard[x][a] == 'Y') {
color = tboard[x][a];
countConsecutive = 1;
}
else color = ' ';
else if (countConsecutive == 4)
return color; if (countConsecutive == 4) return color;
countConsecutive = 0;
color = ' ';
}
for (i = x - 3, a = y - 3; i <= x + 3; i++, a++) { // diagonal 1 topleft - bottomright
if (a >= 0 && a < tboard.length && i >= 0 && i < tboard.length && countConsecutive < 4) {
if (tboard[i][a] == color)
countConsecutive++;
else if (tboard[i][a] == 'R' || tboard[i][a] == 'Y') {
color = tboard[i][a];
countConsecutive = 1;
}
else color = ' ';
}
else if (countConsecutive == 4)
return color;
if (countConsecutive == 4)
return color;
countConsecutive = 0;
color = ' ';
}
for (i = x - 3, a = y + 3; i <= x + 3; i++, a--) { // diagonal 1 topright - bottomleft
if (a >= 0 && a < tboard.length && i >= 0 && i < tboard.length && countConsecutive < 4)
if (tboard[i][a] == color) countConsecutive++; else if (tboard[i][a] == 'R' || tboard[i][a] == 'Y') {
color = tboard[i][a];
countConsecutive = 1;
}
else color = ' ';
else if (countConsecutive == 4)
return color;
if (countConsecutive == 4)
return color;
for (i = 0; i < tboard.length; i++)
if (tboard[i][0] == ' ')
return 'f';
}
return 't';
}
}