|
indvdum@6
|
1 |
package ru.bosony.model.mine;
|
|
indvdum@6
|
2 |
|
|
indvdum@8
|
3 |
import java.util.Arrays;
|
|
indvdum@8
|
4 |
import java.util.Collection;
|
|
indvdum@7
|
5 |
import java.util.HashSet;
|
|
indvdum@8
|
6 |
import java.util.List;
|
|
indvdum@7
|
7 |
import java.util.Set;
|
|
indvdum@7
|
8 |
|
|
indvdum@8
|
9 |
import org.hamcrest.CoreMatchers;
|
|
indvdum@8
|
10 |
|
|
indvdum@6
|
11 |
import ru.bosony.model.cellscontents.CellContent;
|
|
indvdum@8
|
12 |
import static ru.bosony.model.cellscontents.CellContent.*;
|
|
indvdum@6
|
13 |
import ru.bosony.model.io.TextRepresentable;
|
|
indvdum@7
|
14 |
import ru.bosony.model.moving.Movement;
|
|
indvdum@6
|
15 |
|
|
indvdum@7
|
16 |
/**
|
|
indvdum@7
|
17 |
* @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
|
|
indvdum@7
|
18 |
* 14.07.2012 2:02:18
|
|
indvdum@7
|
19 |
*
|
|
indvdum@7
|
20 |
*/
|
|
indvdum@6
|
21 |
public class Mine implements TextRepresentable {
|
|
indvdum@6
|
22 |
|
|
indvdum@7
|
23 |
private Cell[][] cells;
|
|
indvdum@8
|
24 |
private Cell[][] prevStepCells;
|
|
indvdum@7
|
25 |
protected int sizeX;
|
|
indvdum@7
|
26 |
protected int sizeY;
|
|
indvdum@7
|
27 |
protected Cell robotCell;
|
|
indvdum@8
|
28 |
protected int score = 0;
|
|
indvdum@8
|
29 |
protected int lastScoreChange = 0;
|
|
indvdum@8
|
30 |
protected GameState state = GameState.Game;
|
|
indvdum@8
|
31 |
protected int lambdaCollectedCount = 0;
|
|
indvdum@6
|
32 |
|
|
indvdum@6
|
33 |
public Mine(String text) {
|
|
indvdum@6
|
34 |
fromText(text);
|
|
indvdum@8
|
35 |
if (findCells(OpenLambdaLift).size() != 0)
|
|
indvdum@8
|
36 |
throw new RuntimeException("Detected OpenLambdaLift in new map!");
|
|
indvdum@8
|
37 |
if (findCells(MiningRobot).size() != 1)
|
|
indvdum@8
|
38 |
throw new RuntimeException("New map does not contain a robot!");
|
|
indvdum@8
|
39 |
if (findCells(ClosedLambdaLift).size() != 1)
|
|
indvdum@8
|
40 |
throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
|
|
indvdum@6
|
41 |
}
|
|
indvdum@6
|
42 |
|
|
indvdum@6
|
43 |
@Override
|
|
indvdum@6
|
44 |
public String toText() {
|
|
indvdum@6
|
45 |
String text = "";
|
|
indvdum@6
|
46 |
for (int y = 0; y < sizeY; y++) {
|
|
indvdum@6
|
47 |
for (int x = 0; x < sizeX; x++) {
|
|
indvdum@6
|
48 |
text += cells[x][y].getContent().toText();
|
|
indvdum@6
|
49 |
}
|
|
indvdum@6
|
50 |
text += "\n";
|
|
indvdum@6
|
51 |
}
|
|
indvdum@6
|
52 |
return text.replaceAll("\n*$", "");
|
|
indvdum@6
|
53 |
}
|
|
indvdum@6
|
54 |
|
|
indvdum@6
|
55 |
private void fromText(String text) {
|
|
indvdum@6
|
56 |
String[] rows = text.split("\n+");
|
|
indvdum@6
|
57 |
int y = rows.length;
|
|
indvdum@6
|
58 |
int x = 0;
|
|
indvdum@6
|
59 |
for (String row : rows) {
|
|
indvdum@6
|
60 |
if (x < row.length())
|
|
indvdum@6
|
61 |
x = row.length();
|
|
indvdum@6
|
62 |
}
|
|
indvdum@6
|
63 |
sizeX = x;
|
|
indvdum@6
|
64 |
sizeY = y;
|
|
indvdum@6
|
65 |
cells = new Cell[x][y];
|
|
indvdum@6
|
66 |
for (int curY = 0; curY < rows.length; curY++) {
|
|
indvdum@6
|
67 |
String row = rows[curY];
|
|
indvdum@6
|
68 |
for (int curX = 0; curX < row.length() || curX < x; curX++) {
|
|
indvdum@6
|
69 |
CellContent content = null;
|
|
indvdum@6
|
70 |
if (curX < row.length())
|
|
indvdum@6
|
71 |
content = CellContent.fromText("" + row.charAt(curX));
|
|
indvdum@6
|
72 |
else
|
|
indvdum@8
|
73 |
content = Empty;
|
|
indvdum@7
|
74 |
Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
|
|
indvdum@6
|
75 |
cells[curX][curY] = cell;
|
|
indvdum@8
|
76 |
if (content == MiningRobot)
|
|
indvdum@7
|
77 |
robotCell = cell;
|
|
indvdum@6
|
78 |
}
|
|
indvdum@6
|
79 |
}
|
|
indvdum@6
|
80 |
}
|
|
indvdum@6
|
81 |
|
|
indvdum@7
|
82 |
public Cell getCell(Coordinate coordinate) {
|
|
indvdum@8
|
83 |
return getCell(coordinate, cells);
|
|
indvdum@8
|
84 |
}
|
|
indvdum@8
|
85 |
|
|
indvdum@8
|
86 |
protected Cell getCell(Coordinate coordinate, Cell[][] cells) {
|
|
indvdum@8
|
87 |
if (coordinate == null)
|
|
indvdum@8
|
88 |
return null;
|
|
indvdum@7
|
89 |
int x = coordinate.x;
|
|
indvdum@7
|
90 |
int y = coordinate.y;
|
|
indvdum@8
|
91 |
if (cells == null)
|
|
indvdum@8
|
92 |
return null;
|
|
indvdum@7
|
93 |
if (x < 1 || x > sizeX || y < 1 || y > sizeY)
|
|
indvdum@7
|
94 |
return null;
|
|
indvdum@7
|
95 |
return cells[x - 1][sizeY - y];
|
|
indvdum@7
|
96 |
}
|
|
indvdum@7
|
97 |
|
|
indvdum@6
|
98 |
@Override
|
|
indvdum@6
|
99 |
public String toString() {
|
|
indvdum@6
|
100 |
return toText();
|
|
indvdum@6
|
101 |
}
|
|
indvdum@6
|
102 |
|
|
indvdum@7
|
103 |
public int getSizeX() {
|
|
indvdum@7
|
104 |
return sizeX;
|
|
indvdum@7
|
105 |
}
|
|
indvdum@7
|
106 |
|
|
indvdum@7
|
107 |
public int getSizeY() {
|
|
indvdum@7
|
108 |
return sizeY;
|
|
indvdum@7
|
109 |
}
|
|
indvdum@7
|
110 |
|
|
indvdum@7
|
111 |
public Set<Movement> getAvailableRobotMovements() {
|
|
indvdum@7
|
112 |
Set<Movement> movs = new HashSet<Movement>();
|
|
indvdum@7
|
113 |
Set<Cell> nCells = getNeighboringCells(robotCell);
|
|
indvdum@7
|
114 |
for (Cell nCell : nCells) {
|
|
indvdum@7
|
115 |
CellContent nContent = nCell.getContent();
|
|
indvdum@7
|
116 |
Coordinate nCoordinate = nCell.getCoordinate();
|
|
indvdum@7
|
117 |
Cell rightNCell = getCell(nCoordinate.right());
|
|
indvdum@7
|
118 |
Cell leftNCell = getCell(nCoordinate.left());
|
|
indvdum@8
|
119 |
if (nContent == Empty
|
|
indvdum@8
|
120 |
|| nContent == Earth
|
|
indvdum@8
|
121 |
|| nContent == Lambda
|
|
indvdum@8
|
122 |
|| nContent == OpenLambdaLift
|
|
indvdum@8
|
123 |
|| (nContent == Rock && robotCell.getCoordinate().right().equals(nCoordinate) && rightNCell != null && rightNCell
|
|
indvdum@8
|
124 |
.getContent() == Empty)
|
|
indvdum@8
|
125 |
|| (nContent == Rock && robotCell.getCoordinate().left().equals(nCoordinate) && leftNCell != null && leftNCell
|
|
indvdum@8
|
126 |
.getContent() == Empty)) {
|
|
indvdum@7
|
127 |
Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate);
|
|
indvdum@7
|
128 |
movs.add(mov);
|
|
indvdum@7
|
129 |
}
|
|
indvdum@7
|
130 |
}
|
|
indvdum@7
|
131 |
movs.add(Movement.ABORT);
|
|
indvdum@7
|
132 |
movs.add(Movement.WAIT);
|
|
indvdum@7
|
133 |
return movs;
|
|
indvdum@7
|
134 |
}
|
|
indvdum@7
|
135 |
|
|
indvdum@7
|
136 |
public Set<Cell> getNeighboringCells(Cell cell) {
|
|
indvdum@7
|
137 |
Set<Cell> cells = new HashSet<Cell>();
|
|
indvdum@7
|
138 |
for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates())
|
|
indvdum@7
|
139 |
cells.add(getCell(coordinate));
|
|
indvdum@7
|
140 |
return cells;
|
|
indvdum@7
|
141 |
}
|
|
indvdum@7
|
142 |
|
|
indvdum@8
|
143 |
protected void moveRobot(Cell nextRobotCell) {
|
|
indvdum@8
|
144 |
robotCell.setContent(Empty);
|
|
indvdum@8
|
145 |
nextRobotCell.setContent(MiningRobot);
|
|
indvdum@8
|
146 |
robotCell = nextRobotCell;
|
|
indvdum@8
|
147 |
}
|
|
indvdum@8
|
148 |
|
|
indvdum@8
|
149 |
public GameState move(Movement mov) {
|
|
indvdum@8
|
150 |
if (state != GameState.Game)
|
|
indvdum@8
|
151 |
throw new RuntimeException("Game over!");
|
|
indvdum@8
|
152 |
|
|
indvdum@8
|
153 |
// Score
|
|
indvdum@8
|
154 |
int prevScore = score;
|
|
indvdum@8
|
155 |
score--;
|
|
indvdum@8
|
156 |
|
|
indvdum@8
|
157 |
// Moving and scoring
|
|
indvdum@8
|
158 |
Coordinate nextRobotCoord = null;
|
|
indvdum@8
|
159 |
switch (mov) {
|
|
indvdum@8
|
160 |
case ABORT:
|
|
indvdum@8
|
161 |
score += 25 * lambdaCollectedCount;
|
|
indvdum@8
|
162 |
|
|
indvdum@8
|
163 |
lastScoreChange = score - prevScore;
|
|
indvdum@8
|
164 |
state = GameState.Abort;
|
|
indvdum@8
|
165 |
return state;
|
|
indvdum@8
|
166 |
case WAIT:
|
|
indvdum@8
|
167 |
break;
|
|
indvdum@8
|
168 |
case LEFT:
|
|
indvdum@8
|
169 |
nextRobotCoord = robotCell.coordinate.left();
|
|
indvdum@8
|
170 |
break;
|
|
indvdum@8
|
171 |
case UP:
|
|
indvdum@8
|
172 |
nextRobotCoord = robotCell.coordinate.up();
|
|
indvdum@8
|
173 |
break;
|
|
indvdum@8
|
174 |
case RIGHT:
|
|
indvdum@8
|
175 |
nextRobotCoord = robotCell.coordinate.right();
|
|
indvdum@8
|
176 |
break;
|
|
indvdum@8
|
177 |
case DOWN:
|
|
indvdum@8
|
178 |
nextRobotCoord = robotCell.coordinate.down();
|
|
indvdum@8
|
179 |
break;
|
|
indvdum@8
|
180 |
}
|
|
indvdum@8
|
181 |
Cell nextRobotCell = getCell(nextRobotCoord);
|
|
indvdum@8
|
182 |
CellContent nextRobotCellContent = getContent(nextRobotCell);
|
|
indvdum@8
|
183 |
if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) {
|
|
indvdum@8
|
184 |
moveRobot(nextRobotCell);
|
|
indvdum@8
|
185 |
} else if (nextRobotCellContent == Lambda) {
|
|
indvdum@8
|
186 |
moveRobot(nextRobotCell);
|
|
indvdum@8
|
187 |
lambdaCollectedCount++;
|
|
indvdum@8
|
188 |
score += 25;
|
|
indvdum@8
|
189 |
} else if (nextRobotCellContent == OpenLambdaLift) {
|
|
indvdum@8
|
190 |
moveRobot(nextRobotCell);
|
|
indvdum@8
|
191 |
score += 50 * lambdaCollectedCount;
|
|
indvdum@8
|
192 |
|
|
indvdum@8
|
193 |
lastScoreChange = score - prevScore;
|
|
indvdum@8
|
194 |
state = GameState.Win;
|
|
indvdum@8
|
195 |
return state;
|
|
indvdum@8
|
196 |
} else if (robotCell.coordinate.right() == nextRobotCoord && nextRobotCellContent == Rock && getContent(getCell(nextRobotCoord.right())) == Empty) {
|
|
indvdum@8
|
197 |
moveRobot(nextRobotCell);
|
|
indvdum@8
|
198 |
getCell(nextRobotCoord.right()).setContent(Rock);
|
|
indvdum@8
|
199 |
} else if (robotCell.coordinate.left() == nextRobotCoord && nextRobotCellContent == Rock && getContent(getCell(nextRobotCoord.left())) == Empty) {
|
|
indvdum@8
|
200 |
moveRobot(nextRobotCell);
|
|
indvdum@8
|
201 |
getCell(nextRobotCoord.left()).setContent(Rock);
|
|
indvdum@8
|
202 |
} else {
|
|
indvdum@8
|
203 |
mov = Movement.WAIT;
|
|
indvdum@8
|
204 |
}
|
|
indvdum@8
|
205 |
|
|
indvdum@8
|
206 |
// Other ending conditions
|
|
indvdum@8
|
207 |
if (getContent(getCell(robotCell.coordinate.up(), prevStepCells)) != Rock
|
|
indvdum@8
|
208 |
&& getContent(getCell(robotCell.coordinate.up())) == Rock) {
|
|
indvdum@8
|
209 |
lastScoreChange = score - prevScore;
|
|
indvdum@8
|
210 |
state = GameState.Lose;
|
|
indvdum@8
|
211 |
return state;
|
|
indvdum@8
|
212 |
}
|
|
indvdum@8
|
213 |
|
|
indvdum@8
|
214 |
// World changes
|
|
indvdum@8
|
215 |
Cell[][] newCells = Arrays.copyOf(cells, cells.length);
|
|
indvdum@8
|
216 |
for (int y = 1; y <= sizeY; y++) {
|
|
indvdum@8
|
217 |
for (int x = 1; x <= sizeX; x++) {
|
|
indvdum@8
|
218 |
Coordinate curCoord = new Coordinate(x, y);
|
|
indvdum@8
|
219 |
Coordinate leftCoord = curCoord.left();
|
|
indvdum@8
|
220 |
Coordinate upCoord = curCoord.up();
|
|
indvdum@8
|
221 |
Coordinate rightCoord = curCoord.right();
|
|
indvdum@8
|
222 |
Coordinate downCoord = curCoord.down();
|
|
indvdum@8
|
223 |
Coordinate leftAndUpCoord = curCoord.leftAndUp();
|
|
indvdum@8
|
224 |
Coordinate rightAndUpCoord = curCoord.rightAndUp();
|
|
indvdum@8
|
225 |
Coordinate rightAndDownCoord = curCoord.rightAndDown();
|
|
indvdum@8
|
226 |
Coordinate leftAndDownCoord = curCoord.leftAndDown();
|
|
indvdum@8
|
227 |
Cell curCell = getCell(curCoord);
|
|
indvdum@8
|
228 |
Cell leftCell = getCell(leftCoord);
|
|
indvdum@8
|
229 |
Cell upCell = getCell(upCoord);
|
|
indvdum@8
|
230 |
Cell rightCell = getCell(rightCoord);
|
|
indvdum@8
|
231 |
Cell downCell = getCell(downCoord);
|
|
indvdum@8
|
232 |
Cell leftAndUpCell = getCell(leftAndUpCoord);
|
|
indvdum@8
|
233 |
Cell rightAndUpCell = getCell(rightAndUpCoord);
|
|
indvdum@8
|
234 |
Cell rightAndDownCell = getCell(rightAndDownCoord);
|
|
indvdum@8
|
235 |
Cell leftAndDownCell = getCell(leftAndDownCoord);
|
|
indvdum@8
|
236 |
|
|
indvdum@8
|
237 |
if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
|
|
indvdum@8
|
238 |
getCell(curCoord, newCells).setContent(Empty);
|
|
indvdum@8
|
239 |
getCell(downCoord, newCells).setContent(Rock);
|
|
indvdum@8
|
240 |
} else if (getContent(curCell) == Rock && getContent(downCell) == Empty
|
|
indvdum@8
|
241 |
&& getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
|
|
indvdum@8
|
242 |
getCell(curCoord, newCells).setContent(Empty);
|
|
indvdum@8
|
243 |
getCell(rightAndDownCoord, newCells).setContent(Rock);
|
|
indvdum@8
|
244 |
} else if (getContent(curCell) == Rock && getContent(downCell) == Rock
|
|
indvdum@8
|
245 |
&& (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
|
|
indvdum@8
|
246 |
&& getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
|
|
indvdum@8
|
247 |
getCell(curCoord, newCells).setContent(Empty);
|
|
indvdum@8
|
248 |
getCell(leftAndDownCoord, newCells).setContent(Rock);
|
|
indvdum@8
|
249 |
} else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
|
|
indvdum@8
|
250 |
&& getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
|
|
indvdum@8
|
251 |
getCell(curCoord, newCells).setContent(Empty);
|
|
indvdum@8
|
252 |
getCell(rightAndDownCoord, newCells).setContent(Rock);
|
|
indvdum@8
|
253 |
} else if (getContent(curCell) == ClosedLambdaLift && findCells(Lambda).size() == 0) {
|
|
indvdum@8
|
254 |
getCell(curCoord, newCells).setContent(OpenLambdaLift);
|
|
indvdum@8
|
255 |
}
|
|
indvdum@8
|
256 |
}
|
|
indvdum@8
|
257 |
}
|
|
indvdum@8
|
258 |
prevStepCells = cells;
|
|
indvdum@8
|
259 |
cells = newCells;
|
|
indvdum@8
|
260 |
|
|
indvdum@8
|
261 |
lastScoreChange = score - prevScore;
|
|
indvdum@8
|
262 |
state = GameState.Game;
|
|
indvdum@8
|
263 |
return state;
|
|
indvdum@8
|
264 |
}
|
|
indvdum@8
|
265 |
|
|
indvdum@8
|
266 |
protected CellContent getContent(Cell cell) {
|
|
indvdum@8
|
267 |
if (cell == null)
|
|
indvdum@8
|
268 |
return null;
|
|
indvdum@8
|
269 |
return cell.getContent();
|
|
indvdum@8
|
270 |
}
|
|
indvdum@8
|
271 |
|
|
indvdum@8
|
272 |
public Collection<Cell> findCells(CellContent content) {
|
|
indvdum@8
|
273 |
Collection<Cell> finded = new HashSet<Cell>();
|
|
indvdum@8
|
274 |
for (int y = sizeY - 1; y >= 0; y--) {
|
|
indvdum@8
|
275 |
for (int x = 0; x < sizeX - 1; x++) {
|
|
indvdum@8
|
276 |
if (cells[x][y].getContent() == content)
|
|
indvdum@8
|
277 |
finded.add(cells[x][y]);
|
|
indvdum@8
|
278 |
}
|
|
indvdum@8
|
279 |
}
|
|
indvdum@8
|
280 |
return finded;
|
|
indvdum@8
|
281 |
}
|
|
indvdum@8
|
282 |
|
|
indvdum@8
|
283 |
public int getLastScoreChange() {
|
|
indvdum@8
|
284 |
return lastScoreChange;
|
|
indvdum@8
|
285 |
}
|
|
indvdum@8
|
286 |
|
|
indvdum@6
|
287 |
}
|