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.Arrays;
13 import ru.bosony.model.cellscontents.CellContent;
14 import ru.bosony.model.mine.Cell;
15 import ru.bosony.model.mine.Coordinate;
16 import ru.bosony.model.mine.Mine;
17 import ru.bosony.model.moving.Movement;
20 * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
27 private Cell[][] prevStepCells;
28 protected int score = 0;
29 protected int lastScoreChange = 0;
30 protected GameState state = GameState.Game;
31 protected int lambdaCollectedCount = 0;
33 public Game(Mine mine) {
37 public Mine getMine() {
41 protected Cell getCell(Coordinate coordinate, Cell[][] cells) {
42 if (coordinate == null)
48 if (x < 1 || x > mine.getSizeX() || y < 1 || y > mine.getSizeY())
50 return cells[x - 1][mine.getSizeY() - y];
53 protected void moveRobot(Cell nextRobotCell) {
54 mine.getRobotCell().setContent(Empty);
55 nextRobotCell.setContent(MiningRobot);
56 mine.setRobotCell(nextRobotCell);
59 public GameState move(Movement mov) {
60 if (state != GameState.Game)
61 throw new RuntimeException("Game over!");
64 int prevScore = score;
68 Coordinate nextRobotCoord = null;
71 score += 25 * lambdaCollectedCount;
73 lastScoreChange = score - prevScore;
74 state = GameState.Abort;
79 nextRobotCoord = mine.getRobotCell().getCoordinate().left();
82 nextRobotCoord = mine.getRobotCell().getCoordinate().up();
85 nextRobotCoord = mine.getRobotCell().getCoordinate().right();
88 nextRobotCoord = mine.getRobotCell().getCoordinate().down();
91 Cell nextRobotCell = mine.getCell(nextRobotCoord);
92 CellContent nextRobotCellContent = getContent(nextRobotCell);
93 if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) {
94 moveRobot(nextRobotCell);
95 } else if (nextRobotCellContent == Lambda) {
96 moveRobot(nextRobotCell);
97 lambdaCollectedCount++;
99 } else if (nextRobotCellContent == OpenLambdaLift) {
100 moveRobot(nextRobotCell);
101 score += 50 * lambdaCollectedCount;
103 lastScoreChange = score - prevScore;
104 state = GameState.Win;
106 } else if (mine.getRobotCell().getCoordinate().right() == nextRobotCoord && nextRobotCellContent == Rock
107 && getContent(mine.getCell(nextRobotCoord.right())) == Empty) {
108 moveRobot(nextRobotCell);
109 mine.getCell(nextRobotCoord.right()).setContent(Rock);
110 } else if (mine.getRobotCell().getCoordinate().left() == nextRobotCoord && nextRobotCellContent == Rock
111 && getContent(mine.getCell(nextRobotCoord.left())) == Empty) {
112 moveRobot(nextRobotCell);
113 mine.getCell(nextRobotCoord.left()).setContent(Rock);
118 // Other ending conditions
119 if (getContent(getCell(mine.getRobotCell().getCoordinate().up(), prevStepCells)) != Rock
120 && getContent(mine.getCell(mine.getRobotCell().getCoordinate().up())) == Rock) {
121 lastScoreChange = score - prevScore;
122 state = GameState.Lose;
127 Cell[][] newCells = Arrays.copyOf(mine.getCells(), mine.getCells().length);
128 for (int y = 1; y <= mine.getSizeY(); y++) {
129 for (int x = 1; x <= mine.getSizeX(); x++) {
130 Coordinate curCoord = new Coordinate(x, y);
131 Coordinate leftCoord = curCoord.left();
132 Coordinate upCoord = curCoord.up();
133 Coordinate rightCoord = curCoord.right();
134 Coordinate downCoord = curCoord.down();
135 Coordinate leftAndUpCoord = curCoord.leftAndUp();
136 Coordinate rightAndUpCoord = curCoord.rightAndUp();
137 Coordinate rightAndDownCoord = curCoord.rightAndDown();
138 Coordinate leftAndDownCoord = curCoord.leftAndDown();
139 Cell curCell = mine.getCell(curCoord);
140 Cell leftCell = mine.getCell(leftCoord);
141 Cell upCell = mine.getCell(upCoord);
142 Cell rightCell = mine.getCell(rightCoord);
143 Cell downCell = mine.getCell(downCoord);
144 Cell leftAndUpCell = mine.getCell(leftAndUpCoord);
145 Cell rightAndUpCell = mine.getCell(rightAndUpCoord);
146 Cell rightAndDownCell = mine.getCell(rightAndDownCoord);
147 Cell leftAndDownCell = mine.getCell(leftAndDownCoord);
149 if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
150 getCell(curCoord, newCells).setContent(Empty);
151 getCell(downCoord, newCells).setContent(Rock);
152 } else if (getContent(curCell) == Rock && getContent(downCell) == Empty
153 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
154 getCell(curCoord, newCells).setContent(Empty);
155 getCell(rightAndDownCoord, newCells).setContent(Rock);
156 } else if (getContent(curCell) == Rock && getContent(downCell) == Rock
157 && (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
158 && getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
159 getCell(curCoord, newCells).setContent(Empty);
160 getCell(leftAndDownCoord, newCells).setContent(Rock);
161 } else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
162 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
163 getCell(curCoord, newCells).setContent(Empty);
164 getCell(rightAndDownCoord, newCells).setContent(Rock);
165 } else if (getContent(curCell) == ClosedLambdaLift && mine.findCells(Lambda).size() == 0) {
166 getCell(curCoord, newCells).setContent(OpenLambdaLift);
170 prevStepCells = mine.getCells();
171 mine.setCells(newCells);
173 lastScoreChange = score - prevScore;
174 state = GameState.Game;
178 protected CellContent getContent(Cell cell) {
181 return cell.getContent();
184 public int getLastScoreChange() {
185 return lastScoreChange;
188 public int getScore() {
192 public GameState getState() {
196 public int getLambdaCollectedCount() {
197 return lambdaCollectedCount;