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.Arrays;
13 import java.util.List;
15 import ru.bosony.model.cellscontents.CellContent;
16 import ru.bosony.model.mine.Cell;
17 import ru.bosony.model.mine.Coordinate;
18 import ru.bosony.model.mine.Mine;
19 import ru.bosony.model.moving.Movement;
22 * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
29 private Cell[][] prevStepCells;
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>();
36 public Game(Mine mine) {
40 public Mine getMine() {
44 protected Cell getCell(Coordinate coordinate, Cell[][] cells) {
45 if (coordinate == null)
51 if (x < 1 || x > mine.getSizeX() || y < 1 || y > mine.getSizeY())
53 return cells[x - 1][mine.getSizeY() - y];
56 protected void moveRobot(Cell nextRobotCell) {
57 mine.getRobotCell().setContent(Empty);
58 nextRobotCell.setContent(MiningRobot);
59 mine.setRobotCell(nextRobotCell);
62 public GameState move(Movement mov) {
63 if (state != GameState.Game)
64 throw new RuntimeException("Game over!");
67 int prevScore = score;
72 Coordinate nextRobotCoord = null;
75 score += 25 * lambdaCollectedCount;
77 lastScoreChange = score - prevScore;
78 state = GameState.Abort;
83 nextRobotCoord = mine.getRobotCell().getCoordinate().left();
86 nextRobotCoord = mine.getRobotCell().getCoordinate().up();
89 nextRobotCoord = mine.getRobotCell().getCoordinate().right();
92 nextRobotCoord = mine.getRobotCell().getCoordinate().down();
95 Cell nextRobotCell = mine.getCell(nextRobotCoord);
96 CellContent nextRobotCellContent = getContent(nextRobotCell);
97 if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) {
98 moveRobot(nextRobotCell);
99 } else if (nextRobotCellContent == Lambda) {
100 moveRobot(nextRobotCell);
101 lambdaCollectedCount++;
103 } else if (nextRobotCellContent == OpenLambdaLift) {
104 moveRobot(nextRobotCell);
105 score += 50 * lambdaCollectedCount;
107 lastScoreChange = score - prevScore;
108 state = GameState.Win;
110 } else if (mine.getRobotCell().getCoordinate().right() == nextRobotCoord && nextRobotCellContent == Rock
111 && getContent(mine.getCell(nextRobotCoord.right())) == Empty) {
112 moveRobot(nextRobotCell);
113 mine.getCell(nextRobotCoord.right()).setContent(Rock);
114 } else if (mine.getRobotCell().getCoordinate().left() == nextRobotCoord && nextRobotCellContent == Rock
115 && getContent(mine.getCell(nextRobotCoord.left())) == Empty) {
116 moveRobot(nextRobotCell);
117 mine.getCell(nextRobotCoord.left()).setContent(Rock);
122 // Other ending conditions
123 if (getContent(getCell(mine.getRobotCell().getCoordinate().up(), prevStepCells)) != Rock
124 && getContent(mine.getCell(mine.getRobotCell().getCoordinate().up())) == Rock) {
125 lastScoreChange = score - prevScore;
126 state = GameState.Lose;
131 Cell[][] newCells = Arrays.copyOf(mine.getCells(), mine.getCells().length);
132 for (int y = 1; y <= mine.getSizeY(); y++) {
133 for (int x = 1; x <= mine.getSizeX(); x++) {
134 Coordinate curCoord = new Coordinate(x, y);
135 Coordinate leftCoord = curCoord.left();
136 Coordinate upCoord = curCoord.up();
137 Coordinate rightCoord = curCoord.right();
138 Coordinate downCoord = curCoord.down();
139 Coordinate leftAndUpCoord = curCoord.leftAndUp();
140 Coordinate rightAndUpCoord = curCoord.rightAndUp();
141 Coordinate rightAndDownCoord = curCoord.rightAndDown();
142 Coordinate leftAndDownCoord = curCoord.leftAndDown();
143 Cell curCell = mine.getCell(curCoord);
144 Cell leftCell = mine.getCell(leftCoord);
145 Cell upCell = mine.getCell(upCoord);
146 Cell rightCell = mine.getCell(rightCoord);
147 Cell downCell = mine.getCell(downCoord);
148 Cell leftAndUpCell = mine.getCell(leftAndUpCoord);
149 Cell rightAndUpCell = mine.getCell(rightAndUpCoord);
150 Cell rightAndDownCell = mine.getCell(rightAndDownCoord);
151 Cell leftAndDownCell = mine.getCell(leftAndDownCoord);
153 if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
154 getCell(curCoord, newCells).setContent(Empty);
155 getCell(downCoord, newCells).setContent(Rock);
156 } else if (getContent(curCell) == Rock && getContent(downCell) == Empty
157 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
158 getCell(curCoord, newCells).setContent(Empty);
159 getCell(rightAndDownCoord, newCells).setContent(Rock);
160 } else if (getContent(curCell) == Rock && getContent(downCell) == Rock
161 && (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
162 && getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
163 getCell(curCoord, newCells).setContent(Empty);
164 getCell(leftAndDownCoord, newCells).setContent(Rock);
165 } else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
166 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
167 getCell(curCoord, newCells).setContent(Empty);
168 getCell(rightAndDownCoord, newCells).setContent(Rock);
169 } else if (getContent(curCell) == ClosedLambdaLift && mine.findCells(Lambda).size() == 0) {
170 getCell(curCoord, newCells).setContent(OpenLambdaLift);
174 prevStepCells = mine.getCells();
175 mine.setCells(newCells);
177 lastScoreChange = score - prevScore;
178 state = GameState.Game;
182 public GameState move(List<Movement> movs) {
183 for (Movement mov : movs) {
184 if (move(mov) != GameState.Game)
190 public GameState move(String route) {
191 List<Movement> movs = new ArrayList<Movement>();
192 for (int i = 0; i < route.length(); i++) {
193 String s = "" + route.charAt(i);
194 for (Movement mov : Movement.values()) {
195 if (mov.toText().equals(s)) {
204 protected CellContent getContent(Cell cell) {
207 return cell.getContent();
210 public int getLastScoreChange() {
211 return lastScoreChange;
214 public int getScore() {
218 public GameState getState() {
222 public int getLambdaCollectedCount() {
223 return lambdaCollectedCount;
226 public List<Movement> getRoute() {
230 public String getStringRoute() {
232 for (Movement mov : route) {
233 movs += mov.toText();