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.List;
17 import ru.bosony.model.cellscontents.CellContent;
18 import ru.bosony.model.mine.Cell;
19 import ru.bosony.model.mine.Mine;
20 import ru.bosony.model.moving.Coordinate;
21 import ru.bosony.model.moving.Movement;
24 * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
30 protected Mine mine = null;
31 protected int score = 0;
32 protected int lastScoreChange = 0;
33 protected GameState state = GameState.Game;
34 protected int lambdaCollectedCount = 0;
35 protected List<Movement> route = new ArrayList<Movement>();
36 protected int underwater = 0;
38 public Game(Mine mine) {
42 public Mine getMine() {
46 protected Cell getCell(Coordinate coordinate, Cell[][] cells) {
47 if (coordinate == null)
53 if (x < 1 || x > mine.getSizeX() || y < 1 || y > mine.getSizeY())
55 return cells[x - 1][mine.getSizeY() - y];
58 protected void moveRobot(Cell nextRobotCell) {
59 mine.getRobotCell().setContent(Empty);
60 nextRobotCell.setContent(MiningRobot);
63 public GameState move(Movement mov) {
64 if (state != GameState.Game)
65 throw new RuntimeException("Game over!");
68 int prevScore = score;
72 Cell robotCell = mine.getRobotCell();
73 Coordinate robotCoord = robotCell.getCoordinate();
74 Coordinate nextRobotCoord = null;
77 score += 25 * lambdaCollectedCount;
79 lastScoreChange = score - prevScore;
80 state = GameState.Abort;
85 if (mine.getRazorsCount() > 0) {
86 for (Cell cell : mine.getNeighboringCells(robotCell)) {
87 if (cell.getContent() == WadlersBeard) {
88 cell.setContent(Empty);
91 mine.setRazorsCount(mine.getRazorsCount() - 1);
95 nextRobotCoord = robotCoord.left();
98 nextRobotCoord = robotCoord.up();
101 nextRobotCoord = robotCoord.right();
104 nextRobotCoord = robotCoord.down();
108 Cell nextRobotCell = mine.getCell(nextRobotCoord);
109 CellContent nextRobotCellContent = getContent(nextRobotCell);
110 if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) {
111 moveRobot(nextRobotCell);
112 } else if (nextRobotCellContent == Lambda) {
113 moveRobot(nextRobotCell);
114 lambdaCollectedCount++;
116 } else if (nextRobotCellContent == OpenLambdaLift) {
117 moveRobot(nextRobotCell);
118 score += 50 * lambdaCollectedCount;
120 lastScoreChange = score - prevScore;
121 state = GameState.Win;
123 } else if (nextRobotCellContent == HuttonsRazor) {
124 moveRobot(nextRobotCell);
125 mine.setRazorsCount(mine.getRazorsCount() + 1);
126 } else if (robotCoord.right().equals(nextRobotCoord) && nextRobotCellContent == Rock
127 && getContent(mine.getCell(nextRobotCoord.right())) == Empty) {
128 moveRobot(nextRobotCell);
129 mine.getCell(nextRobotCoord.right()).setContent(Rock);
130 } else if (robotCoord.left().equals(nextRobotCoord) && nextRobotCellContent == Rock
131 && getContent(mine.getCell(nextRobotCoord.left())) == Empty) {
132 moveRobot(nextRobotCell);
133 mine.getCell(nextRobotCoord.left()).setContent(Rock);
134 } else if (robotCoord.right().equals(nextRobotCoord) && nextRobotCellContent == HighOrderRock
135 && getContent(mine.getCell(nextRobotCoord.right())) == Empty) {
136 moveRobot(nextRobotCell);
137 mine.getCell(nextRobotCoord.right()).setContent(HighOrderRock);
138 } else if (robotCoord.left().equals(nextRobotCoord) && nextRobotCellContent == HighOrderRock
139 && getContent(mine.getCell(nextRobotCoord.left())) == Empty) {
140 moveRobot(nextRobotCell);
141 mine.getCell(nextRobotCoord.left()).setContent(HighOrderRock);
142 } else if (CellContent.getTrampolines().contains(nextRobotCellContent)
143 && CellContent.getTargets().contains(nextRobotCellContent.getTrampolineTarget())) {
144 CellContent trampoline = nextRobotCellContent;
145 CellContent target = trampoline.getTrampolineTarget();
146 for (Cell targetCell : mine.findCells(target)) {
147 moveRobot(targetCell);
149 for (CellContent tramp : CellContent.getTrampolines()) {
150 if (tramp.getTrampolineTarget() == target) {
151 for (Cell trampolineCell : mine.findCells(tramp)) {
152 trampolineCell.setContent(Empty);
161 Cell[][] newCells = mine.cloneCells();
162 for (int y = 1; y <= mine.getSizeY(); y++) {
163 for (int x = 1; x <= mine.getSizeX(); x++) {
164 Coordinate curCoord = new Coordinate(x, y);
165 Coordinate leftCoord = curCoord.left();
166 Coordinate upCoord = curCoord.up();
167 Coordinate rightCoord = curCoord.right();
168 Coordinate downCoord = curCoord.down();
169 Coordinate leftAndUpCoord = curCoord.leftAndUp();
170 Coordinate rightAndUpCoord = curCoord.rightAndUp();
171 Coordinate rightAndDownCoord = curCoord.rightAndDown();
172 Coordinate leftAndDownCoord = curCoord.leftAndDown();
173 Cell curCell = mine.getCell(curCoord);
174 Cell leftCell = mine.getCell(leftCoord);
175 Cell upCell = mine.getCell(upCoord);
176 Cell rightCell = mine.getCell(rightCoord);
177 Cell downCell = mine.getCell(downCoord);
178 Cell leftAndUpCell = mine.getCell(leftAndUpCoord);
179 Cell rightAndUpCell = mine.getCell(rightAndUpCoord);
180 Cell rightAndDownCell = mine.getCell(rightAndDownCoord);
181 Cell leftAndDownCell = mine.getCell(leftAndDownCoord);
183 if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
184 getCell(curCoord, newCells).setContent(Empty);
185 getCell(downCoord, newCells).setContent(Rock);
186 } else if (getContent(curCell) == HighOrderRock && getContent(downCell) == Empty) {
187 getCell(curCoord, newCells).setContent(Empty);
188 if (getContent(mine.getCell(downCoord.down())) != Empty)
189 getCell(downCoord, newCells).setContent(Lambda);
191 getCell(downCoord, newCells).setContent(HighOrderRock);
192 } else if (getContent(curCell) == Rock
193 && (getContent(downCell) == Rock || getContent(downCell) == HighOrderRock)
194 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
195 getCell(curCoord, newCells).setContent(Empty);
196 getCell(rightAndDownCoord, newCells).setContent(Rock);
197 } else if (getContent(curCell) == HighOrderRock
198 && (getContent(downCell) == Rock || getContent(downCell) == HighOrderRock)
199 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
200 getCell(curCoord, newCells).setContent(Empty);
201 if (getContent(mine.getCell(rightAndDownCoord.down())) != Empty)
202 getCell(rightAndDownCoord, newCells).setContent(Lambda);
204 getCell(rightAndDownCoord, newCells).setContent(HighOrderRock);
205 } else if (getContent(curCell) == Rock
206 && (getContent(downCell) == Rock || getContent(downCell) == HighOrderRock)
207 && (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
208 && getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
209 getCell(curCoord, newCells).setContent(Empty);
210 getCell(leftAndDownCoord, newCells).setContent(Rock);
211 } else if (getContent(curCell) == HighOrderRock
212 && (getContent(downCell) == Rock || getContent(downCell) == HighOrderRock)
213 && (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
214 && getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
215 getCell(curCoord, newCells).setContent(Empty);
216 if (getContent(mine.getCell(leftAndDownCoord.down())) != Empty)
217 getCell(leftAndDownCoord, newCells).setContent(Lambda);
219 getCell(leftAndDownCoord, newCells).setContent(HighOrderRock);
220 } else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
221 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
222 getCell(curCoord, newCells).setContent(Empty);
223 getCell(rightAndDownCoord, newCells).setContent(Rock);
224 } else if (getContent(curCell) == HighOrderRock && getContent(downCell) == Lambda
225 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
226 getCell(curCoord, newCells).setContent(Empty);
227 if (getContent(mine.getCell(rightAndDownCoord.down())) != Empty)
228 getCell(rightAndDownCoord, newCells).setContent(Lambda);
230 getCell(rightAndDownCoord, newCells).setContent(HighOrderRock);
231 } else if (getContent(curCell) == ClosedLambdaLift
232 && mine.getLambdasAndHighOrderRocksStartCount() == lambdaCollectedCount) {
233 getCell(curCoord, newCells).setContent(OpenLambdaLift);
234 } else if (getContent(curCell) == WadlersBeard && mine.getGrowth() > 0
235 && route.size() % mine.getGrowth() == 0) {
236 if (getContent(leftCell) == Empty)
237 getCell(leftCoord, newCells).setContent(WadlersBeard);
238 if (getContent(upCell) == Empty)
239 getCell(upCoord, newCells).setContent(WadlersBeard);
240 if (getContent(rightCell) == Empty)
241 getCell(rightCoord, newCells).setContent(WadlersBeard);
242 if (getContent(downCell) == Empty)
243 getCell(downCoord, newCells).setContent(WadlersBeard);
244 if (getContent(leftAndUpCell) == Empty)
245 getCell(leftAndUpCoord, newCells).setContent(WadlersBeard);
246 if (getContent(rightAndUpCell) == Empty)
247 getCell(rightAndUpCoord, newCells).setContent(WadlersBeard);
248 if (getContent(leftAndDownCell) == Empty)
249 getCell(leftAndDownCoord, newCells).setContent(WadlersBeard);
250 if (getContent(rightAndDownCell) == Empty)
251 getCell(rightAndDownCoord, newCells).setContent(WadlersBeard);
256 // Other ending conditions
257 robotCoord = mine.getRobotCell().getCoordinate();
258 if (getContent(mine.getCell(robotCoord.up())) != Rock && getContent(getCell(robotCoord.up(), newCells)) == Rock
259 || getContent(mine.getCell(robotCoord.up())) != HighOrderRock
260 && getContent(getCell(robotCoord.up(), newCells)) == HighOrderRock
261 || getContent(mine.getCell(robotCoord.up())) == Empty
262 && getContent(getCell(robotCoord.up(), newCells)) == Lambda) {
263 mine.setCells(newCells);
265 lastScoreChange = score - prevScore;
266 state = GameState.Lose;
270 mine.setCells(newCells);
273 robotCoord = mine.getRobotCell().getCoordinate();
274 if (robotCoord.y <= mine.getWaterLevel()) {
279 if (underwater > mine.getRobotWaterproof()) {
280 lastScoreChange = score - prevScore;
281 state = GameState.Lose;
284 if (mine.getFlooding() > 0 && route.size() % mine.getFlooding() == 0) {
285 mine.setWaterLevel(mine.getWaterLevel() + 1);
288 lastScoreChange = score - prevScore;
289 state = GameState.Game;
293 public GameState move(List<Movement> movs) {
294 for (Movement mov : movs) {
295 if (move(mov) != GameState.Game)
301 public GameState move(String route) {
302 List<Movement> movs = new ArrayList<Movement>();
303 for (int i = 0; i < route.length(); i++) {
304 String s = "" + route.charAt(i);
305 for (Movement mov : Movement.values()) {
306 if (mov.toText().equals(s)) {
315 protected CellContent getContent(Cell cell) {
318 return cell.getContent();
321 public int getLastScoreChange() {
322 return lastScoreChange;
325 public int getScore() {
329 public GameState getState() {
333 public int getLambdaCollectedCount() {
334 return lambdaCollectedCount;
337 public List<Movement> getRoute() {
341 public String getStringRoute() {
343 for (Movement mov : route) {
344 movs += mov.toText();
349 public int getUnderwater() {