icfp-model/src/main/java/ru/bosony/model/game/Game.java
branchindvdum
changeset 9 336dcbecb53c
child 16 4e0d86c1c22a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/icfp-model/src/main/java/ru/bosony/model/game/Game.java	Sat Jul 14 23:21:08 2012 +0400
     1.3 @@ -0,0 +1,199 @@
     1.4 +package ru.bosony.model.game;
     1.5 +
     1.6 +import static ru.bosony.model.cellscontents.CellContent.ClosedLambdaLift;
     1.7 +import static ru.bosony.model.cellscontents.CellContent.Earth;
     1.8 +import static ru.bosony.model.cellscontents.CellContent.Empty;
     1.9 +import static ru.bosony.model.cellscontents.CellContent.Lambda;
    1.10 +import static ru.bosony.model.cellscontents.CellContent.MiningRobot;
    1.11 +import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift;
    1.12 +import static ru.bosony.model.cellscontents.CellContent.Rock;
    1.13 +
    1.14 +import java.util.Arrays;
    1.15 +
    1.16 +import ru.bosony.model.cellscontents.CellContent;
    1.17 +import ru.bosony.model.mine.Cell;
    1.18 +import ru.bosony.model.mine.Coordinate;
    1.19 +import ru.bosony.model.mine.Mine;
    1.20 +import ru.bosony.model.moving.Movement;
    1.21 +
    1.22 +/**
    1.23 + * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
    1.24 + *         14.07.2012 22:51:20
    1.25 + * 
    1.26 + */
    1.27 +public class Game {
    1.28 +
    1.29 +	Mine				mine					= null;
    1.30 +	private Cell[][]	prevStepCells;
    1.31 +	protected int		score					= 0;
    1.32 +	protected int		lastScoreChange			= 0;
    1.33 +	protected GameState	state					= GameState.Game;
    1.34 +	protected int		lambdaCollectedCount	= 0;
    1.35 +
    1.36 +	public Game(Mine mine) {
    1.37 +		this.mine = mine;
    1.38 +	}
    1.39 +
    1.40 +	public Mine getMine() {
    1.41 +		return mine;
    1.42 +	}
    1.43 +
    1.44 +	protected Cell getCell(Coordinate coordinate, Cell[][] cells) {
    1.45 +		if (coordinate == null)
    1.46 +			return null;
    1.47 +		int x = coordinate.x;
    1.48 +		int y = coordinate.y;
    1.49 +		if (cells == null)
    1.50 +			return null;
    1.51 +		if (x < 1 || x > mine.getSizeX() || y < 1 || y > mine.getSizeY())
    1.52 +			return null;
    1.53 +		return cells[x - 1][mine.getSizeY() - y];
    1.54 +	}
    1.55 +
    1.56 +	protected void moveRobot(Cell nextRobotCell) {
    1.57 +		mine.getRobotCell().setContent(Empty);
    1.58 +		nextRobotCell.setContent(MiningRobot);
    1.59 +		mine.setRobotCell(nextRobotCell);
    1.60 +	}
    1.61 +
    1.62 +	public GameState move(Movement mov) {
    1.63 +		if (state != GameState.Game)
    1.64 +			throw new RuntimeException("Game over!");
    1.65 +
    1.66 +		// Score
    1.67 +		int prevScore = score;
    1.68 +		score--;
    1.69 +
    1.70 +		// Moving and scoring
    1.71 +		Coordinate nextRobotCoord = null;
    1.72 +		switch (mov) {
    1.73 +		case ABORT:
    1.74 +			score += 25 * lambdaCollectedCount;
    1.75 +
    1.76 +			lastScoreChange = score - prevScore;
    1.77 +			state = GameState.Abort;
    1.78 +			return state;
    1.79 +		case WAIT:
    1.80 +			break;
    1.81 +		case LEFT:
    1.82 +			nextRobotCoord = mine.getRobotCell().getCoordinate().left();
    1.83 +			break;
    1.84 +		case UP:
    1.85 +			nextRobotCoord = mine.getRobotCell().getCoordinate().up();
    1.86 +			break;
    1.87 +		case RIGHT:
    1.88 +			nextRobotCoord = mine.getRobotCell().getCoordinate().right();
    1.89 +			break;
    1.90 +		case DOWN:
    1.91 +			nextRobotCoord = mine.getRobotCell().getCoordinate().down();
    1.92 +			break;
    1.93 +		}
    1.94 +		Cell nextRobotCell = mine.getCell(nextRobotCoord);
    1.95 +		CellContent nextRobotCellContent = getContent(nextRobotCell);
    1.96 +		if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) {
    1.97 +			moveRobot(nextRobotCell);
    1.98 +		} else if (nextRobotCellContent == Lambda) {
    1.99 +			moveRobot(nextRobotCell);
   1.100 +			lambdaCollectedCount++;
   1.101 +			score += 25;
   1.102 +		} else if (nextRobotCellContent == OpenLambdaLift) {
   1.103 +			moveRobot(nextRobotCell);
   1.104 +			score += 50 * lambdaCollectedCount;
   1.105 +
   1.106 +			lastScoreChange = score - prevScore;
   1.107 +			state = GameState.Win;
   1.108 +			return state;
   1.109 +		} else if (mine.getRobotCell().getCoordinate().right() == nextRobotCoord && nextRobotCellContent == Rock
   1.110 +				&& getContent(mine.getCell(nextRobotCoord.right())) == Empty) {
   1.111 +			moveRobot(nextRobotCell);
   1.112 +			mine.getCell(nextRobotCoord.right()).setContent(Rock);
   1.113 +		} else if (mine.getRobotCell().getCoordinate().left() == nextRobotCoord && nextRobotCellContent == Rock
   1.114 +				&& getContent(mine.getCell(nextRobotCoord.left())) == Empty) {
   1.115 +			moveRobot(nextRobotCell);
   1.116 +			mine.getCell(nextRobotCoord.left()).setContent(Rock);
   1.117 +		} else {
   1.118 +			mov = Movement.WAIT;
   1.119 +		}
   1.120 +
   1.121 +		// Other ending conditions
   1.122 +		if (getContent(getCell(mine.getRobotCell().getCoordinate().up(), prevStepCells)) != Rock
   1.123 +				&& getContent(mine.getCell(mine.getRobotCell().getCoordinate().up())) == Rock) {
   1.124 +			lastScoreChange = score - prevScore;
   1.125 +			state = GameState.Lose;
   1.126 +			return state;
   1.127 +		}
   1.128 +
   1.129 +		// World changes
   1.130 +		Cell[][] newCells = Arrays.copyOf(mine.getCells(), mine.getCells().length);
   1.131 +		for (int y = 1; y <= mine.getSizeY(); y++) {
   1.132 +			for (int x = 1; x <= mine.getSizeX(); x++) {
   1.133 +				Coordinate curCoord = new Coordinate(x, y);
   1.134 +				Coordinate leftCoord = curCoord.left();
   1.135 +				Coordinate upCoord = curCoord.up();
   1.136 +				Coordinate rightCoord = curCoord.right();
   1.137 +				Coordinate downCoord = curCoord.down();
   1.138 +				Coordinate leftAndUpCoord = curCoord.leftAndUp();
   1.139 +				Coordinate rightAndUpCoord = curCoord.rightAndUp();
   1.140 +				Coordinate rightAndDownCoord = curCoord.rightAndDown();
   1.141 +				Coordinate leftAndDownCoord = curCoord.leftAndDown();
   1.142 +				Cell curCell = mine.getCell(curCoord);
   1.143 +				Cell leftCell = mine.getCell(leftCoord);
   1.144 +				Cell upCell = mine.getCell(upCoord);
   1.145 +				Cell rightCell = mine.getCell(rightCoord);
   1.146 +				Cell downCell = mine.getCell(downCoord);
   1.147 +				Cell leftAndUpCell = mine.getCell(leftAndUpCoord);
   1.148 +				Cell rightAndUpCell = mine.getCell(rightAndUpCoord);
   1.149 +				Cell rightAndDownCell = mine.getCell(rightAndDownCoord);
   1.150 +				Cell leftAndDownCell = mine.getCell(leftAndDownCoord);
   1.151 +
   1.152 +				if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
   1.153 +					getCell(curCoord, newCells).setContent(Empty);
   1.154 +					getCell(downCoord, newCells).setContent(Rock);
   1.155 +				} else if (getContent(curCell) == Rock && getContent(downCell) == Empty
   1.156 +						&& getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
   1.157 +					getCell(curCoord, newCells).setContent(Empty);
   1.158 +					getCell(rightAndDownCoord, newCells).setContent(Rock);
   1.159 +				} else if (getContent(curCell) == Rock && getContent(downCell) == Rock
   1.160 +						&& (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
   1.161 +						&& getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
   1.162 +					getCell(curCoord, newCells).setContent(Empty);
   1.163 +					getCell(leftAndDownCoord, newCells).setContent(Rock);
   1.164 +				} else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
   1.165 +						&& getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
   1.166 +					getCell(curCoord, newCells).setContent(Empty);
   1.167 +					getCell(rightAndDownCoord, newCells).setContent(Rock);
   1.168 +				} else if (getContent(curCell) == ClosedLambdaLift && mine.findCells(Lambda).size() == 0) {
   1.169 +					getCell(curCoord, newCells).setContent(OpenLambdaLift);
   1.170 +				}
   1.171 +			}
   1.172 +		}
   1.173 +		prevStepCells = mine.getCells();
   1.174 +		mine.setCells(newCells);
   1.175 +
   1.176 +		lastScoreChange = score - prevScore;
   1.177 +		state = GameState.Game;
   1.178 +		return state;
   1.179 +	}
   1.180 +
   1.181 +	protected CellContent getContent(Cell cell) {
   1.182 +		if (cell == null)
   1.183 +			return null;
   1.184 +		return cell.getContent();
   1.185 +	}
   1.186 +
   1.187 +	public int getLastScoreChange() {
   1.188 +		return lastScoreChange;
   1.189 +	}
   1.190 +
   1.191 +	public int getScore() {
   1.192 +		return score;
   1.193 +	}
   1.194 +
   1.195 +	public GameState getState() {
   1.196 +		return state;
   1.197 +	}
   1.198 +
   1.199 +	public int getLambdaCollectedCount() {
   1.200 +		return lambdaCollectedCount;
   1.201 +	}
   1.202 +}