# HG changeset patch # User indvdum (gotoindvdum[at]gmail[dot]com) # Date 1342217271 -14400 # Node ID 251961e44b5f22af207462e6a06afe6c017a461b # Parent 0dbbc8983926cf8b984a4d3f006e8f31bf14d683 Movements and Coordinates diff -r 0dbbc8983926 -r 251961e44b5f icfp-model/src/main/java/ru/bosony/model/mine/Cell.java --- a/icfp-model/src/main/java/ru/bosony/model/mine/Cell.java Sat Jul 14 00:36:43 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Cell.java Sat Jul 14 02:07:51 2012 +0400 @@ -9,13 +9,11 @@ */ public class Cell { - protected int x; - protected int y; + protected Coordinate coordinate; protected CellContent content; - public Cell(int x, int y, CellContent content) { - this.x = x; - this.y = y; + public Cell(Coordinate coordinate, CellContent content) { + this.coordinate = coordinate; this.content = content; } @@ -27,12 +25,8 @@ this.content = content; } - public int getX() { - return x; - } - - public int getY() { - return y; + public Coordinate getCoordinate() { + return coordinate; } } diff -r 0dbbc8983926 -r 251961e44b5f icfp-model/src/main/java/ru/bosony/model/mine/Coordinate.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Coordinate.java Sat Jul 14 02:07:51 2012 +0400 @@ -0,0 +1,92 @@ +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 top() { + return new Coordinate(x, y + 1); + } + + public Coordinate right() { + return new Coordinate(x + 1, y); + } + + public Coordinate bottom() { + return new Coordinate(x, y - 1); + } + + public Coordinate leftAndTop() { + return new Coordinate(x - 1, y + 1); + } + + public Coordinate rightAndTop() { + return new Coordinate(x + 1, y + 1); + } + + public Coordinate rightAndBottom() { + return new Coordinate(x + 1, y - 1); + } + + public Coordinate leftAndBottom() { + return new Coordinate(x - 1, y - 1); + } + + public Set getNeighboringCoordinates() { + Set coords = new HashSet(); + coords.add(left()); + coords.add(top()); + coords.add(right()); + coords.add(bottom()); + coords.add(leftAndTop()); + coords.add(rightAndTop()); + coords.add(rightAndBottom()); + coords.add(leftAndBottom()); + return coords; + } + + public Movement getNecessaryMovement(Coordinate nCoordinate) { + if (left().equals(nCoordinate)) + return Movement.LEFT; + if (top().equals(nCoordinate)) + return Movement.UP; + if (right().equals(nCoordinate)) + return Movement.RIGHT; + if (bottom().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 0dbbc8983926 -r 251961e44b5f icfp-model/src/main/java/ru/bosony/model/mine/Mine.java --- a/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Sat Jul 14 00:36:43 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Sat Jul 14 02:07:51 2012 +0400 @@ -1,13 +1,23 @@ package ru.bosony.model.mine; +import java.util.HashSet; +import java.util.Set; + import ru.bosony.model.cellscontents.CellContent; import ru.bosony.model.io.TextRepresentable; +import ru.bosony.model.moving.Movement; +/** + * @author indvdum (gotoindvdum[at]gmail[dot]com)
+ * 14.07.2012 2:02:18 + * + */ public class Mine implements TextRepresentable { - public Cell[][] cells; - protected int sizeX; - protected int sizeY; + private Cell[][] cells; + protected int sizeX; + protected int sizeY; + protected Cell robotCell; public Mine(String text) { fromText(text); @@ -44,15 +54,65 @@ content = CellContent.fromText("" + row.charAt(curX)); else content = CellContent.Empty; - Cell cell = new Cell(curX + 1, rows.length - curY, content); + Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content); cells[curX][curY] = cell; + if (content == CellContent.MiningRobot) + robotCell = cell; } } } + public Cell getCell(Coordinate coordinate) { + int x = coordinate.x; + int y = coordinate.y; + if (x < 1 || x > sizeX || y < 1 || y > sizeY) + return null; + return cells[x - 1][sizeY - y]; + } + @Override public String toString() { return toText(); } + public int getSizeX() { + return sizeX; + } + + public int getSizeY() { + return sizeY; + } + + public Set getAvailableRobotMovements() { + Set movs = new HashSet(); + Set nCells = getNeighboringCells(robotCell); + for (Cell nCell : nCells) { + CellContent nContent = nCell.getContent(); + Coordinate nCoordinate = nCell.getCoordinate(); + Cell rightNCell = getCell(nCoordinate.right()); + Cell leftNCell = getCell(nCoordinate.left()); + if (nContent == CellContent.Empty + || nContent == CellContent.Earth + || nContent == CellContent.Lambda + || nContent == CellContent.OpenLambdaLift + || (nContent == CellContent.Rock && robotCell.getCoordinate().right().equals(nCoordinate) + && rightNCell != null && rightNCell.getContent() == CellContent.Empty) + || (nContent == CellContent.Rock && robotCell.getCoordinate().left().equals(nCoordinate) + && leftNCell != null && leftNCell.getContent() == CellContent.Empty)) { + Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate); + movs.add(mov); + } + } + movs.add(Movement.ABORT); + movs.add(Movement.WAIT); + return movs; + } + + public Set getNeighboringCells(Cell cell) { + Set cells = new HashSet(); + for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates()) + cells.add(getCell(coordinate)); + return cells; + } + } diff -r 0dbbc8983926 -r 251961e44b5f icfp-model/src/main/java/ru/bosony/model/moving/Movement.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/icfp-model/src/main/java/ru/bosony/model/moving/Movement.java Sat Jul 14 02:07:51 2012 +0400 @@ -0,0 +1,38 @@ +package ru.bosony.model.moving; + +import ru.bosony.model.io.TextRepresentable; + +/** + * @author indvdum (gotoindvdum[at]gmail[dot]com)
+ * 14.07.2012 0:40:35 + * + */ +public enum Movement implements TextRepresentable { + + LEFT("L"), + RIGHT("R"), + UP("U"), + DOWN("D"), + WAIT("W"), + ABORT("A"); + + private String text; + + private Movement(String text) { + this.text = text; + } + + @Override + public String toText() { + return text; + } + + public static Movement fromText(String text) { + for (Movement content : values()) { + if (text != null && text.equals(content.toText())) + return content; + } + return null; + } + +} diff -r 0dbbc8983926 -r 251961e44b5f icfp-model/src/test/java/ru/bosony/tests/TestMineParsing.java --- a/icfp-model/src/test/java/ru/bosony/tests/TestMineParsing.java Sat Jul 14 00:36:43 2012 +0400 +++ b/icfp-model/src/test/java/ru/bosony/tests/TestMineParsing.java Sat Jul 14 02:07:51 2012 +0400 @@ -8,6 +8,11 @@ import ru.bosony.model.mine.Cell; import ru.bosony.model.mine.Mine; +/** + * @author indvdum (gotoindvdum[at]gmail[dot]com)
+ * 14.07.2012 0:37:14 + * + */ public class TestMineParsing { protected static String alignMap(String map) {