1 package ru.bosony.model.game;
3 import static ru.bosony.model.cellscontents.CellContent.ClosedLambdaLift;
4 import static ru.bosony.model.cellscontents.CellContent.Earth;
5 import static ru.bosony.model.cellscontents.CellContent.Empty;
6 import static ru.bosony.model.cellscontents.CellContent.HighOrderRock;
7 import static ru.bosony.model.cellscontents.CellContent.HuttonsRazor;
8 import static ru.bosony.model.cellscontents.CellContent.Lambda;
9 import static ru.bosony.model.cellscontents.CellContent.MiningRobot;
10 import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift;
11 import static ru.bosony.model.cellscontents.CellContent.Rock;
12 import static ru.bosony.model.cellscontents.CellContent.WadlersBeard;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
19 import ru.bosony.model.cellscontents.CellContent;
20 import ru.bosony.model.mine.Cell;
21 import ru.bosony.model.mine.Mine;
22 import ru.bosony.model.moving.Coordinate;
23 import ru.bosony.model.moving.Movement;
26 * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
32 protected Mine mine = null;
33 protected int score = 0;
34 protected int lastScoreChange = 0;
35 protected GameState state = GameState.Game;
36 protected int lambdaCollectedCount = 0;
37 protected List<Mine> history = new ArrayList<Mine>();
38 protected List<Movement> route = new ArrayList<Movement>();
39 protected int underwater = 0;
41 public Game(Mine mine) {
45 public Mine getMine() {
49 protected Cell getCell(Coordinate coordinate, Cell[][] cells) {
50 if (coordinate == null)
56 if (x < 1 || x > mine.getSizeX() || y < 1 || y > mine.getSizeY())
58 return cells[x - 1][mine.getSizeY() - y];
61 protected void moveRobot(Cell nextRobotCell) {
62 mine.getRobotCell().setContent(Empty);
63 nextRobotCell.setContent(MiningRobot);
66 public GameState move(Movement mov) {
67 if (state != GameState.Game)
68 throw new RuntimeException("Game over!");
71 int prevScore = score;
76 history.add(mine.clone());
77 } catch (CloneNotSupportedException e) {
78 throw new RuntimeException(e);
80 Cell robotCell = mine.getRobotCell();
81 Coordinate robotCoord = robotCell.getCoordinate();
82 Coordinate nextRobotCoord = null;
85 score += 25 * lambdaCollectedCount;
87 lastScoreChange = score - prevScore;
88 state = GameState.Abort;
93 if (mine.getRazorsCount() > 0) {
94 for (Cell cell : mine.getNeighboringCells(robotCell)) {
95 if (cell.getContent() == WadlersBeard) {
96 cell.setContent(Empty);
99 mine.setRazorsCount(mine.getRazorsCount() - 1);
103 nextRobotCoord = robotCoord.left();
106 nextRobotCoord = robotCoord.up();
109 nextRobotCoord = robotCoord.right();
112 nextRobotCoord = robotCoord.down();
116 Cell nextRobotCell = mine.getCell(nextRobotCoord);
117 CellContent nextRobotCellContent = getContent(nextRobotCell);
118 if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) {
119 moveRobot(nextRobotCell);
120 } else if (nextRobotCellContent == Lambda) {
121 moveRobot(nextRobotCell);
122 lambdaCollectedCount++;
124 } else if (nextRobotCellContent == OpenLambdaLift) {
125 moveRobot(nextRobotCell);
126 score += 50 * lambdaCollectedCount;
128 lastScoreChange = score - prevScore;
129 state = GameState.Win;
131 } else if (nextRobotCellContent == HuttonsRazor) {
132 moveRobot(nextRobotCell);
133 mine.setRazorsCount(mine.getRazorsCount() + 1);
134 } else if (robotCoord.right().equals(nextRobotCoord) && nextRobotCellContent == Rock
135 && getContent(mine.getCell(nextRobotCoord.right())) == Empty) {
136 moveRobot(nextRobotCell);
137 mine.getCell(nextRobotCoord.right()).setContent(Rock);
138 } else if (robotCoord.left().equals(nextRobotCoord) && nextRobotCellContent == Rock
139 && getContent(mine.getCell(nextRobotCoord.left())) == Empty) {
140 moveRobot(nextRobotCell);
141 mine.getCell(nextRobotCoord.left()).setContent(Rock);
142 } else if (robotCoord.right().equals(nextRobotCoord) && nextRobotCellContent == HighOrderRock
143 && getContent(mine.getCell(nextRobotCoord.right())) == Empty) {
144 moveRobot(nextRobotCell);
145 mine.getCell(nextRobotCoord.right()).setContent(HighOrderRock);
146 } else if (robotCoord.left().equals(nextRobotCoord) && nextRobotCellContent == HighOrderRock
147 && getContent(mine.getCell(nextRobotCoord.left())) == Empty) {
148 moveRobot(nextRobotCell);
149 mine.getCell(nextRobotCoord.left()).setContent(HighOrderRock);
150 } else if (CellContent.getTrampolines().contains(nextRobotCellContent)
151 && CellContent.getTargets().contains(nextRobotCellContent.getTrampolineTarget())) {
152 CellContent trampoline = nextRobotCellContent;
153 CellContent target = trampoline.getTrampolineTarget();
154 for (Cell targetCell : mine.findCells(target)) {
155 moveRobot(targetCell);
157 for (CellContent tramp : CellContent.getTrampolines()) {
158 if (tramp.getTrampolineTarget() == target) {
159 for (Cell trampolineCell : mine.findCells(tramp)) {
160 trampolineCell.setContent(Empty);
169 Cell[][] newCells = mine.cloneCells();
170 for (int y = 1; y <= mine.getSizeY(); y++) {
171 for (int x = 1; x <= mine.getSizeX(); x++) {
172 Coordinate curCoord = new Coordinate(x, y);
173 Coordinate leftCoord = curCoord.left();
174 Coordinate upCoord = curCoord.up();
175 Coordinate rightCoord = curCoord.right();
176 Coordinate downCoord = curCoord.down();
177 Coordinate leftAndUpCoord = curCoord.leftAndUp();
178 Coordinate rightAndUpCoord = curCoord.rightAndUp();
179 Coordinate rightAndDownCoord = curCoord.rightAndDown();
180 Coordinate leftAndDownCoord = curCoord.leftAndDown();
181 Cell curCell = mine.getCell(curCoord);
182 Cell leftCell = mine.getCell(leftCoord);
183 Cell upCell = mine.getCell(upCoord);
184 Cell rightCell = mine.getCell(rightCoord);
185 Cell downCell = mine.getCell(downCoord);
186 Cell leftAndUpCell = mine.getCell(leftAndUpCoord);
187 Cell rightAndUpCell = mine.getCell(rightAndUpCoord);
188 Cell rightAndDownCell = mine.getCell(rightAndDownCoord);
189 Cell leftAndDownCell = mine.getCell(leftAndDownCoord);
191 if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
192 getCell(curCoord, newCells).setContent(Empty);
193 getCell(downCoord, newCells).setContent(Rock);
194 } else if (getContent(curCell) == HighOrderRock && getContent(downCell) == Empty) {
195 getCell(curCoord, newCells).setContent(Empty);
196 if (getContent(mine.getCell(downCoord.down())) != Empty)
197 getCell(downCoord, newCells).setContent(Lambda);
199 getCell(downCoord, newCells).setContent(HighOrderRock);
200 } else if (getContent(curCell) == Rock
201 && (getContent(downCell) == Rock || getContent(downCell) == HighOrderRock)
202 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
203 getCell(curCoord, newCells).setContent(Empty);
204 getCell(rightAndDownCoord, newCells).setContent(Rock);
205 } else if (getContent(curCell) == HighOrderRock
206 && (getContent(downCell) == Rock || getContent(downCell) == HighOrderRock)
207 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
208 getCell(curCoord, newCells).setContent(Empty);
209 if (getContent(mine.getCell(rightAndDownCoord.down())) != Empty)
210 getCell(rightAndDownCoord, newCells).setContent(Lambda);
212 getCell(rightAndDownCoord, newCells).setContent(HighOrderRock);
213 } else if (getContent(curCell) == Rock
214 && (getContent(downCell) == Rock || getContent(downCell) == HighOrderRock)
215 && (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
216 && getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
217 getCell(curCoord, newCells).setContent(Empty);
218 getCell(leftAndDownCoord, newCells).setContent(Rock);
219 } else if (getContent(curCell) == HighOrderRock
220 && (getContent(downCell) == Rock || getContent(downCell) == HighOrderRock)
221 && (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
222 && getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
223 getCell(curCoord, newCells).setContent(Empty);
224 if (getContent(mine.getCell(leftAndDownCoord.down())) != Empty)
225 getCell(leftAndDownCoord, newCells).setContent(Lambda);
227 getCell(leftAndDownCoord, newCells).setContent(HighOrderRock);
228 } else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
229 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
230 getCell(curCoord, newCells).setContent(Empty);
231 getCell(rightAndDownCoord, newCells).setContent(Rock);
232 } else if (getContent(curCell) == HighOrderRock && getContent(downCell) == Lambda
233 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
234 getCell(curCoord, newCells).setContent(Empty);
235 if (getContent(mine.getCell(rightAndDownCoord.down())) != Empty)
236 getCell(rightAndDownCoord, newCells).setContent(Lambda);
238 getCell(rightAndDownCoord, newCells).setContent(HighOrderRock);
239 } else if (getContent(curCell) == ClosedLambdaLift
240 && mine.getLambdasAndHighOrderRocksStartCount() == lambdaCollectedCount) {
241 getCell(curCoord, newCells).setContent(OpenLambdaLift);
242 } else if (getContent(curCell) == WadlersBeard && mine.getGrowth() > 0
243 && route.size() % mine.getGrowth() == 0) {
244 if (getContent(leftCell) == Empty)
245 getCell(leftCoord, newCells).setContent(WadlersBeard);
246 if (getContent(upCell) == Empty)
247 getCell(upCoord, newCells).setContent(WadlersBeard);
248 if (getContent(rightCell) == Empty)
249 getCell(rightCoord, newCells).setContent(WadlersBeard);
250 if (getContent(downCell) == Empty)
251 getCell(downCoord, newCells).setContent(WadlersBeard);
252 if (getContent(leftAndUpCell) == Empty)
253 getCell(leftAndUpCoord, newCells).setContent(WadlersBeard);
254 if (getContent(rightAndUpCell) == Empty)
255 getCell(rightAndUpCoord, newCells).setContent(WadlersBeard);
256 if (getContent(leftAndDownCell) == Empty)
257 getCell(leftAndDownCoord, newCells).setContent(WadlersBeard);
258 if (getContent(rightAndDownCell) == Empty)
259 getCell(rightAndDownCoord, newCells).setContent(WadlersBeard);
264 // Other ending conditions
265 robotCoord = mine.getRobotCell().getCoordinate();
266 if (getContent(mine.getCell(robotCoord.up())) != Rock && getContent(getCell(robotCoord.up(), newCells)) == Rock
267 || getContent(mine.getCell(robotCoord.up())) != HighOrderRock
268 && getContent(getCell(robotCoord.up(), newCells)) == HighOrderRock
269 || getContent(mine.getCell(robotCoord.up())) == Empty
270 && getContent(getCell(robotCoord.up(), newCells)) == Lambda) {
271 mine.setCells(newCells);
273 lastScoreChange = score - prevScore;
274 state = GameState.Lose;
278 mine.setCells(newCells);
281 robotCoord = mine.getRobotCell().getCoordinate();
282 if (robotCoord.y <= mine.getWaterLevel()) {
287 if (underwater > mine.getRobotWaterproof()) {
288 lastScoreChange = score - prevScore;
289 state = GameState.Lose;
292 if (mine.getFlooding() > 0 && route.size() % mine.getFlooding() == 0) {
293 mine.setWaterLevel(mine.getWaterLevel() + 1);
296 lastScoreChange = score - prevScore;
297 state = GameState.Game;
301 public GameState move(List<Movement> movs) {
302 for (Movement mov : movs) {
303 if (move(mov) != GameState.Game)
309 public GameState move(String route) {
310 List<Movement> movs = new ArrayList<Movement>();
311 for (int i = 0; i < route.length(); i++) {
312 String s = "" + route.charAt(i);
313 for (Movement mov : Movement.values()) {
314 if (mov.toText().equals(s)) {
323 protected CellContent getContent(Cell cell) {
326 return cell.getContent();
329 public int getLastScoreChange() {
330 return lastScoreChange;
333 public int getScore() {
337 public GameState getState() {
341 public int getLambdaCollectedCount() {
342 return lambdaCollectedCount;
345 public List<Movement> getRoute() {
349 public String getStringRoute() {
351 for (Movement mov : route) {
352 movs += mov.toText();
357 public int getUnderwater() {
361 public boolean hasViciousCircle() {
362 if (route.size() != history.size())
363 throw new RuntimeException("Desynchronized history");
364 Map<Mine, Movement> state = new HashMap<Mine, Movement>();
365 for (int i = route.size() - 1; i >= 0; i--) {
366 Mine mine = history.get(i);
367 Movement mov = route.get(i);
368 if (state.get(mine) == mov)
370 state.put(mine, mov);