# HG changeset patch # User indvdum (gotoindvdum[at]gmail[dot]com) # Date 1342310954 -14400 # Node ID a46d0179df7aa7d216969c27dc78fc4fcf241730 # Parent fade486c8eda42a4fb33a63cf6cbc556ecc390a2 Fixes according to FAQ diff -r fade486c8eda -r a46d0179df7a icfp-interpreter/src/main/java/ru/bosony/interpreters/TextInterpreter.java --- a/icfp-interpreter/src/main/java/ru/bosony/interpreters/TextInterpreter.java Sun Jul 15 02:08:45 2012 +0400 +++ b/icfp-interpreter/src/main/java/ru/bosony/interpreters/TextInterpreter.java Sun Jul 15 04:09:14 2012 +0400 @@ -51,6 +51,8 @@ writer.write("Score: " + game.getScore() + " (" + (game.getLastScoreChange() >= 0 ? "+" + game.getLastScoreChange() : game.getLastScoreChange()) + ")"); writer.newLine(); + writer.write("Steps: " + game.getRoute().size()); + writer.newLine(); writer.write("Route: " + game.getStringRoute()); writer.newLine(); writer.newLine(); @@ -60,9 +62,9 @@ writer.write("Available actions: "); String movs = ""; for (Movement mov : Movement.values()) { - movs += mov.toText() + ", "; + movs += mov.toText() + " (" + mov.toString().toLowerCase() + "), "; } - movs = movs.replaceAll(",\\\\s*$", ""); + movs = movs.replaceAll(",\\s*$", ""); writer.write(movs); writer.newLine(); writer.write("Print action(s): "); diff -r fade486c8eda -r a46d0179df7a icfp-model/src/main/java/ru/bosony/model/game/Game.java --- a/icfp-model/src/main/java/ru/bosony/model/game/Game.java Sun Jul 15 02:08:45 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/game/Game.java Sun Jul 15 04:09:14 2012 +0400 @@ -9,13 +9,12 @@ import static ru.bosony.model.cellscontents.CellContent.Rock; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import ru.bosony.model.cellscontents.CellContent; import ru.bosony.model.mine.Cell; -import ru.bosony.model.mine.Coordinate; import ru.bosony.model.mine.Mine; +import ru.bosony.model.moving.Coordinate; import ru.bosony.model.moving.Movement; /** @@ -25,8 +24,7 @@ */ public class Game { - Mine mine = null; - private Cell[][] prevStepCells; + protected Mine mine = null; protected int score = 0; protected int lastScoreChange = 0; protected GameState state = GameState.Game; @@ -56,7 +54,6 @@ protected void moveRobot(Cell nextRobotCell) { mine.getRobotCell().setContent(Empty); nextRobotCell.setContent(MiningRobot); - mine.setRobotCell(nextRobotCell); } public GameState move(Movement mov) { @@ -69,6 +66,7 @@ // Moving and scoring route.add(mov); + Coordinate robotCoord = mine.getRobotCell().getCoordinate(); Coordinate nextRobotCoord = null; switch (mov) { case ABORT: @@ -80,16 +78,16 @@ case WAIT: break; case LEFT: - nextRobotCoord = mine.getRobotCell().getCoordinate().left(); + nextRobotCoord = robotCoord.left(); break; case UP: - nextRobotCoord = mine.getRobotCell().getCoordinate().up(); + nextRobotCoord = robotCoord.up(); break; case RIGHT: - nextRobotCoord = mine.getRobotCell().getCoordinate().right(); + nextRobotCoord = robotCoord.right(); break; case DOWN: - nextRobotCoord = mine.getRobotCell().getCoordinate().down(); + nextRobotCoord = robotCoord.down(); break; } Cell nextRobotCell = mine.getCell(nextRobotCoord); @@ -107,11 +105,11 @@ lastScoreChange = score - prevScore; state = GameState.Win; return state; - } else if (mine.getRobotCell().getCoordinate().right() == nextRobotCoord && nextRobotCellContent == Rock + } else if (robotCoord.right() == nextRobotCoord && nextRobotCellContent == Rock && getContent(mine.getCell(nextRobotCoord.right())) == Empty) { moveRobot(nextRobotCell); mine.getCell(nextRobotCoord.right()).setContent(Rock); - } else if (mine.getRobotCell().getCoordinate().left() == nextRobotCoord && nextRobotCellContent == Rock + } else if (robotCoord.left() == nextRobotCoord && nextRobotCellContent == Rock && getContent(mine.getCell(nextRobotCoord.left())) == Empty) { moveRobot(nextRobotCell); mine.getCell(nextRobotCoord.left()).setContent(Rock); @@ -119,16 +117,8 @@ mov = Movement.WAIT; } - // Other ending conditions - if (getContent(getCell(mine.getRobotCell().getCoordinate().up(), prevStepCells)) != Rock - && getContent(mine.getCell(mine.getRobotCell().getCoordinate().up())) == Rock) { - lastScoreChange = score - prevScore; - state = GameState.Lose; - return state; - } - // World changes - Cell[][] newCells = Arrays.copyOf(mine.getCells(), mine.getCells().length); + Cell[][] newCells = mine.cloneCells(); for (int y = 1; y <= mine.getSizeY(); y++) { for (int x = 1; x <= mine.getSizeX(); x++) { Coordinate curCoord = new Coordinate(x, y); @@ -171,7 +161,16 @@ } } } - prevStepCells = mine.getCells(); + + // Other ending conditions + robotCoord = mine.getRobotCell().getCoordinate(); + if (getContent(mine.getCell(robotCoord.up())) != Rock && getContent(getCell(robotCoord.up(), newCells)) == Rock) { + mine.setCells(newCells); + lastScoreChange = score - prevScore; + state = GameState.Lose; + return state; + } + mine.setCells(newCells); lastScoreChange = score - prevScore; diff -r fade486c8eda -r a46d0179df7a icfp-model/src/main/java/ru/bosony/model/mine/Cell.java --- a/icfp-model/src/main/java/ru/bosony/model/mine/Cell.java Sun Jul 15 02:08:45 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Cell.java Sun Jul 15 04:09:14 2012 +0400 @@ -1,13 +1,14 @@ package ru.bosony.model.mine; import ru.bosony.model.cellscontents.CellContent; +import ru.bosony.model.moving.Coordinate; /** * @author indvdum (gotoindvdum[at]gmail[dot]com)
* 13.07.2012 22:10:18 * */ -public class Cell { +public class Cell implements Cloneable { protected Coordinate coordinate; protected CellContent content; @@ -29,4 +30,10 @@ return coordinate; } + @Override + protected Cell clone() throws CloneNotSupportedException { + Cell clone = new Cell(coordinate, content); + return clone; + } + } diff -r fade486c8eda -r a46d0179df7a icfp-model/src/main/java/ru/bosony/model/mine/Coordinate.java --- a/icfp-model/src/main/java/ru/bosony/model/mine/Coordinate.java Sun Jul 15 02:08:45 2012 +0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,92 +0,0 @@ -package ru.bosony.model.mine; - -import java.util.HashSet; -import java.util.Set; - -import ru.bosony.model.moving.Movement; - -/** - * @author indvdum (gotoindvdum[at]gmail[dot]com)
- * 14.07.2012 1:07:51 - * - */ -public class Coordinate { - - public Coordinate(int x, int y) { - this.x = x; - this.y = y; - } - - public int x; - public int y; - - public Coordinate left() { - return new Coordinate(x - 1, y); - } - - public Coordinate up() { - return new Coordinate(x, y + 1); - } - - public Coordinate right() { - return new Coordinate(x + 1, y); - } - - public Coordinate down() { - return new Coordinate(x, y - 1); - } - - public Coordinate leftAndUp() { - return new Coordinate(x - 1, y + 1); - } - - public Coordinate rightAndUp() { - return new Coordinate(x + 1, y + 1); - } - - public Coordinate rightAndDown() { - return new Coordinate(x + 1, y - 1); - } - - public Coordinate leftAndDown() { - return new Coordinate(x - 1, y - 1); - } - - public Set getNeighboringCoordinates() { - Set coords = new HashSet(); - coords.add(left()); - coords.add(up()); - coords.add(right()); - coords.add(down()); - coords.add(leftAndUp()); - coords.add(rightAndUp()); - coords.add(rightAndDown()); - coords.add(leftAndDown()); - return coords; - } - - public Movement getNecessaryMovement(Coordinate nCoordinate) { - if (left().equals(nCoordinate)) - return Movement.LEFT; - if (up().equals(nCoordinate)) - return Movement.UP; - if (right().equals(nCoordinate)) - return Movement.RIGHT; - if (down().equals(nCoordinate)) - return Movement.DOWN; - return null; - } - - @Override - public int hashCode() { - return x ^ y; - } - - @Override - public boolean equals(Object obj) { - if (obj instanceof Coordinate) - return x == ((Coordinate)obj).x && y == ((Coordinate)obj).y; - return super.equals(obj); - } - -} diff -r fade486c8eda -r a46d0179df7a icfp-model/src/main/java/ru/bosony/model/mine/Mine.java --- a/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Sun Jul 15 02:08:45 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Sun Jul 15 04:09:14 2012 +0400 @@ -13,6 +13,7 @@ import ru.bosony.model.cellscontents.CellContent; import ru.bosony.model.io.TextRepresentable; +import ru.bosony.model.moving.Coordinate; import ru.bosony.model.moving.Movement; /** @@ -25,7 +26,6 @@ protected Cell[][] cells; protected int sizeX; protected int sizeY; - protected Cell robotCell; public Mine(String text) { fromText(text); @@ -35,8 +35,6 @@ throw new RuntimeException("New map does not contain a robot!"); if (findCells(ClosedLambdaLift).size() != 1) throw new RuntimeException("New map does not contain a ClosedLambdaLift!"); - for (Cell cell : findCells(MiningRobot)) - robotCell = cell; } @Override @@ -70,6 +68,8 @@ content = CellContent.fromText("" + row.charAt(curX)); else content = Empty; + if (content == null) + content = Empty; Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content); cells[curX][curY] = cell; } @@ -101,6 +101,7 @@ public Set getAvailableRobotMovements() { Set movs = new HashSet(); + Cell robotCell = getRobotCell(); Set nCells = getNeighboringCells(robotCell); for (Cell nCell : nCells) { CellContent nContent = nCell.getContent(); @@ -142,20 +143,29 @@ return finded; } - public Cell[][] getCells() { - return cells; - } - public void setCells(Cell[][] cells) { this.cells = cells; } public Cell getRobotCell() { - return robotCell; + for (Cell cell : findCells(MiningRobot)) { + return cell; + } + throw new RuntimeException("Where is robot!?"); } - public void setRobotCell(Cell robotCell) { - this.robotCell = robotCell; + public Cell[][] cloneCells() { + Cell[][] clone = new Cell[sizeX][sizeY]; + for (int y = sizeY - 1; y >= 0; y--) { + for (int x = 0; x < sizeX; x++) { + try { + clone[x][y] = cells[x][y].clone(); + } catch (CloneNotSupportedException e) { + throw new RuntimeException(e); + } + } + } + return clone; } } diff -r fade486c8eda -r a46d0179df7a icfp-model/src/main/java/ru/bosony/model/moving/Coordinate.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/icfp-model/src/main/java/ru/bosony/model/moving/Coordinate.java Sun Jul 15 04:09:14 2012 +0400 @@ -0,0 +1,91 @@ +package ru.bosony.model.moving; + +import java.util.HashSet; +import java.util.Set; + + +/** + * @author indvdum (gotoindvdum[at]gmail[dot]com)
+ * 14.07.2012 1:07:51 + * + */ +public class Coordinate { + + public Coordinate(int x, int y) { + this.x = x; + this.y = y; + } + + public int x; + public int y; + + public Coordinate left() { + return new Coordinate(x - 1, y); + } + + public Coordinate up() { + return new Coordinate(x, y + 1); + } + + public Coordinate right() { + return new Coordinate(x + 1, y); + } + + public Coordinate down() { + return new Coordinate(x, y - 1); + } + + public Coordinate leftAndUp() { + return new Coordinate(x - 1, y + 1); + } + + public Coordinate rightAndUp() { + return new Coordinate(x + 1, y + 1); + } + + public Coordinate rightAndDown() { + return new Coordinate(x + 1, y - 1); + } + + public Coordinate leftAndDown() { + return new Coordinate(x - 1, y - 1); + } + + public Set getNeighboringCoordinates() { + Set coords = new HashSet(); + coords.add(left()); + coords.add(up()); + coords.add(right()); + coords.add(down()); + coords.add(leftAndUp()); + coords.add(rightAndUp()); + coords.add(rightAndDown()); + coords.add(leftAndDown()); + return coords; + } + + public Movement getNecessaryMovement(Coordinate nCoordinate) { + if (left().equals(nCoordinate)) + return Movement.LEFT; + if (up().equals(nCoordinate)) + return Movement.UP; + if (right().equals(nCoordinate)) + return Movement.RIGHT; + if (down().equals(nCoordinate)) + return Movement.DOWN; + return null; + } + + @Override + public int hashCode() { + return x ^ y; + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof Coordinate) + return x == ((Coordinate)obj).x && y == ((Coordinate)obj).y; + return super.equals(obj); + } + +}