1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/icfp-model/src/main/java/ru/bosony/model/game/Game.java Sat Jul 14 23:21:08 2012 +0400
1.3 @@ -0,0 +1,199 @@
1.4 +package ru.bosony.model.game;
1.5 +
1.6 +import static ru.bosony.model.cellscontents.CellContent.ClosedLambdaLift;
1.7 +import static ru.bosony.model.cellscontents.CellContent.Earth;
1.8 +import static ru.bosony.model.cellscontents.CellContent.Empty;
1.9 +import static ru.bosony.model.cellscontents.CellContent.Lambda;
1.10 +import static ru.bosony.model.cellscontents.CellContent.MiningRobot;
1.11 +import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift;
1.12 +import static ru.bosony.model.cellscontents.CellContent.Rock;
1.13 +
1.14 +import java.util.Arrays;
1.15 +
1.16 +import ru.bosony.model.cellscontents.CellContent;
1.17 +import ru.bosony.model.mine.Cell;
1.18 +import ru.bosony.model.mine.Coordinate;
1.19 +import ru.bosony.model.mine.Mine;
1.20 +import ru.bosony.model.moving.Movement;
1.21 +
1.22 +/**
1.23 + * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
1.24 + * 14.07.2012 22:51:20
1.25 + *
1.26 + */
1.27 +public class Game {
1.28 +
1.29 + Mine mine = null;
1.30 + private Cell[][] prevStepCells;
1.31 + protected int score = 0;
1.32 + protected int lastScoreChange = 0;
1.33 + protected GameState state = GameState.Game;
1.34 + protected int lambdaCollectedCount = 0;
1.35 +
1.36 + public Game(Mine mine) {
1.37 + this.mine = mine;
1.38 + }
1.39 +
1.40 + public Mine getMine() {
1.41 + return mine;
1.42 + }
1.43 +
1.44 + protected Cell getCell(Coordinate coordinate, Cell[][] cells) {
1.45 + if (coordinate == null)
1.46 + return null;
1.47 + int x = coordinate.x;
1.48 + int y = coordinate.y;
1.49 + if (cells == null)
1.50 + return null;
1.51 + if (x < 1 || x > mine.getSizeX() || y < 1 || y > mine.getSizeY())
1.52 + return null;
1.53 + return cells[x - 1][mine.getSizeY() - y];
1.54 + }
1.55 +
1.56 + protected void moveRobot(Cell nextRobotCell) {
1.57 + mine.getRobotCell().setContent(Empty);
1.58 + nextRobotCell.setContent(MiningRobot);
1.59 + mine.setRobotCell(nextRobotCell);
1.60 + }
1.61 +
1.62 + public GameState move(Movement mov) {
1.63 + if (state != GameState.Game)
1.64 + throw new RuntimeException("Game over!");
1.65 +
1.66 + // Score
1.67 + int prevScore = score;
1.68 + score--;
1.69 +
1.70 + // Moving and scoring
1.71 + Coordinate nextRobotCoord = null;
1.72 + switch (mov) {
1.73 + case ABORT:
1.74 + score += 25 * lambdaCollectedCount;
1.75 +
1.76 + lastScoreChange = score - prevScore;
1.77 + state = GameState.Abort;
1.78 + return state;
1.79 + case WAIT:
1.80 + break;
1.81 + case LEFT:
1.82 + nextRobotCoord = mine.getRobotCell().getCoordinate().left();
1.83 + break;
1.84 + case UP:
1.85 + nextRobotCoord = mine.getRobotCell().getCoordinate().up();
1.86 + break;
1.87 + case RIGHT:
1.88 + nextRobotCoord = mine.getRobotCell().getCoordinate().right();
1.89 + break;
1.90 + case DOWN:
1.91 + nextRobotCoord = mine.getRobotCell().getCoordinate().down();
1.92 + break;
1.93 + }
1.94 + Cell nextRobotCell = mine.getCell(nextRobotCoord);
1.95 + CellContent nextRobotCellContent = getContent(nextRobotCell);
1.96 + if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) {
1.97 + moveRobot(nextRobotCell);
1.98 + } else if (nextRobotCellContent == Lambda) {
1.99 + moveRobot(nextRobotCell);
1.100 + lambdaCollectedCount++;
1.101 + score += 25;
1.102 + } else if (nextRobotCellContent == OpenLambdaLift) {
1.103 + moveRobot(nextRobotCell);
1.104 + score += 50 * lambdaCollectedCount;
1.105 +
1.106 + lastScoreChange = score - prevScore;
1.107 + state = GameState.Win;
1.108 + return state;
1.109 + } else if (mine.getRobotCell().getCoordinate().right() == nextRobotCoord && nextRobotCellContent == Rock
1.110 + && getContent(mine.getCell(nextRobotCoord.right())) == Empty) {
1.111 + moveRobot(nextRobotCell);
1.112 + mine.getCell(nextRobotCoord.right()).setContent(Rock);
1.113 + } else if (mine.getRobotCell().getCoordinate().left() == nextRobotCoord && nextRobotCellContent == Rock
1.114 + && getContent(mine.getCell(nextRobotCoord.left())) == Empty) {
1.115 + moveRobot(nextRobotCell);
1.116 + mine.getCell(nextRobotCoord.left()).setContent(Rock);
1.117 + } else {
1.118 + mov = Movement.WAIT;
1.119 + }
1.120 +
1.121 + // Other ending conditions
1.122 + if (getContent(getCell(mine.getRobotCell().getCoordinate().up(), prevStepCells)) != Rock
1.123 + && getContent(mine.getCell(mine.getRobotCell().getCoordinate().up())) == Rock) {
1.124 + lastScoreChange = score - prevScore;
1.125 + state = GameState.Lose;
1.126 + return state;
1.127 + }
1.128 +
1.129 + // World changes
1.130 + Cell[][] newCells = Arrays.copyOf(mine.getCells(), mine.getCells().length);
1.131 + for (int y = 1; y <= mine.getSizeY(); y++) {
1.132 + for (int x = 1; x <= mine.getSizeX(); x++) {
1.133 + Coordinate curCoord = new Coordinate(x, y);
1.134 + Coordinate leftCoord = curCoord.left();
1.135 + Coordinate upCoord = curCoord.up();
1.136 + Coordinate rightCoord = curCoord.right();
1.137 + Coordinate downCoord = curCoord.down();
1.138 + Coordinate leftAndUpCoord = curCoord.leftAndUp();
1.139 + Coordinate rightAndUpCoord = curCoord.rightAndUp();
1.140 + Coordinate rightAndDownCoord = curCoord.rightAndDown();
1.141 + Coordinate leftAndDownCoord = curCoord.leftAndDown();
1.142 + Cell curCell = mine.getCell(curCoord);
1.143 + Cell leftCell = mine.getCell(leftCoord);
1.144 + Cell upCell = mine.getCell(upCoord);
1.145 + Cell rightCell = mine.getCell(rightCoord);
1.146 + Cell downCell = mine.getCell(downCoord);
1.147 + Cell leftAndUpCell = mine.getCell(leftAndUpCoord);
1.148 + Cell rightAndUpCell = mine.getCell(rightAndUpCoord);
1.149 + Cell rightAndDownCell = mine.getCell(rightAndDownCoord);
1.150 + Cell leftAndDownCell = mine.getCell(leftAndDownCoord);
1.151 +
1.152 + if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
1.153 + getCell(curCoord, newCells).setContent(Empty);
1.154 + getCell(downCoord, newCells).setContent(Rock);
1.155 + } else if (getContent(curCell) == Rock && getContent(downCell) == Empty
1.156 + && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
1.157 + getCell(curCoord, newCells).setContent(Empty);
1.158 + getCell(rightAndDownCoord, newCells).setContent(Rock);
1.159 + } else if (getContent(curCell) == Rock && getContent(downCell) == Rock
1.160 + && (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
1.161 + && getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
1.162 + getCell(curCoord, newCells).setContent(Empty);
1.163 + getCell(leftAndDownCoord, newCells).setContent(Rock);
1.164 + } else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
1.165 + && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
1.166 + getCell(curCoord, newCells).setContent(Empty);
1.167 + getCell(rightAndDownCoord, newCells).setContent(Rock);
1.168 + } else if (getContent(curCell) == ClosedLambdaLift && mine.findCells(Lambda).size() == 0) {
1.169 + getCell(curCoord, newCells).setContent(OpenLambdaLift);
1.170 + }
1.171 + }
1.172 + }
1.173 + prevStepCells = mine.getCells();
1.174 + mine.setCells(newCells);
1.175 +
1.176 + lastScoreChange = score - prevScore;
1.177 + state = GameState.Game;
1.178 + return state;
1.179 + }
1.180 +
1.181 + protected CellContent getContent(Cell cell) {
1.182 + if (cell == null)
1.183 + return null;
1.184 + return cell.getContent();
1.185 + }
1.186 +
1.187 + public int getLastScoreChange() {
1.188 + return lastScoreChange;
1.189 + }
1.190 +
1.191 + public int getScore() {
1.192 + return score;
1.193 + }
1.194 +
1.195 + public GameState getState() {
1.196 + return state;
1.197 + }
1.198 +
1.199 + public int getLambdaCollectedCount() {
1.200 + return lambdaCollectedCount;
1.201 + }
1.202 +}
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/icfp-model/src/main/java/ru/bosony/model/game/GameState.java Sat Jul 14 23:21:08 2012 +0400
2.3 @@ -0,0 +1,13 @@
2.4 +package ru.bosony.model.game;
2.5 +
2.6 +/**
2.7 + * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
2.8 + * 14.07.2012 21:15:05
2.9 + *
2.10 + */
2.11 +public enum GameState {
2.12 + Game,
2.13 + Win,
2.14 + Abort,
2.15 + Lose,
2.16 +}
3.1 --- a/icfp-model/src/main/java/ru/bosony/model/mine/GameState.java Sat Jul 14 22:49:59 2012 +0400
3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
3.3 @@ -1,13 +0,0 @@
3.4 -package ru.bosony.model.mine;
3.5 -
3.6 -/**
3.7 - * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
3.8 - * 14.07.2012 21:15:05
3.9 - *
3.10 - */
3.11 -public enum GameState {
3.12 - Game,
3.13 - Win,
3.14 - Abort,
3.15 - Lose,
3.16 -}
4.1 --- a/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Sat Jul 14 22:49:59 2012 +0400
4.2 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Sat Jul 14 23:21:08 2012 +0400
4.3 @@ -1,15 +1,17 @@
4.4 package ru.bosony.model.mine;
4.5
4.6 -import java.util.Arrays;
4.7 -import java.util.Collection;
4.8 +import static ru.bosony.model.cellscontents.CellContent.ClosedLambdaLift;
4.9 +import static ru.bosony.model.cellscontents.CellContent.Earth;
4.10 +import static ru.bosony.model.cellscontents.CellContent.Empty;
4.11 +import static ru.bosony.model.cellscontents.CellContent.Lambda;
4.12 +import static ru.bosony.model.cellscontents.CellContent.MiningRobot;
4.13 +import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift;
4.14 +import static ru.bosony.model.cellscontents.CellContent.Rock;
4.15 +
4.16 import java.util.HashSet;
4.17 -import java.util.List;
4.18 import java.util.Set;
4.19
4.20 -import org.hamcrest.CoreMatchers;
4.21 -
4.22 import ru.bosony.model.cellscontents.CellContent;
4.23 -import static ru.bosony.model.cellscontents.CellContent.*;
4.24 import ru.bosony.model.io.TextRepresentable;
4.25 import ru.bosony.model.moving.Movement;
4.26
4.27 @@ -20,15 +22,10 @@
4.28 */
4.29 public class Mine implements TextRepresentable {
4.30
4.31 - private Cell[][] cells;
4.32 - private Cell[][] prevStepCells;
4.33 + protected Cell[][] cells;
4.34 protected int sizeX;
4.35 protected int sizeY;
4.36 protected Cell robotCell;
4.37 - protected int score = 0;
4.38 - protected int lastScoreChange = 0;
4.39 - protected GameState state = GameState.Game;
4.40 - protected int lambdaCollectedCount = 0;
4.41
4.42 public Mine(String text) {
4.43 fromText(text);
4.44 @@ -38,6 +35,8 @@
4.45 throw new RuntimeException("New map does not contain a robot!");
4.46 if (findCells(ClosedLambdaLift).size() != 1)
4.47 throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
4.48 + for (Cell cell : findCells(MiningRobot))
4.49 + robotCell = cell;
4.50 }
4.51
4.52 @Override
4.53 @@ -52,7 +51,7 @@
4.54 return text.replaceAll("\n*$", "");
4.55 }
4.56
4.57 - private void fromText(String text) {
4.58 + protected void fromText(String text) {
4.59 String[] rows = text.split("\n+");
4.60 int y = rows.length;
4.61 int x = 0;
4.62 @@ -73,23 +72,15 @@
4.63 content = Empty;
4.64 Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
4.65 cells[curX][curY] = cell;
4.66 - if (content == MiningRobot)
4.67 - robotCell = cell;
4.68 }
4.69 }
4.70 }
4.71
4.72 public Cell getCell(Coordinate coordinate) {
4.73 - return getCell(coordinate, cells);
4.74 - }
4.75 -
4.76 - protected Cell getCell(Coordinate coordinate, Cell[][] cells) {
4.77 if (coordinate == null)
4.78 return null;
4.79 int x = coordinate.x;
4.80 int y = coordinate.y;
4.81 - if (cells == null)
4.82 - return null;
4.83 if (x < 1 || x > sizeX || y < 1 || y > sizeY)
4.84 return null;
4.85 return cells[x - 1][sizeY - y];
4.86 @@ -140,137 +131,8 @@
4.87 return cells;
4.88 }
4.89
4.90 - protected void moveRobot(Cell nextRobotCell) {
4.91 - robotCell.setContent(Empty);
4.92 - nextRobotCell.setContent(MiningRobot);
4.93 - robotCell = nextRobotCell;
4.94 - }
4.95 -
4.96 - public GameState move(Movement mov) {
4.97 - if (state != GameState.Game)
4.98 - throw new RuntimeException("Game over!");
4.99 -
4.100 - // Score
4.101 - int prevScore = score;
4.102 - score--;
4.103 -
4.104 - // Moving and scoring
4.105 - Coordinate nextRobotCoord = null;
4.106 - switch (mov) {
4.107 - case ABORT:
4.108 - score += 25 * lambdaCollectedCount;
4.109 -
4.110 - lastScoreChange = score - prevScore;
4.111 - state = GameState.Abort;
4.112 - return state;
4.113 - case WAIT:
4.114 - break;
4.115 - case LEFT:
4.116 - nextRobotCoord = robotCell.coordinate.left();
4.117 - break;
4.118 - case UP:
4.119 - nextRobotCoord = robotCell.coordinate.up();
4.120 - break;
4.121 - case RIGHT:
4.122 - nextRobotCoord = robotCell.coordinate.right();
4.123 - break;
4.124 - case DOWN:
4.125 - nextRobotCoord = robotCell.coordinate.down();
4.126 - break;
4.127 - }
4.128 - Cell nextRobotCell = getCell(nextRobotCoord);
4.129 - CellContent nextRobotCellContent = getContent(nextRobotCell);
4.130 - if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) {
4.131 - moveRobot(nextRobotCell);
4.132 - } else if (nextRobotCellContent == Lambda) {
4.133 - moveRobot(nextRobotCell);
4.134 - lambdaCollectedCount++;
4.135 - score += 25;
4.136 - } else if (nextRobotCellContent == OpenLambdaLift) {
4.137 - moveRobot(nextRobotCell);
4.138 - score += 50 * lambdaCollectedCount;
4.139 -
4.140 - lastScoreChange = score - prevScore;
4.141 - state = GameState.Win;
4.142 - return state;
4.143 - } else if (robotCell.coordinate.right() == nextRobotCoord && nextRobotCellContent == Rock && getContent(getCell(nextRobotCoord.right())) == Empty) {
4.144 - moveRobot(nextRobotCell);
4.145 - getCell(nextRobotCoord.right()).setContent(Rock);
4.146 - } else if (robotCell.coordinate.left() == nextRobotCoord && nextRobotCellContent == Rock && getContent(getCell(nextRobotCoord.left())) == Empty) {
4.147 - moveRobot(nextRobotCell);
4.148 - getCell(nextRobotCoord.left()).setContent(Rock);
4.149 - } else {
4.150 - mov = Movement.WAIT;
4.151 - }
4.152 -
4.153 - // Other ending conditions
4.154 - if (getContent(getCell(robotCell.coordinate.up(), prevStepCells)) != Rock
4.155 - && getContent(getCell(robotCell.coordinate.up())) == Rock) {
4.156 - lastScoreChange = score - prevScore;
4.157 - state = GameState.Lose;
4.158 - return state;
4.159 - }
4.160 -
4.161 - // World changes
4.162 - Cell[][] newCells = Arrays.copyOf(cells, cells.length);
4.163 - for (int y = 1; y <= sizeY; y++) {
4.164 - for (int x = 1; x <= sizeX; x++) {
4.165 - Coordinate curCoord = new Coordinate(x, y);
4.166 - Coordinate leftCoord = curCoord.left();
4.167 - Coordinate upCoord = curCoord.up();
4.168 - Coordinate rightCoord = curCoord.right();
4.169 - Coordinate downCoord = curCoord.down();
4.170 - Coordinate leftAndUpCoord = curCoord.leftAndUp();
4.171 - Coordinate rightAndUpCoord = curCoord.rightAndUp();
4.172 - Coordinate rightAndDownCoord = curCoord.rightAndDown();
4.173 - Coordinate leftAndDownCoord = curCoord.leftAndDown();
4.174 - Cell curCell = getCell(curCoord);
4.175 - Cell leftCell = getCell(leftCoord);
4.176 - Cell upCell = getCell(upCoord);
4.177 - Cell rightCell = getCell(rightCoord);
4.178 - Cell downCell = getCell(downCoord);
4.179 - Cell leftAndUpCell = getCell(leftAndUpCoord);
4.180 - Cell rightAndUpCell = getCell(rightAndUpCoord);
4.181 - Cell rightAndDownCell = getCell(rightAndDownCoord);
4.182 - Cell leftAndDownCell = getCell(leftAndDownCoord);
4.183 -
4.184 - if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
4.185 - getCell(curCoord, newCells).setContent(Empty);
4.186 - getCell(downCoord, newCells).setContent(Rock);
4.187 - } else if (getContent(curCell) == Rock && getContent(downCell) == Empty
4.188 - && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
4.189 - getCell(curCoord, newCells).setContent(Empty);
4.190 - getCell(rightAndDownCoord, newCells).setContent(Rock);
4.191 - } else if (getContent(curCell) == Rock && getContent(downCell) == Rock
4.192 - && (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
4.193 - && getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
4.194 - getCell(curCoord, newCells).setContent(Empty);
4.195 - getCell(leftAndDownCoord, newCells).setContent(Rock);
4.196 - } else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
4.197 - && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
4.198 - getCell(curCoord, newCells).setContent(Empty);
4.199 - getCell(rightAndDownCoord, newCells).setContent(Rock);
4.200 - } else if (getContent(curCell) == ClosedLambdaLift && findCells(Lambda).size() == 0) {
4.201 - getCell(curCoord, newCells).setContent(OpenLambdaLift);
4.202 - }
4.203 - }
4.204 - }
4.205 - prevStepCells = cells;
4.206 - cells = newCells;
4.207 -
4.208 - lastScoreChange = score - prevScore;
4.209 - state = GameState.Game;
4.210 - return state;
4.211 - }
4.212 -
4.213 - protected CellContent getContent(Cell cell) {
4.214 - if (cell == null)
4.215 - return null;
4.216 - return cell.getContent();
4.217 - }
4.218 -
4.219 - public Collection<Cell> findCells(CellContent content) {
4.220 - Collection<Cell> finded = new HashSet<Cell>();
4.221 + public Set<Cell> findCells(CellContent content) {
4.222 + Set<Cell> finded = new HashSet<Cell>();
4.223 for (int y = sizeY - 1; y >= 0; y--) {
4.224 for (int x = 0; x < sizeX - 1; x++) {
4.225 if (cells[x][y].getContent() == content)
4.226 @@ -280,8 +142,20 @@
4.227 return finded;
4.228 }
4.229
4.230 - public int getLastScoreChange() {
4.231 - return lastScoreChange;
4.232 + public Cell[][] getCells() {
4.233 + return cells;
4.234 + }
4.235 +
4.236 + public void setCells(Cell[][] cells) {
4.237 + this.cells = cells;
4.238 + }
4.239 +
4.240 + public Cell getRobotCell() {
4.241 + return robotCell;
4.242 + }
4.243 +
4.244 + public void setRobotCell(Cell robotCell) {
4.245 + this.robotCell = robotCell;
4.246 }
4.247
4.248 }
5.1 --- a/icfp-model/src/test/java/ru/bosony/tests/TestScore.java Sat Jul 14 22:49:59 2012 +0400
5.2 +++ b/icfp-model/src/test/java/ru/bosony/tests/TestScore.java Sat Jul 14 23:21:08 2012 +0400
5.3 @@ -4,6 +4,7 @@
5.4
5.5 import org.junit.Test;
5.6
5.7 +import ru.bosony.model.game.Game;
5.8 import ru.bosony.model.mine.Mine;
5.9 import ru.bosony.model.moving.Movement;
5.10
5.11 @@ -28,7 +29,9 @@
5.12 "L .\\#\n" +
5.13 "######";
5.14 Mine mine = new Mine(map);
5.15 - mine.move(Movement.LEFT);
5.16 + Game game = new Game(mine);
5.17 + game.move(Movement.ABORT);
5.18 + assertEquals(game.getScore(), -1);
5.19 }
5.20
5.21 }