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);
122 Cell[][] newCells = mine.cloneCells();
123 for (int y = 1; y <= mine.getSizeY(); y++) {
124 for (int x = 1; x <= mine.getSizeX(); x++) {
125 Coordinate curCoord = new Coordinate(x, y);
126 Coordinate leftCoord = curCoord.left();
127 Coordinate upCoord = curCoord.up();
128 Coordinate rightCoord = curCoord.right();
129 Coordinate downCoord = curCoord.down();
130 Coordinate leftAndUpCoord = curCoord.leftAndUp();
131 Coordinate rightAndUpCoord = curCoord.rightAndUp();
132 Coordinate rightAndDownCoord = curCoord.rightAndDown();
133 Coordinate leftAndDownCoord = curCoord.leftAndDown();
134 Cell curCell = mine.getCell(curCoord);
135 Cell leftCell = mine.getCell(leftCoord);
136 Cell upCell = mine.getCell(upCoord);
137 Cell rightCell = mine.getCell(rightCoord);
138 Cell downCell = mine.getCell(downCoord);
139 Cell leftAndUpCell = mine.getCell(leftAndUpCoord);
140 Cell rightAndUpCell = mine.getCell(rightAndUpCoord);
141 Cell rightAndDownCell = mine.getCell(rightAndDownCoord);
142 Cell leftAndDownCell = mine.getCell(leftAndDownCoord);
144 if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
145 getCell(curCoord, newCells).setContent(Empty);
146 getCell(downCoord, newCells).setContent(Rock);
147 } else if (getContent(curCell) == Rock && getContent(downCell) == Empty
148 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
149 getCell(curCoord, newCells).setContent(Empty);
150 getCell(rightAndDownCoord, newCells).setContent(Rock);
151 } else if (getContent(curCell) == Rock && getContent(downCell) == Rock
152 && (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
153 && getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
154 getCell(curCoord, newCells).setContent(Empty);
155 getCell(leftAndDownCoord, newCells).setContent(Rock);
156 } else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
157 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
158 getCell(curCoord, newCells).setContent(Empty);
159 getCell(rightAndDownCoord, newCells).setContent(Rock);
160 } else if (getContent(curCell) == ClosedLambdaLift && mine.findCells(Lambda).size() == 0) {
161 getCell(curCoord, newCells).setContent(OpenLambdaLift);
166 // Other ending conditions
167 robotCoord = mine.getRobotCell().getCoordinate();
168 if (getContent(mine.getCell(robotCoord.up())) != Rock && getContent(getCell(robotCoord.up(), newCells)) == Rock) {
169 mine.setCells(newCells);
171 lastScoreChange = score - prevScore;
172 state = GameState.Lose;
176 mine.setCells(newCells);
179 robotCoord = mine.getRobotCell().getCoordinate();
180 if (robotCoord.y <= mine.getWaterLevel()) {
185 if (underwater > mine.getRobotWaterproof()) {
186 lastScoreChange = score - prevScore;
187 state = GameState.Lose;
190 if (mine.getFlooding() > 0 && route.size() % mine.getFlooding() == 0) {
191 mine.setWaterLevel(mine.getWaterLevel() + 1);
194 lastScoreChange = score - prevScore;
195 state = GameState.Game;
199 public GameState move(List<Movement> movs) {
200 for (Movement mov : movs) {
201 if (move(mov) != GameState.Game)
207 public GameState move(String route) {
208 List<Movement> movs = new ArrayList<Movement>();
209 for (int i = 0; i < route.length(); i++) {
210 String s = "" + route.charAt(i);
211 for (Movement mov : Movement.values()) {
212 if (mov.toText().equals(s)) {
221 protected CellContent getContent(Cell cell) {
224 return cell.getContent();
227 public int getLastScoreChange() {
228 return lastScoreChange;
231 public int getScore() {
235 public GameState getState() {
239 public int getLambdaCollectedCount() {
240 return lambdaCollectedCount;
243 public List<Movement> getRoute() {
247 public String getStringRoute() {
249 for (Movement mov : route) {
250 movs += mov.toText();
255 public int getUnderwater() {