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