1 package ru.bosony.model.mine; |
1 package ru.bosony.model.mine; |
2 |
2 |
3 import java.util.Arrays; |
3 import static ru.bosony.model.cellscontents.CellContent.ClosedLambdaLift; |
4 import java.util.Collection; |
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; |
|
10 |
5 import java.util.HashSet; |
11 import java.util.HashSet; |
6 import java.util.List; |
|
7 import java.util.Set; |
12 import java.util.Set; |
8 |
13 |
9 import org.hamcrest.CoreMatchers; |
|
10 |
|
11 import ru.bosony.model.cellscontents.CellContent; |
14 import ru.bosony.model.cellscontents.CellContent; |
12 import static ru.bosony.model.cellscontents.CellContent.*; |
|
13 import ru.bosony.model.io.TextRepresentable; |
15 import ru.bosony.model.io.TextRepresentable; |
14 import ru.bosony.model.moving.Movement; |
16 import ru.bosony.model.moving.Movement; |
15 |
17 |
16 /** |
18 /** |
17 * @author indvdum (gotoindvdum[at]gmail[dot]com) <br> |
19 * @author indvdum (gotoindvdum[at]gmail[dot]com) <br> |
18 * 14.07.2012 2:02:18 |
20 * 14.07.2012 2:02:18 |
19 * |
21 * |
20 */ |
22 */ |
21 public class Mine implements TextRepresentable { |
23 public class Mine implements TextRepresentable { |
22 |
24 |
23 private Cell[][] cells; |
25 protected Cell[][] cells; |
24 private Cell[][] prevStepCells; |
|
25 protected int sizeX; |
26 protected int sizeX; |
26 protected int sizeY; |
27 protected int sizeY; |
27 protected Cell robotCell; |
28 protected Cell robotCell; |
28 protected int score = 0; |
|
29 protected int lastScoreChange = 0; |
|
30 protected GameState state = GameState.Game; |
|
31 protected int lambdaCollectedCount = 0; |
|
32 |
29 |
33 public Mine(String text) { |
30 public Mine(String text) { |
34 fromText(text); |
31 fromText(text); |
35 if (findCells(OpenLambdaLift).size() != 0) |
32 if (findCells(OpenLambdaLift).size() != 0) |
36 throw new RuntimeException("Detected OpenLambdaLift in new map!"); |
33 throw new RuntimeException("Detected OpenLambdaLift in new map!"); |
37 if (findCells(MiningRobot).size() != 1) |
34 if (findCells(MiningRobot).size() != 1) |
38 throw new RuntimeException("New map does not contain a robot!"); |
35 throw new RuntimeException("New map does not contain a robot!"); |
39 if (findCells(ClosedLambdaLift).size() != 1) |
36 if (findCells(ClosedLambdaLift).size() != 1) |
40 throw new RuntimeException("New map does not contain a ClosedLambdaLift!"); |
37 throw new RuntimeException("New map does not contain a ClosedLambdaLift!"); |
|
38 for (Cell cell : findCells(MiningRobot)) |
|
39 robotCell = cell; |
41 } |
40 } |
42 |
41 |
43 @Override |
42 @Override |
44 public String toText() { |
43 public String toText() { |
45 String text = ""; |
44 String text = ""; |
138 for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates()) |
129 for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates()) |
139 cells.add(getCell(coordinate)); |
130 cells.add(getCell(coordinate)); |
140 return cells; |
131 return cells; |
141 } |
132 } |
142 |
133 |
143 protected void moveRobot(Cell nextRobotCell) { |
134 public Set<Cell> findCells(CellContent content) { |
144 robotCell.setContent(Empty); |
135 Set<Cell> finded = new HashSet<Cell>(); |
145 nextRobotCell.setContent(MiningRobot); |
|
146 robotCell = nextRobotCell; |
|
147 } |
|
148 |
|
149 public GameState move(Movement mov) { |
|
150 if (state != GameState.Game) |
|
151 throw new RuntimeException("Game over!"); |
|
152 |
|
153 // Score |
|
154 int prevScore = score; |
|
155 score--; |
|
156 |
|
157 // Moving and scoring |
|
158 Coordinate nextRobotCoord = null; |
|
159 switch (mov) { |
|
160 case ABORT: |
|
161 score += 25 * lambdaCollectedCount; |
|
162 |
|
163 lastScoreChange = score - prevScore; |
|
164 state = GameState.Abort; |
|
165 return state; |
|
166 case WAIT: |
|
167 break; |
|
168 case LEFT: |
|
169 nextRobotCoord = robotCell.coordinate.left(); |
|
170 break; |
|
171 case UP: |
|
172 nextRobotCoord = robotCell.coordinate.up(); |
|
173 break; |
|
174 case RIGHT: |
|
175 nextRobotCoord = robotCell.coordinate.right(); |
|
176 break; |
|
177 case DOWN: |
|
178 nextRobotCoord = robotCell.coordinate.down(); |
|
179 break; |
|
180 } |
|
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++; |
|
188 score += 25; |
|
189 } else if (nextRobotCellContent == OpenLambdaLift) { |
|
190 moveRobot(nextRobotCell); |
|
191 score += 50 * lambdaCollectedCount; |
|
192 |
|
193 lastScoreChange = score - prevScore; |
|
194 state = GameState.Win; |
|
195 return state; |
|
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); |
|
202 } else { |
|
203 mov = Movement.WAIT; |
|
204 } |
|
205 |
|
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; |
|
211 return state; |
|
212 } |
|
213 |
|
214 // World changes |
|
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); |
|
236 |
|
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); |
|
255 } |
|
256 } |
|
257 } |
|
258 prevStepCells = cells; |
|
259 cells = newCells; |
|
260 |
|
261 lastScoreChange = score - prevScore; |
|
262 state = GameState.Game; |
|
263 return state; |
|
264 } |
|
265 |
|
266 protected CellContent getContent(Cell cell) { |
|
267 if (cell == null) |
|
268 return null; |
|
269 return cell.getContent(); |
|
270 } |
|
271 |
|
272 public Collection<Cell> findCells(CellContent content) { |
|
273 Collection<Cell> finded = new HashSet<Cell>(); |
|
274 for (int y = sizeY - 1; y >= 0; y--) { |
136 for (int y = sizeY - 1; y >= 0; y--) { |
275 for (int x = 0; x < sizeX - 1; x++) { |
137 for (int x = 0; x < sizeX - 1; x++) { |
276 if (cells[x][y].getContent() == content) |
138 if (cells[x][y].getContent() == content) |
277 finded.add(cells[x][y]); |
139 finded.add(cells[x][y]); |
278 } |
140 } |
279 } |
141 } |
280 return finded; |
142 return finded; |
281 } |
143 } |
282 |
144 |
283 public int getLastScoreChange() { |
145 public Cell[][] getCells() { |
284 return lastScoreChange; |
146 return cells; |
|
147 } |
|
148 |
|
149 public void setCells(Cell[][] cells) { |
|
150 this.cells = cells; |
|
151 } |
|
152 |
|
153 public Cell getRobotCell() { |
|
154 return robotCell; |
|
155 } |
|
156 |
|
157 public void setRobotCell(Cell robotCell) { |
|
158 this.robotCell = robotCell; |
285 } |
159 } |
286 |
160 |
287 } |
161 } |