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@30: import static ru.bosony.model.cellscontents.CellContent.HighOrderRock; indvdum@30: import static ru.bosony.model.cellscontents.CellContent.HuttonsRazor; 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@29: import static ru.bosony.model.cellscontents.CellContent.WadlersBeard; indvdum@9: indvdum@16: import java.util.ArrayList; indvdum@47: import java.util.HashMap; indvdum@16: import java.util.List; indvdum@47: import java.util.Map; 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.Mine; indvdum@22: import ru.bosony.model.moving.Coordinate; 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@22: protected Mine mine = null; indvdum@20: protected int score = 0; indvdum@20: protected int lastScoreChange = 0; indvdum@20: protected GameState state = GameState.Game; indvdum@20: protected int lambdaCollectedCount = 0; indvdum@47: protected List history = new ArrayList(); indvdum@20: protected List route = new ArrayList(); indvdum@25: protected int underwater = 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: } 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: indvdum@9: // Moving and scoring indvdum@20: route.add(mov); indvdum@47: try { indvdum@47: history.add(mine.clone()); indvdum@47: } catch (CloneNotSupportedException e) { indvdum@47: throw new RuntimeException(e); indvdum@47: } indvdum@29: Cell robotCell = mine.getRobotCell(); indvdum@29: Coordinate robotCoord = robotCell.getCoordinate(); 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@29: case RAZOR: indvdum@29: if (mine.getRazorsCount() > 0) { indvdum@29: for (Cell cell : mine.getNeighboringCells(robotCell)) { indvdum@29: if (cell.getContent() == WadlersBeard) { indvdum@29: cell.setContent(Empty); indvdum@29: } indvdum@29: } indvdum@29: mine.setRazorsCount(mine.getRazorsCount() - 1); indvdum@29: } indvdum@29: break; indvdum@9: case LEFT: indvdum@22: nextRobotCoord = robotCoord.left(); indvdum@9: break; indvdum@9: case UP: indvdum@22: nextRobotCoord = robotCoord.up(); indvdum@9: break; indvdum@9: case RIGHT: indvdum@22: nextRobotCoord = robotCoord.right(); indvdum@9: break; indvdum@9: case DOWN: indvdum@22: nextRobotCoord = robotCoord.down(); indvdum@9: break; indvdum@9: } indvdum@23: score--; 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@29: } else if (nextRobotCellContent == HuttonsRazor) { indvdum@29: moveRobot(nextRobotCell); indvdum@29: mine.setRazorsCount(mine.getRazorsCount() + 1); indvdum@23: } else if (robotCoord.right().equals(nextRobotCoord) && nextRobotCellContent == Rock indvdum@9: && getContent(mine.getCell(nextRobotCoord.right())) == Empty) { indvdum@9: moveRobot(nextRobotCell); indvdum@9: mine.getCell(nextRobotCoord.right()).setContent(Rock); indvdum@23: } else if (robotCoord.left().equals(nextRobotCoord) && nextRobotCellContent == Rock indvdum@9: && getContent(mine.getCell(nextRobotCoord.left())) == Empty) { indvdum@9: moveRobot(nextRobotCell); indvdum@9: mine.getCell(nextRobotCoord.left()).setContent(Rock); indvdum@30: } else if (robotCoord.right().equals(nextRobotCoord) && nextRobotCellContent == HighOrderRock indvdum@30: && getContent(mine.getCell(nextRobotCoord.right())) == Empty) { indvdum@30: moveRobot(nextRobotCell); indvdum@30: mine.getCell(nextRobotCoord.right()).setContent(HighOrderRock); indvdum@30: } else if (robotCoord.left().equals(nextRobotCoord) && nextRobotCellContent == HighOrderRock indvdum@30: && getContent(mine.getCell(nextRobotCoord.left())) == Empty) { indvdum@30: moveRobot(nextRobotCell); indvdum@30: mine.getCell(nextRobotCoord.left()).setContent(HighOrderRock); indvdum@27: } else if (CellContent.getTrampolines().contains(nextRobotCellContent) indvdum@27: && CellContent.getTargets().contains(nextRobotCellContent.getTrampolineTarget())) { indvdum@27: CellContent trampoline = nextRobotCellContent; indvdum@27: CellContent target = trampoline.getTrampolineTarget(); indvdum@27: for (Cell targetCell : mine.findCells(target)) { indvdum@27: moveRobot(targetCell); indvdum@27: } indvdum@27: for (CellContent tramp : CellContent.getTrampolines()) { indvdum@27: if (tramp.getTrampolineTarget() == target) { indvdum@27: for (Cell trampolineCell : mine.findCells(tramp)) { indvdum@27: trampolineCell.setContent(Empty); indvdum@27: } indvdum@27: } indvdum@27: } indvdum@9: } else { indvdum@9: mov = Movement.WAIT; indvdum@9: } indvdum@9: indvdum@9: // World changes indvdum@22: Cell[][] newCells = mine.cloneCells(); 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@30: } else if (getContent(curCell) == HighOrderRock && getContent(downCell) == Empty) { indvdum@30: getCell(curCoord, newCells).setContent(Empty); indvdum@30: if (getContent(mine.getCell(downCoord.down())) != Empty) indvdum@30: getCell(downCoord, newCells).setContent(Lambda); indvdum@30: else indvdum@30: getCell(downCoord, newCells).setContent(HighOrderRock); indvdum@30: } else if (getContent(curCell) == Rock indvdum@30: && (getContent(downCell) == Rock || getContent(downCell) == HighOrderRock) indvdum@9: && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) { indvdum@9: getCell(curCoord, newCells).setContent(Empty); indvdum@9: getCell(rightAndDownCoord, newCells).setContent(Rock); indvdum@30: } else if (getContent(curCell) == HighOrderRock indvdum@30: && (getContent(downCell) == Rock || getContent(downCell) == HighOrderRock) indvdum@30: && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) { indvdum@30: getCell(curCoord, newCells).setContent(Empty); indvdum@30: if (getContent(mine.getCell(rightAndDownCoord.down())) != Empty) indvdum@30: getCell(rightAndDownCoord, newCells).setContent(Lambda); indvdum@30: else indvdum@30: getCell(rightAndDownCoord, newCells).setContent(HighOrderRock); indvdum@30: } else if (getContent(curCell) == Rock indvdum@30: && (getContent(downCell) == Rock || getContent(downCell) == HighOrderRock) 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@30: } else if (getContent(curCell) == HighOrderRock indvdum@30: && (getContent(downCell) == Rock || getContent(downCell) == HighOrderRock) indvdum@30: && (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty) indvdum@30: && getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) { indvdum@30: getCell(curCoord, newCells).setContent(Empty); indvdum@30: if (getContent(mine.getCell(leftAndDownCoord.down())) != Empty) indvdum@30: getCell(leftAndDownCoord, newCells).setContent(Lambda); indvdum@30: else indvdum@30: getCell(leftAndDownCoord, newCells).setContent(HighOrderRock); 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@30: } else if (getContent(curCell) == HighOrderRock && getContent(downCell) == Lambda indvdum@30: && getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) { indvdum@30: getCell(curCoord, newCells).setContent(Empty); indvdum@30: if (getContent(mine.getCell(rightAndDownCoord.down())) != Empty) indvdum@30: getCell(rightAndDownCoord, newCells).setContent(Lambda); indvdum@30: else indvdum@30: getCell(rightAndDownCoord, newCells).setContent(HighOrderRock); indvdum@30: } else if (getContent(curCell) == ClosedLambdaLift indvdum@30: && mine.getLambdasAndHighOrderRocksStartCount() == lambdaCollectedCount) { indvdum@9: getCell(curCoord, newCells).setContent(OpenLambdaLift); indvdum@29: } else if (getContent(curCell) == WadlersBeard && mine.getGrowth() > 0 indvdum@29: && route.size() % mine.getGrowth() == 0) { indvdum@29: if (getContent(leftCell) == Empty) indvdum@29: getCell(leftCoord, newCells).setContent(WadlersBeard); indvdum@29: if (getContent(upCell) == Empty) indvdum@29: getCell(upCoord, newCells).setContent(WadlersBeard); indvdum@29: if (getContent(rightCell) == Empty) indvdum@29: getCell(rightCoord, newCells).setContent(WadlersBeard); indvdum@29: if (getContent(downCell) == Empty) indvdum@29: getCell(downCoord, newCells).setContent(WadlersBeard); indvdum@29: if (getContent(leftAndUpCell) == Empty) indvdum@29: getCell(leftAndUpCoord, newCells).setContent(WadlersBeard); indvdum@29: if (getContent(rightAndUpCell) == Empty) indvdum@29: getCell(rightAndUpCoord, newCells).setContent(WadlersBeard); indvdum@29: if (getContent(leftAndDownCell) == Empty) indvdum@29: getCell(leftAndDownCoord, newCells).setContent(WadlersBeard); indvdum@29: if (getContent(rightAndDownCell) == Empty) indvdum@29: getCell(rightAndDownCoord, newCells).setContent(WadlersBeard); indvdum@9: } indvdum@9: } indvdum@9: } indvdum@22: indvdum@22: // Other ending conditions indvdum@22: robotCoord = mine.getRobotCell().getCoordinate(); indvdum@30: if (getContent(mine.getCell(robotCoord.up())) != Rock && getContent(getCell(robotCoord.up(), newCells)) == Rock indvdum@30: || getContent(mine.getCell(robotCoord.up())) != HighOrderRock indvdum@30: && getContent(getCell(robotCoord.up(), newCells)) == HighOrderRock indvdum@30: || getContent(mine.getCell(robotCoord.up())) == Empty indvdum@30: && getContent(getCell(robotCoord.up(), newCells)) == Lambda) { indvdum@22: mine.setCells(newCells); indvdum@25: indvdum@22: lastScoreChange = score - prevScore; indvdum@22: state = GameState.Lose; indvdum@22: return state; indvdum@22: } indvdum@22: indvdum@9: mine.setCells(newCells); indvdum@9: indvdum@25: // Water flooding indvdum@25: robotCoord = mine.getRobotCell().getCoordinate(); indvdum@25: if (robotCoord.y <= mine.getWaterLevel()) { indvdum@25: underwater++; indvdum@25: } else { indvdum@25: underwater = 0; indvdum@25: } indvdum@25: if (underwater > mine.getRobotWaterproof()) { indvdum@25: lastScoreChange = score - prevScore; indvdum@25: state = GameState.Lose; indvdum@25: return state; indvdum@25: } indvdum@25: if (mine.getFlooding() > 0 && route.size() % mine.getFlooding() == 0) { indvdum@25: mine.setWaterLevel(mine.getWaterLevel() + 1); indvdum@25: } indvdum@25: 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@20: indvdum@20: public List getRoute() { indvdum@20: return route; indvdum@20: } indvdum@20: indvdum@20: public String getStringRoute() { indvdum@20: String movs = ""; indvdum@20: for (Movement mov : route) { indvdum@20: movs += mov.toText(); indvdum@20: } indvdum@20: return movs; indvdum@20: } indvdum@25: indvdum@25: public int getUnderwater() { indvdum@25: return underwater; indvdum@25: } indvdum@47: indvdum@47: public boolean hasViciousCircle() { indvdum@47: if (route.size() != history.size()) indvdum@47: throw new RuntimeException("Desynchronized history"); indvdum@47: Map state = new HashMap(); indvdum@47: for (int i = route.size() - 1; i >= 0; i--) { indvdum@47: Mine mine = history.get(i); indvdum@47: Movement mov = route.get(i); indvdum@47: if (state.get(mine) == mov) indvdum@47: return true; indvdum@47: state.put(mine, mov); indvdum@47: } indvdum@47: return false; indvdum@47: } indvdum@9: }