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