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;
75 history.add(mine.clone());
76 Cell robotCell = mine.getRobotCell();
77 Coordinate robotCoord = robotCell.getCoordinate();
78 Coordinate nextRobotCoord = null;
81 score += 25 * lambdaCollectedCount;
83 lastScoreChange = score - prevScore;
84 state = GameState.Abort;
89 if (mine.getRazorsCount() > 0) {
90 for (Cell cell : mine.getNeighboringCells(robotCell)) {
91 if (cell.getContent() == WadlersBeard) {
92 cell.setContent(Empty);
95 mine.setRazorsCount(mine.getRazorsCount() - 1);
99 nextRobotCoord = robotCoord.left();
102 nextRobotCoord = robotCoord.up();
105 nextRobotCoord = robotCoord.right();
108 nextRobotCoord = robotCoord.down();
112 Cell nextRobotCell = mine.getCell(nextRobotCoord);
113 CellContent nextRobotCellContent = getContent(nextRobotCell);
114 if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) {
115 moveRobot(nextRobotCell);
116 } else if (nextRobotCellContent == Lambda) {
117 moveRobot(nextRobotCell);
118 lambdaCollectedCount++;
120 } else if (nextRobotCellContent == OpenLambdaLift) {
121 moveRobot(nextRobotCell);
122 score += 50 * lambdaCollectedCount;
124 lastScoreChange = score - prevScore;
125 state = GameState.Win;
127 } else if (nextRobotCellContent == HuttonsRazor) {
128 moveRobot(nextRobotCell);
129 mine.setRazorsCount(mine.getRazorsCount() + 1);
130 } else if (robotCoord.right().equals(nextRobotCoord) && nextRobotCellContent == Rock
131 && getContent(mine.getCell(nextRobotCoord.right())) == Empty) {
132 moveRobot(nextRobotCell);
133 mine.getCell(nextRobotCoord.right()).setContent(Rock);
134 } else if (robotCoord.left().equals(nextRobotCoord) && nextRobotCellContent == Rock
135 && getContent(mine.getCell(nextRobotCoord.left())) == Empty) {
136 moveRobot(nextRobotCell);
137 mine.getCell(nextRobotCoord.left()).setContent(Rock);
138 } else if (robotCoord.right().equals(nextRobotCoord) && nextRobotCellContent == HighOrderRock
139 && getContent(mine.getCell(nextRobotCoord.right())) == Empty) {
140 moveRobot(nextRobotCell);
141 mine.getCell(nextRobotCoord.right()).setContent(HighOrderRock);
142 } else if (robotCoord.left().equals(nextRobotCoord) && nextRobotCellContent == HighOrderRock
143 && getContent(mine.getCell(nextRobotCoord.left())) == Empty) {
144 moveRobot(nextRobotCell);
145 mine.getCell(nextRobotCoord.left()).setContent(HighOrderRock);
146 } else if (CellContent.getTrampolines().contains(nextRobotCellContent)
147 && CellContent.getTargets().contains(nextRobotCellContent.getTrampolineTarget())) {
148 CellContent trampoline = nextRobotCellContent;
149 CellContent target = trampoline.getTrampolineTarget();
150 for (Cell targetCell : mine.findCells(target)) {
151 moveRobot(targetCell);
153 for (CellContent tramp : CellContent.getTrampolines()) {
154 if (tramp.getTrampolineTarget() == target) {
155 for (Cell trampolineCell : mine.findCells(tramp)) {
156 trampolineCell.setContent(Empty);
165 Cell[][] newCells = mine.cloneCells();
166 for (int y = 1; y <= mine.getSizeY(); y++) {
167 for (int x = 1; x <= mine.getSizeX(); x++) {
168 Coordinate curCoord = new Coordinate(x, y);
169 Coordinate leftCoord = curCoord.left();
170 Coordinate upCoord = curCoord.up();
171 Coordinate rightCoord = curCoord.right();
172 Coordinate downCoord = curCoord.down();
173 Coordinate leftAndUpCoord = curCoord.leftAndUp();
174 Coordinate rightAndUpCoord = curCoord.rightAndUp();
175 Coordinate rightAndDownCoord = curCoord.rightAndDown();
176 Coordinate leftAndDownCoord = curCoord.leftAndDown();
177 Cell curCell = mine.getCell(curCoord);
178 Cell leftCell = mine.getCell(leftCoord);
179 Cell upCell = mine.getCell(upCoord);
180 Cell rightCell = mine.getCell(rightCoord);
181 Cell downCell = mine.getCell(downCoord);
182 Cell leftAndUpCell = mine.getCell(leftAndUpCoord);
183 Cell rightAndUpCell = mine.getCell(rightAndUpCoord);
184 Cell rightAndDownCell = mine.getCell(rightAndDownCoord);
185 Cell leftAndDownCell = mine.getCell(leftAndDownCoord);
187 if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
188 getCell(curCoord, newCells).setContent(Empty);
189 getCell(downCoord, newCells).setContent(Rock);
190 } else if (getContent(curCell) == HighOrderRock && getContent(downCell) == Empty) {
191 getCell(curCoord, newCells).setContent(Empty);
192 if (getContent(mine.getCell(downCoord.down())) != Empty)
193 getCell(downCoord, newCells).setContent(Lambda);
195 getCell(downCoord, newCells).setContent(HighOrderRock);
196 } else if (getContent(curCell) == Rock
197 && (getContent(downCell) == Rock || getContent(downCell) == HighOrderRock)
198 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
199 getCell(curCoord, newCells).setContent(Empty);
200 getCell(rightAndDownCoord, newCells).setContent(Rock);
201 } else if (getContent(curCell) == HighOrderRock
202 && (getContent(downCell) == Rock || getContent(downCell) == HighOrderRock)
203 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
204 getCell(curCoord, newCells).setContent(Empty);
205 if (getContent(mine.getCell(rightAndDownCoord.down())) != Empty)
206 getCell(rightAndDownCoord, newCells).setContent(Lambda);
208 getCell(rightAndDownCoord, newCells).setContent(HighOrderRock);
209 } else if (getContent(curCell) == Rock
210 && (getContent(downCell) == Rock || getContent(downCell) == HighOrderRock)
211 && (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
212 && getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
213 getCell(curCoord, newCells).setContent(Empty);
214 getCell(leftAndDownCoord, newCells).setContent(Rock);
215 } else if (getContent(curCell) == HighOrderRock
216 && (getContent(downCell) == Rock || getContent(downCell) == HighOrderRock)
217 && (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
218 && getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
219 getCell(curCoord, newCells).setContent(Empty);
220 if (getContent(mine.getCell(leftAndDownCoord.down())) != Empty)
221 getCell(leftAndDownCoord, newCells).setContent(Lambda);
223 getCell(leftAndDownCoord, newCells).setContent(HighOrderRock);
224 } else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
225 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
226 getCell(curCoord, newCells).setContent(Empty);
227 getCell(rightAndDownCoord, newCells).setContent(Rock);
228 } else if (getContent(curCell) == HighOrderRock && getContent(downCell) == Lambda
229 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
230 getCell(curCoord, newCells).setContent(Empty);
231 if (getContent(mine.getCell(rightAndDownCoord.down())) != Empty)
232 getCell(rightAndDownCoord, newCells).setContent(Lambda);
234 getCell(rightAndDownCoord, newCells).setContent(HighOrderRock);
235 } else if (getContent(curCell) == ClosedLambdaLift
236 && mine.getLambdasAndHighOrderRocksStartCount() == lambdaCollectedCount) {
237 getCell(curCoord, newCells).setContent(OpenLambdaLift);
238 } else if (getContent(curCell) == WadlersBeard && mine.getGrowth() > 0
239 && route.size() % mine.getGrowth() == 0) {
240 if (getContent(leftCell) == Empty)
241 getCell(leftCoord, newCells).setContent(WadlersBeard);
242 if (getContent(upCell) == Empty)
243 getCell(upCoord, newCells).setContent(WadlersBeard);
244 if (getContent(rightCell) == Empty)
245 getCell(rightCoord, newCells).setContent(WadlersBeard);
246 if (getContent(downCell) == Empty)
247 getCell(downCoord, newCells).setContent(WadlersBeard);
248 if (getContent(leftAndUpCell) == Empty)
249 getCell(leftAndUpCoord, newCells).setContent(WadlersBeard);
250 if (getContent(rightAndUpCell) == Empty)
251 getCell(rightAndUpCoord, newCells).setContent(WadlersBeard);
252 if (getContent(leftAndDownCell) == Empty)
253 getCell(leftAndDownCoord, newCells).setContent(WadlersBeard);
254 if (getContent(rightAndDownCell) == Empty)
255 getCell(rightAndDownCoord, newCells).setContent(WadlersBeard);
260 // Other ending conditions
261 robotCoord = mine.getRobotCell().getCoordinate();
262 if (getContent(mine.getCell(robotCoord.up())) != Rock && getContent(getCell(robotCoord.up(), newCells)) == Rock
263 || getContent(mine.getCell(robotCoord.up())) != HighOrderRock
264 && getContent(getCell(robotCoord.up(), newCells)) == HighOrderRock
265 || getContent(mine.getCell(robotCoord.up())) == Empty
266 && getContent(getCell(robotCoord.up(), newCells)) == Lambda) {
267 mine.setCells(newCells);
269 lastScoreChange = score - prevScore;
270 state = GameState.Lose;
274 mine.setCells(newCells);
277 robotCoord = mine.getRobotCell().getCoordinate();
278 if (robotCoord.y <= mine.getWaterLevel()) {
283 if (underwater > mine.getRobotWaterproof()) {
284 lastScoreChange = score - prevScore;
285 state = GameState.Lose;
288 if (mine.getFlooding() > 0 && route.size() % mine.getFlooding() == 0) {
289 mine.setWaterLevel(mine.getWaterLevel() + 1);
292 lastScoreChange = score - prevScore;
293 state = GameState.Game;
297 public GameState move(List<Movement> movs) {
298 for (Movement mov : movs) {
299 if (move(mov) != GameState.Game)
305 public GameState move(String route) {
306 List<Movement> movs = new ArrayList<Movement>();
307 for (int i = 0; i < route.length(); i++) {
308 String s = "" + route.charAt(i);
309 for (Movement mov : Movement.values()) {
310 if (mov.toText().equals(s)) {
319 protected CellContent getContent(Cell cell) {
322 return cell.getContent();
325 public int getLastScoreChange() {
326 return lastScoreChange;
329 public int getScore() {
333 public GameState getState() {
337 public int getLambdaCollectedCount() {
338 return lambdaCollectedCount;
341 public List<Movement> getRoute() {
345 public String getStringRoute() {
347 for (Movement mov : route) {
348 movs += mov.toText();
353 public int getUnderwater() {
357 public boolean hasViciousCircle() {
358 if (route.size() != history.size())
359 throw new RuntimeException("Desynchronized history");
360 Map<Mine, Movement> state = new HashMap<Mine, Movement>();
361 for (int i = route.size() - 1; i >= 0; i--) {
362 Mine mine = history.get(i);
363 Movement mov = route.get(i);
364 if (state.get(mine) == mov)
366 state.put(mine, mov);
372 public String toString() {
373 return state.toString() + ", score = " + score + ", " + route.size() + " steps: "
374 + (route.size() > 100 ? getStringRoute().substring(0, 100) + "..." : getStringRoute()) + "\n\n" + mine;