|
indvdum@9
|
1 |
package ru.bosony.model.game;
|
|
indvdum@9
|
2 |
|
|
indvdum@9
|
3 |
import static ru.bosony.model.cellscontents.CellContent.ClosedLambdaLift;
|
|
indvdum@9
|
4 |
import static ru.bosony.model.cellscontents.CellContent.Earth;
|
|
indvdum@9
|
5 |
import static ru.bosony.model.cellscontents.CellContent.Empty;
|
|
indvdum@9
|
6 |
import static ru.bosony.model.cellscontents.CellContent.Lambda;
|
|
indvdum@9
|
7 |
import static ru.bosony.model.cellscontents.CellContent.MiningRobot;
|
|
indvdum@9
|
8 |
import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift;
|
|
indvdum@9
|
9 |
import static ru.bosony.model.cellscontents.CellContent.Rock;
|
|
indvdum@9
|
10 |
|
|
indvdum@16
|
11 |
import java.util.ArrayList;
|
|
indvdum@16
|
12 |
import java.util.List;
|
|
indvdum@9
|
13 |
|
|
indvdum@9
|
14 |
import ru.bosony.model.cellscontents.CellContent;
|
|
indvdum@9
|
15 |
import ru.bosony.model.mine.Cell;
|
|
indvdum@9
|
16 |
import ru.bosony.model.mine.Mine;
|
|
indvdum@22
|
17 |
import ru.bosony.model.moving.Coordinate;
|
|
indvdum@9
|
18 |
import ru.bosony.model.moving.Movement;
|
|
indvdum@9
|
19 |
|
|
indvdum@9
|
20 |
/**
|
|
indvdum@9
|
21 |
* @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
|
|
indvdum@9
|
22 |
* 14.07.2012 22:51:20
|
|
indvdum@9
|
23 |
*
|
|
indvdum@9
|
24 |
*/
|
|
indvdum@9
|
25 |
public class Game {
|
|
indvdum@9
|
26 |
|
|
indvdum@22
|
27 |
protected Mine mine = null;
|
|
indvdum@20
|
28 |
protected int score = 0;
|
|
indvdum@20
|
29 |
protected int lastScoreChange = 0;
|
|
indvdum@20
|
30 |
protected GameState state = GameState.Game;
|
|
indvdum@20
|
31 |
protected int lambdaCollectedCount = 0;
|
|
indvdum@20
|
32 |
protected List<Movement> route = new ArrayList<Movement>();
|
|
indvdum@25
|
33 |
protected int underwater = 0;
|
|
indvdum@9
|
34 |
|
|
indvdum@9
|
35 |
public Game(Mine mine) {
|
|
indvdum@9
|
36 |
this.mine = mine;
|
|
indvdum@9
|
37 |
}
|
|
indvdum@9
|
38 |
|
|
indvdum@9
|
39 |
public Mine getMine() {
|
|
indvdum@9
|
40 |
return mine;
|
|
indvdum@9
|
41 |
}
|
|
indvdum@9
|
42 |
|
|
indvdum@9
|
43 |
protected Cell getCell(Coordinate coordinate, Cell[][] cells) {
|
|
indvdum@9
|
44 |
if (coordinate == null)
|
|
indvdum@9
|
45 |
return null;
|
|
indvdum@9
|
46 |
int x = coordinate.x;
|
|
indvdum@9
|
47 |
int y = coordinate.y;
|
|
indvdum@9
|
48 |
if (cells == null)
|
|
indvdum@9
|
49 |
return null;
|
|
indvdum@9
|
50 |
if (x < 1 || x > mine.getSizeX() || y < 1 || y > mine.getSizeY())
|
|
indvdum@9
|
51 |
return null;
|
|
indvdum@9
|
52 |
return cells[x - 1][mine.getSizeY() - y];
|
|
indvdum@9
|
53 |
}
|
|
indvdum@9
|
54 |
|
|
indvdum@9
|
55 |
protected void moveRobot(Cell nextRobotCell) {
|
|
indvdum@9
|
56 |
mine.getRobotCell().setContent(Empty);
|
|
indvdum@9
|
57 |
nextRobotCell.setContent(MiningRobot);
|
|
indvdum@9
|
58 |
}
|
|
indvdum@9
|
59 |
|
|
indvdum@9
|
60 |
public GameState move(Movement mov) {
|
|
indvdum@9
|
61 |
if (state != GameState.Game)
|
|
indvdum@9
|
62 |
throw new RuntimeException("Game over!");
|
|
indvdum@9
|
63 |
|
|
indvdum@9
|
64 |
// Score
|
|
indvdum@9
|
65 |
int prevScore = score;
|
|
indvdum@9
|
66 |
|
|
indvdum@9
|
67 |
// Moving and scoring
|
|
indvdum@20
|
68 |
route.add(mov);
|
|
indvdum@22
|
69 |
Coordinate robotCoord = mine.getRobotCell().getCoordinate();
|
|
indvdum@9
|
70 |
Coordinate nextRobotCoord = null;
|
|
indvdum@9
|
71 |
switch (mov) {
|
|
indvdum@9
|
72 |
case ABORT:
|
|
indvdum@9
|
73 |
score += 25 * lambdaCollectedCount;
|
|
indvdum@9
|
74 |
|
|
indvdum@9
|
75 |
lastScoreChange = score - prevScore;
|
|
indvdum@9
|
76 |
state = GameState.Abort;
|
|
indvdum@9
|
77 |
return state;
|
|
indvdum@9
|
78 |
case WAIT:
|
|
indvdum@9
|
79 |
break;
|
|
indvdum@9
|
80 |
case LEFT:
|
|
indvdum@22
|
81 |
nextRobotCoord = robotCoord.left();
|
|
indvdum@9
|
82 |
break;
|
|
indvdum@9
|
83 |
case UP:
|
|
indvdum@22
|
84 |
nextRobotCoord = robotCoord.up();
|
|
indvdum@9
|
85 |
break;
|
|
indvdum@9
|
86 |
case RIGHT:
|
|
indvdum@22
|
87 |
nextRobotCoord = robotCoord.right();
|
|
indvdum@9
|
88 |
break;
|
|
indvdum@9
|
89 |
case DOWN:
|
|
indvdum@22
|
90 |
nextRobotCoord = robotCoord.down();
|
|
indvdum@9
|
91 |
break;
|
|
indvdum@9
|
92 |
}
|
|
indvdum@23
|
93 |
score--;
|
|
indvdum@9
|
94 |
Cell nextRobotCell = mine.getCell(nextRobotCoord);
|
|
indvdum@9
|
95 |
CellContent nextRobotCellContent = getContent(nextRobotCell);
|
|
indvdum@9
|
96 |
if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) {
|
|
indvdum@9
|
97 |
moveRobot(nextRobotCell);
|
|
indvdum@9
|
98 |
} else if (nextRobotCellContent == Lambda) {
|
|
indvdum@9
|
99 |
moveRobot(nextRobotCell);
|
|
indvdum@9
|
100 |
lambdaCollectedCount++;
|
|
indvdum@9
|
101 |
score += 25;
|
|
indvdum@9
|
102 |
} else if (nextRobotCellContent == OpenLambdaLift) {
|
|
indvdum@9
|
103 |
moveRobot(nextRobotCell);
|
|
indvdum@9
|
104 |
score += 50 * lambdaCollectedCount;
|
|
indvdum@9
|
105 |
|
|
indvdum@9
|
106 |
lastScoreChange = score - prevScore;
|
|
indvdum@9
|
107 |
state = GameState.Win;
|
|
indvdum@9
|
108 |
return state;
|
|
indvdum@23
|
109 |
} else if (robotCoord.right().equals(nextRobotCoord) && nextRobotCellContent == Rock
|
|
indvdum@9
|
110 |
&& getContent(mine.getCell(nextRobotCoord.right())) == Empty) {
|
|
indvdum@9
|
111 |
moveRobot(nextRobotCell);
|
|
indvdum@9
|
112 |
mine.getCell(nextRobotCoord.right()).setContent(Rock);
|
|
indvdum@23
|
113 |
} else if (robotCoord.left().equals(nextRobotCoord) && nextRobotCellContent == Rock
|
|
indvdum@9
|
114 |
&& getContent(mine.getCell(nextRobotCoord.left())) == Empty) {
|
|
indvdum@9
|
115 |
moveRobot(nextRobotCell);
|
|
indvdum@9
|
116 |
mine.getCell(nextRobotCoord.left()).setContent(Rock);
|
|
indvdum@9
|
117 |
} else {
|
|
indvdum@9
|
118 |
mov = Movement.WAIT;
|
|
indvdum@9
|
119 |
}
|
|
indvdum@9
|
120 |
|
|
indvdum@9
|
121 |
// World changes
|
|
indvdum@22
|
122 |
Cell[][] newCells = mine.cloneCells();
|
|
indvdum@9
|
123 |
for (int y = 1; y <= mine.getSizeY(); y++) {
|
|
indvdum@9
|
124 |
for (int x = 1; x <= mine.getSizeX(); x++) {
|
|
indvdum@9
|
125 |
Coordinate curCoord = new Coordinate(x, y);
|
|
indvdum@9
|
126 |
Coordinate leftCoord = curCoord.left();
|
|
indvdum@9
|
127 |
Coordinate upCoord = curCoord.up();
|
|
indvdum@9
|
128 |
Coordinate rightCoord = curCoord.right();
|
|
indvdum@9
|
129 |
Coordinate downCoord = curCoord.down();
|
|
indvdum@9
|
130 |
Coordinate leftAndUpCoord = curCoord.leftAndUp();
|
|
indvdum@9
|
131 |
Coordinate rightAndUpCoord = curCoord.rightAndUp();
|
|
indvdum@9
|
132 |
Coordinate rightAndDownCoord = curCoord.rightAndDown();
|
|
indvdum@9
|
133 |
Coordinate leftAndDownCoord = curCoord.leftAndDown();
|
|
indvdum@9
|
134 |
Cell curCell = mine.getCell(curCoord);
|
|
indvdum@9
|
135 |
Cell leftCell = mine.getCell(leftCoord);
|
|
indvdum@9
|
136 |
Cell upCell = mine.getCell(upCoord);
|
|
indvdum@9
|
137 |
Cell rightCell = mine.getCell(rightCoord);
|
|
indvdum@9
|
138 |
Cell downCell = mine.getCell(downCoord);
|
|
indvdum@9
|
139 |
Cell leftAndUpCell = mine.getCell(leftAndUpCoord);
|
|
indvdum@9
|
140 |
Cell rightAndUpCell = mine.getCell(rightAndUpCoord);
|
|
indvdum@9
|
141 |
Cell rightAndDownCell = mine.getCell(rightAndDownCoord);
|
|
indvdum@9
|
142 |
Cell leftAndDownCell = mine.getCell(leftAndDownCoord);
|
|
indvdum@9
|
143 |
|
|
indvdum@9
|
144 |
if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
|
|
indvdum@9
|
145 |
getCell(curCoord, newCells).setContent(Empty);
|
|
indvdum@9
|
146 |
getCell(downCoord, newCells).setContent(Rock);
|
|
indvdum@9
|
147 |
} else if (getContent(curCell) == Rock && getContent(downCell) == Empty
|
|
indvdum@9
|
148 |
&& getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
|
|
indvdum@9
|
149 |
getCell(curCoord, newCells).setContent(Empty);
|
|
indvdum@9
|
150 |
getCell(rightAndDownCoord, newCells).setContent(Rock);
|
|
indvdum@9
|
151 |
} else if (getContent(curCell) == Rock && getContent(downCell) == Rock
|
|
indvdum@9
|
152 |
&& (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
|
|
indvdum@9
|
153 |
&& getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
|
|
indvdum@9
|
154 |
getCell(curCoord, newCells).setContent(Empty);
|
|
indvdum@9
|
155 |
getCell(leftAndDownCoord, newCells).setContent(Rock);
|
|
indvdum@9
|
156 |
} else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
|
|
indvdum@9
|
157 |
&& getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
|
|
indvdum@9
|
158 |
getCell(curCoord, newCells).setContent(Empty);
|
|
indvdum@9
|
159 |
getCell(rightAndDownCoord, newCells).setContent(Rock);
|
|
indvdum@9
|
160 |
} else if (getContent(curCell) == ClosedLambdaLift && mine.findCells(Lambda).size() == 0) {
|
|
indvdum@9
|
161 |
getCell(curCoord, newCells).setContent(OpenLambdaLift);
|
|
indvdum@9
|
162 |
}
|
|
indvdum@9
|
163 |
}
|
|
indvdum@9
|
164 |
}
|
|
indvdum@22
|
165 |
|
|
indvdum@22
|
166 |
// Other ending conditions
|
|
indvdum@22
|
167 |
robotCoord = mine.getRobotCell().getCoordinate();
|
|
indvdum@22
|
168 |
if (getContent(mine.getCell(robotCoord.up())) != Rock && getContent(getCell(robotCoord.up(), newCells)) == Rock) {
|
|
indvdum@22
|
169 |
mine.setCells(newCells);
|
|
indvdum@25
|
170 |
|
|
indvdum@22
|
171 |
lastScoreChange = score - prevScore;
|
|
indvdum@22
|
172 |
state = GameState.Lose;
|
|
indvdum@22
|
173 |
return state;
|
|
indvdum@22
|
174 |
}
|
|
indvdum@22
|
175 |
|
|
indvdum@9
|
176 |
mine.setCells(newCells);
|
|
indvdum@9
|
177 |
|
|
indvdum@25
|
178 |
// Water flooding
|
|
indvdum@25
|
179 |
robotCoord = mine.getRobotCell().getCoordinate();
|
|
indvdum@25
|
180 |
if (robotCoord.y <= mine.getWaterLevel()) {
|
|
indvdum@25
|
181 |
underwater++;
|
|
indvdum@25
|
182 |
} else {
|
|
indvdum@25
|
183 |
underwater = 0;
|
|
indvdum@25
|
184 |
}
|
|
indvdum@25
|
185 |
if (underwater > mine.getRobotWaterproof()) {
|
|
indvdum@25
|
186 |
lastScoreChange = score - prevScore;
|
|
indvdum@25
|
187 |
state = GameState.Lose;
|
|
indvdum@25
|
188 |
return state;
|
|
indvdum@25
|
189 |
}
|
|
indvdum@25
|
190 |
if (mine.getFlooding() > 0 && route.size() % mine.getFlooding() == 0) {
|
|
indvdum@25
|
191 |
mine.setWaterLevel(mine.getWaterLevel() + 1);
|
|
indvdum@25
|
192 |
}
|
|
indvdum@25
|
193 |
|
|
indvdum@9
|
194 |
lastScoreChange = score - prevScore;
|
|
indvdum@9
|
195 |
state = GameState.Game;
|
|
indvdum@9
|
196 |
return state;
|
|
indvdum@9
|
197 |
}
|
|
indvdum@9
|
198 |
|
|
indvdum@16
|
199 |
public GameState move(List<Movement> movs) {
|
|
indvdum@16
|
200 |
for (Movement mov : movs) {
|
|
indvdum@16
|
201 |
if (move(mov) != GameState.Game)
|
|
indvdum@16
|
202 |
return state;
|
|
indvdum@16
|
203 |
}
|
|
indvdum@16
|
204 |
return state;
|
|
indvdum@16
|
205 |
}
|
|
indvdum@16
|
206 |
|
|
indvdum@19
|
207 |
public GameState move(String route) {
|
|
indvdum@16
|
208 |
List<Movement> movs = new ArrayList<Movement>();
|
|
indvdum@19
|
209 |
for (int i = 0; i < route.length(); i++) {
|
|
indvdum@19
|
210 |
String s = "" + route.charAt(i);
|
|
indvdum@16
|
211 |
for (Movement mov : Movement.values()) {
|
|
indvdum@16
|
212 |
if (mov.toText().equals(s)) {
|
|
indvdum@16
|
213 |
movs.add(mov);
|
|
indvdum@16
|
214 |
break;
|
|
indvdum@16
|
215 |
}
|
|
indvdum@16
|
216 |
}
|
|
indvdum@16
|
217 |
}
|
|
indvdum@16
|
218 |
return move(movs);
|
|
indvdum@16
|
219 |
}
|
|
indvdum@16
|
220 |
|
|
indvdum@9
|
221 |
protected CellContent getContent(Cell cell) {
|
|
indvdum@9
|
222 |
if (cell == null)
|
|
indvdum@9
|
223 |
return null;
|
|
indvdum@9
|
224 |
return cell.getContent();
|
|
indvdum@9
|
225 |
}
|
|
indvdum@9
|
226 |
|
|
indvdum@9
|
227 |
public int getLastScoreChange() {
|
|
indvdum@9
|
228 |
return lastScoreChange;
|
|
indvdum@9
|
229 |
}
|
|
indvdum@9
|
230 |
|
|
indvdum@9
|
231 |
public int getScore() {
|
|
indvdum@9
|
232 |
return score;
|
|
indvdum@9
|
233 |
}
|
|
indvdum@9
|
234 |
|
|
indvdum@9
|
235 |
public GameState getState() {
|
|
indvdum@9
|
236 |
return state;
|
|
indvdum@9
|
237 |
}
|
|
indvdum@9
|
238 |
|
|
indvdum@9
|
239 |
public int getLambdaCollectedCount() {
|
|
indvdum@9
|
240 |
return lambdaCollectedCount;
|
|
indvdum@9
|
241 |
}
|
|
indvdum@20
|
242 |
|
|
indvdum@20
|
243 |
public List<Movement> getRoute() {
|
|
indvdum@20
|
244 |
return route;
|
|
indvdum@20
|
245 |
}
|
|
indvdum@20
|
246 |
|
|
indvdum@20
|
247 |
public String getStringRoute() {
|
|
indvdum@20
|
248 |
String movs = "";
|
|
indvdum@20
|
249 |
for (Movement mov : route) {
|
|
indvdum@20
|
250 |
movs += mov.toText();
|
|
indvdum@20
|
251 |
}
|
|
indvdum@20
|
252 |
return movs;
|
|
indvdum@20
|
253 |
}
|
|
indvdum@25
|
254 |
|
|
indvdum@25
|
255 |
public int getUnderwater() {
|
|
indvdum@25
|
256 |
return underwater;
|
|
indvdum@25
|
257 |
}
|
|
indvdum@9
|
258 |
}
|