Repository navigation
Expand file tree
/
Copy pathJudge.java
More file actions
178 lines (161 loc) · 7.89 KB
/
Copy pathJudge.java
File metadata and controls
178 lines (161 loc) · 7.89 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package logic;
/**
* Created by cjy970910 on 2016/11/28.
*/
public class Judge {
public static boolean ifOutOfBoard(Animal animal, int[] direction) {
return animal.getX() + direction[0] < 0 || animal.getX() + direction[0] > 6 || animal.getY() + direction[1] < 0 || animal.getY() + direction[1] > 8;
}
public static boolean ifRiver(Animal animal, int[] direction) {
return Board.landForm[animal.getX() + direction[0]][animal.getY() + direction[1]] == 1;
}
public static boolean ifHome(Animal animal, int[] direction) {
return Board.landForm[animal.getX() + direction[0]][animal.getY() + direction[1]] == (animal.getCamp() == -1 ? 3 : 5);
}
public static boolean ifVacant(Animal animal, int[] direction) {
return Board.animalBoard[animal.getX() + direction[0]][animal.getY() + direction[1]].getValue() == 0;
}
/**
* judge whether the aniaml on next location is enemy.
* it is used only when the judgeIfVacant is false.
*
* @return true means yes and false means no.
*/
public static boolean ifAnimalEn(Animal animal, int[] direction) {
return Board.animalBoard[animal.getX() + direction[0]][animal.getY() + direction[1]].getCamp() != animal.getCamp();
}
/**
* judge whether the aniaml on next location can be eaten.
* it is used only when the judgeIfAnimalEn is true.
*
* @return true means yes and false means no.
*/
public static boolean ifAnimalCanEat(Animal animal, int[] direction) {
if (Board.landForm[animal.getX() + direction[0]][animal.getY() + direction[1]] == (animal.getCamp() == -1 ? 2 : 4)) {
return true;
}
int animalValue = animal.getValue();
int targetValue = Board.animalBoard[animal.getX() + direction[0]][animal.getY() + direction[1]].getValue();
if (animalValue == 1) {
return targetValue == 1 | (Board.landForm[animal.getX()][animal.getY()] != 1 & targetValue == 8);
} else if (animalValue == 8) {
return targetValue != 1;
} else {
return animalValue >= targetValue;
}
}
/**
* judge whether the tiger or lion can jump over the river.
* this method only judge if there is a mouse in the route,not considering the condition on the opposite bank.
*
* @param direction the location the animal will arrive.
* @return true means yes and false means no.
*/
public static boolean ifCanJump(Animal animal, int[] direction) {
if (direction[0] == 0 && direction[1] == 1) {
return Board.animalBoard[animal.getX()][animal.getY() + 1].getCamp() != (animal.getCamp() == -1 ? 1 : -1) &&
Board.animalBoard[animal.getX()][animal.getY() + 2].getCamp() != (animal.getCamp() == -1 ? 1 : -1) &&
Board.animalBoard[animal.getX()][animal.getY() + 3].getCamp() != (animal.getCamp() == -1 ? 1 : -1);
} else if (direction[0] == 0 && direction[1] == -1) {
return Board.animalBoard[animal.getX()][animal.getY() - 1].getCamp() != (animal.getCamp() == -1 ? 1 : -1) &&
Board.animalBoard[animal.getX()][animal.getY() - 2].getCamp() != (animal.getCamp() == -1 ? 1 : -1) &&
Board.animalBoard[animal.getX()][animal.getY() - 3].getCamp() != (animal.getCamp() == -1 ? 1 : -1);
} else if (direction[0] == 1 && direction[1] == 0) {
return Board.animalBoard[animal.getX() + 1][animal.getY()].getCamp() != (animal.getCamp() == -1 ? 1 : -1) &&
Board.animalBoard[animal.getX() + 2][animal.getY()].getCamp() != (animal.getCamp() == -1 ? 1 : -1);
} else if (direction[0] == -1 && direction[1] == 0) {
return Board.animalBoard[animal.getX() - 1][animal.getY()].getCamp() != (animal.getCamp() == -1 ? 1 : -1) &&
Board.animalBoard[animal.getX() - 2][animal.getY()].getCamp() != (animal.getCamp() == -1 ? 1 : -1);
} else {
return false;
}
}
public static boolean ifHomeOccupied() {
return Board.animalBoard[3][0].getValue() != 0 || Board.animalBoard[3][8].getValue() != 0;
}
/**
* end judge
* judge if there is animal alive.
* it scans the map from (0,0) to (6,8),when it found an animal of the enemy,it break.
* when it go to (6,8) and still no animal has been found,it means the animal of the enemy has all died.
*
* @return true means yes and false means no.
*/
public static boolean ifDead() {
int leftAlive = 0;
int rightAlive = 0;
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 9; j++) {
if (Board.animalBoard[i][j].getCamp() == -1) {
leftAlive++;
} else if (Board.animalBoard[i][j].getCamp() == 1) {
rightAlive++;
}
}
}
return leftAlive == 0 || rightAlive == 0;
}
public static boolean ifTrapped() {
int leftTrapped = 0;
int rightTrapped = 0;
int leftAlive = 0;
int rightAlive = 0;
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 9; j++) {
int[] directionUp = {-1, 0};
int[] directionDown = {1, 0};
int[] directionLeft = {0, -1};
int[] directionRight = {0, 1};
if (Board.animalBoard[i][j].getCamp() == -1) {
leftAlive++;
if (!Board.animalBoard[i][j].move(directionUp) && !Board.animalBoard[i][j].move(directionDown) && !Board.animalBoard[i][j].move(directionLeft) && !Board.animalBoard[i][j].move(directionRight)) {
leftTrapped++;
}
} else if (Board.animalBoard[i][j].getCamp() == 1) {
rightAlive++;
if (!Board.animalBoard[i][j].move(directionUp) && !Board.animalBoard[i][j].move(directionDown) && !Board.animalBoard[i][j].move(directionLeft) && !Board.animalBoard[i][j].move(directionRight)) {
rightTrapped++;
}
}
}
}
return leftTrapped == leftAlive || rightTrapped == rightAlive;
}
public static boolean ifDanger(boolean player) {
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 9; j++) {
int[] directionUp = {-1, 0};
int[] directionDown = {1, 0};
int[] directionLeft = {0, -1};
int[] directionRight = {0, 1};
if (Board.animalBoard[i][j].getCamp() == (player ? 1 : -1)) {
try {
if (Board.animalBoard[i][j].move(directionUp) & Board.animalBoard[i + directionUp[0]][j + directionUp[1]].getCamp() == (player ? -1 : 1)) {
return true;
}
} catch (Exception e) {
}
try {
if (Board.animalBoard[i][j].move(directionDown) & Board.animalBoard[i + directionDown[0]][j + directionDown[1]].getCamp() == (player ? -1 : 1)) {
return true;
}
} catch (Exception e) {
}
try {
if (Board.animalBoard[i][j].move(directionLeft) & Board.animalBoard[i + directionLeft[0]][j + directionLeft[1]].getCamp() == (player ? -1 : 1)) {
return true;
}
} catch (Exception e) {
}
try {
if (Board.animalBoard[i][j].move(directionRight) & Board.animalBoard[i + directionRight[0]][j + directionRight[1]].getCamp() == (player ? -1 : 1)) {
return true;
}
} catch (Exception e) {
}
}
}
}
return false;
}
}