13 * |
19 * |
14 */ |
20 */ |
15 public class Mine implements TextRepresentable { |
21 public class Mine implements TextRepresentable { |
16 |
22 |
17 private Cell[][] cells; |
23 private Cell[][] cells; |
|
24 private Cell[][] prevStepCells; |
18 protected int sizeX; |
25 protected int sizeX; |
19 protected int sizeY; |
26 protected int sizeY; |
20 protected Cell robotCell; |
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; |
21 |
32 |
22 public Mine(String text) { |
33 public Mine(String text) { |
23 fromText(text); |
34 fromText(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!"); |
24 } |
41 } |
25 |
42 |
26 @Override |
43 @Override |
27 public String toText() { |
44 public String toText() { |
28 String text = ""; |
45 String text = ""; |
51 for (int curX = 0; curX < row.length() || curX < x; curX++) { |
68 for (int curX = 0; curX < row.length() || curX < x; curX++) { |
52 CellContent content = null; |
69 CellContent content = null; |
53 if (curX < row.length()) |
70 if (curX < row.length()) |
54 content = CellContent.fromText("" + row.charAt(curX)); |
71 content = CellContent.fromText("" + row.charAt(curX)); |
55 else |
72 else |
56 content = CellContent.Empty; |
73 content = Empty; |
57 Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content); |
74 Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content); |
58 cells[curX][curY] = cell; |
75 cells[curX][curY] = cell; |
59 if (content == CellContent.MiningRobot) |
76 if (content == MiningRobot) |
60 robotCell = cell; |
77 robotCell = cell; |
61 } |
78 } |
62 } |
79 } |
63 } |
80 } |
64 |
81 |
65 public Cell getCell(Coordinate coordinate) { |
82 public Cell getCell(Coordinate coordinate) { |
|
83 return getCell(coordinate, cells); |
|
84 } |
|
85 |
|
86 protected Cell getCell(Coordinate coordinate, Cell[][] cells) { |
|
87 if (coordinate == null) |
|
88 return null; |
66 int x = coordinate.x; |
89 int x = coordinate.x; |
67 int y = coordinate.y; |
90 int y = coordinate.y; |
|
91 if (cells == null) |
|
92 return null; |
68 if (x < 1 || x > sizeX || y < 1 || y > sizeY) |
93 if (x < 1 || x > sizeX || y < 1 || y > sizeY) |
69 return null; |
94 return null; |
70 return cells[x - 1][sizeY - y]; |
95 return cells[x - 1][sizeY - y]; |
71 } |
96 } |
72 |
97 |
89 for (Cell nCell : nCells) { |
114 for (Cell nCell : nCells) { |
90 CellContent nContent = nCell.getContent(); |
115 CellContent nContent = nCell.getContent(); |
91 Coordinate nCoordinate = nCell.getCoordinate(); |
116 Coordinate nCoordinate = nCell.getCoordinate(); |
92 Cell rightNCell = getCell(nCoordinate.right()); |
117 Cell rightNCell = getCell(nCoordinate.right()); |
93 Cell leftNCell = getCell(nCoordinate.left()); |
118 Cell leftNCell = getCell(nCoordinate.left()); |
94 if (nContent == CellContent.Empty |
119 if (nContent == Empty |
95 || nContent == CellContent.Earth |
120 || nContent == Earth |
96 || nContent == CellContent.Lambda |
121 || nContent == Lambda |
97 || nContent == CellContent.OpenLambdaLift |
122 || nContent == OpenLambdaLift |
98 || (nContent == CellContent.Rock && robotCell.getCoordinate().right().equals(nCoordinate) |
123 || (nContent == Rock && robotCell.getCoordinate().right().equals(nCoordinate) && rightNCell != null && rightNCell |
99 && rightNCell != null && rightNCell.getContent() == CellContent.Empty) |
124 .getContent() == Empty) |
100 || (nContent == CellContent.Rock && robotCell.getCoordinate().left().equals(nCoordinate) |
125 || (nContent == Rock && robotCell.getCoordinate().left().equals(nCoordinate) && leftNCell != null && leftNCell |
101 && leftNCell != null && leftNCell.getContent() == CellContent.Empty)) { |
126 .getContent() == Empty)) { |
102 Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate); |
127 Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate); |
103 movs.add(mov); |
128 movs.add(mov); |
104 } |
129 } |
105 } |
130 } |
106 movs.add(Movement.ABORT); |
131 movs.add(Movement.ABORT); |
113 for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates()) |
138 for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates()) |
114 cells.add(getCell(coordinate)); |
139 cells.add(getCell(coordinate)); |
115 return cells; |
140 return cells; |
116 } |
141 } |
117 |
142 |
|
143 protected void moveRobot(Cell nextRobotCell) { |
|
144 robotCell.setContent(Empty); |
|
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--) { |
|
275 for (int x = 0; x < sizeX - 1; x++) { |
|
276 if (cells[x][y].getContent() == content) |
|
277 finded.add(cells[x][y]); |
|
278 } |
|
279 } |
|
280 return finded; |
|
281 } |
|
282 |
|
283 public int getLastScoreChange() { |
|
284 return lastScoreChange; |
|
285 } |
|
286 |
118 } |
287 } |