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;
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);
58 mine.setRobotCell(nextRobotCell);
61 public GameState move(Movement mov) {
62 if (state != GameState.Game)
63 throw new RuntimeException("Game over!");
66 int prevScore = score;
70 Coordinate nextRobotCoord = null;
73 score += 25 * lambdaCollectedCount;
75 lastScoreChange = score - prevScore;
76 state = GameState.Abort;
81 nextRobotCoord = mine.getRobotCell().getCoordinate().left();
84 nextRobotCoord = mine.getRobotCell().getCoordinate().up();
87 nextRobotCoord = mine.getRobotCell().getCoordinate().right();
90 nextRobotCoord = mine.getRobotCell().getCoordinate().down();
93 Cell nextRobotCell = mine.getCell(nextRobotCoord);
94 CellContent nextRobotCellContent = getContent(nextRobotCell);
95 if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) {
96 moveRobot(nextRobotCell);
97 } else if (nextRobotCellContent == Lambda) {
98 moveRobot(nextRobotCell);
99 lambdaCollectedCount++;
101 } else if (nextRobotCellContent == OpenLambdaLift) {
102 moveRobot(nextRobotCell);
103 score += 50 * lambdaCollectedCount;
105 lastScoreChange = score - prevScore;
106 state = GameState.Win;
108 } else if (mine.getRobotCell().getCoordinate().right() == nextRobotCoord && nextRobotCellContent == Rock
109 && getContent(mine.getCell(nextRobotCoord.right())) == Empty) {
110 moveRobot(nextRobotCell);
111 mine.getCell(nextRobotCoord.right()).setContent(Rock);
112 } else if (mine.getRobotCell().getCoordinate().left() == nextRobotCoord && nextRobotCellContent == Rock
113 && getContent(mine.getCell(nextRobotCoord.left())) == Empty) {
114 moveRobot(nextRobotCell);
115 mine.getCell(nextRobotCoord.left()).setContent(Rock);
120 // Other ending conditions
121 if (getContent(getCell(mine.getRobotCell().getCoordinate().up(), prevStepCells)) != Rock
122 && getContent(mine.getCell(mine.getRobotCell().getCoordinate().up())) == Rock) {
123 lastScoreChange = score - prevScore;
124 state = GameState.Lose;
129 Cell[][] newCells = Arrays.copyOf(mine.getCells(), mine.getCells().length);
130 for (int y = 1; y <= mine.getSizeY(); y++) {
131 for (int x = 1; x <= mine.getSizeX(); x++) {
132 Coordinate curCoord = new Coordinate(x, y);
133 Coordinate leftCoord = curCoord.left();
134 Coordinate upCoord = curCoord.up();
135 Coordinate rightCoord = curCoord.right();
136 Coordinate downCoord = curCoord.down();
137 Coordinate leftAndUpCoord = curCoord.leftAndUp();
138 Coordinate rightAndUpCoord = curCoord.rightAndUp();
139 Coordinate rightAndDownCoord = curCoord.rightAndDown();
140 Coordinate leftAndDownCoord = curCoord.leftAndDown();
141 Cell curCell = mine.getCell(curCoord);
142 Cell leftCell = mine.getCell(leftCoord);
143 Cell upCell = mine.getCell(upCoord);
144 Cell rightCell = mine.getCell(rightCoord);
145 Cell downCell = mine.getCell(downCoord);
146 Cell leftAndUpCell = mine.getCell(leftAndUpCoord);
147 Cell rightAndUpCell = mine.getCell(rightAndUpCoord);
148 Cell rightAndDownCell = mine.getCell(rightAndDownCoord);
149 Cell leftAndDownCell = mine.getCell(leftAndDownCoord);
151 if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
152 getCell(curCoord, newCells).setContent(Empty);
153 getCell(downCoord, newCells).setContent(Rock);
154 } else if (getContent(curCell) == Rock && getContent(downCell) == Empty
155 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
156 getCell(curCoord, newCells).setContent(Empty);
157 getCell(rightAndDownCoord, newCells).setContent(Rock);
158 } else if (getContent(curCell) == Rock && getContent(downCell) == Rock
159 && (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
160 && getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
161 getCell(curCoord, newCells).setContent(Empty);
162 getCell(leftAndDownCoord, newCells).setContent(Rock);
163 } else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
164 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
165 getCell(curCoord, newCells).setContent(Empty);
166 getCell(rightAndDownCoord, newCells).setContent(Rock);
167 } else if (getContent(curCell) == ClosedLambdaLift && mine.findCells(Lambda).size() == 0) {
168 getCell(curCoord, newCells).setContent(OpenLambdaLift);
172 prevStepCells = mine.getCells();
173 mine.setCells(newCells);
175 lastScoreChange = score - prevScore;
176 state = GameState.Game;
180 public GameState move(List<Movement> movs) {
181 for (Movement mov : movs) {
182 if (move(mov) != GameState.Game)
188 public GameState move(String route) {
189 List<Movement> movs = new ArrayList<Movement>();
190 for (int i = 0; i < route.length(); i++) {
191 String s = "" + route.charAt(i);
192 for (Movement mov : Movement.values()) {
193 if (mov.toText().equals(s)) {
202 protected CellContent getContent(Cell cell) {
205 return cell.getContent();
208 public int getLastScoreChange() {
209 return lastScoreChange;
212 public int getScore() {
216 public GameState getState() {
220 public int getLambdaCollectedCount() {
221 return lambdaCollectedCount;