1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/.hgignore Sat Jul 14 23:27:11 2012 +0400
1.3 @@ -0,0 +1,8 @@
1.4 +syntax: glob
1.5 +*.log
1.6 +
1.7 +syntax: regexp
1.8 +.*target\/.*
1.9 +.*\.settings\/.*
1.10 +\.classpath
1.11 +\.project
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/icfp-model/pom.xml Sat Jul 14 23:27:11 2012 +0400
2.3 @@ -0,0 +1,61 @@
2.4 +<?xml version="1.0" encoding="UTF-8"?>
2.5 +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2.6 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2.7 + <modelVersion>4.0.0</modelVersion>
2.8 + <artifactId>icfp-model</artifactId>
2.9 + <name>icfp-model</name>
2.10 + <packaging>jar</packaging>
2.11 + <description>icfp-model</description>
2.12 + <parent>
2.13 + <groupId>ru.bosony.icfp-2012</groupId>
2.14 + <artifactId>icfp-2012-all</artifactId>
2.15 + <version>0.0.1.0-SNAPSHOT</version>
2.16 + </parent>
2.17 + <build>
2.18 + <plugins>
2.19 + <plugin>
2.20 + <artifactId>maven-compiler-plugin</artifactId>
2.21 + <version>${maven-compiler-plugin.version}</version>
2.22 + <configuration>
2.23 + <compilerId>groovy-eclipse-compiler</compilerId>
2.24 + <compilerArgument>nowarn</compilerArgument>
2.25 + <verbose>true</verbose>
2.26 + </configuration>
2.27 + <dependencies>
2.28 + <dependency>
2.29 + <groupId>org.codehaus.groovy</groupId>
2.30 + <artifactId>groovy-eclipse-compiler</artifactId>
2.31 + <version>${groovy-eclipse-compiler.version}</version>
2.32 + </dependency>
2.33 + </dependencies>
2.34 + </plugin>
2.35 + <plugin>
2.36 + <groupId>org.codehaus.mojo</groupId>
2.37 + <artifactId>build-helper-maven-plugin</artifactId>
2.38 + <version>${build-helper-maven-plugin.version}</version>
2.39 + <executions>
2.40 + <execution>
2.41 + <id>add-source</id>
2.42 + <phase>generate-sources</phase>
2.43 + <goals>
2.44 + <goal>add-source</goal>
2.45 + </goals>
2.46 + <configuration>
2.47 + <sources>
2.48 + <source>src/main/groovy</source>
2.49 + <source>src/test/groovy</source>
2.50 + </sources>
2.51 + </configuration>
2.52 + </execution>
2.53 + </executions>
2.54 + </plugin>
2.55 + </plugins>
2.56 + </build>
2.57 + <dependencies>
2.58 + <dependency>
2.59 + <groupId>org.codehaus.groovy</groupId>
2.60 + <artifactId>groovy-all</artifactId>
2.61 + <version>${groovy.version}</version>
2.62 + </dependency>
2.63 + </dependencies>
2.64 +</project>
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/icfp-model/src/main/java/ru/bosony/model/cellscontents/CellContent.java Sat Jul 14 23:27:11 2012 +0400
3.3 @@ -0,0 +1,40 @@
3.4 +package ru.bosony.model.cellscontents;
3.5 +
3.6 +import ru.bosony.model.io.TextRepresentable;
3.7 +
3.8 +/**
3.9 + * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
3.10 + * 13.07.2012 21:25:49
3.11 + *
3.12 + */
3.13 +public enum CellContent implements TextRepresentable {
3.14 +
3.15 + ClosedLambdaLift("L"),
3.16 + Earth("."),
3.17 + Empty(" "),
3.18 + Lambda("\\"),
3.19 + MiningRobot("R"),
3.20 + OpenLambdaLift("O"),
3.21 + Rock("*"),
3.22 + Wall("#");
3.23 +
3.24 + private String text;
3.25 +
3.26 + private CellContent(String text) {
3.27 + this.text = text;
3.28 + }
3.29 +
3.30 + @Override
3.31 + public String toText() {
3.32 + return text;
3.33 + }
3.34 +
3.35 + public static CellContent fromText(String text) {
3.36 + for (CellContent content : values()) {
3.37 + if (text != null && text.equals(content.toText()))
3.38 + return content;
3.39 + }
3.40 + return null;
3.41 + }
3.42 +
3.43 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/icfp-model/src/main/java/ru/bosony/model/game/Game.java Sat Jul 14 23:27:11 2012 +0400
4.3 @@ -0,0 +1,199 @@
4.4 +package ru.bosony.model.game;
4.5 +
4.6 +import static ru.bosony.model.cellscontents.CellContent.ClosedLambdaLift;
4.7 +import static ru.bosony.model.cellscontents.CellContent.Earth;
4.8 +import static ru.bosony.model.cellscontents.CellContent.Empty;
4.9 +import static ru.bosony.model.cellscontents.CellContent.Lambda;
4.10 +import static ru.bosony.model.cellscontents.CellContent.MiningRobot;
4.11 +import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift;
4.12 +import static ru.bosony.model.cellscontents.CellContent.Rock;
4.13 +
4.14 +import java.util.Arrays;
4.15 +
4.16 +import ru.bosony.model.cellscontents.CellContent;
4.17 +import ru.bosony.model.mine.Cell;
4.18 +import ru.bosony.model.mine.Coordinate;
4.19 +import ru.bosony.model.mine.Mine;
4.20 +import ru.bosony.model.moving.Movement;
4.21 +
4.22 +/**
4.23 + * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
4.24 + * 14.07.2012 22:51:20
4.25 + *
4.26 + */
4.27 +public class Game {
4.28 +
4.29 + Mine mine = null;
4.30 + private Cell[][] prevStepCells;
4.31 + protected int score = 0;
4.32 + protected int lastScoreChange = 0;
4.33 + protected GameState state = GameState.Game;
4.34 + protected int lambdaCollectedCount = 0;
4.35 +
4.36 + public Game(Mine mine) {
4.37 + this.mine = mine;
4.38 + }
4.39 +
4.40 + public Mine getMine() {
4.41 + return mine;
4.42 + }
4.43 +
4.44 + protected Cell getCell(Coordinate coordinate, Cell[][] cells) {
4.45 + if (coordinate == null)
4.46 + return null;
4.47 + int x = coordinate.x;
4.48 + int y = coordinate.y;
4.49 + if (cells == null)
4.50 + return null;
4.51 + if (x < 1 || x > mine.getSizeX() || y < 1 || y > mine.getSizeY())
4.52 + return null;
4.53 + return cells[x - 1][mine.getSizeY() - y];
4.54 + }
4.55 +
4.56 + protected void moveRobot(Cell nextRobotCell) {
4.57 + mine.getRobotCell().setContent(Empty);
4.58 + nextRobotCell.setContent(MiningRobot);
4.59 + mine.setRobotCell(nextRobotCell);
4.60 + }
4.61 +
4.62 + public GameState move(Movement mov) {
4.63 + if (state != GameState.Game)
4.64 + throw new RuntimeException("Game over!");
4.65 +
4.66 + // Score
4.67 + int prevScore = score;
4.68 + score--;
4.69 +
4.70 + // Moving and scoring
4.71 + Coordinate nextRobotCoord = null;
4.72 + switch (mov) {
4.73 + case ABORT:
4.74 + score += 25 * lambdaCollectedCount;
4.75 +
4.76 + lastScoreChange = score - prevScore;
4.77 + state = GameState.Abort;
4.78 + return state;
4.79 + case WAIT:
4.80 + break;
4.81 + case LEFT:
4.82 + nextRobotCoord = mine.getRobotCell().getCoordinate().left();
4.83 + break;
4.84 + case UP:
4.85 + nextRobotCoord = mine.getRobotCell().getCoordinate().up();
4.86 + break;
4.87 + case RIGHT:
4.88 + nextRobotCoord = mine.getRobotCell().getCoordinate().right();
4.89 + break;
4.90 + case DOWN:
4.91 + nextRobotCoord = mine.getRobotCell().getCoordinate().down();
4.92 + break;
4.93 + }
4.94 + Cell nextRobotCell = mine.getCell(nextRobotCoord);
4.95 + CellContent nextRobotCellContent = getContent(nextRobotCell);
4.96 + if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) {
4.97 + moveRobot(nextRobotCell);
4.98 + } else if (nextRobotCellContent == Lambda) {
4.99 + moveRobot(nextRobotCell);
4.100 + lambdaCollectedCount++;
4.101 + score += 25;
4.102 + } else if (nextRobotCellContent == OpenLambdaLift) {
4.103 + moveRobot(nextRobotCell);
4.104 + score += 50 * lambdaCollectedCount;
4.105 +
4.106 + lastScoreChange = score - prevScore;
4.107 + state = GameState.Win;
4.108 + return state;
4.109 + } else if (mine.getRobotCell().getCoordinate().right() == nextRobotCoord && nextRobotCellContent == Rock
4.110 + && getContent(mine.getCell(nextRobotCoord.right())) == Empty) {
4.111 + moveRobot(nextRobotCell);
4.112 + mine.getCell(nextRobotCoord.right()).setContent(Rock);
4.113 + } else if (mine.getRobotCell().getCoordinate().left() == nextRobotCoord && nextRobotCellContent == Rock
4.114 + && getContent(mine.getCell(nextRobotCoord.left())) == Empty) {
4.115 + moveRobot(nextRobotCell);
4.116 + mine.getCell(nextRobotCoord.left()).setContent(Rock);
4.117 + } else {
4.118 + mov = Movement.WAIT;
4.119 + }
4.120 +
4.121 + // Other ending conditions
4.122 + if (getContent(getCell(mine.getRobotCell().getCoordinate().up(), prevStepCells)) != Rock
4.123 + && getContent(mine.getCell(mine.getRobotCell().getCoordinate().up())) == Rock) {
4.124 + lastScoreChange = score - prevScore;
4.125 + state = GameState.Lose;
4.126 + return state;
4.127 + }
4.128 +
4.129 + // World changes
4.130 + Cell[][] newCells = Arrays.copyOf(mine.getCells(), mine.getCells().length);
4.131 + for (int y = 1; y <= mine.getSizeY(); y++) {
4.132 + for (int x = 1; x <= mine.getSizeX(); x++) {
4.133 + Coordinate curCoord = new Coordinate(x, y);
4.134 + Coordinate leftCoord = curCoord.left();
4.135 + Coordinate upCoord = curCoord.up();
4.136 + Coordinate rightCoord = curCoord.right();
4.137 + Coordinate downCoord = curCoord.down();
4.138 + Coordinate leftAndUpCoord = curCoord.leftAndUp();
4.139 + Coordinate rightAndUpCoord = curCoord.rightAndUp();
4.140 + Coordinate rightAndDownCoord = curCoord.rightAndDown();
4.141 + Coordinate leftAndDownCoord = curCoord.leftAndDown();
4.142 + Cell curCell = mine.getCell(curCoord);
4.143 + Cell leftCell = mine.getCell(leftCoord);
4.144 + Cell upCell = mine.getCell(upCoord);
4.145 + Cell rightCell = mine.getCell(rightCoord);
4.146 + Cell downCell = mine.getCell(downCoord);
4.147 + Cell leftAndUpCell = mine.getCell(leftAndUpCoord);
4.148 + Cell rightAndUpCell = mine.getCell(rightAndUpCoord);
4.149 + Cell rightAndDownCell = mine.getCell(rightAndDownCoord);
4.150 + Cell leftAndDownCell = mine.getCell(leftAndDownCoord);
4.151 +
4.152 + if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
4.153 + getCell(curCoord, newCells).setContent(Empty);
4.154 + getCell(downCoord, newCells).setContent(Rock);
4.155 + } else if (getContent(curCell) == Rock && getContent(downCell) == Empty
4.156 + && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
4.157 + getCell(curCoord, newCells).setContent(Empty);
4.158 + getCell(rightAndDownCoord, newCells).setContent(Rock);
4.159 + } else if (getContent(curCell) == Rock && getContent(downCell) == Rock
4.160 + && (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
4.161 + && getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
4.162 + getCell(curCoord, newCells).setContent(Empty);
4.163 + getCell(leftAndDownCoord, newCells).setContent(Rock);
4.164 + } else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
4.165 + && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
4.166 + getCell(curCoord, newCells).setContent(Empty);
4.167 + getCell(rightAndDownCoord, newCells).setContent(Rock);
4.168 + } else if (getContent(curCell) == ClosedLambdaLift && mine.findCells(Lambda).size() == 0) {
4.169 + getCell(curCoord, newCells).setContent(OpenLambdaLift);
4.170 + }
4.171 + }
4.172 + }
4.173 + prevStepCells = mine.getCells();
4.174 + mine.setCells(newCells);
4.175 +
4.176 + lastScoreChange = score - prevScore;
4.177 + state = GameState.Game;
4.178 + return state;
4.179 + }
4.180 +
4.181 + protected CellContent getContent(Cell cell) {
4.182 + if (cell == null)
4.183 + return null;
4.184 + return cell.getContent();
4.185 + }
4.186 +
4.187 + public int getLastScoreChange() {
4.188 + return lastScoreChange;
4.189 + }
4.190 +
4.191 + public int getScore() {
4.192 + return score;
4.193 + }
4.194 +
4.195 + public GameState getState() {
4.196 + return state;
4.197 + }
4.198 +
4.199 + public int getLambdaCollectedCount() {
4.200 + return lambdaCollectedCount;
4.201 + }
4.202 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/icfp-model/src/main/java/ru/bosony/model/game/GameState.java Sat Jul 14 23:27:11 2012 +0400
5.3 @@ -0,0 +1,13 @@
5.4 +package ru.bosony.model.game;
5.5 +
5.6 +/**
5.7 + * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
5.8 + * 14.07.2012 21:15:05
5.9 + *
5.10 + */
5.11 +public enum GameState {
5.12 + Game,
5.13 + Win,
5.14 + Abort,
5.15 + Lose,
5.16 +}
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/icfp-model/src/main/java/ru/bosony/model/io/TextRepresentable.java Sat Jul 14 23:27:11 2012 +0400
6.3 @@ -0,0 +1,12 @@
6.4 +package ru.bosony.model.io;
6.5 +
6.6 +/**
6.7 + * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
6.8 + * 13.07.2012 21:32:34
6.9 + *
6.10 + */
6.11 +public interface TextRepresentable {
6.12 +
6.13 + public String toText();
6.14 +
6.15 +}
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Cell.java Sat Jul 14 23:27:11 2012 +0400
7.3 @@ -0,0 +1,32 @@
7.4 +package ru.bosony.model.mine;
7.5 +
7.6 +import ru.bosony.model.cellscontents.CellContent;
7.7 +
7.8 +/**
7.9 + * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
7.10 + * 13.07.2012 22:10:18
7.11 + *
7.12 + */
7.13 +public class Cell {
7.14 +
7.15 + protected Coordinate coordinate;
7.16 + protected CellContent content;
7.17 +
7.18 + public Cell(Coordinate coordinate, CellContent content) {
7.19 + this.coordinate = coordinate;
7.20 + this.content = content;
7.21 + }
7.22 +
7.23 + public CellContent getContent() {
7.24 + return content;
7.25 + }
7.26 +
7.27 + public void setContent(CellContent content) {
7.28 + this.content = content;
7.29 + }
7.30 +
7.31 + public Coordinate getCoordinate() {
7.32 + return coordinate;
7.33 + }
7.34 +
7.35 +}
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
8.2 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Coordinate.java Sat Jul 14 23:27:11 2012 +0400
8.3 @@ -0,0 +1,92 @@
8.4 +package ru.bosony.model.mine;
8.5 +
8.6 +import java.util.HashSet;
8.7 +import java.util.Set;
8.8 +
8.9 +import ru.bosony.model.moving.Movement;
8.10 +
8.11 +/**
8.12 + * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
8.13 + * 14.07.2012 1:07:51
8.14 + *
8.15 + */
8.16 +public class Coordinate {
8.17 +
8.18 + public Coordinate(int x, int y) {
8.19 + this.x = x;
8.20 + this.y = y;
8.21 + }
8.22 +
8.23 + public int x;
8.24 + public int y;
8.25 +
8.26 + public Coordinate left() {
8.27 + return new Coordinate(x - 1, y);
8.28 + }
8.29 +
8.30 + public Coordinate up() {
8.31 + return new Coordinate(x, y + 1);
8.32 + }
8.33 +
8.34 + public Coordinate right() {
8.35 + return new Coordinate(x + 1, y);
8.36 + }
8.37 +
8.38 + public Coordinate down() {
8.39 + return new Coordinate(x, y - 1);
8.40 + }
8.41 +
8.42 + public Coordinate leftAndUp() {
8.43 + return new Coordinate(x - 1, y + 1);
8.44 + }
8.45 +
8.46 + public Coordinate rightAndUp() {
8.47 + return new Coordinate(x + 1, y + 1);
8.48 + }
8.49 +
8.50 + public Coordinate rightAndDown() {
8.51 + return new Coordinate(x + 1, y - 1);
8.52 + }
8.53 +
8.54 + public Coordinate leftAndDown() {
8.55 + return new Coordinate(x - 1, y - 1);
8.56 + }
8.57 +
8.58 + public Set<Coordinate> getNeighboringCoordinates() {
8.59 + Set<Coordinate> coords = new HashSet<Coordinate>();
8.60 + coords.add(left());
8.61 + coords.add(up());
8.62 + coords.add(right());
8.63 + coords.add(down());
8.64 + coords.add(leftAndUp());
8.65 + coords.add(rightAndUp());
8.66 + coords.add(rightAndDown());
8.67 + coords.add(leftAndDown());
8.68 + return coords;
8.69 + }
8.70 +
8.71 + public Movement getNecessaryMovement(Coordinate nCoordinate) {
8.72 + if (left().equals(nCoordinate))
8.73 + return Movement.LEFT;
8.74 + if (up().equals(nCoordinate))
8.75 + return Movement.UP;
8.76 + if (right().equals(nCoordinate))
8.77 + return Movement.RIGHT;
8.78 + if (down().equals(nCoordinate))
8.79 + return Movement.DOWN;
8.80 + return null;
8.81 + }
8.82 +
8.83 + @Override
8.84 + public int hashCode() {
8.85 + return x ^ y;
8.86 + }
8.87 +
8.88 + @Override
8.89 + public boolean equals(Object obj) {
8.90 + if (obj instanceof Coordinate)
8.91 + return x == ((Coordinate)obj).x && y == ((Coordinate)obj).y;
8.92 + return super.equals(obj);
8.93 + }
8.94 +
8.95 +}
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
9.2 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Sat Jul 14 23:27:11 2012 +0400
9.3 @@ -0,0 +1,161 @@
9.4 +package ru.bosony.model.mine;
9.5 +
9.6 +import static ru.bosony.model.cellscontents.CellContent.ClosedLambdaLift;
9.7 +import static ru.bosony.model.cellscontents.CellContent.Earth;
9.8 +import static ru.bosony.model.cellscontents.CellContent.Empty;
9.9 +import static ru.bosony.model.cellscontents.CellContent.Lambda;
9.10 +import static ru.bosony.model.cellscontents.CellContent.MiningRobot;
9.11 +import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift;
9.12 +import static ru.bosony.model.cellscontents.CellContent.Rock;
9.13 +
9.14 +import java.util.HashSet;
9.15 +import java.util.Set;
9.16 +
9.17 +import ru.bosony.model.cellscontents.CellContent;
9.18 +import ru.bosony.model.io.TextRepresentable;
9.19 +import ru.bosony.model.moving.Movement;
9.20 +
9.21 +/**
9.22 + * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
9.23 + * 14.07.2012 2:02:18
9.24 + *
9.25 + */
9.26 +public class Mine implements TextRepresentable {
9.27 +
9.28 + protected Cell[][] cells;
9.29 + protected int sizeX;
9.30 + protected int sizeY;
9.31 + protected Cell robotCell;
9.32 +
9.33 + public Mine(String text) {
9.34 + fromText(text);
9.35 + if (findCells(OpenLambdaLift).size() != 0)
9.36 + throw new RuntimeException("Detected OpenLambdaLift in new map!");
9.37 + if (findCells(MiningRobot).size() != 1)
9.38 + throw new RuntimeException("New map does not contain a robot!");
9.39 + if (findCells(ClosedLambdaLift).size() != 1)
9.40 + throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
9.41 + for (Cell cell : findCells(MiningRobot))
9.42 + robotCell = cell;
9.43 + }
9.44 +
9.45 + @Override
9.46 + public String toText() {
9.47 + String text = "";
9.48 + for (int y = 0; y < sizeY; y++) {
9.49 + for (int x = 0; x < sizeX; x++) {
9.50 + text += cells[x][y].getContent().toText();
9.51 + }
9.52 + text += "\n";
9.53 + }
9.54 + return text.replaceAll("\n*$", "");
9.55 + }
9.56 +
9.57 + protected void fromText(String text) {
9.58 + String[] rows = text.split("\n+");
9.59 + int y = rows.length;
9.60 + int x = 0;
9.61 + for (String row : rows) {
9.62 + if (x < row.length())
9.63 + x = row.length();
9.64 + }
9.65 + sizeX = x;
9.66 + sizeY = y;
9.67 + cells = new Cell[x][y];
9.68 + for (int curY = 0; curY < rows.length; curY++) {
9.69 + String row = rows[curY];
9.70 + for (int curX = 0; curX < row.length() || curX < x; curX++) {
9.71 + CellContent content = null;
9.72 + if (curX < row.length())
9.73 + content = CellContent.fromText("" + row.charAt(curX));
9.74 + else
9.75 + content = Empty;
9.76 + Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
9.77 + cells[curX][curY] = cell;
9.78 + }
9.79 + }
9.80 + }
9.81 +
9.82 + public Cell getCell(Coordinate coordinate) {
9.83 + if (coordinate == null)
9.84 + return null;
9.85 + int x = coordinate.x;
9.86 + int y = coordinate.y;
9.87 + if (x < 1 || x > sizeX || y < 1 || y > sizeY)
9.88 + return null;
9.89 + return cells[x - 1][sizeY - y];
9.90 + }
9.91 +
9.92 + @Override
9.93 + public String toString() {
9.94 + return toText();
9.95 + }
9.96 +
9.97 + public int getSizeX() {
9.98 + return sizeX;
9.99 + }
9.100 +
9.101 + public int getSizeY() {
9.102 + return sizeY;
9.103 + }
9.104 +
9.105 + public Set<Movement> getAvailableRobotMovements() {
9.106 + Set<Movement> movs = new HashSet<Movement>();
9.107 + Set<Cell> nCells = getNeighboringCells(robotCell);
9.108 + for (Cell nCell : nCells) {
9.109 + CellContent nContent = nCell.getContent();
9.110 + Coordinate nCoordinate = nCell.getCoordinate();
9.111 + Cell rightNCell = getCell(nCoordinate.right());
9.112 + Cell leftNCell = getCell(nCoordinate.left());
9.113 + if (nContent == Empty
9.114 + || nContent == Earth
9.115 + || nContent == Lambda
9.116 + || nContent == OpenLambdaLift
9.117 + || (nContent == Rock && robotCell.getCoordinate().right().equals(nCoordinate) && rightNCell != null && rightNCell
9.118 + .getContent() == Empty)
9.119 + || (nContent == Rock && robotCell.getCoordinate().left().equals(nCoordinate) && leftNCell != null && leftNCell
9.120 + .getContent() == Empty)) {
9.121 + Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate);
9.122 + movs.add(mov);
9.123 + }
9.124 + }
9.125 + movs.add(Movement.ABORT);
9.126 + movs.add(Movement.WAIT);
9.127 + return movs;
9.128 + }
9.129 +
9.130 + public Set<Cell> getNeighboringCells(Cell cell) {
9.131 + Set<Cell> cells = new HashSet<Cell>();
9.132 + for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates())
9.133 + cells.add(getCell(coordinate));
9.134 + return cells;
9.135 + }
9.136 +
9.137 + public Set<Cell> findCells(CellContent content) {
9.138 + Set<Cell> finded = new HashSet<Cell>();
9.139 + for (int y = sizeY - 1; y >= 0; y--) {
9.140 + for (int x = 0; x < sizeX - 1; x++) {
9.141 + if (cells[x][y].getContent() == content)
9.142 + finded.add(cells[x][y]);
9.143 + }
9.144 + }
9.145 + return finded;
9.146 + }
9.147 +
9.148 + public Cell[][] getCells() {
9.149 + return cells;
9.150 + }
9.151 +
9.152 + public void setCells(Cell[][] cells) {
9.153 + this.cells = cells;
9.154 + }
9.155 +
9.156 + public Cell getRobotCell() {
9.157 + return robotCell;
9.158 + }
9.159 +
9.160 + public void setRobotCell(Cell robotCell) {
9.161 + this.robotCell = robotCell;
9.162 + }
9.163 +
9.164 +}
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
10.2 +++ b/icfp-model/src/main/java/ru/bosony/model/moving/Movement.java Sat Jul 14 23:27:11 2012 +0400
10.3 @@ -0,0 +1,38 @@
10.4 +package ru.bosony.model.moving;
10.5 +
10.6 +import ru.bosony.model.io.TextRepresentable;
10.7 +
10.8 +/**
10.9 + * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
10.10 + * 14.07.2012 0:40:35
10.11 + *
10.12 + */
10.13 +public enum Movement implements TextRepresentable {
10.14 +
10.15 + LEFT("L"),
10.16 + RIGHT("R"),
10.17 + UP("U"),
10.18 + DOWN("D"),
10.19 + WAIT("W"),
10.20 + ABORT("A");
10.21 +
10.22 + private String text;
10.23 +
10.24 + private Movement(String text) {
10.25 + this.text = text;
10.26 + }
10.27 +
10.28 + @Override
10.29 + public String toText() {
10.30 + return text;
10.31 + }
10.32 +
10.33 + public static Movement fromText(String text) {
10.34 + for (Movement content : values()) {
10.35 + if (text != null && text.equals(content.toText()))
10.36 + return content;
10.37 + }
10.38 + return null;
10.39 + }
10.40 +
10.41 +}
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
11.2 +++ b/icfp-model/src/test/java/ru/bosony/tests/TestMineParsing.java Sat Jul 14 23:27:11 2012 +0400
11.3 @@ -0,0 +1,246 @@
11.4 +package ru.bosony.tests;
11.5 +
11.6 +import static org.junit.Assert.*;
11.7 +
11.8 +import org.junit.Test;
11.9 +
11.10 +import ru.bosony.model.cellscontents.CellContent;
11.11 +import ru.bosony.model.mine.Cell;
11.12 +import ru.bosony.model.mine.Mine;
11.13 +
11.14 +/**
11.15 + * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
11.16 + * 14.07.2012 0:37:14
11.17 + *
11.18 + */
11.19 +public class TestMineParsing {
11.20 +
11.21 + protected static String alignMap(String map) {
11.22 + String alignedMap = "";
11.23 + String[] rows = map.split("\n");
11.24 + int y = rows.length;
11.25 + int x = 0;
11.26 + for (String row : rows) {
11.27 + if (x < row.length())
11.28 + x = row.length();
11.29 + }
11.30 + for (int curY = 0; curY < rows.length; curY++) {
11.31 + String row = rows[curY];
11.32 + for (int curX = 0; curX < row.length() || curX < x; curX++) {
11.33 + if (curX < row.length())
11.34 + alignedMap += row.charAt(curX);
11.35 + else
11.36 + alignedMap += " ";
11.37 + }
11.38 + alignedMap += "\n";
11.39 + }
11.40 + return alignedMap.replaceAll("\n$", "");
11.41 + }
11.42 +
11.43 + @Test
11.44 + public void testMap01() {
11.45 + String map =
11.46 + "######\n" +
11.47 + "#. *R#\n" +
11.48 + "# \\.#\n" +
11.49 + "#\\ * #\n" +
11.50 + "L .\\#\n" +
11.51 + "######";
11.52 + Mine mine = new Mine(map);
11.53 + String parsed = mine.toText();
11.54 + assertEquals(alignMap(map), parsed);
11.55 + }
11.56 +
11.57 + @Test
11.58 + public void testMap02() {
11.59 + String map =
11.60 + "#######\n" +
11.61 + "#..***#\n" +
11.62 + "#..\\\\\\#\n" +
11.63 + "#...**#\n" +
11.64 + "#.*.*\\#\n" +
11.65 + "LR....#\n" +
11.66 + "#######";
11.67 + Mine mine = new Mine(map);
11.68 + String parsed = mine.toText();
11.69 + assertEquals(alignMap(map), parsed);
11.70 + }
11.71 +
11.72 + @Test
11.73 + public void testMap03() {
11.74 + String map =
11.75 + "########\n" +
11.76 + "#..R...#\n" +
11.77 + "#..*...#\n" +
11.78 + "#..#...#\n" +
11.79 + "#.\\.\\..L\n" +
11.80 + "####**.#\n" +
11.81 + "#\\.....#\n" +
11.82 + "#\\..* .#\n" +
11.83 + "########";
11.84 + Mine mine = new Mine(map);
11.85 + String parsed = mine.toText();
11.86 + assertEquals(alignMap(map), parsed);
11.87 + }
11.88 +
11.89 + @Test
11.90 + public void testMap04() {
11.91 + String map =
11.92 + "#########\n" +
11.93 + "#.*..#\\.#\n" +
11.94 + "#.\\..#\\.L\n" +
11.95 + "#.R .##.#\n" +
11.96 + "#.\\ ...#\n" +
11.97 + "#..\\ ..#\n" +
11.98 + "#...\\ ##\n" +
11.99 + "#....\\ \\#\n" +
11.100 + "#########";
11.101 + Mine mine = new Mine(map);
11.102 + String parsed = mine.toText();
11.103 + assertEquals(alignMap(map), parsed);
11.104 + }
11.105 +
11.106 + @Test
11.107 + public void testMap05() {
11.108 + String map =
11.109 + "############\n" +
11.110 + "#..........#\n" +
11.111 + "#.....*....#\n" +
11.112 + "#..\\\\\\\\\\\\..#\n" +
11.113 + "#. ....#\n" +
11.114 + "#..\\\\\\\\\\\\\\.#\n" +
11.115 + "#..\\.. .#\n" +
11.116 + "#..\\.. ....#\n" +
11.117 + "#..... ..* #\n" +
11.118 + "#..### ### #\n" +
11.119 + "#...R#\\#\\\\.#\n" +
11.120 + "######L#####";
11.121 + Mine mine = new Mine(map);
11.122 + String parsed = mine.toText();
11.123 + assertEquals(alignMap(map), parsed);
11.124 + }
11.125 +
11.126 + @Test
11.127 + public void testMap06() {
11.128 + String map =
11.129 + "###############\n" +
11.130 + "#\\\\\\.......** #\n" +
11.131 + "#\\\\#.#####...##\n" +
11.132 + "#\\\\#.....*##. #\n" +
11.133 + "#\\#####\\...## #\n" +
11.134 + "#\\......####* #\n" +
11.135 + "#\\.######* #.\\#\n" +
11.136 + "#\\.#. *...##.##\n" +
11.137 + "#\\##. .. *...#\n" +
11.138 + "#\\...... L#.#.#\n" +
11.139 + "###########.#.#\n" +
11.140 + "#\\..........#.#\n" +
11.141 + "##.##########.#\n" +
11.142 + "#R.#\\.........#\n" +
11.143 + "###############";
11.144 + Mine mine = new Mine(map);
11.145 + String parsed = mine.toText();
11.146 + assertEquals(alignMap(map), parsed);
11.147 + }
11.148 +
11.149 + @Test
11.150 + public void testMap07() {
11.151 + String map =
11.152 + " #######\n" +
11.153 + " ## *#\n" +
11.154 + " ##R *##\n" +
11.155 + " ##\\\\\\\\##\n" +
11.156 + " ##....##\n" +
11.157 + " ##..\\ . ##\n" +
11.158 + " ## . L . ##\n" +
11.159 + " ##\\\\\\# #\\\\\\\\##\n" +
11.160 + " ###### #######";
11.161 + Mine mine = new Mine(map);
11.162 + String parsed = mine.toText();
11.163 + assertEquals(alignMap(map), parsed);
11.164 + }
11.165 +
11.166 + @Test
11.167 + public void testMap08() {
11.168 + String map =
11.169 + "##############\n" +
11.170 + "#\\\\... ......#\n" +
11.171 + "###.#. ...*..#\n" +
11.172 + " #.#. ... ..#\n" +
11.173 + "### #. \\ ..#\n" +
11.174 + "#. .#..... **#######\n" +
11.175 + "#.#\\#..... ..\\\\\\*. #\n" +
11.176 + "#*\\\\#.###. ####\\\\\\ #\n" +
11.177 + "#\\\\.#. ...## \\ #\n" +
11.178 + "#\\#.#..... ....# \\ # \n" +
11.179 + "###.#..... ....# ##\n" +
11.180 + "#\\\\.#..... ....#\\ # \n" +
11.181 + "########.. ..###*####\n" +
11.182 + "#......... .........#\n" +
11.183 + "#......... ....***..#\n" +
11.184 + "#..\\\\\\\\\\ # ####.....#\n" +
11.185 + "#........*R..\\\\\\ .#\n" +
11.186 + "##########L##########";
11.187 + Mine mine = new Mine(map);
11.188 + String parsed = mine.toText();
11.189 + assertEquals(alignMap(map), parsed);
11.190 + }
11.191 +
11.192 + @Test
11.193 + public void testMap09() {
11.194 + String map =
11.195 + " #L#######\n" +
11.196 + " #*** \\\\ #\n" +
11.197 + " #\\\\\\ .. #\n" +
11.198 + "#########.## ##########\n" +
11.199 + "#.......\\ ..........* .#\n" +
11.200 + "#*******\\......#....#\\\\ .#\n" +
11.201 + "###\\.\\\\\\...**..#....... *#\n" +
11.202 + "#*****\\\\ .\\\\..## #\\.#\n" +
11.203 + "######### .... ##########\n" +
11.204 + " # #\n" +
11.205 + " ####*#### \n" +
11.206 + " #.......#\n" +
11.207 + "######### \\\\\\\\*##########\n" +
11.208 + "#*\\\\ **# *..*\\ \\\\\\\\\\#\n" +
11.209 + "#.\\**\\*** .....**.# \\\\##\\#\n" +
11.210 + "#\\R...... .\\\\.. \\\\\\\\\\#\n" +
11.211 + "##########################";
11.212 + Mine mine = new Mine(map);
11.213 + String parsed = mine.toText();
11.214 + assertEquals(alignMap(map), parsed);
11.215 + }
11.216 +
11.217 + @Test
11.218 + public void testMap10() {
11.219 + String map =
11.220 + "#############################\n" +
11.221 + "#..........................\\#\n" +
11.222 + "#..\\\\###...#.... ###.#\n" +
11.223 + "#..\\*\\\\\\.. #.... ..##\\\\..\\#.#\n" +
11.224 + "#..\\*\\.... #.... ..#\\#....#.#\n" +
11.225 + "#...\\###.. #.... ....#....#.#\n" +
11.226 + "#... ..... ..... .####......#\n" +
11.227 + "#\\\\. #.... .......#\n" +
11.228 + "#... #..#. .....*\\ ##.......#\n" +
11.229 + "#.#....... ...#.. ....######\n" +
11.230 + "#. ...#... ...#.\\ ....#..* #\n" +
11.231 + "##........ ...#.. #....#.#\\\\#\n" +
11.232 + "#.....*... .....*\\#\\\\.....*.#\n" +
11.233 + "#.***.* .......*\\****.....#.#\n" +
11.234 + "#.\\\\\\.. ................ .#\n" +
11.235 + "#.##### .###### ##### #\n" +
11.236 + "#....\\\\.................... #\n" +
11.237 + "#....****...#.##.....\\\\\\\\..\\#\n" +
11.238 + "#....\\\\\\\\...#.........*....\\#\n" +
11.239 + "#....\\\\\\\\...#.\\\\. #\\###.\\#\n" +
11.240 + "#.... ..#.... ...#\\\\\\\\. #\n" +
11.241 + "#........ ..#.... ...#..... #\n" +
11.242 + "#........ ........#R#\n" +
11.243 + "###########################L#";
11.244 + Mine mine = new Mine(map);
11.245 + String parsed = mine.toText();
11.246 + assertEquals(alignMap(map), parsed);
11.247 + }
11.248 +
11.249 +}
12.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
12.2 +++ b/icfp-model/src/test/java/ru/bosony/tests/TestMovements.java Sat Jul 14 23:27:11 2012 +0400
12.3 @@ -0,0 +1,47 @@
12.4 +package ru.bosony.tests;
12.5 +
12.6 +import static org.junit.Assert.*;
12.7 +
12.8 +import java.util.Set;
12.9 +
12.10 +import org.junit.Test;
12.11 +
12.12 +import ru.bosony.model.mine.Mine;
12.13 +import ru.bosony.model.moving.Movement;
12.14 +
12.15 +/**
12.16 + * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
12.17 + * 14.07.2012 2:29:05
12.18 + *
12.19 + */
12.20 +public class TestMovements {
12.21 +
12.22 + @Test
12.23 + public void test() {
12.24 + /* Map 01 (6x6)
12.25 + *
12.26 + ######
12.27 + #. *R#
12.28 + # \.#
12.29 + #\ * #
12.30 + L .\#
12.31 + ######
12.32 + */
12.33 + String map =
12.34 + "######\n" +
12.35 + "#. *R#\n" +
12.36 + "# \\.#\n" +
12.37 + "#\\ * #\n" +
12.38 + "L .\\#\n" +
12.39 + "######";
12.40 + Mine mine = new Mine(map);
12.41 + assertEquals(mine.getSizeX(), 6);
12.42 + assertEquals(mine.getSizeY(), 6);
12.43 + Set<Movement> movs = mine.getAvailableRobotMovements();
12.44 + assertFalse(movs.contains(Movement.UP));
12.45 + assertFalse(movs.contains(Movement.RIGHT));
12.46 + assertTrue(movs.contains(Movement.DOWN));
12.47 + assertTrue(movs.contains(Movement.LEFT));
12.48 + }
12.49 +
12.50 +}
13.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
13.2 +++ b/icfp-model/src/test/java/ru/bosony/tests/TestScore.java Sat Jul 14 23:27:11 2012 +0400
13.3 @@ -0,0 +1,37 @@
13.4 +package ru.bosony.tests;
13.5 +
13.6 +import static org.junit.Assert.*;
13.7 +
13.8 +import org.junit.Test;
13.9 +
13.10 +import ru.bosony.model.game.Game;
13.11 +import ru.bosony.model.mine.Mine;
13.12 +import ru.bosony.model.moving.Movement;
13.13 +
13.14 +public class TestScore {
13.15 +
13.16 + @Test
13.17 + public void test() {
13.18 + /* Map 01 (6x6)
13.19 + *
13.20 + ######
13.21 + #. *R#
13.22 + # \.#
13.23 + #\ * #
13.24 + L .\#
13.25 + ######
13.26 + */
13.27 + String map =
13.28 + "######\n" +
13.29 + "#. *R#\n" +
13.30 + "# \\.#\n" +
13.31 + "#\\ * #\n" +
13.32 + "L .\\#\n" +
13.33 + "######";
13.34 + Mine mine = new Mine(map);
13.35 + Game game = new Game(mine);
13.36 + game.move(Movement.ABORT);
13.37 + assertEquals(game.getScore(), -1);
13.38 + }
13.39 +
13.40 +}
14.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
14.2 +++ b/pom.xml Sat Jul 14 23:27:11 2012 +0400
14.3 @@ -0,0 +1,75 @@
14.4 +<?xml version="1.0" encoding="UTF-8"?>
14.5 +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
14.6 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
14.7 + <modelVersion>4.0.0</modelVersion>
14.8 + <groupId>ru.bosony.icfp-2012</groupId>
14.9 + <artifactId>icfp-2012-all</artifactId>
14.10 + <name>icfp-2012-all</name>
14.11 + <version>0.0.1.0-SNAPSHOT</version>
14.12 + <packaging>pom</packaging>
14.13 + <properties>
14.14 + <groovy.version>2.0.0</groovy.version>
14.15 + <maven-compiler-plugin.version>2.3.2</maven-compiler-plugin.version>
14.16 + <groovy-eclipse-compiler.version>2.5.1-1</groovy-eclipse-compiler.version>
14.17 + <build-helper-maven-plugin.version>1.5</build-helper-maven-plugin.version>
14.18 + <slf4j-simple.version>1.6.6</slf4j-simple.version>
14.19 + <maven-resources-plugin.version>2.5</maven-resources-plugin.version>
14.20 + <junit.version>4.10</junit.version>
14.21 + </properties>
14.22 + <build>
14.23 + <resources>
14.24 + <resource>
14.25 + <directory>src/main/resources</directory>
14.26 + <filtering>true</filtering>
14.27 + </resource>
14.28 + </resources>
14.29 + <plugins>
14.30 + <plugin>
14.31 + <groupId>org.apache.maven.plugins</groupId>
14.32 + <artifactId>maven-compiler-plugin</artifactId>
14.33 + <version>${maven-compiler-plugin.version}</version>
14.34 + <configuration>
14.35 + <source>1.6</source>
14.36 + <target>1.6</target>
14.37 + <encoding>UTF-8</encoding>
14.38 + <fork>true</fork>
14.39 + <meminitial>128m</meminitial>
14.40 + <maxmem>512m</maxmem>
14.41 + </configuration>
14.42 + </plugin>
14.43 + <plugin>
14.44 + <groupId>org.apache.maven.plugins</groupId>
14.45 + <artifactId>maven-resources-plugin</artifactId>
14.46 + <version>${maven-resources-plugin.version}</version>
14.47 + <configuration>
14.48 + <encoding>UTF-8</encoding>
14.49 + </configuration>
14.50 + </plugin>
14.51 + </plugins>
14.52 + </build>
14.53 + <dependencyManagement>
14.54 + <dependencies>
14.55 + <dependency>
14.56 + <groupId>ru.bosony.icfp-2012</groupId>
14.57 + <artifactId>icfp-model</artifactId>
14.58 + <version>${project.version}</version>
14.59 + </dependency>
14.60 + </dependencies>
14.61 + </dependencyManagement>
14.62 + <dependencies>
14.63 + <dependency>
14.64 + <groupId>org.slf4j</groupId>
14.65 + <artifactId>slf4j-simple</artifactId>
14.66 + <version>${slf4j-simple.version}</version>
14.67 + </dependency>
14.68 + <dependency>
14.69 + <groupId>junit</groupId>
14.70 + <artifactId>junit</artifactId>
14.71 + <version>${junit.version}</version>
14.72 + <scope>test</scope>
14.73 + </dependency>
14.74 + </dependencies>
14.75 + <modules>
14.76 + <module>icfp-model</module>
14.77 + </modules>
14.78 +</project>