icfp-model/src/main/java/ru/bosony/model/game/Game.java
author indvdum (gotoindvdum[at]gmail[dot]com)
Mon, 16 Jul 2012 18:58:08 +0400
branchCleverSearch
changeset 47 b3d48e364f9e
parent 30 666821484adc
child 50 9530f55e9639
permissions -rw-r--r--
Moar
     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.HighOrderRock;
     7 import static ru.bosony.model.cellscontents.CellContent.HuttonsRazor;
     8 import static ru.bosony.model.cellscontents.CellContent.Lambda;
     9 import static ru.bosony.model.cellscontents.CellContent.MiningRobot;
    10 import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift;
    11 import static ru.bosony.model.cellscontents.CellContent.Rock;
    12 import static ru.bosony.model.cellscontents.CellContent.WadlersBeard;
    13 
    14 import java.util.ArrayList;
    15 import java.util.HashMap;
    16 import java.util.List;
    17 import java.util.Map;
    18 
    19 import ru.bosony.model.cellscontents.CellContent;
    20 import ru.bosony.model.mine.Cell;
    21 import ru.bosony.model.mine.Mine;
    22 import ru.bosony.model.moving.Coordinate;
    23 import ru.bosony.model.moving.Movement;
    24 
    25 /**
    26  * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
    27  *         14.07.2012 22:51:20
    28  * 
    29  */
    30 public class Game {
    31 
    32 	protected Mine				mine					= null;
    33 	protected int				score					= 0;
    34 	protected int				lastScoreChange			= 0;
    35 	protected GameState			state					= GameState.Game;
    36 	protected int				lambdaCollectedCount	= 0;
    37 	protected List<Mine>		history					= new ArrayList<Mine>();
    38 	protected List<Movement>	route					= new ArrayList<Movement>();
    39 	protected int				underwater				= 0;
    40 
    41 	public Game(Mine mine) {
    42 		this.mine = mine;
    43 	}
    44 
    45 	public Mine getMine() {
    46 		return mine;
    47 	}
    48 
    49 	protected Cell getCell(Coordinate coordinate, Cell[][] cells) {
    50 		if (coordinate == null)
    51 			return null;
    52 		int x = coordinate.x;
    53 		int y = coordinate.y;
    54 		if (cells == null)
    55 			return null;
    56 		if (x < 1 || x > mine.getSizeX() || y < 1 || y > mine.getSizeY())
    57 			return null;
    58 		return cells[x - 1][mine.getSizeY() - y];
    59 	}
    60 
    61 	protected void moveRobot(Cell nextRobotCell) {
    62 		mine.getRobotCell().setContent(Empty);
    63 		nextRobotCell.setContent(MiningRobot);
    64 	}
    65 
    66 	public GameState move(Movement mov) {
    67 		if (state != GameState.Game)
    68 			throw new RuntimeException("Game over!");
    69 
    70 		// Score
    71 		int prevScore = score;
    72 
    73 		// Moving and scoring
    74 		route.add(mov);
    75 		try {
    76 			history.add(mine.clone());
    77 		} catch (CloneNotSupportedException e) {
    78 			throw new RuntimeException(e);
    79 		}
    80 		Cell robotCell = mine.getRobotCell();
    81 		Coordinate robotCoord = robotCell.getCoordinate();
    82 		Coordinate nextRobotCoord = null;
    83 		switch (mov) {
    84 		case ABORT:
    85 			score += 25 * lambdaCollectedCount;
    86 
    87 			lastScoreChange = score - prevScore;
    88 			state = GameState.Abort;
    89 			return state;
    90 		case WAIT:
    91 			break;
    92 		case RAZOR:
    93 			if (mine.getRazorsCount() > 0) {
    94 				for (Cell cell : mine.getNeighboringCells(robotCell)) {
    95 					if (cell.getContent() == WadlersBeard) {
    96 						cell.setContent(Empty);
    97 					}
    98 				}
    99 				mine.setRazorsCount(mine.getRazorsCount() - 1);
   100 			}
   101 			break;
   102 		case LEFT:
   103 			nextRobotCoord = robotCoord.left();
   104 			break;
   105 		case UP:
   106 			nextRobotCoord = robotCoord.up();
   107 			break;
   108 		case RIGHT:
   109 			nextRobotCoord = robotCoord.right();
   110 			break;
   111 		case DOWN:
   112 			nextRobotCoord = robotCoord.down();
   113 			break;
   114 		}
   115 		score--;
   116 		Cell nextRobotCell = mine.getCell(nextRobotCoord);
   117 		CellContent nextRobotCellContent = getContent(nextRobotCell);
   118 		if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) {
   119 			moveRobot(nextRobotCell);
   120 		} else if (nextRobotCellContent == Lambda) {
   121 			moveRobot(nextRobotCell);
   122 			lambdaCollectedCount++;
   123 			score += 25;
   124 		} else if (nextRobotCellContent == OpenLambdaLift) {
   125 			moveRobot(nextRobotCell);
   126 			score += 50 * lambdaCollectedCount;
   127 
   128 			lastScoreChange = score - prevScore;
   129 			state = GameState.Win;
   130 			return state;
   131 		} else if (nextRobotCellContent == HuttonsRazor) {
   132 			moveRobot(nextRobotCell);
   133 			mine.setRazorsCount(mine.getRazorsCount() + 1);
   134 		} else if (robotCoord.right().equals(nextRobotCoord) && nextRobotCellContent == Rock
   135 				&& getContent(mine.getCell(nextRobotCoord.right())) == Empty) {
   136 			moveRobot(nextRobotCell);
   137 			mine.getCell(nextRobotCoord.right()).setContent(Rock);
   138 		} else if (robotCoord.left().equals(nextRobotCoord) && nextRobotCellContent == Rock
   139 				&& getContent(mine.getCell(nextRobotCoord.left())) == Empty) {
   140 			moveRobot(nextRobotCell);
   141 			mine.getCell(nextRobotCoord.left()).setContent(Rock);
   142 		} else if (robotCoord.right().equals(nextRobotCoord) && nextRobotCellContent == HighOrderRock
   143 				&& getContent(mine.getCell(nextRobotCoord.right())) == Empty) {
   144 			moveRobot(nextRobotCell);
   145 			mine.getCell(nextRobotCoord.right()).setContent(HighOrderRock);
   146 		} else if (robotCoord.left().equals(nextRobotCoord) && nextRobotCellContent == HighOrderRock
   147 				&& getContent(mine.getCell(nextRobotCoord.left())) == Empty) {
   148 			moveRobot(nextRobotCell);
   149 			mine.getCell(nextRobotCoord.left()).setContent(HighOrderRock);
   150 		} else if (CellContent.getTrampolines().contains(nextRobotCellContent)
   151 				&& CellContent.getTargets().contains(nextRobotCellContent.getTrampolineTarget())) {
   152 			CellContent trampoline = nextRobotCellContent;
   153 			CellContent target = trampoline.getTrampolineTarget();
   154 			for (Cell targetCell : mine.findCells(target)) {
   155 				moveRobot(targetCell);
   156 			}
   157 			for (CellContent tramp : CellContent.getTrampolines()) {
   158 				if (tramp.getTrampolineTarget() == target) {
   159 					for (Cell trampolineCell : mine.findCells(tramp)) {
   160 						trampolineCell.setContent(Empty);
   161 					}
   162 				}
   163 			}
   164 		} else {
   165 			mov = Movement.WAIT;
   166 		}
   167 
   168 		// World changes
   169 		Cell[][] newCells = mine.cloneCells();
   170 		for (int y = 1; y <= mine.getSizeY(); y++) {
   171 			for (int x = 1; x <= mine.getSizeX(); x++) {
   172 				Coordinate curCoord = new Coordinate(x, y);
   173 				Coordinate leftCoord = curCoord.left();
   174 				Coordinate upCoord = curCoord.up();
   175 				Coordinate rightCoord = curCoord.right();
   176 				Coordinate downCoord = curCoord.down();
   177 				Coordinate leftAndUpCoord = curCoord.leftAndUp();
   178 				Coordinate rightAndUpCoord = curCoord.rightAndUp();
   179 				Coordinate rightAndDownCoord = curCoord.rightAndDown();
   180 				Coordinate leftAndDownCoord = curCoord.leftAndDown();
   181 				Cell curCell = mine.getCell(curCoord);
   182 				Cell leftCell = mine.getCell(leftCoord);
   183 				Cell upCell = mine.getCell(upCoord);
   184 				Cell rightCell = mine.getCell(rightCoord);
   185 				Cell downCell = mine.getCell(downCoord);
   186 				Cell leftAndUpCell = mine.getCell(leftAndUpCoord);
   187 				Cell rightAndUpCell = mine.getCell(rightAndUpCoord);
   188 				Cell rightAndDownCell = mine.getCell(rightAndDownCoord);
   189 				Cell leftAndDownCell = mine.getCell(leftAndDownCoord);
   190 
   191 				if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
   192 					getCell(curCoord, newCells).setContent(Empty);
   193 					getCell(downCoord, newCells).setContent(Rock);
   194 				} else if (getContent(curCell) == HighOrderRock && getContent(downCell) == Empty) {
   195 					getCell(curCoord, newCells).setContent(Empty);
   196 					if (getContent(mine.getCell(downCoord.down())) != Empty)
   197 						getCell(downCoord, newCells).setContent(Lambda);
   198 					else
   199 						getCell(downCoord, newCells).setContent(HighOrderRock);
   200 				} else if (getContent(curCell) == Rock
   201 						&& (getContent(downCell) == Rock || getContent(downCell) == HighOrderRock)
   202 						&& getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
   203 					getCell(curCoord, newCells).setContent(Empty);
   204 					getCell(rightAndDownCoord, newCells).setContent(Rock);
   205 				} else if (getContent(curCell) == HighOrderRock
   206 						&& (getContent(downCell) == Rock || getContent(downCell) == HighOrderRock)
   207 						&& getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
   208 					getCell(curCoord, newCells).setContent(Empty);
   209 					if (getContent(mine.getCell(rightAndDownCoord.down())) != Empty)
   210 						getCell(rightAndDownCoord, newCells).setContent(Lambda);
   211 					else
   212 						getCell(rightAndDownCoord, newCells).setContent(HighOrderRock);
   213 				} else if (getContent(curCell) == Rock
   214 						&& (getContent(downCell) == Rock || getContent(downCell) == HighOrderRock)
   215 						&& (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
   216 						&& getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
   217 					getCell(curCoord, newCells).setContent(Empty);
   218 					getCell(leftAndDownCoord, newCells).setContent(Rock);
   219 				} else if (getContent(curCell) == HighOrderRock
   220 						&& (getContent(downCell) == Rock || getContent(downCell) == HighOrderRock)
   221 						&& (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
   222 						&& getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
   223 					getCell(curCoord, newCells).setContent(Empty);
   224 					if (getContent(mine.getCell(leftAndDownCoord.down())) != Empty)
   225 						getCell(leftAndDownCoord, newCells).setContent(Lambda);
   226 					else
   227 						getCell(leftAndDownCoord, newCells).setContent(HighOrderRock);
   228 				} else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
   229 						&& getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
   230 					getCell(curCoord, newCells).setContent(Empty);
   231 					getCell(rightAndDownCoord, newCells).setContent(Rock);
   232 				} else if (getContent(curCell) == HighOrderRock && getContent(downCell) == Lambda
   233 						&& getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
   234 					getCell(curCoord, newCells).setContent(Empty);
   235 					if (getContent(mine.getCell(rightAndDownCoord.down())) != Empty)
   236 						getCell(rightAndDownCoord, newCells).setContent(Lambda);
   237 					else
   238 						getCell(rightAndDownCoord, newCells).setContent(HighOrderRock);
   239 				} else if (getContent(curCell) == ClosedLambdaLift
   240 						&& mine.getLambdasAndHighOrderRocksStartCount() == lambdaCollectedCount) {
   241 					getCell(curCoord, newCells).setContent(OpenLambdaLift);
   242 				} else if (getContent(curCell) == WadlersBeard && mine.getGrowth() > 0
   243 						&& route.size() % mine.getGrowth() == 0) {
   244 					if (getContent(leftCell) == Empty)
   245 						getCell(leftCoord, newCells).setContent(WadlersBeard);
   246 					if (getContent(upCell) == Empty)
   247 						getCell(upCoord, newCells).setContent(WadlersBeard);
   248 					if (getContent(rightCell) == Empty)
   249 						getCell(rightCoord, newCells).setContent(WadlersBeard);
   250 					if (getContent(downCell) == Empty)
   251 						getCell(downCoord, newCells).setContent(WadlersBeard);
   252 					if (getContent(leftAndUpCell) == Empty)
   253 						getCell(leftAndUpCoord, newCells).setContent(WadlersBeard);
   254 					if (getContent(rightAndUpCell) == Empty)
   255 						getCell(rightAndUpCoord, newCells).setContent(WadlersBeard);
   256 					if (getContent(leftAndDownCell) == Empty)
   257 						getCell(leftAndDownCoord, newCells).setContent(WadlersBeard);
   258 					if (getContent(rightAndDownCell) == Empty)
   259 						getCell(rightAndDownCoord, newCells).setContent(WadlersBeard);
   260 				}
   261 			}
   262 		}
   263 
   264 		// Other ending conditions
   265 		robotCoord = mine.getRobotCell().getCoordinate();
   266 		if (getContent(mine.getCell(robotCoord.up())) != Rock && getContent(getCell(robotCoord.up(), newCells)) == Rock
   267 				|| getContent(mine.getCell(robotCoord.up())) != HighOrderRock
   268 				&& getContent(getCell(robotCoord.up(), newCells)) == HighOrderRock
   269 				|| getContent(mine.getCell(robotCoord.up())) == Empty
   270 				&& getContent(getCell(robotCoord.up(), newCells)) == Lambda) {
   271 			mine.setCells(newCells);
   272 
   273 			lastScoreChange = score - prevScore;
   274 			state = GameState.Lose;
   275 			return state;
   276 		}
   277 
   278 		mine.setCells(newCells);
   279 
   280 		// Water flooding
   281 		robotCoord = mine.getRobotCell().getCoordinate();
   282 		if (robotCoord.y <= mine.getWaterLevel()) {
   283 			underwater++;
   284 		} else {
   285 			underwater = 0;
   286 		}
   287 		if (underwater > mine.getRobotWaterproof()) {
   288 			lastScoreChange = score - prevScore;
   289 			state = GameState.Lose;
   290 			return state;
   291 		}
   292 		if (mine.getFlooding() > 0 && route.size() % mine.getFlooding() == 0) {
   293 			mine.setWaterLevel(mine.getWaterLevel() + 1);
   294 		}
   295 
   296 		lastScoreChange = score - prevScore;
   297 		state = GameState.Game;
   298 		return state;
   299 	}
   300 
   301 	public GameState move(List<Movement> movs) {
   302 		for (Movement mov : movs) {
   303 			if (move(mov) != GameState.Game)
   304 				return state;
   305 		}
   306 		return state;
   307 	}
   308 
   309 	public GameState move(String route) {
   310 		List<Movement> movs = new ArrayList<Movement>();
   311 		for (int i = 0; i < route.length(); i++) {
   312 			String s = "" + route.charAt(i);
   313 			for (Movement mov : Movement.values()) {
   314 				if (mov.toText().equals(s)) {
   315 					movs.add(mov);
   316 					break;
   317 				}
   318 			}
   319 		}
   320 		return move(movs);
   321 	}
   322 
   323 	protected CellContent getContent(Cell cell) {
   324 		if (cell == null)
   325 			return null;
   326 		return cell.getContent();
   327 	}
   328 
   329 	public int getLastScoreChange() {
   330 		return lastScoreChange;
   331 	}
   332 
   333 	public int getScore() {
   334 		return score;
   335 	}
   336 
   337 	public GameState getState() {
   338 		return state;
   339 	}
   340 
   341 	public int getLambdaCollectedCount() {
   342 		return lambdaCollectedCount;
   343 	}
   344 
   345 	public List<Movement> getRoute() {
   346 		return route;
   347 	}
   348 
   349 	public String getStringRoute() {
   350 		String movs = "";
   351 		for (Movement mov : route) {
   352 			movs += mov.toText();
   353 		}
   354 		return movs;
   355 	}
   356 
   357 	public int getUnderwater() {
   358 		return underwater;
   359 	}
   360 
   361 	public boolean hasViciousCircle() {
   362 		if (route.size() != history.size())
   363 			throw new RuntimeException("Desynchronized history");
   364 		Map<Mine, Movement> state = new HashMap<Mine, Movement>();
   365 		for (int i = route.size() - 1; i >= 0; i--) {
   366 			Mine mine = history.get(i);
   367 			Movement mov = route.get(i);
   368 			if (state.get(mine) == mov)
   369 				return true;
   370 			state.put(mine, mov);
   371 		}
   372 		return false;
   373 	}
   374 }