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;
11 import java.util.ArrayList;
12 import java.util.List;
14 import ru.bosony.model.cellscontents.CellContent;
15 import ru.bosony.model.mine.Cell;
16 import ru.bosony.model.mine.Mine;
17 import ru.bosony.model.moving.Coordinate;
18 import ru.bosony.model.moving.Movement;
21 * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
27 protected Mine mine = null;
28 protected int score = 0;
29 protected int lastScoreChange = 0;
30 protected GameState state = GameState.Game;
31 protected int lambdaCollectedCount = 0;
32 protected List<Movement> route = new ArrayList<Movement>();
33 protected int underwater = 0;
35 public Game(Mine mine) {
39 public Mine getMine() {
43 protected Cell getCell(Coordinate coordinate, Cell[][] cells) {
44 if (coordinate == null)
50 if (x < 1 || x > mine.getSizeX() || y < 1 || y > mine.getSizeY())
52 return cells[x - 1][mine.getSizeY() - y];
55 protected void moveRobot(Cell nextRobotCell) {
56 mine.getRobotCell().setContent(Empty);
57 nextRobotCell.setContent(MiningRobot);
60 public GameState move(Movement mov) {
61 if (state != GameState.Game)
62 throw new RuntimeException("Game over!");
65 int prevScore = score;
69 Coordinate robotCoord = mine.getRobotCell().getCoordinate();
70 Coordinate nextRobotCoord = null;
73 score += 25 * lambdaCollectedCount;
75 lastScoreChange = score - prevScore;
76 state = GameState.Abort;
81 nextRobotCoord = robotCoord.left();
84 nextRobotCoord = robotCoord.up();
87 nextRobotCoord = robotCoord.right();
90 nextRobotCoord = robotCoord.down();
94 Cell nextRobotCell = mine.getCell(nextRobotCoord);
95 CellContent nextRobotCellContent = getContent(nextRobotCell);
96 if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) {
97 moveRobot(nextRobotCell);
98 } else if (nextRobotCellContent == Lambda) {
99 moveRobot(nextRobotCell);
100 lambdaCollectedCount++;
102 } else if (nextRobotCellContent == OpenLambdaLift) {
103 moveRobot(nextRobotCell);
104 score += 50 * lambdaCollectedCount;
106 lastScoreChange = score - prevScore;
107 state = GameState.Win;
109 } else if (robotCoord.right().equals(nextRobotCoord) && nextRobotCellContent == Rock
110 && getContent(mine.getCell(nextRobotCoord.right())) == Empty) {
111 moveRobot(nextRobotCell);
112 mine.getCell(nextRobotCoord.right()).setContent(Rock);
113 } else if (robotCoord.left().equals(nextRobotCoord) && nextRobotCellContent == Rock
114 && getContent(mine.getCell(nextRobotCoord.left())) == Empty) {
115 moveRobot(nextRobotCell);
116 mine.getCell(nextRobotCoord.left()).setContent(Rock);
117 } else if (CellContent.getTrampolines().contains(nextRobotCellContent)
118 && CellContent.getTargets().contains(nextRobotCellContent.getTrampolineTarget())) {
119 CellContent trampoline = nextRobotCellContent;
120 CellContent target = trampoline.getTrampolineTarget();
121 for (Cell targetCell : mine.findCells(target)) {
122 moveRobot(targetCell);
124 for (CellContent tramp : CellContent.getTrampolines()) {
125 if (tramp.getTrampolineTarget() == target) {
126 for (Cell trampolineCell : mine.findCells(tramp)) {
127 trampolineCell.setContent(Empty);
136 Cell[][] newCells = mine.cloneCells();
137 for (int y = 1; y <= mine.getSizeY(); y++) {
138 for (int x = 1; x <= mine.getSizeX(); x++) {
139 Coordinate curCoord = new Coordinate(x, y);
140 Coordinate leftCoord = curCoord.left();
141 Coordinate upCoord = curCoord.up();
142 Coordinate rightCoord = curCoord.right();
143 Coordinate downCoord = curCoord.down();
144 Coordinate leftAndUpCoord = curCoord.leftAndUp();
145 Coordinate rightAndUpCoord = curCoord.rightAndUp();
146 Coordinate rightAndDownCoord = curCoord.rightAndDown();
147 Coordinate leftAndDownCoord = curCoord.leftAndDown();
148 Cell curCell = mine.getCell(curCoord);
149 Cell leftCell = mine.getCell(leftCoord);
150 Cell upCell = mine.getCell(upCoord);
151 Cell rightCell = mine.getCell(rightCoord);
152 Cell downCell = mine.getCell(downCoord);
153 Cell leftAndUpCell = mine.getCell(leftAndUpCoord);
154 Cell rightAndUpCell = mine.getCell(rightAndUpCoord);
155 Cell rightAndDownCell = mine.getCell(rightAndDownCoord);
156 Cell leftAndDownCell = mine.getCell(leftAndDownCoord);
158 if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
159 getCell(curCoord, newCells).setContent(Empty);
160 getCell(downCoord, newCells).setContent(Rock);
161 } else if (getContent(curCell) == Rock && getContent(downCell) == Rock
162 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
163 getCell(curCoord, newCells).setContent(Empty);
164 getCell(rightAndDownCoord, newCells).setContent(Rock);
165 } else if (getContent(curCell) == Rock && getContent(downCell) == Rock
166 && (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
167 && getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
168 getCell(curCoord, newCells).setContent(Empty);
169 getCell(leftAndDownCoord, newCells).setContent(Rock);
170 } else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
171 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
172 getCell(curCoord, newCells).setContent(Empty);
173 getCell(rightAndDownCoord, newCells).setContent(Rock);
174 } else if (getContent(curCell) == ClosedLambdaLift && mine.findCells(Lambda).size() == 0) {
175 getCell(curCoord, newCells).setContent(OpenLambdaLift);
180 // Other ending conditions
181 robotCoord = mine.getRobotCell().getCoordinate();
182 if (getContent(mine.getCell(robotCoord.up())) != Rock && getContent(getCell(robotCoord.up(), newCells)) == Rock) {
183 mine.setCells(newCells);
185 lastScoreChange = score - prevScore;
186 state = GameState.Lose;
190 mine.setCells(newCells);
193 robotCoord = mine.getRobotCell().getCoordinate();
194 if (robotCoord.y <= mine.getWaterLevel()) {
199 if (underwater > mine.getRobotWaterproof()) {
200 lastScoreChange = score - prevScore;
201 state = GameState.Lose;
204 if (mine.getFlooding() > 0 && route.size() % mine.getFlooding() == 0) {
205 mine.setWaterLevel(mine.getWaterLevel() + 1);
208 lastScoreChange = score - prevScore;
209 state = GameState.Game;
213 public GameState move(List<Movement> movs) {
214 for (Movement mov : movs) {
215 if (move(mov) != GameState.Game)
221 public GameState move(String route) {
222 List<Movement> movs = new ArrayList<Movement>();
223 for (int i = 0; i < route.length(); i++) {
224 String s = "" + route.charAt(i);
225 for (Movement mov : Movement.values()) {
226 if (mov.toText().equals(s)) {
235 protected CellContent getContent(Cell cell) {
238 return cell.getContent();
241 public int getLastScoreChange() {
242 return lastScoreChange;
245 public int getScore() {
249 public GameState getState() {
253 public int getLambdaCollectedCount() {
254 return lambdaCollectedCount;
257 public List<Movement> getRoute() {
261 public String getStringRoute() {
263 for (Movement mov : route) {
264 movs += mov.toText();
269 public int getUnderwater() {