# HG changeset patch # User indvdum (gotoindvdum[at]gmail[dot]com) # Date 1342291799 -14400 # Node ID ad884dc6877c751bcd6660cd78012aceaf6222e2 # Parent 251961e44b5f22af207462e6a06afe6c017a461b World processor diff -r 251961e44b5f -r ad884dc6877c icfp-model/src/main/java/ru/bosony/model/mine/Coordinate.java --- a/icfp-model/src/main/java/ru/bosony/model/mine/Coordinate.java Sat Jul 14 02:07:51 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Coordinate.java Sat Jul 14 22:49:59 2012 +0400 @@ -24,7 +24,7 @@ return new Coordinate(x - 1, y); } - public Coordinate top() { + public Coordinate up() { return new Coordinate(x, y + 1); } @@ -32,47 +32,47 @@ return new Coordinate(x + 1, y); } - public Coordinate bottom() { + public Coordinate down() { return new Coordinate(x, y - 1); } - public Coordinate leftAndTop() { + public Coordinate leftAndUp() { return new Coordinate(x - 1, y + 1); } - public Coordinate rightAndTop() { + public Coordinate rightAndUp() { return new Coordinate(x + 1, y + 1); } - public Coordinate rightAndBottom() { + public Coordinate rightAndDown() { return new Coordinate(x + 1, y - 1); } - public Coordinate leftAndBottom() { + public Coordinate leftAndDown() { return new Coordinate(x - 1, y - 1); } public Set getNeighboringCoordinates() { Set coords = new HashSet(); coords.add(left()); - coords.add(top()); + coords.add(up()); coords.add(right()); - coords.add(bottom()); - coords.add(leftAndTop()); - coords.add(rightAndTop()); - coords.add(rightAndBottom()); - coords.add(leftAndBottom()); + 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 (top().equals(nCoordinate)) + if (up().equals(nCoordinate)) return Movement.UP; if (right().equals(nCoordinate)) return Movement.RIGHT; - if (bottom().equals(nCoordinate)) + if (down().equals(nCoordinate)) return Movement.DOWN; return null; } diff -r 251961e44b5f -r ad884dc6877c icfp-model/src/main/java/ru/bosony/model/mine/GameState.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/GameState.java Sat Jul 14 22:49:59 2012 +0400 @@ -0,0 +1,13 @@ +package ru.bosony.model.mine; + +/** + * @author indvdum (gotoindvdum[at]gmail[dot]com)
+ * 14.07.2012 21:15:05 + * + */ +public enum GameState { + Game, + Win, + Abort, + Lose, +} diff -r 251961e44b5f -r ad884dc6877c 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 02:07:51 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Sat Jul 14 22:49:59 2012 +0400 @@ -1,9 +1,15 @@ package ru.bosony.model.mine; +import java.util.Arrays; +import java.util.Collection; import java.util.HashSet; +import java.util.List; import java.util.Set; +import org.hamcrest.CoreMatchers; + import ru.bosony.model.cellscontents.CellContent; +import static ru.bosony.model.cellscontents.CellContent.*; import ru.bosony.model.io.TextRepresentable; import ru.bosony.model.moving.Movement; @@ -15,12 +21,23 @@ public class Mine implements TextRepresentable { private Cell[][] cells; + private Cell[][] prevStepCells; protected int sizeX; protected int sizeY; protected Cell robotCell; + protected int score = 0; + protected int lastScoreChange = 0; + protected GameState state = GameState.Game; + protected int lambdaCollectedCount = 0; public Mine(String text) { fromText(text); + if (findCells(OpenLambdaLift).size() != 0) + throw new RuntimeException("Detected OpenLambdaLift in new map!"); + if (findCells(MiningRobot).size() != 1) + 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!"); } @Override @@ -53,18 +70,26 @@ if (curX < row.length()) content = CellContent.fromText("" + row.charAt(curX)); else - content = CellContent.Empty; + content = Empty; Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content); cells[curX][curY] = cell; - if (content == CellContent.MiningRobot) + if (content == MiningRobot) robotCell = cell; } } } public Cell getCell(Coordinate coordinate) { + return getCell(coordinate, cells); + } + + protected Cell getCell(Coordinate coordinate, Cell[][] cells) { + if (coordinate == null) + return null; int x = coordinate.x; int y = coordinate.y; + if (cells == null) + return null; if (x < 1 || x > sizeX || y < 1 || y > sizeY) return null; return cells[x - 1][sizeY - y]; @@ -91,14 +116,14 @@ 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)) { + if (nContent == Empty + || nContent == Earth + || nContent == Lambda + || nContent == OpenLambdaLift + || (nContent == Rock && robotCell.getCoordinate().right().equals(nCoordinate) && rightNCell != null && rightNCell + .getContent() == Empty) + || (nContent == Rock && robotCell.getCoordinate().left().equals(nCoordinate) && leftNCell != null && leftNCell + .getContent() == Empty)) { Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate); movs.add(mov); } @@ -115,4 +140,148 @@ return cells; } + protected void moveRobot(Cell nextRobotCell) { + robotCell.setContent(Empty); + nextRobotCell.setContent(MiningRobot); + robotCell = nextRobotCell; + } + + public GameState move(Movement mov) { + if (state != GameState.Game) + throw new RuntimeException("Game over!"); + + // Score + int prevScore = score; + score--; + + // Moving and scoring + Coordinate nextRobotCoord = null; + switch (mov) { + case ABORT: + score += 25 * lambdaCollectedCount; + + lastScoreChange = score - prevScore; + state = GameState.Abort; + return state; + case WAIT: + break; + case LEFT: + nextRobotCoord = robotCell.coordinate.left(); + break; + case UP: + nextRobotCoord = robotCell.coordinate.up(); + break; + case RIGHT: + nextRobotCoord = robotCell.coordinate.right(); + break; + case DOWN: + nextRobotCoord = robotCell.coordinate.down(); + break; + } + Cell nextRobotCell = getCell(nextRobotCoord); + CellContent nextRobotCellContent = getContent(nextRobotCell); + if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) { + moveRobot(nextRobotCell); + } else if (nextRobotCellContent == Lambda) { + moveRobot(nextRobotCell); + lambdaCollectedCount++; + score += 25; + } else if (nextRobotCellContent == OpenLambdaLift) { + moveRobot(nextRobotCell); + score += 50 * lambdaCollectedCount; + + lastScoreChange = score - prevScore; + state = GameState.Win; + return state; + } else if (robotCell.coordinate.right() == nextRobotCoord && nextRobotCellContent == Rock && getContent(getCell(nextRobotCoord.right())) == Empty) { + moveRobot(nextRobotCell); + getCell(nextRobotCoord.right()).setContent(Rock); + } else if (robotCell.coordinate.left() == nextRobotCoord && nextRobotCellContent == Rock && getContent(getCell(nextRobotCoord.left())) == Empty) { + moveRobot(nextRobotCell); + getCell(nextRobotCoord.left()).setContent(Rock); + } else { + mov = Movement.WAIT; + } + + // Other ending conditions + if (getContent(getCell(robotCell.coordinate.up(), prevStepCells)) != Rock + && getContent(getCell(robotCell.coordinate.up())) == Rock) { + lastScoreChange = score - prevScore; + state = GameState.Lose; + return state; + } + + // World changes + Cell[][] newCells = Arrays.copyOf(cells, cells.length); + for (int y = 1; y <= sizeY; y++) { + for (int x = 1; x <= sizeX; x++) { + Coordinate curCoord = new Coordinate(x, y); + Coordinate leftCoord = curCoord.left(); + Coordinate upCoord = curCoord.up(); + Coordinate rightCoord = curCoord.right(); + Coordinate downCoord = curCoord.down(); + Coordinate leftAndUpCoord = curCoord.leftAndUp(); + Coordinate rightAndUpCoord = curCoord.rightAndUp(); + Coordinate rightAndDownCoord = curCoord.rightAndDown(); + Coordinate leftAndDownCoord = curCoord.leftAndDown(); + Cell curCell = getCell(curCoord); + Cell leftCell = getCell(leftCoord); + Cell upCell = getCell(upCoord); + Cell rightCell = getCell(rightCoord); + Cell downCell = getCell(downCoord); + Cell leftAndUpCell = getCell(leftAndUpCoord); + Cell rightAndUpCell = getCell(rightAndUpCoord); + Cell rightAndDownCell = getCell(rightAndDownCoord); + Cell leftAndDownCell = getCell(leftAndDownCoord); + + if (getContent(curCell) == Rock && getContent(downCell) == Empty) { + getCell(curCoord, newCells).setContent(Empty); + getCell(downCoord, newCells).setContent(Rock); + } else if (getContent(curCell) == Rock && getContent(downCell) == Empty + && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) { + getCell(curCoord, newCells).setContent(Empty); + getCell(rightAndDownCoord, newCells).setContent(Rock); + } else if (getContent(curCell) == Rock && getContent(downCell) == Rock + && (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty) + && getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) { + getCell(curCoord, newCells).setContent(Empty); + getCell(leftAndDownCoord, newCells).setContent(Rock); + } else if (getContent(curCell) == Rock && getContent(downCell) == Lambda + && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) { + getCell(curCoord, newCells).setContent(Empty); + getCell(rightAndDownCoord, newCells).setContent(Rock); + } else if (getContent(curCell) == ClosedLambdaLift && findCells(Lambda).size() == 0) { + getCell(curCoord, newCells).setContent(OpenLambdaLift); + } + } + } + prevStepCells = cells; + cells = newCells; + + lastScoreChange = score - prevScore; + state = GameState.Game; + return state; + } + + protected CellContent getContent(Cell cell) { + if (cell == null) + return null; + return cell.getContent(); + } + + public Collection findCells(CellContent content) { + Collection finded = new HashSet(); + for (int y = sizeY - 1; y >= 0; y--) { + for (int x = 0; x < sizeX - 1; x++) { + if (cells[x][y].getContent() == content) + finded.add(cells[x][y]); + } + } + return finded; + } + + public int getLastScoreChange() { + return lastScoreChange; + } + } diff -r 251961e44b5f -r ad884dc6877c icfp-model/src/test/java/ru/bosony/tests/TestMovements.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/icfp-model/src/test/java/ru/bosony/tests/TestMovements.java Sat Jul 14 22:49:59 2012 +0400 @@ -0,0 +1,47 @@ +package ru.bosony.tests; + +import static org.junit.Assert.*; + +import java.util.Set; + +import org.junit.Test; + +import ru.bosony.model.mine.Mine; +import ru.bosony.model.moving.Movement; + +/** + * @author indvdum (gotoindvdum[at]gmail[dot]com)
+ * 14.07.2012 2:29:05 + * + */ +public class TestMovements { + + @Test + public void test() { + /* Map 01 (6x6) + * + ###### + #. *R# + # \.# + #\ * # + L .\# + ###### + */ + String map = + "######\n" + + "#. *R#\n" + + "# \\.#\n" + + "#\\ * #\n" + + "L .\\#\n" + + "######"; + Mine mine = new Mine(map); + assertEquals(mine.getSizeX(), 6); + assertEquals(mine.getSizeY(), 6); + Set movs = mine.getAvailableRobotMovements(); + assertFalse(movs.contains(Movement.UP)); + assertFalse(movs.contains(Movement.RIGHT)); + assertTrue(movs.contains(Movement.DOWN)); + assertTrue(movs.contains(Movement.LEFT)); + } + +} diff -r 251961e44b5f -r ad884dc6877c icfp-model/src/test/java/ru/bosony/tests/TestScore.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/icfp-model/src/test/java/ru/bosony/tests/TestScore.java Sat Jul 14 22:49:59 2012 +0400 @@ -0,0 +1,34 @@ +package ru.bosony.tests; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import ru.bosony.model.mine.Mine; +import ru.bosony.model.moving.Movement; + +public class TestScore { + + @Test + public void test() { + /* Map 01 (6x6) + * + ###### + #. *R# + # \.# + #\ * # + L .\# + ###### + */ + String map = + "######\n" + + "#. *R#\n" + + "# \\.#\n" + + "#\\ * #\n" + + "L .\\#\n" + + "######"; + Mine mine = new Mine(map); + mine.move(Movement.LEFT); + } + +}