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