1 package ru.bosony.model.mine;
3 import java.util.Arrays;
4 import java.util.Collection;
5 import java.util.HashSet;
9 import org.hamcrest.CoreMatchers;
11 import ru.bosony.model.cellscontents.CellContent;
12 import static ru.bosony.model.cellscontents.CellContent.*;
13 import ru.bosony.model.io.TextRepresentable;
14 import ru.bosony.model.moving.Movement;
17 * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
21 public class Mine implements TextRepresentable {
23 private Cell[][] cells;
24 private Cell[][] prevStepCells;
27 protected Cell robotCell;
28 protected int score = 0;
29 protected int lastScoreChange = 0;
30 protected GameState state = GameState.Game;
31 protected int lambdaCollectedCount = 0;
33 public Mine(String text) {
35 if (findCells(OpenLambdaLift).size() != 0)
36 throw new RuntimeException("Detected OpenLambdaLift in new map!");
37 if (findCells(MiningRobot).size() != 1)
38 throw new RuntimeException("New map does not contain a robot!");
39 if (findCells(ClosedLambdaLift).size() != 1)
40 throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
44 public String toText() {
46 for (int y = 0; y < sizeY; y++) {
47 for (int x = 0; x < sizeX; x++) {
48 text += cells[x][y].getContent().toText();
52 return text.replaceAll("\n*$", "");
55 private void fromText(String text) {
56 String[] rows = text.split("\n+");
59 for (String row : rows) {
65 cells = new Cell[x][y];
66 for (int curY = 0; curY < rows.length; curY++) {
67 String row = rows[curY];
68 for (int curX = 0; curX < row.length() || curX < x; curX++) {
69 CellContent content = null;
70 if (curX < row.length())
71 content = CellContent.fromText("" + row.charAt(curX));
74 Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
75 cells[curX][curY] = cell;
76 if (content == MiningRobot)
82 public Cell getCell(Coordinate coordinate) {
83 return getCell(coordinate, cells);
86 protected Cell getCell(Coordinate coordinate, Cell[][] cells) {
87 if (coordinate == null)
93 if (x < 1 || x > sizeX || y < 1 || y > sizeY)
95 return cells[x - 1][sizeY - y];
99 public String toString() {
103 public int getSizeX() {
107 public int getSizeY() {
111 public Set<Movement> getAvailableRobotMovements() {
112 Set<Movement> movs = new HashSet<Movement>();
113 Set<Cell> nCells = getNeighboringCells(robotCell);
114 for (Cell nCell : nCells) {
115 CellContent nContent = nCell.getContent();
116 Coordinate nCoordinate = nCell.getCoordinate();
117 Cell rightNCell = getCell(nCoordinate.right());
118 Cell leftNCell = getCell(nCoordinate.left());
119 if (nContent == Empty
121 || nContent == Lambda
122 || nContent == OpenLambdaLift
123 || (nContent == Rock && robotCell.getCoordinate().right().equals(nCoordinate) && rightNCell != null && rightNCell
124 .getContent() == Empty)
125 || (nContent == Rock && robotCell.getCoordinate().left().equals(nCoordinate) && leftNCell != null && leftNCell
126 .getContent() == Empty)) {
127 Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate);
131 movs.add(Movement.ABORT);
132 movs.add(Movement.WAIT);
136 public Set<Cell> getNeighboringCells(Cell cell) {
137 Set<Cell> cells = new HashSet<Cell>();
138 for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates())
139 cells.add(getCell(coordinate));
143 protected void moveRobot(Cell nextRobotCell) {
144 robotCell.setContent(Empty);
145 nextRobotCell.setContent(MiningRobot);
146 robotCell = nextRobotCell;
149 public GameState move(Movement mov) {
150 if (state != GameState.Game)
151 throw new RuntimeException("Game over!");
154 int prevScore = score;
157 // Moving and scoring
158 Coordinate nextRobotCoord = null;
161 score += 25 * lambdaCollectedCount;
163 lastScoreChange = score - prevScore;
164 state = GameState.Abort;
169 nextRobotCoord = robotCell.coordinate.left();
172 nextRobotCoord = robotCell.coordinate.up();
175 nextRobotCoord = robotCell.coordinate.right();
178 nextRobotCoord = robotCell.coordinate.down();
181 Cell nextRobotCell = getCell(nextRobotCoord);
182 CellContent nextRobotCellContent = getContent(nextRobotCell);
183 if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) {
184 moveRobot(nextRobotCell);
185 } else if (nextRobotCellContent == Lambda) {
186 moveRobot(nextRobotCell);
187 lambdaCollectedCount++;
189 } else if (nextRobotCellContent == OpenLambdaLift) {
190 moveRobot(nextRobotCell);
191 score += 50 * lambdaCollectedCount;
193 lastScoreChange = score - prevScore;
194 state = GameState.Win;
196 } else if (robotCell.coordinate.right() == nextRobotCoord && nextRobotCellContent == Rock && getContent(getCell(nextRobotCoord.right())) == Empty) {
197 moveRobot(nextRobotCell);
198 getCell(nextRobotCoord.right()).setContent(Rock);
199 } else if (robotCell.coordinate.left() == nextRobotCoord && nextRobotCellContent == Rock && getContent(getCell(nextRobotCoord.left())) == Empty) {
200 moveRobot(nextRobotCell);
201 getCell(nextRobotCoord.left()).setContent(Rock);
206 // Other ending conditions
207 if (getContent(getCell(robotCell.coordinate.up(), prevStepCells)) != Rock
208 && getContent(getCell(robotCell.coordinate.up())) == Rock) {
209 lastScoreChange = score - prevScore;
210 state = GameState.Lose;
215 Cell[][] newCells = Arrays.copyOf(cells, cells.length);
216 for (int y = 1; y <= sizeY; y++) {
217 for (int x = 1; x <= sizeX; x++) {
218 Coordinate curCoord = new Coordinate(x, y);
219 Coordinate leftCoord = curCoord.left();
220 Coordinate upCoord = curCoord.up();
221 Coordinate rightCoord = curCoord.right();
222 Coordinate downCoord = curCoord.down();
223 Coordinate leftAndUpCoord = curCoord.leftAndUp();
224 Coordinate rightAndUpCoord = curCoord.rightAndUp();
225 Coordinate rightAndDownCoord = curCoord.rightAndDown();
226 Coordinate leftAndDownCoord = curCoord.leftAndDown();
227 Cell curCell = getCell(curCoord);
228 Cell leftCell = getCell(leftCoord);
229 Cell upCell = getCell(upCoord);
230 Cell rightCell = getCell(rightCoord);
231 Cell downCell = getCell(downCoord);
232 Cell leftAndUpCell = getCell(leftAndUpCoord);
233 Cell rightAndUpCell = getCell(rightAndUpCoord);
234 Cell rightAndDownCell = getCell(rightAndDownCoord);
235 Cell leftAndDownCell = getCell(leftAndDownCoord);
237 if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
238 getCell(curCoord, newCells).setContent(Empty);
239 getCell(downCoord, newCells).setContent(Rock);
240 } else if (getContent(curCell) == Rock && getContent(downCell) == Empty
241 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
242 getCell(curCoord, newCells).setContent(Empty);
243 getCell(rightAndDownCoord, newCells).setContent(Rock);
244 } else if (getContent(curCell) == Rock && getContent(downCell) == Rock
245 && (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
246 && getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
247 getCell(curCoord, newCells).setContent(Empty);
248 getCell(leftAndDownCoord, newCells).setContent(Rock);
249 } else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
250 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
251 getCell(curCoord, newCells).setContent(Empty);
252 getCell(rightAndDownCoord, newCells).setContent(Rock);
253 } else if (getContent(curCell) == ClosedLambdaLift && findCells(Lambda).size() == 0) {
254 getCell(curCoord, newCells).setContent(OpenLambdaLift);
258 prevStepCells = cells;
261 lastScoreChange = score - prevScore;
262 state = GameState.Game;
266 protected CellContent getContent(Cell cell) {
269 return cell.getContent();
272 public Collection<Cell> findCells(CellContent content) {
273 Collection<Cell> finded = new HashSet<Cell>();
274 for (int y = sizeY - 1; y >= 0; y--) {
275 for (int x = 0; x < sizeX - 1; x++) {
276 if (cells[x][y].getContent() == content)
277 finded.add(cells[x][y]);
283 public int getLastScoreChange() {
284 return lastScoreChange;