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.Lambda;
7 import static ru.bosony.model.cellscontents.CellContent.MiningRobot;
8 import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift;
9 import static ru.bosony.model.cellscontents.CellContent.Rock;
10 import static ru.bosony.model.cellscontents.CellContent.HuttonsRazor;
11 import static ru.bosony.model.cellscontents.CellContent.WadlersBeard;
13 import java.util.ArrayList;
14 import java.util.List;
16 import ru.bosony.model.cellscontents.CellContent;
17 import ru.bosony.model.mine.Cell;
18 import ru.bosony.model.mine.Mine;
19 import ru.bosony.model.moving.Coordinate;
20 import ru.bosony.model.moving.Movement;
23 * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
29 protected Mine mine = null;
30 protected int score = 0;
31 protected int lastScoreChange = 0;
32 protected GameState state = GameState.Game;
33 protected int lambdaCollectedCount = 0;
34 protected List<Movement> route = new ArrayList<Movement>();
35 protected int underwater = 0;
37 public Game(Mine mine) {
41 public Mine getMine() {
45 protected Cell getCell(Coordinate coordinate, Cell[][] cells) {
46 if (coordinate == null)
52 if (x < 1 || x > mine.getSizeX() || y < 1 || y > mine.getSizeY())
54 return cells[x - 1][mine.getSizeY() - y];
57 protected void moveRobot(Cell nextRobotCell) {
58 mine.getRobotCell().setContent(Empty);
59 nextRobotCell.setContent(MiningRobot);
62 public GameState move(Movement mov) {
63 if (state != GameState.Game)
64 throw new RuntimeException("Game over!");
67 int prevScore = score;
71 Cell robotCell = mine.getRobotCell();
72 Coordinate robotCoord = robotCell.getCoordinate();
73 Coordinate nextRobotCoord = null;
76 score += 25 * lambdaCollectedCount;
78 lastScoreChange = score - prevScore;
79 state = GameState.Abort;
84 if (mine.getRazorsCount() > 0) {
85 for (Cell cell : mine.getNeighboringCells(robotCell)) {
86 if (cell.getContent() == WadlersBeard) {
87 cell.setContent(Empty);
90 mine.setRazorsCount(mine.getRazorsCount() - 1);
94 nextRobotCoord = robotCoord.left();
97 nextRobotCoord = robotCoord.up();
100 nextRobotCoord = robotCoord.right();
103 nextRobotCoord = robotCoord.down();
107 Cell nextRobotCell = mine.getCell(nextRobotCoord);
108 CellContent nextRobotCellContent = getContent(nextRobotCell);
109 if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) {
110 moveRobot(nextRobotCell);
111 } else if (nextRobotCellContent == Lambda) {
112 moveRobot(nextRobotCell);
113 lambdaCollectedCount++;
115 } else if (nextRobotCellContent == OpenLambdaLift) {
116 moveRobot(nextRobotCell);
117 score += 50 * lambdaCollectedCount;
119 lastScoreChange = score - prevScore;
120 state = GameState.Win;
122 } else if (nextRobotCellContent == HuttonsRazor) {
123 moveRobot(nextRobotCell);
124 mine.setRazorsCount(mine.getRazorsCount() + 1);
125 } else if (robotCoord.right().equals(nextRobotCoord) && nextRobotCellContent == Rock
126 && getContent(mine.getCell(nextRobotCoord.right())) == Empty) {
127 moveRobot(nextRobotCell);
128 mine.getCell(nextRobotCoord.right()).setContent(Rock);
129 } else if (robotCoord.left().equals(nextRobotCoord) && nextRobotCellContent == Rock
130 && getContent(mine.getCell(nextRobotCoord.left())) == Empty) {
131 moveRobot(nextRobotCell);
132 mine.getCell(nextRobotCoord.left()).setContent(Rock);
133 } else if (CellContent.getTrampolines().contains(nextRobotCellContent)
134 && CellContent.getTargets().contains(nextRobotCellContent.getTrampolineTarget())) {
135 CellContent trampoline = nextRobotCellContent;
136 CellContent target = trampoline.getTrampolineTarget();
137 for (Cell targetCell : mine.findCells(target)) {
138 moveRobot(targetCell);
140 for (CellContent tramp : CellContent.getTrampolines()) {
141 if (tramp.getTrampolineTarget() == target) {
142 for (Cell trampolineCell : mine.findCells(tramp)) {
143 trampolineCell.setContent(Empty);
152 Cell[][] newCells = mine.cloneCells();
153 for (int y = 1; y <= mine.getSizeY(); y++) {
154 for (int x = 1; x <= mine.getSizeX(); x++) {
155 Coordinate curCoord = new Coordinate(x, y);
156 Coordinate leftCoord = curCoord.left();
157 Coordinate upCoord = curCoord.up();
158 Coordinate rightCoord = curCoord.right();
159 Coordinate downCoord = curCoord.down();
160 Coordinate leftAndUpCoord = curCoord.leftAndUp();
161 Coordinate rightAndUpCoord = curCoord.rightAndUp();
162 Coordinate rightAndDownCoord = curCoord.rightAndDown();
163 Coordinate leftAndDownCoord = curCoord.leftAndDown();
164 Cell curCell = mine.getCell(curCoord);
165 Cell leftCell = mine.getCell(leftCoord);
166 Cell upCell = mine.getCell(upCoord);
167 Cell rightCell = mine.getCell(rightCoord);
168 Cell downCell = mine.getCell(downCoord);
169 Cell leftAndUpCell = mine.getCell(leftAndUpCoord);
170 Cell rightAndUpCell = mine.getCell(rightAndUpCoord);
171 Cell rightAndDownCell = mine.getCell(rightAndDownCoord);
172 Cell leftAndDownCell = mine.getCell(leftAndDownCoord);
174 if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
175 getCell(curCoord, newCells).setContent(Empty);
176 getCell(downCoord, newCells).setContent(Rock);
177 } else if (getContent(curCell) == Rock && getContent(downCell) == Rock
178 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
179 getCell(curCoord, newCells).setContent(Empty);
180 getCell(rightAndDownCoord, newCells).setContent(Rock);
181 } else if (getContent(curCell) == Rock && getContent(downCell) == Rock
182 && (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
183 && getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
184 getCell(curCoord, newCells).setContent(Empty);
185 getCell(leftAndDownCoord, newCells).setContent(Rock);
186 } else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
187 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
188 getCell(curCoord, newCells).setContent(Empty);
189 getCell(rightAndDownCoord, newCells).setContent(Rock);
190 } else if (getContent(curCell) == ClosedLambdaLift && mine.findCells(Lambda).size() == 0) {
191 getCell(curCoord, newCells).setContent(OpenLambdaLift);
192 } else if (getContent(curCell) == WadlersBeard && mine.getGrowth() > 0
193 && route.size() % mine.getGrowth() == 0) {
194 if (getContent(leftCell) == Empty)
195 getCell(leftCoord, newCells).setContent(WadlersBeard);
196 if (getContent(upCell) == Empty)
197 getCell(upCoord, newCells).setContent(WadlersBeard);
198 if (getContent(rightCell) == Empty)
199 getCell(rightCoord, newCells).setContent(WadlersBeard);
200 if (getContent(downCell) == Empty)
201 getCell(downCoord, newCells).setContent(WadlersBeard);
202 if (getContent(leftAndUpCell) == Empty)
203 getCell(leftAndUpCoord, newCells).setContent(WadlersBeard);
204 if (getContent(rightAndUpCell) == Empty)
205 getCell(rightAndUpCoord, newCells).setContent(WadlersBeard);
206 if (getContent(leftAndDownCell) == Empty)
207 getCell(leftAndDownCoord, newCells).setContent(WadlersBeard);
208 if (getContent(rightAndDownCell) == Empty)
209 getCell(rightAndDownCoord, newCells).setContent(WadlersBeard);
214 // Other ending conditions
215 robotCoord = mine.getRobotCell().getCoordinate();
216 if (getContent(mine.getCell(robotCoord.up())) != Rock && getContent(getCell(robotCoord.up(), newCells)) == Rock) {
217 mine.setCells(newCells);
219 lastScoreChange = score - prevScore;
220 state = GameState.Lose;
224 mine.setCells(newCells);
227 robotCoord = mine.getRobotCell().getCoordinate();
228 if (robotCoord.y <= mine.getWaterLevel()) {
233 if (underwater > mine.getRobotWaterproof()) {
234 lastScoreChange = score - prevScore;
235 state = GameState.Lose;
238 if (mine.getFlooding() > 0 && route.size() % mine.getFlooding() == 0) {
239 mine.setWaterLevel(mine.getWaterLevel() + 1);
242 lastScoreChange = score - prevScore;
243 state = GameState.Game;
247 public GameState move(List<Movement> movs) {
248 for (Movement mov : movs) {
249 if (move(mov) != GameState.Game)
255 public GameState move(String route) {
256 List<Movement> movs = new ArrayList<Movement>();
257 for (int i = 0; i < route.length(); i++) {
258 String s = "" + route.charAt(i);
259 for (Movement mov : Movement.values()) {
260 if (mov.toText().equals(s)) {
269 protected CellContent getContent(Cell cell) {
272 return cell.getContent();
275 public int getLastScoreChange() {
276 return lastScoreChange;
279 public int getScore() {
283 public GameState getState() {
287 public int getLambdaCollectedCount() {
288 return lambdaCollectedCount;
291 public List<Movement> getRoute() {
295 public String getStringRoute() {
297 for (Movement mov : route) {
298 movs += mov.toText();
303 public int getUnderwater() {