indvdum@6: package ru.bosony.model.mine;
indvdum@6:
indvdum@8: import java.util.Arrays;
indvdum@8: import java.util.Collection;
indvdum@7: import java.util.HashSet;
indvdum@8: import java.util.List;
indvdum@7: import java.util.Set;
indvdum@7:
indvdum@8: import org.hamcrest.CoreMatchers;
indvdum@8:
indvdum@6: import ru.bosony.model.cellscontents.CellContent;
indvdum@8: import static ru.bosony.model.cellscontents.CellContent.*;
indvdum@6: import ru.bosony.model.io.TextRepresentable;
indvdum@7: import ru.bosony.model.moving.Movement;
indvdum@6:
indvdum@7: /**
indvdum@7: * @author indvdum (gotoindvdum[at]gmail[dot]com)
indvdum@7: * 14.07.2012 2:02:18
indvdum@7: *
indvdum@7: */
indvdum@6: public class Mine implements TextRepresentable {
indvdum@6:
indvdum@7: private Cell[][] cells;
indvdum@8: private Cell[][] prevStepCells;
indvdum@7: protected int sizeX;
indvdum@7: protected int sizeY;
indvdum@7: protected Cell robotCell;
indvdum@8: protected int score = 0;
indvdum@8: protected int lastScoreChange = 0;
indvdum@8: protected GameState state = GameState.Game;
indvdum@8: protected int lambdaCollectedCount = 0;
indvdum@6:
indvdum@6: public Mine(String text) {
indvdum@6: fromText(text);
indvdum@8: if (findCells(OpenLambdaLift).size() != 0)
indvdum@8: throw new RuntimeException("Detected OpenLambdaLift in new map!");
indvdum@8: if (findCells(MiningRobot).size() != 1)
indvdum@8: throw new RuntimeException("New map does not contain a robot!");
indvdum@8: if (findCells(ClosedLambdaLift).size() != 1)
indvdum@8: throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
indvdum@6: }
indvdum@6:
indvdum@6: @Override
indvdum@6: public String toText() {
indvdum@6: String text = "";
indvdum@6: for (int y = 0; y < sizeY; y++) {
indvdum@6: for (int x = 0; x < sizeX; x++) {
indvdum@6: text += cells[x][y].getContent().toText();
indvdum@6: }
indvdum@6: text += "\n";
indvdum@6: }
indvdum@6: return text.replaceAll("\n*$", "");
indvdum@6: }
indvdum@6:
indvdum@6: private void fromText(String text) {
indvdum@6: String[] rows = text.split("\n+");
indvdum@6: int y = rows.length;
indvdum@6: int x = 0;
indvdum@6: for (String row : rows) {
indvdum@6: if (x < row.length())
indvdum@6: x = row.length();
indvdum@6: }
indvdum@6: sizeX = x;
indvdum@6: sizeY = y;
indvdum@6: cells = new Cell[x][y];
indvdum@6: for (int curY = 0; curY < rows.length; curY++) {
indvdum@6: String row = rows[curY];
indvdum@6: for (int curX = 0; curX < row.length() || curX < x; curX++) {
indvdum@6: CellContent content = null;
indvdum@6: if (curX < row.length())
indvdum@6: content = CellContent.fromText("" + row.charAt(curX));
indvdum@6: else
indvdum@8: content = Empty;
indvdum@7: Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
indvdum@6: cells[curX][curY] = cell;
indvdum@8: if (content == MiningRobot)
indvdum@7: robotCell = cell;
indvdum@6: }
indvdum@6: }
indvdum@6: }
indvdum@6:
indvdum@7: public Cell getCell(Coordinate coordinate) {
indvdum@8: return getCell(coordinate, cells);
indvdum@8: }
indvdum@8:
indvdum@8: protected Cell getCell(Coordinate coordinate, Cell[][] cells) {
indvdum@8: if (coordinate == null)
indvdum@8: return null;
indvdum@7: int x = coordinate.x;
indvdum@7: int y = coordinate.y;
indvdum@8: if (cells == null)
indvdum@8: return null;
indvdum@7: if (x < 1 || x > sizeX || y < 1 || y > sizeY)
indvdum@7: return null;
indvdum@7: return cells[x - 1][sizeY - y];
indvdum@7: }
indvdum@7:
indvdum@6: @Override
indvdum@6: public String toString() {
indvdum@6: return toText();
indvdum@6: }
indvdum@6:
indvdum@7: public int getSizeX() {
indvdum@7: return sizeX;
indvdum@7: }
indvdum@7:
indvdum@7: public int getSizeY() {
indvdum@7: return sizeY;
indvdum@7: }
indvdum@7:
indvdum@7: public Set getAvailableRobotMovements() {
indvdum@7: Set movs = new HashSet();
indvdum@7: Set nCells = getNeighboringCells(robotCell);
indvdum@7: for (Cell nCell : nCells) {
indvdum@7: CellContent nContent = nCell.getContent();
indvdum@7: Coordinate nCoordinate = nCell.getCoordinate();
indvdum@7: Cell rightNCell = getCell(nCoordinate.right());
indvdum@7: Cell leftNCell = getCell(nCoordinate.left());
indvdum@8: if (nContent == Empty
indvdum@8: || nContent == Earth
indvdum@8: || nContent == Lambda
indvdum@8: || nContent == OpenLambdaLift
indvdum@8: || (nContent == Rock && robotCell.getCoordinate().right().equals(nCoordinate) && rightNCell != null && rightNCell
indvdum@8: .getContent() == Empty)
indvdum@8: || (nContent == Rock && robotCell.getCoordinate().left().equals(nCoordinate) && leftNCell != null && leftNCell
indvdum@8: .getContent() == Empty)) {
indvdum@7: Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate);
indvdum@7: movs.add(mov);
indvdum@7: }
indvdum@7: }
indvdum@7: movs.add(Movement.ABORT);
indvdum@7: movs.add(Movement.WAIT);
indvdum@7: return movs;
indvdum@7: }
indvdum@7:
indvdum@7: public Set getNeighboringCells(Cell cell) {
indvdum@7: Set cells = new HashSet();
indvdum@7: for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates())
indvdum@7: cells.add(getCell(coordinate));
indvdum@7: return cells;
indvdum@7: }
indvdum@7:
indvdum@8: protected void moveRobot(Cell nextRobotCell) {
indvdum@8: robotCell.setContent(Empty);
indvdum@8: nextRobotCell.setContent(MiningRobot);
indvdum@8: robotCell = nextRobotCell;
indvdum@8: }
indvdum@8:
indvdum@8: public GameState move(Movement mov) {
indvdum@8: if (state != GameState.Game)
indvdum@8: throw new RuntimeException("Game over!");
indvdum@8:
indvdum@8: // Score
indvdum@8: int prevScore = score;
indvdum@8: score--;
indvdum@8:
indvdum@8: // Moving and scoring
indvdum@8: Coordinate nextRobotCoord = null;
indvdum@8: switch (mov) {
indvdum@8: case ABORT:
indvdum@8: score += 25 * lambdaCollectedCount;
indvdum@8:
indvdum@8: lastScoreChange = score - prevScore;
indvdum@8: state = GameState.Abort;
indvdum@8: return state;
indvdum@8: case WAIT:
indvdum@8: break;
indvdum@8: case LEFT:
indvdum@8: nextRobotCoord = robotCell.coordinate.left();
indvdum@8: break;
indvdum@8: case UP:
indvdum@8: nextRobotCoord = robotCell.coordinate.up();
indvdum@8: break;
indvdum@8: case RIGHT:
indvdum@8: nextRobotCoord = robotCell.coordinate.right();
indvdum@8: break;
indvdum@8: case DOWN:
indvdum@8: nextRobotCoord = robotCell.coordinate.down();
indvdum@8: break;
indvdum@8: }
indvdum@8: Cell nextRobotCell = getCell(nextRobotCoord);
indvdum@8: CellContent nextRobotCellContent = getContent(nextRobotCell);
indvdum@8: if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) {
indvdum@8: moveRobot(nextRobotCell);
indvdum@8: } else if (nextRobotCellContent == Lambda) {
indvdum@8: moveRobot(nextRobotCell);
indvdum@8: lambdaCollectedCount++;
indvdum@8: score += 25;
indvdum@8: } else if (nextRobotCellContent == OpenLambdaLift) {
indvdum@8: moveRobot(nextRobotCell);
indvdum@8: score += 50 * lambdaCollectedCount;
indvdum@8:
indvdum@8: lastScoreChange = score - prevScore;
indvdum@8: state = GameState.Win;
indvdum@8: return state;
indvdum@8: } else if (robotCell.coordinate.right() == nextRobotCoord && nextRobotCellContent == Rock && getContent(getCell(nextRobotCoord.right())) == Empty) {
indvdum@8: moveRobot(nextRobotCell);
indvdum@8: getCell(nextRobotCoord.right()).setContent(Rock);
indvdum@8: } else if (robotCell.coordinate.left() == nextRobotCoord && nextRobotCellContent == Rock && getContent(getCell(nextRobotCoord.left())) == Empty) {
indvdum@8: moveRobot(nextRobotCell);
indvdum@8: getCell(nextRobotCoord.left()).setContent(Rock);
indvdum@8: } else {
indvdum@8: mov = Movement.WAIT;
indvdum@8: }
indvdum@8:
indvdum@8: // Other ending conditions
indvdum@8: if (getContent(getCell(robotCell.coordinate.up(), prevStepCells)) != Rock
indvdum@8: && getContent(getCell(robotCell.coordinate.up())) == Rock) {
indvdum@8: lastScoreChange = score - prevScore;
indvdum@8: state = GameState.Lose;
indvdum@8: return state;
indvdum@8: }
indvdum@8:
indvdum@8: // World changes
indvdum@8: Cell[][] newCells = Arrays.copyOf(cells, cells.length);
indvdum@8: for (int y = 1; y <= sizeY; y++) {
indvdum@8: for (int x = 1; x <= sizeX; x++) {
indvdum@8: Coordinate curCoord = new Coordinate(x, y);
indvdum@8: Coordinate leftCoord = curCoord.left();
indvdum@8: Coordinate upCoord = curCoord.up();
indvdum@8: Coordinate rightCoord = curCoord.right();
indvdum@8: Coordinate downCoord = curCoord.down();
indvdum@8: Coordinate leftAndUpCoord = curCoord.leftAndUp();
indvdum@8: Coordinate rightAndUpCoord = curCoord.rightAndUp();
indvdum@8: Coordinate rightAndDownCoord = curCoord.rightAndDown();
indvdum@8: Coordinate leftAndDownCoord = curCoord.leftAndDown();
indvdum@8: Cell curCell = getCell(curCoord);
indvdum@8: Cell leftCell = getCell(leftCoord);
indvdum@8: Cell upCell = getCell(upCoord);
indvdum@8: Cell rightCell = getCell(rightCoord);
indvdum@8: Cell downCell = getCell(downCoord);
indvdum@8: Cell leftAndUpCell = getCell(leftAndUpCoord);
indvdum@8: Cell rightAndUpCell = getCell(rightAndUpCoord);
indvdum@8: Cell rightAndDownCell = getCell(rightAndDownCoord);
indvdum@8: Cell leftAndDownCell = getCell(leftAndDownCoord);
indvdum@8:
indvdum@8: if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
indvdum@8: getCell(curCoord, newCells).setContent(Empty);
indvdum@8: getCell(downCoord, newCells).setContent(Rock);
indvdum@8: } else if (getContent(curCell) == Rock && getContent(downCell) == Empty
indvdum@8: && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
indvdum@8: getCell(curCoord, newCells).setContent(Empty);
indvdum@8: getCell(rightAndDownCoord, newCells).setContent(Rock);
indvdum@8: } else if (getContent(curCell) == Rock && getContent(downCell) == Rock
indvdum@8: && (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
indvdum@8: && getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
indvdum@8: getCell(curCoord, newCells).setContent(Empty);
indvdum@8: getCell(leftAndDownCoord, newCells).setContent(Rock);
indvdum@8: } else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
indvdum@8: && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
indvdum@8: getCell(curCoord, newCells).setContent(Empty);
indvdum@8: getCell(rightAndDownCoord, newCells).setContent(Rock);
indvdum@8: } else if (getContent(curCell) == ClosedLambdaLift && findCells(Lambda).size() == 0) {
indvdum@8: getCell(curCoord, newCells).setContent(OpenLambdaLift);
indvdum@8: }
indvdum@8: }
indvdum@8: }
indvdum@8: prevStepCells = cells;
indvdum@8: cells = newCells;
indvdum@8:
indvdum@8: lastScoreChange = score - prevScore;
indvdum@8: state = GameState.Game;
indvdum@8: return state;
indvdum@8: }
indvdum@8:
indvdum@8: protected CellContent getContent(Cell cell) {
indvdum@8: if (cell == null)
indvdum@8: return null;
indvdum@8: return cell.getContent();
indvdum@8: }
indvdum@8:
indvdum@8: public Collection findCells(CellContent content) {
indvdum@8: Collection finded = new HashSet| ();
indvdum@8: for (int y = sizeY - 1; y >= 0; y--) {
indvdum@8: for (int x = 0; x < sizeX - 1; x++) {
indvdum@8: if (cells[x][y].getContent() == content)
indvdum@8: finded.add(cells[x][y]);
indvdum@8: }
indvdum@8: }
indvdum@8: return finded;
indvdum@8: }
indvdum@8:
indvdum@8: public int getLastScoreChange() {
indvdum@8: return lastScoreChange;
indvdum@8: }
indvdum@8:
indvdum@6: }
| | | | | | |