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