icfp-model/src/main/java/ru/bosony/model/game/Game.java
author indvdum (gotoindvdum[at]gmail[dot]com)
Sat, 14 Jul 2012 23:21:08 +0400
branchindvdum
changeset 9 336dcbecb53c
child 16 4e0d86c1c22a
permissions -rw-r--r--
Some refactoring
     1 package ru.bosony.model.game;
     2 
     3 import static ru.bosony.model.cellscontents.CellContent.ClosedLambdaLift;
     4 import static ru.bosony.model.cellscontents.CellContent.Earth;
     5 import static ru.bosony.model.cellscontents.CellContent.Empty;
     6 import static ru.bosony.model.cellscontents.CellContent.Lambda;
     7 import static ru.bosony.model.cellscontents.CellContent.MiningRobot;
     8 import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift;
     9 import static ru.bosony.model.cellscontents.CellContent.Rock;
    10 
    11 import java.util.Arrays;
    12 
    13 import ru.bosony.model.cellscontents.CellContent;
    14 import ru.bosony.model.mine.Cell;
    15 import ru.bosony.model.mine.Coordinate;
    16 import ru.bosony.model.mine.Mine;
    17 import ru.bosony.model.moving.Movement;
    18 
    19 /**
    20  * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
    21  *         14.07.2012 22:51:20
    22  * 
    23  */
    24 public class Game {
    25 
    26 	Mine				mine					= null;
    27 	private Cell[][]	prevStepCells;
    28 	protected int		score					= 0;
    29 	protected int		lastScoreChange			= 0;
    30 	protected GameState	state					= GameState.Game;
    31 	protected int		lambdaCollectedCount	= 0;
    32 
    33 	public Game(Mine mine) {
    34 		this.mine = mine;
    35 	}
    36 
    37 	public Mine getMine() {
    38 		return mine;
    39 	}
    40 
    41 	protected Cell getCell(Coordinate coordinate, Cell[][] cells) {
    42 		if (coordinate == null)
    43 			return null;
    44 		int x = coordinate.x;
    45 		int y = coordinate.y;
    46 		if (cells == null)
    47 			return null;
    48 		if (x < 1 || x > mine.getSizeX() || y < 1 || y > mine.getSizeY())
    49 			return null;
    50 		return cells[x - 1][mine.getSizeY() - y];
    51 	}
    52 
    53 	protected void moveRobot(Cell nextRobotCell) {
    54 		mine.getRobotCell().setContent(Empty);
    55 		nextRobotCell.setContent(MiningRobot);
    56 		mine.setRobotCell(nextRobotCell);
    57 	}
    58 
    59 	public GameState move(Movement mov) {
    60 		if (state != GameState.Game)
    61 			throw new RuntimeException("Game over!");
    62 
    63 		// Score
    64 		int prevScore = score;
    65 		score--;
    66 
    67 		// Moving and scoring
    68 		Coordinate nextRobotCoord = null;
    69 		switch (mov) {
    70 		case ABORT:
    71 			score += 25 * lambdaCollectedCount;
    72 
    73 			lastScoreChange = score - prevScore;
    74 			state = GameState.Abort;
    75 			return state;
    76 		case WAIT:
    77 			break;
    78 		case LEFT:
    79 			nextRobotCoord = mine.getRobotCell().getCoordinate().left();
    80 			break;
    81 		case UP:
    82 			nextRobotCoord = mine.getRobotCell().getCoordinate().up();
    83 			break;
    84 		case RIGHT:
    85 			nextRobotCoord = mine.getRobotCell().getCoordinate().right();
    86 			break;
    87 		case DOWN:
    88 			nextRobotCoord = mine.getRobotCell().getCoordinate().down();
    89 			break;
    90 		}
    91 		Cell nextRobotCell = mine.getCell(nextRobotCoord);
    92 		CellContent nextRobotCellContent = getContent(nextRobotCell);
    93 		if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) {
    94 			moveRobot(nextRobotCell);
    95 		} else if (nextRobotCellContent == Lambda) {
    96 			moveRobot(nextRobotCell);
    97 			lambdaCollectedCount++;
    98 			score += 25;
    99 		} else if (nextRobotCellContent == OpenLambdaLift) {
   100 			moveRobot(nextRobotCell);
   101 			score += 50 * lambdaCollectedCount;
   102 
   103 			lastScoreChange = score - prevScore;
   104 			state = GameState.Win;
   105 			return state;
   106 		} else if (mine.getRobotCell().getCoordinate().right() == nextRobotCoord && nextRobotCellContent == Rock
   107 				&& getContent(mine.getCell(nextRobotCoord.right())) == Empty) {
   108 			moveRobot(nextRobotCell);
   109 			mine.getCell(nextRobotCoord.right()).setContent(Rock);
   110 		} else if (mine.getRobotCell().getCoordinate().left() == nextRobotCoord && nextRobotCellContent == Rock
   111 				&& getContent(mine.getCell(nextRobotCoord.left())) == Empty) {
   112 			moveRobot(nextRobotCell);
   113 			mine.getCell(nextRobotCoord.left()).setContent(Rock);
   114 		} else {
   115 			mov = Movement.WAIT;
   116 		}
   117 
   118 		// Other ending conditions
   119 		if (getContent(getCell(mine.getRobotCell().getCoordinate().up(), prevStepCells)) != Rock
   120 				&& getContent(mine.getCell(mine.getRobotCell().getCoordinate().up())) == Rock) {
   121 			lastScoreChange = score - prevScore;
   122 			state = GameState.Lose;
   123 			return state;
   124 		}
   125 
   126 		// World changes
   127 		Cell[][] newCells = Arrays.copyOf(mine.getCells(), mine.getCells().length);
   128 		for (int y = 1; y <= mine.getSizeY(); y++) {
   129 			for (int x = 1; x <= mine.getSizeX(); x++) {
   130 				Coordinate curCoord = new Coordinate(x, y);
   131 				Coordinate leftCoord = curCoord.left();
   132 				Coordinate upCoord = curCoord.up();
   133 				Coordinate rightCoord = curCoord.right();
   134 				Coordinate downCoord = curCoord.down();
   135 				Coordinate leftAndUpCoord = curCoord.leftAndUp();
   136 				Coordinate rightAndUpCoord = curCoord.rightAndUp();
   137 				Coordinate rightAndDownCoord = curCoord.rightAndDown();
   138 				Coordinate leftAndDownCoord = curCoord.leftAndDown();
   139 				Cell curCell = mine.getCell(curCoord);
   140 				Cell leftCell = mine.getCell(leftCoord);
   141 				Cell upCell = mine.getCell(upCoord);
   142 				Cell rightCell = mine.getCell(rightCoord);
   143 				Cell downCell = mine.getCell(downCoord);
   144 				Cell leftAndUpCell = mine.getCell(leftAndUpCoord);
   145 				Cell rightAndUpCell = mine.getCell(rightAndUpCoord);
   146 				Cell rightAndDownCell = mine.getCell(rightAndDownCoord);
   147 				Cell leftAndDownCell = mine.getCell(leftAndDownCoord);
   148 
   149 				if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
   150 					getCell(curCoord, newCells).setContent(Empty);
   151 					getCell(downCoord, newCells).setContent(Rock);
   152 				} else if (getContent(curCell) == Rock && getContent(downCell) == Empty
   153 						&& getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
   154 					getCell(curCoord, newCells).setContent(Empty);
   155 					getCell(rightAndDownCoord, newCells).setContent(Rock);
   156 				} else if (getContent(curCell) == Rock && getContent(downCell) == Rock
   157 						&& (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
   158 						&& getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
   159 					getCell(curCoord, newCells).setContent(Empty);
   160 					getCell(leftAndDownCoord, newCells).setContent(Rock);
   161 				} else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
   162 						&& getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
   163 					getCell(curCoord, newCells).setContent(Empty);
   164 					getCell(rightAndDownCoord, newCells).setContent(Rock);
   165 				} else if (getContent(curCell) == ClosedLambdaLift && mine.findCells(Lambda).size() == 0) {
   166 					getCell(curCoord, newCells).setContent(OpenLambdaLift);
   167 				}
   168 			}
   169 		}
   170 		prevStepCells = mine.getCells();
   171 		mine.setCells(newCells);
   172 
   173 		lastScoreChange = score - prevScore;
   174 		state = GameState.Game;
   175 		return state;
   176 	}
   177 
   178 	protected CellContent getContent(Cell cell) {
   179 		if (cell == null)
   180 			return null;
   181 		return cell.getContent();
   182 	}
   183 
   184 	public int getLastScoreChange() {
   185 		return lastScoreChange;
   186 	}
   187 
   188 	public int getScore() {
   189 		return score;
   190 	}
   191 
   192 	public GameState getState() {
   193 		return state;
   194 	}
   195 
   196 	public int getLambdaCollectedCount() {
   197 		return lambdaCollectedCount;
   198 	}
   199 }