icfp-model/src/main/java/ru/bosony/model/game/Game.java
author indvdum (gotoindvdum[at]gmail[dot]com)
Sun, 15 Jul 2012 17:58:43 +0400
changeset 25 d6a75f29ba9b
parent 23 606bba9825c8
child 27 505c25412b16
permissions -rw-r--r--
Add water flooding
     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 {
   118 			mov = Movement.WAIT;
   119 		}
   120 
   121 		// World changes
   122 		Cell[][] newCells = mine.cloneCells();
   123 		for (int y = 1; y <= mine.getSizeY(); y++) {
   124 			for (int x = 1; x <= mine.getSizeX(); x++) {
   125 				Coordinate curCoord = new Coordinate(x, y);
   126 				Coordinate leftCoord = curCoord.left();
   127 				Coordinate upCoord = curCoord.up();
   128 				Coordinate rightCoord = curCoord.right();
   129 				Coordinate downCoord = curCoord.down();
   130 				Coordinate leftAndUpCoord = curCoord.leftAndUp();
   131 				Coordinate rightAndUpCoord = curCoord.rightAndUp();
   132 				Coordinate rightAndDownCoord = curCoord.rightAndDown();
   133 				Coordinate leftAndDownCoord = curCoord.leftAndDown();
   134 				Cell curCell = mine.getCell(curCoord);
   135 				Cell leftCell = mine.getCell(leftCoord);
   136 				Cell upCell = mine.getCell(upCoord);
   137 				Cell rightCell = mine.getCell(rightCoord);
   138 				Cell downCell = mine.getCell(downCoord);
   139 				Cell leftAndUpCell = mine.getCell(leftAndUpCoord);
   140 				Cell rightAndUpCell = mine.getCell(rightAndUpCoord);
   141 				Cell rightAndDownCell = mine.getCell(rightAndDownCoord);
   142 				Cell leftAndDownCell = mine.getCell(leftAndDownCoord);
   143 
   144 				if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
   145 					getCell(curCoord, newCells).setContent(Empty);
   146 					getCell(downCoord, newCells).setContent(Rock);
   147 				} else if (getContent(curCell) == Rock && getContent(downCell) == Empty
   148 						&& getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
   149 					getCell(curCoord, newCells).setContent(Empty);
   150 					getCell(rightAndDownCoord, newCells).setContent(Rock);
   151 				} else if (getContent(curCell) == Rock && getContent(downCell) == Rock
   152 						&& (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
   153 						&& getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
   154 					getCell(curCoord, newCells).setContent(Empty);
   155 					getCell(leftAndDownCoord, newCells).setContent(Rock);
   156 				} else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
   157 						&& getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
   158 					getCell(curCoord, newCells).setContent(Empty);
   159 					getCell(rightAndDownCoord, newCells).setContent(Rock);
   160 				} else if (getContent(curCell) == ClosedLambdaLift && mine.findCells(Lambda).size() == 0) {
   161 					getCell(curCoord, newCells).setContent(OpenLambdaLift);
   162 				}
   163 			}
   164 		}
   165 
   166 		// Other ending conditions
   167 		robotCoord = mine.getRobotCell().getCoordinate();
   168 		if (getContent(mine.getCell(robotCoord.up())) != Rock && getContent(getCell(robotCoord.up(), newCells)) == Rock) {
   169 			mine.setCells(newCells);
   170 
   171 			lastScoreChange = score - prevScore;
   172 			state = GameState.Lose;
   173 			return state;
   174 		}
   175 
   176 		mine.setCells(newCells);
   177 
   178 		// Water flooding
   179 		robotCoord = mine.getRobotCell().getCoordinate();
   180 		if (robotCoord.y <= mine.getWaterLevel()) {
   181 			underwater++;
   182 		} else {
   183 			underwater = 0;
   184 		}
   185 		if (underwater > mine.getRobotWaterproof()) {
   186 			lastScoreChange = score - prevScore;
   187 			state = GameState.Lose;
   188 			return state;
   189 		}
   190 		if (mine.getFlooding() > 0 && route.size() % mine.getFlooding() == 0) {
   191 			mine.setWaterLevel(mine.getWaterLevel() + 1);
   192 		}
   193 
   194 		lastScoreChange = score - prevScore;
   195 		state = GameState.Game;
   196 		return state;
   197 	}
   198 
   199 	public GameState move(List<Movement> movs) {
   200 		for (Movement mov : movs) {
   201 			if (move(mov) != GameState.Game)
   202 				return state;
   203 		}
   204 		return state;
   205 	}
   206 
   207 	public GameState move(String route) {
   208 		List<Movement> movs = new ArrayList<Movement>();
   209 		for (int i = 0; i < route.length(); i++) {
   210 			String s = "" + route.charAt(i);
   211 			for (Movement mov : Movement.values()) {
   212 				if (mov.toText().equals(s)) {
   213 					movs.add(mov);
   214 					break;
   215 				}
   216 			}
   217 		}
   218 		return move(movs);
   219 	}
   220 
   221 	protected CellContent getContent(Cell cell) {
   222 		if (cell == null)
   223 			return null;
   224 		return cell.getContent();
   225 	}
   226 
   227 	public int getLastScoreChange() {
   228 		return lastScoreChange;
   229 	}
   230 
   231 	public int getScore() {
   232 		return score;
   233 	}
   234 
   235 	public GameState getState() {
   236 		return state;
   237 	}
   238 
   239 	public int getLambdaCollectedCount() {
   240 		return lambdaCollectedCount;
   241 	}
   242 
   243 	public List<Movement> getRoute() {
   244 		return route;
   245 	}
   246 
   247 	public String getStringRoute() {
   248 		String movs = "";
   249 		for (Movement mov : route) {
   250 			movs += mov.toText();
   251 		}
   252 		return movs;
   253 	}
   254 
   255 	public int getUnderwater() {
   256 		return underwater;
   257 	}
   258 }