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>();
34 public Game(Mine mine) {
38 public Mine getMine() {
42 protected Cell getCell(Coordinate coordinate, Cell[][] cells) {
43 if (coordinate == null)
49 if (x < 1 || x > mine.getSizeX() || y < 1 || y > mine.getSizeY())
51 return cells[x - 1][mine.getSizeY() - y];
54 protected void moveRobot(Cell nextRobotCell) {
55 mine.getRobotCell().setContent(Empty);
56 nextRobotCell.setContent(MiningRobot);
59 public GameState move(Movement mov) {
60 if (state != GameState.Game)
61 throw new RuntimeException("Game over!");
64 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();
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 (robotCoord.right() == nextRobotCoord && nextRobotCellContent == Rock
109 && getContent(mine.getCell(nextRobotCoord.right())) == Empty) {
110 moveRobot(nextRobotCell);
111 mine.getCell(nextRobotCoord.right()).setContent(Rock);
112 } else if (robotCoord.left() == nextRobotCoord && nextRobotCellContent == Rock
113 && getContent(mine.getCell(nextRobotCoord.left())) == Empty) {
114 moveRobot(nextRobotCell);
115 mine.getCell(nextRobotCoord.left()).setContent(Rock);
121 Cell[][] newCells = mine.cloneCells();
122 for (int y = 1; y <= mine.getSizeY(); y++) {
123 for (int x = 1; x <= mine.getSizeX(); x++) {
124 Coordinate curCoord = new Coordinate(x, y);
125 Coordinate leftCoord = curCoord.left();
126 Coordinate upCoord = curCoord.up();
127 Coordinate rightCoord = curCoord.right();
128 Coordinate downCoord = curCoord.down();
129 Coordinate leftAndUpCoord = curCoord.leftAndUp();
130 Coordinate rightAndUpCoord = curCoord.rightAndUp();
131 Coordinate rightAndDownCoord = curCoord.rightAndDown();
132 Coordinate leftAndDownCoord = curCoord.leftAndDown();
133 Cell curCell = mine.getCell(curCoord);
134 Cell leftCell = mine.getCell(leftCoord);
135 Cell upCell = mine.getCell(upCoord);
136 Cell rightCell = mine.getCell(rightCoord);
137 Cell downCell = mine.getCell(downCoord);
138 Cell leftAndUpCell = mine.getCell(leftAndUpCoord);
139 Cell rightAndUpCell = mine.getCell(rightAndUpCoord);
140 Cell rightAndDownCell = mine.getCell(rightAndDownCoord);
141 Cell leftAndDownCell = mine.getCell(leftAndDownCoord);
143 if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
144 getCell(curCoord, newCells).setContent(Empty);
145 getCell(downCoord, newCells).setContent(Rock);
146 } else if (getContent(curCell) == Rock && getContent(downCell) == Empty
147 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
148 getCell(curCoord, newCells).setContent(Empty);
149 getCell(rightAndDownCoord, newCells).setContent(Rock);
150 } else if (getContent(curCell) == Rock && getContent(downCell) == Rock
151 && (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
152 && getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
153 getCell(curCoord, newCells).setContent(Empty);
154 getCell(leftAndDownCoord, newCells).setContent(Rock);
155 } else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
156 && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
157 getCell(curCoord, newCells).setContent(Empty);
158 getCell(rightAndDownCoord, newCells).setContent(Rock);
159 } else if (getContent(curCell) == ClosedLambdaLift && mine.findCells(Lambda).size() == 0) {
160 getCell(curCoord, newCells).setContent(OpenLambdaLift);
165 // Other ending conditions
166 robotCoord = mine.getRobotCell().getCoordinate();
167 if (getContent(mine.getCell(robotCoord.up())) != Rock && getContent(getCell(robotCoord.up(), newCells)) == Rock) {
168 mine.setCells(newCells);
169 lastScoreChange = score - prevScore;
170 state = GameState.Lose;
174 mine.setCells(newCells);
176 lastScoreChange = score - prevScore;
177 state = GameState.Game;
181 public GameState move(List<Movement> movs) {
182 for (Movement mov : movs) {
183 if (move(mov) != GameState.Game)
189 public GameState move(String route) {
190 List<Movement> movs = new ArrayList<Movement>();
191 for (int i = 0; i < route.length(); i++) {
192 String s = "" + route.charAt(i);
193 for (Movement mov : Movement.values()) {
194 if (mov.toText().equals(s)) {
203 protected CellContent getContent(Cell cell) {
206 return cell.getContent();
209 public int getLastScoreChange() {
210 return lastScoreChange;
213 public int getScore() {
217 public GameState getState() {
221 public int getLambdaCollectedCount() {
222 return lambdaCollectedCount;
225 public List<Movement> getRoute() {
229 public String getStringRoute() {
231 for (Movement mov : route) {
232 movs += mov.toText();