1.1 --- a/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Sat Jul 14 22:49:59 2012 +0400
1.2 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Sat Jul 14 23:21:08 2012 +0400
1.3 @@ -1,15 +1,17 @@
1.4 package ru.bosony.model.mine;
1.5
1.6 -import java.util.Arrays;
1.7 -import java.util.Collection;
1.8 +import static ru.bosony.model.cellscontents.CellContent.ClosedLambdaLift;
1.9 +import static ru.bosony.model.cellscontents.CellContent.Earth;
1.10 +import static ru.bosony.model.cellscontents.CellContent.Empty;
1.11 +import static ru.bosony.model.cellscontents.CellContent.Lambda;
1.12 +import static ru.bosony.model.cellscontents.CellContent.MiningRobot;
1.13 +import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift;
1.14 +import static ru.bosony.model.cellscontents.CellContent.Rock;
1.15 +
1.16 import java.util.HashSet;
1.17 -import java.util.List;
1.18 import java.util.Set;
1.19
1.20 -import org.hamcrest.CoreMatchers;
1.21 -
1.22 import ru.bosony.model.cellscontents.CellContent;
1.23 -import static ru.bosony.model.cellscontents.CellContent.*;
1.24 import ru.bosony.model.io.TextRepresentable;
1.25 import ru.bosony.model.moving.Movement;
1.26
1.27 @@ -20,15 +22,10 @@
1.28 */
1.29 public class Mine implements TextRepresentable {
1.30
1.31 - private Cell[][] cells;
1.32 - private Cell[][] prevStepCells;
1.33 + protected Cell[][] cells;
1.34 protected int sizeX;
1.35 protected int sizeY;
1.36 protected Cell robotCell;
1.37 - protected int score = 0;
1.38 - protected int lastScoreChange = 0;
1.39 - protected GameState state = GameState.Game;
1.40 - protected int lambdaCollectedCount = 0;
1.41
1.42 public Mine(String text) {
1.43 fromText(text);
1.44 @@ -38,6 +35,8 @@
1.45 throw new RuntimeException("New map does not contain a robot!");
1.46 if (findCells(ClosedLambdaLift).size() != 1)
1.47 throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
1.48 + for (Cell cell : findCells(MiningRobot))
1.49 + robotCell = cell;
1.50 }
1.51
1.52 @Override
1.53 @@ -52,7 +51,7 @@
1.54 return text.replaceAll("\n*$", "");
1.55 }
1.56
1.57 - private void fromText(String text) {
1.58 + protected void fromText(String text) {
1.59 String[] rows = text.split("\n+");
1.60 int y = rows.length;
1.61 int x = 0;
1.62 @@ -73,23 +72,15 @@
1.63 content = Empty;
1.64 Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
1.65 cells[curX][curY] = cell;
1.66 - if (content == MiningRobot)
1.67 - robotCell = cell;
1.68 }
1.69 }
1.70 }
1.71
1.72 public Cell getCell(Coordinate coordinate) {
1.73 - return getCell(coordinate, cells);
1.74 - }
1.75 -
1.76 - protected Cell getCell(Coordinate coordinate, Cell[][] cells) {
1.77 if (coordinate == null)
1.78 return null;
1.79 int x = coordinate.x;
1.80 int y = coordinate.y;
1.81 - if (cells == null)
1.82 - return null;
1.83 if (x < 1 || x > sizeX || y < 1 || y > sizeY)
1.84 return null;
1.85 return cells[x - 1][sizeY - y];
1.86 @@ -140,137 +131,8 @@
1.87 return cells;
1.88 }
1.89
1.90 - protected void moveRobot(Cell nextRobotCell) {
1.91 - robotCell.setContent(Empty);
1.92 - nextRobotCell.setContent(MiningRobot);
1.93 - robotCell = nextRobotCell;
1.94 - }
1.95 -
1.96 - public GameState move(Movement mov) {
1.97 - if (state != GameState.Game)
1.98 - throw new RuntimeException("Game over!");
1.99 -
1.100 - // Score
1.101 - int prevScore = score;
1.102 - score--;
1.103 -
1.104 - // Moving and scoring
1.105 - Coordinate nextRobotCoord = null;
1.106 - switch (mov) {
1.107 - case ABORT:
1.108 - score += 25 * lambdaCollectedCount;
1.109 -
1.110 - lastScoreChange = score - prevScore;
1.111 - state = GameState.Abort;
1.112 - return state;
1.113 - case WAIT:
1.114 - break;
1.115 - case LEFT:
1.116 - nextRobotCoord = robotCell.coordinate.left();
1.117 - break;
1.118 - case UP:
1.119 - nextRobotCoord = robotCell.coordinate.up();
1.120 - break;
1.121 - case RIGHT:
1.122 - nextRobotCoord = robotCell.coordinate.right();
1.123 - break;
1.124 - case DOWN:
1.125 - nextRobotCoord = robotCell.coordinate.down();
1.126 - break;
1.127 - }
1.128 - Cell nextRobotCell = getCell(nextRobotCoord);
1.129 - CellContent nextRobotCellContent = getContent(nextRobotCell);
1.130 - if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) {
1.131 - moveRobot(nextRobotCell);
1.132 - } else if (nextRobotCellContent == Lambda) {
1.133 - moveRobot(nextRobotCell);
1.134 - lambdaCollectedCount++;
1.135 - score += 25;
1.136 - } else if (nextRobotCellContent == OpenLambdaLift) {
1.137 - moveRobot(nextRobotCell);
1.138 - score += 50 * lambdaCollectedCount;
1.139 -
1.140 - lastScoreChange = score - prevScore;
1.141 - state = GameState.Win;
1.142 - return state;
1.143 - } else if (robotCell.coordinate.right() == nextRobotCoord && nextRobotCellContent == Rock && getContent(getCell(nextRobotCoord.right())) == Empty) {
1.144 - moveRobot(nextRobotCell);
1.145 - getCell(nextRobotCoord.right()).setContent(Rock);
1.146 - } else if (robotCell.coordinate.left() == nextRobotCoord && nextRobotCellContent == Rock && getContent(getCell(nextRobotCoord.left())) == Empty) {
1.147 - moveRobot(nextRobotCell);
1.148 - getCell(nextRobotCoord.left()).setContent(Rock);
1.149 - } else {
1.150 - mov = Movement.WAIT;
1.151 - }
1.152 -
1.153 - // Other ending conditions
1.154 - if (getContent(getCell(robotCell.coordinate.up(), prevStepCells)) != Rock
1.155 - && getContent(getCell(robotCell.coordinate.up())) == Rock) {
1.156 - lastScoreChange = score - prevScore;
1.157 - state = GameState.Lose;
1.158 - return state;
1.159 - }
1.160 -
1.161 - // World changes
1.162 - Cell[][] newCells = Arrays.copyOf(cells, cells.length);
1.163 - for (int y = 1; y <= sizeY; y++) {
1.164 - for (int x = 1; x <= sizeX; x++) {
1.165 - Coordinate curCoord = new Coordinate(x, y);
1.166 - Coordinate leftCoord = curCoord.left();
1.167 - Coordinate upCoord = curCoord.up();
1.168 - Coordinate rightCoord = curCoord.right();
1.169 - Coordinate downCoord = curCoord.down();
1.170 - Coordinate leftAndUpCoord = curCoord.leftAndUp();
1.171 - Coordinate rightAndUpCoord = curCoord.rightAndUp();
1.172 - Coordinate rightAndDownCoord = curCoord.rightAndDown();
1.173 - Coordinate leftAndDownCoord = curCoord.leftAndDown();
1.174 - Cell curCell = getCell(curCoord);
1.175 - Cell leftCell = getCell(leftCoord);
1.176 - Cell upCell = getCell(upCoord);
1.177 - Cell rightCell = getCell(rightCoord);
1.178 - Cell downCell = getCell(downCoord);
1.179 - Cell leftAndUpCell = getCell(leftAndUpCoord);
1.180 - Cell rightAndUpCell = getCell(rightAndUpCoord);
1.181 - Cell rightAndDownCell = getCell(rightAndDownCoord);
1.182 - Cell leftAndDownCell = getCell(leftAndDownCoord);
1.183 -
1.184 - if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
1.185 - getCell(curCoord, newCells).setContent(Empty);
1.186 - getCell(downCoord, newCells).setContent(Rock);
1.187 - } else if (getContent(curCell) == Rock && getContent(downCell) == Empty
1.188 - && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
1.189 - getCell(curCoord, newCells).setContent(Empty);
1.190 - getCell(rightAndDownCoord, newCells).setContent(Rock);
1.191 - } else if (getContent(curCell) == Rock && getContent(downCell) == Rock
1.192 - && (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
1.193 - && getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
1.194 - getCell(curCoord, newCells).setContent(Empty);
1.195 - getCell(leftAndDownCoord, newCells).setContent(Rock);
1.196 - } else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
1.197 - && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
1.198 - getCell(curCoord, newCells).setContent(Empty);
1.199 - getCell(rightAndDownCoord, newCells).setContent(Rock);
1.200 - } else if (getContent(curCell) == ClosedLambdaLift && findCells(Lambda).size() == 0) {
1.201 - getCell(curCoord, newCells).setContent(OpenLambdaLift);
1.202 - }
1.203 - }
1.204 - }
1.205 - prevStepCells = cells;
1.206 - cells = newCells;
1.207 -
1.208 - lastScoreChange = score - prevScore;
1.209 - state = GameState.Game;
1.210 - return state;
1.211 - }
1.212 -
1.213 - protected CellContent getContent(Cell cell) {
1.214 - if (cell == null)
1.215 - return null;
1.216 - return cell.getContent();
1.217 - }
1.218 -
1.219 - public Collection<Cell> findCells(CellContent content) {
1.220 - Collection<Cell> finded = new HashSet<Cell>();
1.221 + public Set<Cell> findCells(CellContent content) {
1.222 + Set<Cell> finded = new HashSet<Cell>();
1.223 for (int y = sizeY - 1; y >= 0; y--) {
1.224 for (int x = 0; x < sizeX - 1; x++) {
1.225 if (cells[x][y].getContent() == content)
1.226 @@ -280,8 +142,20 @@
1.227 return finded;
1.228 }
1.229
1.230 - public int getLastScoreChange() {
1.231 - return lastScoreChange;
1.232 + public Cell[][] getCells() {
1.233 + return cells;
1.234 + }
1.235 +
1.236 + public void setCells(Cell[][] cells) {
1.237 + this.cells = cells;
1.238 + }
1.239 +
1.240 + public Cell getRobotCell() {
1.241 + return robotCell;
1.242 + }
1.243 +
1.244 + public void setRobotCell(Cell robotCell) {
1.245 + this.robotCell = robotCell;
1.246 }
1.247
1.248 }