icfp-model/src/main/java/ru/bosony/model/game/Game.java
author indvdum (gotoindvdum[at]gmail[dot]com)
Sun, 15 Jul 2012 21:20:04 +0400
changeset 27 505c25412b16
parent 25 d6a75f29ba9b
child 28 9c68562715ed
permissions -rw-r--r--
Add trampolines and targets
     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.List;
    13 
    14 import ru.bosony.model.cellscontents.CellContent;
    15 import ru.bosony.model.mine.Cell;
    16 import ru.bosony.model.mine.Mine;
    17 import ru.bosony.model.moving.Coordinate;
    18 import ru.bosony.model.moving.Movement;
    19 
    20 /**
    21  * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
    22  *         14.07.2012 22:51:20
    23  * 
    24  */
    25 public class Game {
    26 
    27 	protected Mine				mine					= null;
    28 	protected int				score					= 0;
    29 	protected int				lastScoreChange			= 0;
    30 	protected GameState			state					= GameState.Game;
    31 	protected int				lambdaCollectedCount	= 0;
    32 	protected List<Movement>	route					= new ArrayList<Movement>();
    33 	protected int				underwater				= 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 	}
    59 
    60 	public GameState move(Movement mov) {
    61 		if (state != GameState.Game)
    62 			throw new RuntimeException("Game over!");
    63 
    64 		// Score
    65 		int prevScore = score;
    66 
    67 		// Moving and scoring
    68 		route.add(mov);
    69 		Coordinate robotCoord = mine.getRobotCell().getCoordinate();
    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 = robotCoord.left();
    82 			break;
    83 		case UP:
    84 			nextRobotCoord = robotCoord.up();
    85 			break;
    86 		case RIGHT:
    87 			nextRobotCoord = robotCoord.right();
    88 			break;
    89 		case DOWN:
    90 			nextRobotCoord = robotCoord.down();
    91 			break;
    92 		}
    93 		score--;
    94 		Cell nextRobotCell = mine.getCell(nextRobotCoord);
    95 		CellContent nextRobotCellContent = getContent(nextRobotCell);
    96 		if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) {
    97 			moveRobot(nextRobotCell);
    98 		} else if (nextRobotCellContent == Lambda) {
    99 			moveRobot(nextRobotCell);
   100 			lambdaCollectedCount++;
   101 			score += 25;
   102 		} else if (nextRobotCellContent == OpenLambdaLift) {
   103 			moveRobot(nextRobotCell);
   104 			score += 50 * lambdaCollectedCount;
   105 
   106 			lastScoreChange = score - prevScore;
   107 			state = GameState.Win;
   108 			return state;
   109 		} else if (robotCoord.right().equals(nextRobotCoord) && nextRobotCellContent == Rock
   110 				&& getContent(mine.getCell(nextRobotCoord.right())) == Empty) {
   111 			moveRobot(nextRobotCell);
   112 			mine.getCell(nextRobotCoord.right()).setContent(Rock);
   113 		} else if (robotCoord.left().equals(nextRobotCoord) && nextRobotCellContent == Rock
   114 				&& getContent(mine.getCell(nextRobotCoord.left())) == Empty) {
   115 			moveRobot(nextRobotCell);
   116 			mine.getCell(nextRobotCoord.left()).setContent(Rock);
   117 		} else if (CellContent.getTrampolines().contains(nextRobotCellContent)
   118 				&& CellContent.getTargets().contains(nextRobotCellContent.getTrampolineTarget())) {
   119 			CellContent trampoline = nextRobotCellContent;
   120 			CellContent target = trampoline.getTrampolineTarget();
   121 			for (Cell targetCell : mine.findCells(target)) {
   122 				moveRobot(targetCell);
   123 			}
   124 			for (CellContent tramp : CellContent.getTrampolines()) {
   125 				if (tramp.getTrampolineTarget() == target) {
   126 					for (Cell trampolineCell : mine.findCells(tramp)) {
   127 						trampolineCell.setContent(Empty);
   128 					}
   129 				}
   130 			}
   131 		} else {
   132 			mov = Movement.WAIT;
   133 		}
   134 
   135 		// World changes
   136 		Cell[][] newCells = mine.cloneCells();
   137 		for (int y = 1; y <= mine.getSizeY(); y++) {
   138 			for (int x = 1; x <= mine.getSizeX(); x++) {
   139 				Coordinate curCoord = new Coordinate(x, y);
   140 				Coordinate leftCoord = curCoord.left();
   141 				Coordinate upCoord = curCoord.up();
   142 				Coordinate rightCoord = curCoord.right();
   143 				Coordinate downCoord = curCoord.down();
   144 				Coordinate leftAndUpCoord = curCoord.leftAndUp();
   145 				Coordinate rightAndUpCoord = curCoord.rightAndUp();
   146 				Coordinate rightAndDownCoord = curCoord.rightAndDown();
   147 				Coordinate leftAndDownCoord = curCoord.leftAndDown();
   148 				Cell curCell = mine.getCell(curCoord);
   149 				Cell leftCell = mine.getCell(leftCoord);
   150 				Cell upCell = mine.getCell(upCoord);
   151 				Cell rightCell = mine.getCell(rightCoord);
   152 				Cell downCell = mine.getCell(downCoord);
   153 				Cell leftAndUpCell = mine.getCell(leftAndUpCoord);
   154 				Cell rightAndUpCell = mine.getCell(rightAndUpCoord);
   155 				Cell rightAndDownCell = mine.getCell(rightAndDownCoord);
   156 				Cell leftAndDownCell = mine.getCell(leftAndDownCoord);
   157 
   158 				if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
   159 					getCell(curCoord, newCells).setContent(Empty);
   160 					getCell(downCoord, newCells).setContent(Rock);
   161 				} else if (getContent(curCell) == Rock && getContent(downCell) == Empty
   162 						&& getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
   163 					getCell(curCoord, newCells).setContent(Empty);
   164 					getCell(rightAndDownCoord, newCells).setContent(Rock);
   165 				} else if (getContent(curCell) == Rock && getContent(downCell) == Rock
   166 						&& (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
   167 						&& getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
   168 					getCell(curCoord, newCells).setContent(Empty);
   169 					getCell(leftAndDownCoord, newCells).setContent(Rock);
   170 				} else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
   171 						&& getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
   172 					getCell(curCoord, newCells).setContent(Empty);
   173 					getCell(rightAndDownCoord, newCells).setContent(Rock);
   174 				} else if (getContent(curCell) == ClosedLambdaLift && mine.findCells(Lambda).size() == 0) {
   175 					getCell(curCoord, newCells).setContent(OpenLambdaLift);
   176 				}
   177 			}
   178 		}
   179 
   180 		// Other ending conditions
   181 		robotCoord = mine.getRobotCell().getCoordinate();
   182 		if (getContent(mine.getCell(robotCoord.up())) != Rock && getContent(getCell(robotCoord.up(), newCells)) == Rock) {
   183 			mine.setCells(newCells);
   184 
   185 			lastScoreChange = score - prevScore;
   186 			state = GameState.Lose;
   187 			return state;
   188 		}
   189 
   190 		mine.setCells(newCells);
   191 
   192 		// Water flooding
   193 		robotCoord = mine.getRobotCell().getCoordinate();
   194 		if (robotCoord.y <= mine.getWaterLevel()) {
   195 			underwater++;
   196 		} else {
   197 			underwater = 0;
   198 		}
   199 		if (underwater > mine.getRobotWaterproof()) {
   200 			lastScoreChange = score - prevScore;
   201 			state = GameState.Lose;
   202 			return state;
   203 		}
   204 		if (mine.getFlooding() > 0 && route.size() % mine.getFlooding() == 0) {
   205 			mine.setWaterLevel(mine.getWaterLevel() + 1);
   206 		}
   207 
   208 		lastScoreChange = score - prevScore;
   209 		state = GameState.Game;
   210 		return state;
   211 	}
   212 
   213 	public GameState move(List<Movement> movs) {
   214 		for (Movement mov : movs) {
   215 			if (move(mov) != GameState.Game)
   216 				return state;
   217 		}
   218 		return state;
   219 	}
   220 
   221 	public GameState move(String route) {
   222 		List<Movement> movs = new ArrayList<Movement>();
   223 		for (int i = 0; i < route.length(); i++) {
   224 			String s = "" + route.charAt(i);
   225 			for (Movement mov : Movement.values()) {
   226 				if (mov.toText().equals(s)) {
   227 					movs.add(mov);
   228 					break;
   229 				}
   230 			}
   231 		}
   232 		return move(movs);
   233 	}
   234 
   235 	protected CellContent getContent(Cell cell) {
   236 		if (cell == null)
   237 			return null;
   238 		return cell.getContent();
   239 	}
   240 
   241 	public int getLastScoreChange() {
   242 		return lastScoreChange;
   243 	}
   244 
   245 	public int getScore() {
   246 		return score;
   247 	}
   248 
   249 	public GameState getState() {
   250 		return state;
   251 	}
   252 
   253 	public int getLambdaCollectedCount() {
   254 		return lambdaCollectedCount;
   255 	}
   256 
   257 	public List<Movement> getRoute() {
   258 		return route;
   259 	}
   260 
   261 	public String getStringRoute() {
   262 		String movs = "";
   263 		for (Movement mov : route) {
   264 			movs += mov.toText();
   265 		}
   266 		return movs;
   267 	}
   268 
   269 	public int getUnderwater() {
   270 		return underwater;
   271 	}
   272 }