7 import static ru.bosony.model.cellscontents.CellContent.MiningRobot; |
7 import static ru.bosony.model.cellscontents.CellContent.MiningRobot; |
8 import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift; |
8 import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift; |
9 import static ru.bosony.model.cellscontents.CellContent.Rock; |
9 import static ru.bosony.model.cellscontents.CellContent.Rock; |
10 |
10 |
11 import java.util.ArrayList; |
11 import java.util.ArrayList; |
12 import java.util.Arrays; |
|
13 import java.util.List; |
12 import java.util.List; |
14 |
13 |
15 import ru.bosony.model.cellscontents.CellContent; |
14 import ru.bosony.model.cellscontents.CellContent; |
16 import ru.bosony.model.mine.Cell; |
15 import ru.bosony.model.mine.Cell; |
17 import ru.bosony.model.mine.Coordinate; |
|
18 import ru.bosony.model.mine.Mine; |
16 import ru.bosony.model.mine.Mine; |
|
17 import ru.bosony.model.moving.Coordinate; |
19 import ru.bosony.model.moving.Movement; |
18 import ru.bosony.model.moving.Movement; |
20 |
19 |
21 /** |
20 /** |
22 * @author indvdum (gotoindvdum[at]gmail[dot]com) <br> |
21 * @author indvdum (gotoindvdum[at]gmail[dot]com) <br> |
23 * 14.07.2012 22:51:20 |
22 * 14.07.2012 22:51:20 |
24 * |
23 * |
25 */ |
24 */ |
26 public class Game { |
25 public class Game { |
27 |
26 |
28 Mine mine = null; |
27 protected Mine mine = null; |
29 private Cell[][] prevStepCells; |
|
30 protected int score = 0; |
28 protected int score = 0; |
31 protected int lastScoreChange = 0; |
29 protected int lastScoreChange = 0; |
32 protected GameState state = GameState.Game; |
30 protected GameState state = GameState.Game; |
33 protected int lambdaCollectedCount = 0; |
31 protected int lambdaCollectedCount = 0; |
34 protected List<Movement> route = new ArrayList<Movement>(); |
32 protected List<Movement> route = new ArrayList<Movement>(); |
54 } |
52 } |
55 |
53 |
56 protected void moveRobot(Cell nextRobotCell) { |
54 protected void moveRobot(Cell nextRobotCell) { |
57 mine.getRobotCell().setContent(Empty); |
55 mine.getRobotCell().setContent(Empty); |
58 nextRobotCell.setContent(MiningRobot); |
56 nextRobotCell.setContent(MiningRobot); |
59 mine.setRobotCell(nextRobotCell); |
|
60 } |
57 } |
61 |
58 |
62 public GameState move(Movement mov) { |
59 public GameState move(Movement mov) { |
63 if (state != GameState.Game) |
60 if (state != GameState.Game) |
64 throw new RuntimeException("Game over!"); |
61 throw new RuntimeException("Game over!"); |
78 state = GameState.Abort; |
76 state = GameState.Abort; |
79 return state; |
77 return state; |
80 case WAIT: |
78 case WAIT: |
81 break; |
79 break; |
82 case LEFT: |
80 case LEFT: |
83 nextRobotCoord = mine.getRobotCell().getCoordinate().left(); |
81 nextRobotCoord = robotCoord.left(); |
84 break; |
82 break; |
85 case UP: |
83 case UP: |
86 nextRobotCoord = mine.getRobotCell().getCoordinate().up(); |
84 nextRobotCoord = robotCoord.up(); |
87 break; |
85 break; |
88 case RIGHT: |
86 case RIGHT: |
89 nextRobotCoord = mine.getRobotCell().getCoordinate().right(); |
87 nextRobotCoord = robotCoord.right(); |
90 break; |
88 break; |
91 case DOWN: |
89 case DOWN: |
92 nextRobotCoord = mine.getRobotCell().getCoordinate().down(); |
90 nextRobotCoord = robotCoord.down(); |
93 break; |
91 break; |
94 } |
92 } |
95 Cell nextRobotCell = mine.getCell(nextRobotCoord); |
93 Cell nextRobotCell = mine.getCell(nextRobotCoord); |
96 CellContent nextRobotCellContent = getContent(nextRobotCell); |
94 CellContent nextRobotCellContent = getContent(nextRobotCell); |
97 if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) { |
95 if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) { |
105 score += 50 * lambdaCollectedCount; |
103 score += 50 * lambdaCollectedCount; |
106 |
104 |
107 lastScoreChange = score - prevScore; |
105 lastScoreChange = score - prevScore; |
108 state = GameState.Win; |
106 state = GameState.Win; |
109 return state; |
107 return state; |
110 } else if (mine.getRobotCell().getCoordinate().right() == nextRobotCoord && nextRobotCellContent == Rock |
108 } else if (robotCoord.right() == nextRobotCoord && nextRobotCellContent == Rock |
111 && getContent(mine.getCell(nextRobotCoord.right())) == Empty) { |
109 && getContent(mine.getCell(nextRobotCoord.right())) == Empty) { |
112 moveRobot(nextRobotCell); |
110 moveRobot(nextRobotCell); |
113 mine.getCell(nextRobotCoord.right()).setContent(Rock); |
111 mine.getCell(nextRobotCoord.right()).setContent(Rock); |
114 } else if (mine.getRobotCell().getCoordinate().left() == nextRobotCoord && nextRobotCellContent == Rock |
112 } else if (robotCoord.left() == nextRobotCoord && nextRobotCellContent == Rock |
115 && getContent(mine.getCell(nextRobotCoord.left())) == Empty) { |
113 && getContent(mine.getCell(nextRobotCoord.left())) == Empty) { |
116 moveRobot(nextRobotCell); |
114 moveRobot(nextRobotCell); |
117 mine.getCell(nextRobotCoord.left()).setContent(Rock); |
115 mine.getCell(nextRobotCoord.left()).setContent(Rock); |
118 } else { |
116 } else { |
119 mov = Movement.WAIT; |
117 mov = Movement.WAIT; |
120 } |
118 } |
121 |
119 |
122 // Other ending conditions |
|
123 if (getContent(getCell(mine.getRobotCell().getCoordinate().up(), prevStepCells)) != Rock |
|
124 && getContent(mine.getCell(mine.getRobotCell().getCoordinate().up())) == Rock) { |
|
125 lastScoreChange = score - prevScore; |
|
126 state = GameState.Lose; |
|
127 return state; |
|
128 } |
|
129 |
|
130 // World changes |
120 // World changes |
131 Cell[][] newCells = Arrays.copyOf(mine.getCells(), mine.getCells().length); |
121 Cell[][] newCells = mine.cloneCells(); |
132 for (int y = 1; y <= mine.getSizeY(); y++) { |
122 for (int y = 1; y <= mine.getSizeY(); y++) { |
133 for (int x = 1; x <= mine.getSizeX(); x++) { |
123 for (int x = 1; x <= mine.getSizeX(); x++) { |
134 Coordinate curCoord = new Coordinate(x, y); |
124 Coordinate curCoord = new Coordinate(x, y); |
135 Coordinate leftCoord = curCoord.left(); |
125 Coordinate leftCoord = curCoord.left(); |
136 Coordinate upCoord = curCoord.up(); |
126 Coordinate upCoord = curCoord.up(); |
169 } else if (getContent(curCell) == ClosedLambdaLift && mine.findCells(Lambda).size() == 0) { |
159 } else if (getContent(curCell) == ClosedLambdaLift && mine.findCells(Lambda).size() == 0) { |
170 getCell(curCoord, newCells).setContent(OpenLambdaLift); |
160 getCell(curCoord, newCells).setContent(OpenLambdaLift); |
171 } |
161 } |
172 } |
162 } |
173 } |
163 } |
174 prevStepCells = mine.getCells(); |
164 |
|
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; |
|
171 return state; |
|
172 } |
|
173 |
175 mine.setCells(newCells); |
174 mine.setCells(newCells); |
176 |
175 |
177 lastScoreChange = score - prevScore; |
176 lastScoreChange = score - prevScore; |
178 state = GameState.Game; |
177 state = GameState.Game; |
179 return state; |
178 return state; |