|
indvdum@9
|
1 |
package ru.bosony.model.game;
|
|
indvdum@9
|
2 |
|
|
indvdum@9
|
3 |
import static ru.bosony.model.cellscontents.CellContent.ClosedLambdaLift;
|
|
indvdum@9
|
4 |
import static ru.bosony.model.cellscontents.CellContent.Earth;
|
|
indvdum@9
|
5 |
import static ru.bosony.model.cellscontents.CellContent.Empty;
|
|
indvdum@9
|
6 |
import static ru.bosony.model.cellscontents.CellContent.Lambda;
|
|
indvdum@9
|
7 |
import static ru.bosony.model.cellscontents.CellContent.MiningRobot;
|
|
indvdum@9
|
8 |
import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift;
|
|
indvdum@9
|
9 |
import static ru.bosony.model.cellscontents.CellContent.Rock;
|
|
indvdum@9
|
10 |
|
|
indvdum@16
|
11 |
import java.util.ArrayList;
|
|
indvdum@16
|
12 |
import java.util.List;
|
|
indvdum@9
|
13 |
|
|
indvdum@9
|
14 |
import ru.bosony.model.cellscontents.CellContent;
|
|
indvdum@9
|
15 |
import ru.bosony.model.mine.Cell;
|
|
indvdum@9
|
16 |
import ru.bosony.model.mine.Mine;
|
|
indvdum@22
|
17 |
import ru.bosony.model.moving.Coordinate;
|
|
indvdum@9
|
18 |
import ru.bosony.model.moving.Movement;
|
|
indvdum@9
|
19 |
|
|
indvdum@9
|
20 |
/**
|
|
indvdum@9
|
21 |
* @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
|
|
indvdum@9
|
22 |
* 14.07.2012 22:51:20
|
|
indvdum@9
|
23 |
*
|
|
indvdum@9
|
24 |
*/
|
|
indvdum@9
|
25 |
public class Game {
|
|
indvdum@9
|
26 |
|
|
indvdum@22
|
27 |
protected Mine mine = null;
|
|
indvdum@20
|
28 |
protected int score = 0;
|
|
indvdum@20
|
29 |
protected int lastScoreChange = 0;
|
|
indvdum@20
|
30 |
protected GameState state = GameState.Game;
|
|
indvdum@20
|
31 |
protected int lambdaCollectedCount = 0;
|
|
indvdum@20
|
32 |
protected List<Movement> route = new ArrayList<Movement>();
|
|
indvdum@25
|
33 |
protected int underwater = 0;
|
|
indvdum@9
|
34 |
|
|
indvdum@9
|
35 |
public Game(Mine mine) {
|
|
indvdum@9
|
36 |
this.mine = mine;
|
|
indvdum@9
|
37 |
}
|
|
indvdum@9
|
38 |
|
|
indvdum@9
|
39 |
public Mine getMine() {
|
|
indvdum@9
|
40 |
return mine;
|
|
indvdum@9
|
41 |
}
|
|
indvdum@9
|
42 |
|
|
indvdum@9
|
43 |
protected Cell getCell(Coordinate coordinate, Cell[][] cells) {
|
|
indvdum@9
|
44 |
if (coordinate == null)
|
|
indvdum@9
|
45 |
return null;
|
|
indvdum@9
|
46 |
int x = coordinate.x;
|
|
indvdum@9
|
47 |
int y = coordinate.y;
|
|
indvdum@9
|
48 |
if (cells == null)
|
|
indvdum@9
|
49 |
return null;
|
|
indvdum@9
|
50 |
if (x < 1 || x > mine.getSizeX() || y < 1 || y > mine.getSizeY())
|
|
indvdum@9
|
51 |
return null;
|
|
indvdum@9
|
52 |
return cells[x - 1][mine.getSizeY() - y];
|
|
indvdum@9
|
53 |
}
|
|
indvdum@9
|
54 |
|
|
indvdum@9
|
55 |
protected void moveRobot(Cell nextRobotCell) {
|
|
indvdum@9
|
56 |
mine.getRobotCell().setContent(Empty);
|
|
indvdum@9
|
57 |
nextRobotCell.setContent(MiningRobot);
|
|
indvdum@9
|
58 |
}
|
|
indvdum@9
|
59 |
|
|
indvdum@9
|
60 |
public GameState move(Movement mov) {
|
|
indvdum@9
|
61 |
if (state != GameState.Game)
|
|
indvdum@9
|
62 |
throw new RuntimeException("Game over!");
|
|
indvdum@9
|
63 |
|
|
indvdum@9
|
64 |
// Score
|
|
indvdum@9
|
65 |
int prevScore = score;
|
|
indvdum@9
|
66 |
|
|
indvdum@9
|
67 |
// Moving and scoring
|
|
indvdum@20
|
68 |
route.add(mov);
|
|
indvdum@22
|
69 |
Coordinate robotCoord = mine.getRobotCell().getCoordinate();
|
|
indvdum@9
|
70 |
Coordinate nextRobotCoord = null;
|
|
indvdum@9
|
71 |
switch (mov) {
|
|
indvdum@9
|
72 |
case ABORT:
|
|
indvdum@9
|
73 |
score += 25 * lambdaCollectedCount;
|
|
indvdum@9
|
74 |
|
|
indvdum@9
|
75 |
lastScoreChange = score - prevScore;
|
|
indvdum@9
|
76 |
state = GameState.Abort;
|
|
indvdum@9
|
77 |
return state;
|
|
indvdum@9
|
78 |
case WAIT:
|
|
indvdum@9
|
79 |
break;
|
|
indvdum@9
|
80 |
case LEFT:
|
|
indvdum@22
|
81 |
nextRobotCoord = robotCoord.left();
|
|
indvdum@9
|
82 |
break;
|
|
indvdum@9
|
83 |
case UP:
|
|
indvdum@22
|
84 |
nextRobotCoord = robotCoord.up();
|
|
indvdum@9
|
85 |
break;
|
|
indvdum@9
|
86 |
case RIGHT:
|
|
indvdum@22
|
87 |
nextRobotCoord = robotCoord.right();
|
|
indvdum@9
|
88 |
break;
|
|
indvdum@9
|
89 |
case DOWN:
|
|
indvdum@22
|
90 |
nextRobotCoord = robotCoord.down();
|
|
indvdum@9
|
91 |
break;
|
|
indvdum@9
|
92 |
}
|
|
indvdum@23
|
93 |
score--;
|
|
indvdum@9
|
94 |
Cell nextRobotCell = mine.getCell(nextRobotCoord);
|
|
indvdum@9
|
95 |
CellContent nextRobotCellContent = getContent(nextRobotCell);
|
|
indvdum@9
|
96 |
if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) {
|
|
indvdum@9
|
97 |
moveRobot(nextRobotCell);
|
|
indvdum@9
|
98 |
} else if (nextRobotCellContent == Lambda) {
|
|
indvdum@9
|
99 |
moveRobot(nextRobotCell);
|
|
indvdum@9
|
100 |
lambdaCollectedCount++;
|
|
indvdum@9
|
101 |
score += 25;
|
|
indvdum@9
|
102 |
} else if (nextRobotCellContent == OpenLambdaLift) {
|
|
indvdum@9
|
103 |
moveRobot(nextRobotCell);
|
|
indvdum@9
|
104 |
score += 50 * lambdaCollectedCount;
|
|
indvdum@9
|
105 |
|
|
indvdum@9
|
106 |
lastScoreChange = score - prevScore;
|
|
indvdum@9
|
107 |
state = GameState.Win;
|
|
indvdum@9
|
108 |
return state;
|
|
indvdum@23
|
109 |
} else if (robotCoord.right().equals(nextRobotCoord) && nextRobotCellContent == Rock
|
|
indvdum@9
|
110 |
&& getContent(mine.getCell(nextRobotCoord.right())) == Empty) {
|
|
indvdum@9
|
111 |
moveRobot(nextRobotCell);
|
|
indvdum@9
|
112 |
mine.getCell(nextRobotCoord.right()).setContent(Rock);
|
|
indvdum@23
|
113 |
} else if (robotCoord.left().equals(nextRobotCoord) && nextRobotCellContent == Rock
|
|
indvdum@9
|
114 |
&& getContent(mine.getCell(nextRobotCoord.left())) == Empty) {
|
|
indvdum@9
|
115 |
moveRobot(nextRobotCell);
|
|
indvdum@9
|
116 |
mine.getCell(nextRobotCoord.left()).setContent(Rock);
|
|
indvdum@27
|
117 |
} else if (CellContent.getTrampolines().contains(nextRobotCellContent)
|
|
indvdum@27
|
118 |
&& CellContent.getTargets().contains(nextRobotCellContent.getTrampolineTarget())) {
|
|
indvdum@27
|
119 |
CellContent trampoline = nextRobotCellContent;
|
|
indvdum@27
|
120 |
CellContent target = trampoline.getTrampolineTarget();
|
|
indvdum@27
|
121 |
for (Cell targetCell : mine.findCells(target)) {
|
|
indvdum@27
|
122 |
moveRobot(targetCell);
|
|
indvdum@27
|
123 |
}
|
|
indvdum@27
|
124 |
for (CellContent tramp : CellContent.getTrampolines()) {
|
|
indvdum@27
|
125 |
if (tramp.getTrampolineTarget() == target) {
|
|
indvdum@27
|
126 |
for (Cell trampolineCell : mine.findCells(tramp)) {
|
|
indvdum@27
|
127 |
trampolineCell.setContent(Empty);
|
|
indvdum@27
|
128 |
}
|
|
indvdum@27
|
129 |
}
|
|
indvdum@27
|
130 |
}
|
|
indvdum@9
|
131 |
} else {
|
|
indvdum@9
|
132 |
mov = Movement.WAIT;
|
|
indvdum@9
|
133 |
}
|
|
indvdum@9
|
134 |
|
|
indvdum@9
|
135 |
// World changes
|
|
indvdum@22
|
136 |
Cell[][] newCells = mine.cloneCells();
|
|
indvdum@9
|
137 |
for (int y = 1; y <= mine.getSizeY(); y++) {
|
|
indvdum@9
|
138 |
for (int x = 1; x <= mine.getSizeX(); x++) {
|
|
indvdum@9
|
139 |
Coordinate curCoord = new Coordinate(x, y);
|
|
indvdum@9
|
140 |
Coordinate leftCoord = curCoord.left();
|
|
indvdum@9
|
141 |
Coordinate upCoord = curCoord.up();
|
|
indvdum@9
|
142 |
Coordinate rightCoord = curCoord.right();
|
|
indvdum@9
|
143 |
Coordinate downCoord = curCoord.down();
|
|
indvdum@9
|
144 |
Coordinate leftAndUpCoord = curCoord.leftAndUp();
|
|
indvdum@9
|
145 |
Coordinate rightAndUpCoord = curCoord.rightAndUp();
|
|
indvdum@9
|
146 |
Coordinate rightAndDownCoord = curCoord.rightAndDown();
|
|
indvdum@9
|
147 |
Coordinate leftAndDownCoord = curCoord.leftAndDown();
|
|
indvdum@9
|
148 |
Cell curCell = mine.getCell(curCoord);
|
|
indvdum@9
|
149 |
Cell leftCell = mine.getCell(leftCoord);
|
|
indvdum@9
|
150 |
Cell upCell = mine.getCell(upCoord);
|
|
indvdum@9
|
151 |
Cell rightCell = mine.getCell(rightCoord);
|
|
indvdum@9
|
152 |
Cell downCell = mine.getCell(downCoord);
|
|
indvdum@9
|
153 |
Cell leftAndUpCell = mine.getCell(leftAndUpCoord);
|
|
indvdum@9
|
154 |
Cell rightAndUpCell = mine.getCell(rightAndUpCoord);
|
|
indvdum@9
|
155 |
Cell rightAndDownCell = mine.getCell(rightAndDownCoord);
|
|
indvdum@9
|
156 |
Cell leftAndDownCell = mine.getCell(leftAndDownCoord);
|
|
indvdum@9
|
157 |
|
|
indvdum@9
|
158 |
if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
|
|
indvdum@9
|
159 |
getCell(curCoord, newCells).setContent(Empty);
|
|
indvdum@9
|
160 |
getCell(downCoord, newCells).setContent(Rock);
|
|
indvdum@9
|
161 |
} else if (getContent(curCell) == Rock && getContent(downCell) == Empty
|
|
indvdum@9
|
162 |
&& getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
|
|
indvdum@9
|
163 |
getCell(curCoord, newCells).setContent(Empty);
|
|
indvdum@9
|
164 |
getCell(rightAndDownCoord, newCells).setContent(Rock);
|
|
indvdum@9
|
165 |
} else if (getContent(curCell) == Rock && getContent(downCell) == Rock
|
|
indvdum@9
|
166 |
&& (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
|
|
indvdum@9
|
167 |
&& getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
|
|
indvdum@9
|
168 |
getCell(curCoord, newCells).setContent(Empty);
|
|
indvdum@9
|
169 |
getCell(leftAndDownCoord, newCells).setContent(Rock);
|
|
indvdum@9
|
170 |
} else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
|
|
indvdum@9
|
171 |
&& getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
|
|
indvdum@9
|
172 |
getCell(curCoord, newCells).setContent(Empty);
|
|
indvdum@9
|
173 |
getCell(rightAndDownCoord, newCells).setContent(Rock);
|
|
indvdum@9
|
174 |
} else if (getContent(curCell) == ClosedLambdaLift && mine.findCells(Lambda).size() == 0) {
|
|
indvdum@9
|
175 |
getCell(curCoord, newCells).setContent(OpenLambdaLift);
|
|
indvdum@9
|
176 |
}
|
|
indvdum@9
|
177 |
}
|
|
indvdum@9
|
178 |
}
|
|
indvdum@22
|
179 |
|
|
indvdum@22
|
180 |
// Other ending conditions
|
|
indvdum@22
|
181 |
robotCoord = mine.getRobotCell().getCoordinate();
|
|
indvdum@22
|
182 |
if (getContent(mine.getCell(robotCoord.up())) != Rock && getContent(getCell(robotCoord.up(), newCells)) == Rock) {
|
|
indvdum@22
|
183 |
mine.setCells(newCells);
|
|
indvdum@25
|
184 |
|
|
indvdum@22
|
185 |
lastScoreChange = score - prevScore;
|
|
indvdum@22
|
186 |
state = GameState.Lose;
|
|
indvdum@22
|
187 |
return state;
|
|
indvdum@22
|
188 |
}
|
|
indvdum@22
|
189 |
|
|
indvdum@9
|
190 |
mine.setCells(newCells);
|
|
indvdum@9
|
191 |
|
|
indvdum@25
|
192 |
// Water flooding
|
|
indvdum@25
|
193 |
robotCoord = mine.getRobotCell().getCoordinate();
|
|
indvdum@25
|
194 |
if (robotCoord.y <= mine.getWaterLevel()) {
|
|
indvdum@25
|
195 |
underwater++;
|
|
indvdum@25
|
196 |
} else {
|
|
indvdum@25
|
197 |
underwater = 0;
|
|
indvdum@25
|
198 |
}
|
|
indvdum@25
|
199 |
if (underwater > mine.getRobotWaterproof()) {
|
|
indvdum@25
|
200 |
lastScoreChange = score - prevScore;
|
|
indvdum@25
|
201 |
state = GameState.Lose;
|
|
indvdum@25
|
202 |
return state;
|
|
indvdum@25
|
203 |
}
|
|
indvdum@25
|
204 |
if (mine.getFlooding() > 0 && route.size() % mine.getFlooding() == 0) {
|
|
indvdum@25
|
205 |
mine.setWaterLevel(mine.getWaterLevel() + 1);
|
|
indvdum@25
|
206 |
}
|
|
indvdum@25
|
207 |
|
|
indvdum@9
|
208 |
lastScoreChange = score - prevScore;
|
|
indvdum@9
|
209 |
state = GameState.Game;
|
|
indvdum@9
|
210 |
return state;
|
|
indvdum@9
|
211 |
}
|
|
indvdum@9
|
212 |
|
|
indvdum@16
|
213 |
public GameState move(List<Movement> movs) {
|
|
indvdum@16
|
214 |
for (Movement mov : movs) {
|
|
indvdum@16
|
215 |
if (move(mov) != GameState.Game)
|
|
indvdum@16
|
216 |
return state;
|
|
indvdum@16
|
217 |
}
|
|
indvdum@16
|
218 |
return state;
|
|
indvdum@16
|
219 |
}
|
|
indvdum@16
|
220 |
|
|
indvdum@19
|
221 |
public GameState move(String route) {
|
|
indvdum@16
|
222 |
List<Movement> movs = new ArrayList<Movement>();
|
|
indvdum@19
|
223 |
for (int i = 0; i < route.length(); i++) {
|
|
indvdum@19
|
224 |
String s = "" + route.charAt(i);
|
|
indvdum@16
|
225 |
for (Movement mov : Movement.values()) {
|
|
indvdum@16
|
226 |
if (mov.toText().equals(s)) {
|
|
indvdum@16
|
227 |
movs.add(mov);
|
|
indvdum@16
|
228 |
break;
|
|
indvdum@16
|
229 |
}
|
|
indvdum@16
|
230 |
}
|
|
indvdum@16
|
231 |
}
|
|
indvdum@16
|
232 |
return move(movs);
|
|
indvdum@16
|
233 |
}
|
|
indvdum@16
|
234 |
|
|
indvdum@9
|
235 |
protected CellContent getContent(Cell cell) {
|
|
indvdum@9
|
236 |
if (cell == null)
|
|
indvdum@9
|
237 |
return null;
|
|
indvdum@9
|
238 |
return cell.getContent();
|
|
indvdum@9
|
239 |
}
|
|
indvdum@9
|
240 |
|
|
indvdum@9
|
241 |
public int getLastScoreChange() {
|
|
indvdum@9
|
242 |
return lastScoreChange;
|
|
indvdum@9
|
243 |
}
|
|
indvdum@9
|
244 |
|
|
indvdum@9
|
245 |
public int getScore() {
|
|
indvdum@9
|
246 |
return score;
|
|
indvdum@9
|
247 |
}
|
|
indvdum@9
|
248 |
|
|
indvdum@9
|
249 |
public GameState getState() {
|
|
indvdum@9
|
250 |
return state;
|
|
indvdum@9
|
251 |
}
|
|
indvdum@9
|
252 |
|
|
indvdum@9
|
253 |
public int getLambdaCollectedCount() {
|
|
indvdum@9
|
254 |
return lambdaCollectedCount;
|
|
indvdum@9
|
255 |
}
|
|
indvdum@20
|
256 |
|
|
indvdum@20
|
257 |
public List<Movement> getRoute() {
|
|
indvdum@20
|
258 |
return route;
|
|
indvdum@20
|
259 |
}
|
|
indvdum@20
|
260 |
|
|
indvdum@20
|
261 |
public String getStringRoute() {
|
|
indvdum@20
|
262 |
String movs = "";
|
|
indvdum@20
|
263 |
for (Movement mov : route) {
|
|
indvdum@20
|
264 |
movs += mov.toText();
|
|
indvdum@20
|
265 |
}
|
|
indvdum@20
|
266 |
return movs;
|
|
indvdum@20
|
267 |
}
|
|
indvdum@25
|
268 |
|
|
indvdum@25
|
269 |
public int getUnderwater() {
|
|
indvdum@25
|
270 |
return underwater;
|
|
indvdum@25
|
271 |
}
|
|
indvdum@9
|
272 |
}
|