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