indvdum@9: package ru.bosony.model.game;
indvdum@9:
indvdum@9: import static ru.bosony.model.cellscontents.CellContent.ClosedLambdaLift;
indvdum@9: import static ru.bosony.model.cellscontents.CellContent.Earth;
indvdum@9: import static ru.bosony.model.cellscontents.CellContent.Empty;
indvdum@9: import static ru.bosony.model.cellscontents.CellContent.Lambda;
indvdum@9: import static ru.bosony.model.cellscontents.CellContent.MiningRobot;
indvdum@9: import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift;
indvdum@9: import static ru.bosony.model.cellscontents.CellContent.Rock;
indvdum@9:
indvdum@16: import java.util.ArrayList;
indvdum@9: import java.util.Arrays;
indvdum@16: import java.util.List;
indvdum@9:
indvdum@9: import ru.bosony.model.cellscontents.CellContent;
indvdum@9: import ru.bosony.model.mine.Cell;
indvdum@9: import ru.bosony.model.mine.Coordinate;
indvdum@9: import ru.bosony.model.mine.Mine;
indvdum@9: import ru.bosony.model.moving.Movement;
indvdum@9:
indvdum@9: /**
indvdum@9: * @author indvdum (gotoindvdum[at]gmail[dot]com)
indvdum@9: * 14.07.2012 22:51:20
indvdum@9: *
indvdum@9: */
indvdum@9: public class Game {
indvdum@9:
indvdum@9: Mine mine = null;
indvdum@9: private Cell[][] prevStepCells;
indvdum@9: protected int score = 0;
indvdum@9: protected int lastScoreChange = 0;
indvdum@9: protected GameState state = GameState.Game;
indvdum@9: protected int lambdaCollectedCount = 0;
indvdum@9:
indvdum@9: public Game(Mine mine) {
indvdum@9: this.mine = mine;
indvdum@9: }
indvdum@9:
indvdum@9: public Mine getMine() {
indvdum@9: return mine;
indvdum@9: }
indvdum@9:
indvdum@9: protected Cell getCell(Coordinate coordinate, Cell[][] cells) {
indvdum@9: if (coordinate == null)
indvdum@9: return null;
indvdum@9: int x = coordinate.x;
indvdum@9: int y = coordinate.y;
indvdum@9: if (cells == null)
indvdum@9: return null;
indvdum@9: if (x < 1 || x > mine.getSizeX() || y < 1 || y > mine.getSizeY())
indvdum@9: return null;
indvdum@9: return cells[x - 1][mine.getSizeY() - y];
indvdum@9: }
indvdum@9:
indvdum@9: protected void moveRobot(Cell nextRobotCell) {
indvdum@9: mine.getRobotCell().setContent(Empty);
indvdum@9: nextRobotCell.setContent(MiningRobot);
indvdum@9: mine.setRobotCell(nextRobotCell);
indvdum@9: }
indvdum@9:
indvdum@9: public GameState move(Movement mov) {
indvdum@9: if (state != GameState.Game)
indvdum@9: throw new RuntimeException("Game over!");
indvdum@9:
indvdum@9: // Score
indvdum@9: int prevScore = score;
indvdum@9: score--;
indvdum@9:
indvdum@9: // Moving and scoring
indvdum@9: Coordinate nextRobotCoord = null;
indvdum@9: switch (mov) {
indvdum@9: case ABORT:
indvdum@9: score += 25 * lambdaCollectedCount;
indvdum@9:
indvdum@9: lastScoreChange = score - prevScore;
indvdum@9: state = GameState.Abort;
indvdum@9: return state;
indvdum@9: case WAIT:
indvdum@9: break;
indvdum@9: case LEFT:
indvdum@9: nextRobotCoord = mine.getRobotCell().getCoordinate().left();
indvdum@9: break;
indvdum@9: case UP:
indvdum@9: nextRobotCoord = mine.getRobotCell().getCoordinate().up();
indvdum@9: break;
indvdum@9: case RIGHT:
indvdum@9: nextRobotCoord = mine.getRobotCell().getCoordinate().right();
indvdum@9: break;
indvdum@9: case DOWN:
indvdum@9: nextRobotCoord = mine.getRobotCell().getCoordinate().down();
indvdum@9: break;
indvdum@9: }
indvdum@9: Cell nextRobotCell = mine.getCell(nextRobotCoord);
indvdum@9: CellContent nextRobotCellContent = getContent(nextRobotCell);
indvdum@9: if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) {
indvdum@9: moveRobot(nextRobotCell);
indvdum@9: } else if (nextRobotCellContent == Lambda) {
indvdum@9: moveRobot(nextRobotCell);
indvdum@9: lambdaCollectedCount++;
indvdum@9: score += 25;
indvdum@9: } else if (nextRobotCellContent == OpenLambdaLift) {
indvdum@9: moveRobot(nextRobotCell);
indvdum@9: score += 50 * lambdaCollectedCount;
indvdum@9:
indvdum@9: lastScoreChange = score - prevScore;
indvdum@9: state = GameState.Win;
indvdum@9: return state;
indvdum@9: } else if (mine.getRobotCell().getCoordinate().right() == nextRobotCoord && nextRobotCellContent == Rock
indvdum@9: && getContent(mine.getCell(nextRobotCoord.right())) == Empty) {
indvdum@9: moveRobot(nextRobotCell);
indvdum@9: mine.getCell(nextRobotCoord.right()).setContent(Rock);
indvdum@9: } else if (mine.getRobotCell().getCoordinate().left() == nextRobotCoord && nextRobotCellContent == Rock
indvdum@9: && getContent(mine.getCell(nextRobotCoord.left())) == Empty) {
indvdum@9: moveRobot(nextRobotCell);
indvdum@9: mine.getCell(nextRobotCoord.left()).setContent(Rock);
indvdum@9: } else {
indvdum@9: mov = Movement.WAIT;
indvdum@9: }
indvdum@9:
indvdum@9: // Other ending conditions
indvdum@9: if (getContent(getCell(mine.getRobotCell().getCoordinate().up(), prevStepCells)) != Rock
indvdum@9: && getContent(mine.getCell(mine.getRobotCell().getCoordinate().up())) == Rock) {
indvdum@9: lastScoreChange = score - prevScore;
indvdum@9: state = GameState.Lose;
indvdum@9: return state;
indvdum@9: }
indvdum@9:
indvdum@9: // World changes
indvdum@9: Cell[][] newCells = Arrays.copyOf(mine.getCells(), mine.getCells().length);
indvdum@9: for (int y = 1; y <= mine.getSizeY(); y++) {
indvdum@9: for (int x = 1; x <= mine.getSizeX(); x++) {
indvdum@9: Coordinate curCoord = new Coordinate(x, y);
indvdum@9: Coordinate leftCoord = curCoord.left();
indvdum@9: Coordinate upCoord = curCoord.up();
indvdum@9: Coordinate rightCoord = curCoord.right();
indvdum@9: Coordinate downCoord = curCoord.down();
indvdum@9: Coordinate leftAndUpCoord = curCoord.leftAndUp();
indvdum@9: Coordinate rightAndUpCoord = curCoord.rightAndUp();
indvdum@9: Coordinate rightAndDownCoord = curCoord.rightAndDown();
indvdum@9: Coordinate leftAndDownCoord = curCoord.leftAndDown();
indvdum@9: Cell curCell = mine.getCell(curCoord);
indvdum@9: Cell leftCell = mine.getCell(leftCoord);
indvdum@9: Cell upCell = mine.getCell(upCoord);
indvdum@9: Cell rightCell = mine.getCell(rightCoord);
indvdum@9: Cell downCell = mine.getCell(downCoord);
indvdum@9: Cell leftAndUpCell = mine.getCell(leftAndUpCoord);
indvdum@9: Cell rightAndUpCell = mine.getCell(rightAndUpCoord);
indvdum@9: Cell rightAndDownCell = mine.getCell(rightAndDownCoord);
indvdum@9: Cell leftAndDownCell = mine.getCell(leftAndDownCoord);
indvdum@9:
indvdum@9: if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
indvdum@9: getCell(curCoord, newCells).setContent(Empty);
indvdum@9: getCell(downCoord, newCells).setContent(Rock);
indvdum@9: } else if (getContent(curCell) == Rock && getContent(downCell) == Empty
indvdum@9: && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
indvdum@9: getCell(curCoord, newCells).setContent(Empty);
indvdum@9: getCell(rightAndDownCoord, newCells).setContent(Rock);
indvdum@9: } else if (getContent(curCell) == Rock && getContent(downCell) == Rock
indvdum@9: && (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
indvdum@9: && getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
indvdum@9: getCell(curCoord, newCells).setContent(Empty);
indvdum@9: getCell(leftAndDownCoord, newCells).setContent(Rock);
indvdum@9: } else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
indvdum@9: && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
indvdum@9: getCell(curCoord, newCells).setContent(Empty);
indvdum@9: getCell(rightAndDownCoord, newCells).setContent(Rock);
indvdum@9: } else if (getContent(curCell) == ClosedLambdaLift && mine.findCells(Lambda).size() == 0) {
indvdum@9: getCell(curCoord, newCells).setContent(OpenLambdaLift);
indvdum@9: }
indvdum@9: }
indvdum@9: }
indvdum@9: prevStepCells = mine.getCells();
indvdum@9: mine.setCells(newCells);
indvdum@9:
indvdum@9: lastScoreChange = score - prevScore;
indvdum@9: state = GameState.Game;
indvdum@9: return state;
indvdum@9: }
indvdum@9:
indvdum@16: public GameState move(List movs) {
indvdum@16: for (Movement mov : movs) {
indvdum@16: if (move(mov) != GameState.Game)
indvdum@16: return state;
indvdum@16: }
indvdum@16: return state;
indvdum@16: }
indvdum@16:
indvdum@19: public GameState move(String route) {
indvdum@16: List movs = new ArrayList();
indvdum@19: for (int i = 0; i < route.length(); i++) {
indvdum@19: String s = "" + route.charAt(i);
indvdum@16: for (Movement mov : Movement.values()) {
indvdum@16: if (mov.toText().equals(s)) {
indvdum@16: movs.add(mov);
indvdum@16: break;
indvdum@16: }
indvdum@16: }
indvdum@16: }
indvdum@16: return move(movs);
indvdum@16: }
indvdum@16:
indvdum@9: protected CellContent getContent(Cell cell) {
indvdum@9: if (cell == null)
indvdum@9: return null;
indvdum@9: return cell.getContent();
indvdum@9: }
indvdum@9:
indvdum@9: public int getLastScoreChange() {
indvdum@9: return lastScoreChange;
indvdum@9: }
indvdum@9:
indvdum@9: public int getScore() {
indvdum@9: return score;
indvdum@9: }
indvdum@9:
indvdum@9: public GameState getState() {
indvdum@9: return state;
indvdum@9: }
indvdum@9:
indvdum@9: public int getLambdaCollectedCount() {
indvdum@9: return lambdaCollectedCount;
indvdum@9: }
indvdum@9: }