icfp-model/src/main/java/ru/bosony/model/mine/Mine.java
author indvdum (gotoindvdum[at]gmail[dot]com)
Sat, 14 Jul 2012 22:49:59 +0400
branchindvdum
changeset 8 ad884dc6877c
parent 7 251961e44b5f
child 9 336dcbecb53c
permissions -rw-r--r--
World processor
     1 package ru.bosony.model.mine;
     2 
     3 import java.util.Arrays;
     4 import java.util.Collection;
     5 import java.util.HashSet;
     6 import java.util.List;
     7 import java.util.Set;
     8 
     9 import org.hamcrest.CoreMatchers;
    10 
    11 import ru.bosony.model.cellscontents.CellContent;
    12 import static ru.bosony.model.cellscontents.CellContent.*;
    13 import ru.bosony.model.io.TextRepresentable;
    14 import ru.bosony.model.moving.Movement;
    15 
    16 /**
    17  * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
    18  *         14.07.2012 2:02:18
    19  * 
    20  */
    21 public class Mine implements TextRepresentable {
    22 
    23 	private Cell[][]	cells;
    24 	private Cell[][]	prevStepCells;
    25 	protected int		sizeX;
    26 	protected int		sizeY;
    27 	protected Cell		robotCell;
    28 	protected int		score					= 0;
    29 	protected int		lastScoreChange			= 0;
    30 	protected GameState	state					= GameState.Game;
    31 	protected int		lambdaCollectedCount	= 0;
    32 
    33 	public Mine(String text) {
    34 		fromText(text);
    35 		if (findCells(OpenLambdaLift).size() != 0)
    36 			throw new RuntimeException("Detected OpenLambdaLift in new map!");
    37 		if (findCells(MiningRobot).size() != 1)
    38 			throw new RuntimeException("New map does not contain a robot!");
    39 		if (findCells(ClosedLambdaLift).size() != 1)
    40 			throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
    41 	}
    42 
    43 	@Override
    44 	public String toText() {
    45 		String text = "";
    46 		for (int y = 0; y < sizeY; y++) {
    47 			for (int x = 0; x < sizeX; x++) {
    48 				text += cells[x][y].getContent().toText();
    49 			}
    50 			text += "\n";
    51 		}
    52 		return text.replaceAll("\n*$", "");
    53 	}
    54 
    55 	private void fromText(String text) {
    56 		String[] rows = text.split("\n+");
    57 		int y = rows.length;
    58 		int x = 0;
    59 		for (String row : rows) {
    60 			if (x < row.length())
    61 				x = row.length();
    62 		}
    63 		sizeX = x;
    64 		sizeY = y;
    65 		cells = new Cell[x][y];
    66 		for (int curY = 0; curY < rows.length; curY++) {
    67 			String row = rows[curY];
    68 			for (int curX = 0; curX < row.length() || curX < x; curX++) {
    69 				CellContent content = null;
    70 				if (curX < row.length())
    71 					content = CellContent.fromText("" + row.charAt(curX));
    72 				else
    73 					content = Empty;
    74 				Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
    75 				cells[curX][curY] = cell;
    76 				if (content == MiningRobot)
    77 					robotCell = cell;
    78 			}
    79 		}
    80 	}
    81 
    82 	public Cell getCell(Coordinate coordinate) {
    83 		return getCell(coordinate, cells);
    84 	}
    85 
    86 	protected Cell getCell(Coordinate coordinate, Cell[][] cells) {
    87 		if (coordinate == null)
    88 			return null;
    89 		int x = coordinate.x;
    90 		int y = coordinate.y;
    91 		if (cells == null)
    92 			return null;
    93 		if (x < 1 || x > sizeX || y < 1 || y > sizeY)
    94 			return null;
    95 		return cells[x - 1][sizeY - y];
    96 	}
    97 
    98 	@Override
    99 	public String toString() {
   100 		return toText();
   101 	}
   102 
   103 	public int getSizeX() {
   104 		return sizeX;
   105 	}
   106 
   107 	public int getSizeY() {
   108 		return sizeY;
   109 	}
   110 
   111 	public Set<Movement> getAvailableRobotMovements() {
   112 		Set<Movement> movs = new HashSet<Movement>();
   113 		Set<Cell> nCells = getNeighboringCells(robotCell);
   114 		for (Cell nCell : nCells) {
   115 			CellContent nContent = nCell.getContent();
   116 			Coordinate nCoordinate = nCell.getCoordinate();
   117 			Cell rightNCell = getCell(nCoordinate.right());
   118 			Cell leftNCell = getCell(nCoordinate.left());
   119 			if (nContent == Empty
   120 					|| nContent == Earth
   121 					|| nContent == Lambda
   122 					|| nContent == OpenLambdaLift
   123 					|| (nContent == Rock && robotCell.getCoordinate().right().equals(nCoordinate) && rightNCell != null && rightNCell
   124 							.getContent() == Empty)
   125 					|| (nContent == Rock && robotCell.getCoordinate().left().equals(nCoordinate) && leftNCell != null && leftNCell
   126 							.getContent() == Empty)) {
   127 				Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate);
   128 				movs.add(mov);
   129 			}
   130 		}
   131 		movs.add(Movement.ABORT);
   132 		movs.add(Movement.WAIT);
   133 		return movs;
   134 	}
   135 
   136 	public Set<Cell> getNeighboringCells(Cell cell) {
   137 		Set<Cell> cells = new HashSet<Cell>();
   138 		for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates())
   139 			cells.add(getCell(coordinate));
   140 		return cells;
   141 	}
   142 
   143 	protected void moveRobot(Cell nextRobotCell) {
   144 		robotCell.setContent(Empty);
   145 		nextRobotCell.setContent(MiningRobot);
   146 		robotCell = nextRobotCell;
   147 	}
   148 
   149 	public GameState move(Movement mov) {
   150 		if (state != GameState.Game)
   151 			throw new RuntimeException("Game over!");
   152 
   153 		// Score
   154 		int prevScore = score;
   155 		score--;
   156 
   157 		// Moving and scoring
   158 		Coordinate nextRobotCoord = null;
   159 		switch (mov) {
   160 		case ABORT:
   161 			score += 25 * lambdaCollectedCount;
   162 			
   163 			lastScoreChange = score - prevScore;
   164 			state = GameState.Abort;
   165 			return state;
   166 		case WAIT:
   167 			break;
   168 		case LEFT:
   169 			nextRobotCoord = robotCell.coordinate.left();
   170 			break;
   171 		case UP:
   172 			nextRobotCoord = robotCell.coordinate.up();
   173 			break;
   174 		case RIGHT:
   175 			nextRobotCoord = robotCell.coordinate.right();
   176 			break;
   177 		case DOWN:
   178 			nextRobotCoord = robotCell.coordinate.down();
   179 			break;
   180 		}
   181 		Cell nextRobotCell = getCell(nextRobotCoord);
   182 		CellContent nextRobotCellContent = getContent(nextRobotCell);
   183 		if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) {
   184 			moveRobot(nextRobotCell);
   185 		} else if (nextRobotCellContent == Lambda) {
   186 			moveRobot(nextRobotCell);
   187 			lambdaCollectedCount++;
   188 			score += 25;
   189 		} else if (nextRobotCellContent == OpenLambdaLift) {
   190 			moveRobot(nextRobotCell);
   191 			score += 50 * lambdaCollectedCount;
   192 			
   193 			lastScoreChange = score - prevScore;
   194 			state = GameState.Win;
   195 			return state;
   196 		} else if (robotCell.coordinate.right() == nextRobotCoord && nextRobotCellContent == Rock && getContent(getCell(nextRobotCoord.right())) == Empty) {
   197 			moveRobot(nextRobotCell);
   198 			getCell(nextRobotCoord.right()).setContent(Rock);
   199 		} else if (robotCell.coordinate.left() == nextRobotCoord && nextRobotCellContent == Rock && getContent(getCell(nextRobotCoord.left())) == Empty) {
   200 			moveRobot(nextRobotCell);
   201 			getCell(nextRobotCoord.left()).setContent(Rock);
   202 		} else {
   203 			mov = Movement.WAIT;
   204 		}
   205 
   206 		// Other ending conditions
   207 		if (getContent(getCell(robotCell.coordinate.up(), prevStepCells)) != Rock
   208 				&& getContent(getCell(robotCell.coordinate.up())) == Rock) {
   209 			lastScoreChange = score - prevScore;
   210 			state = GameState.Lose;
   211 			return state;
   212 		}
   213 
   214 		// World changes
   215 		Cell[][] newCells = Arrays.copyOf(cells, cells.length);
   216 		for (int y = 1; y <= sizeY; y++) {
   217 			for (int x = 1; x <= sizeX; x++) {
   218 				Coordinate curCoord = new Coordinate(x, y);
   219 				Coordinate leftCoord = curCoord.left();
   220 				Coordinate upCoord = curCoord.up();
   221 				Coordinate rightCoord = curCoord.right();
   222 				Coordinate downCoord = curCoord.down();
   223 				Coordinate leftAndUpCoord = curCoord.leftAndUp();
   224 				Coordinate rightAndUpCoord = curCoord.rightAndUp();
   225 				Coordinate rightAndDownCoord = curCoord.rightAndDown();
   226 				Coordinate leftAndDownCoord = curCoord.leftAndDown();
   227 				Cell curCell = getCell(curCoord);
   228 				Cell leftCell = getCell(leftCoord);
   229 				Cell upCell = getCell(upCoord);
   230 				Cell rightCell = getCell(rightCoord);
   231 				Cell downCell = getCell(downCoord);
   232 				Cell leftAndUpCell = getCell(leftAndUpCoord);
   233 				Cell rightAndUpCell = getCell(rightAndUpCoord);
   234 				Cell rightAndDownCell = getCell(rightAndDownCoord);
   235 				Cell leftAndDownCell = getCell(leftAndDownCoord);
   236 
   237 				if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
   238 					getCell(curCoord, newCells).setContent(Empty);
   239 					getCell(downCoord, newCells).setContent(Rock);
   240 				} else if (getContent(curCell) == Rock && getContent(downCell) == Empty
   241 						&& getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
   242 					getCell(curCoord, newCells).setContent(Empty);
   243 					getCell(rightAndDownCoord, newCells).setContent(Rock);
   244 				} else if (getContent(curCell) == Rock && getContent(downCell) == Rock
   245 						&& (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
   246 						&& getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
   247 					getCell(curCoord, newCells).setContent(Empty);
   248 					getCell(leftAndDownCoord, newCells).setContent(Rock);
   249 				} else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
   250 						&& getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
   251 					getCell(curCoord, newCells).setContent(Empty);
   252 					getCell(rightAndDownCoord, newCells).setContent(Rock);
   253 				} else if (getContent(curCell) == ClosedLambdaLift && findCells(Lambda).size() == 0) {
   254 					getCell(curCoord, newCells).setContent(OpenLambdaLift);
   255 				}
   256 			}
   257 		}
   258 		prevStepCells = cells;
   259 		cells = newCells;
   260 
   261 		lastScoreChange = score - prevScore;
   262 		state = GameState.Game;
   263 		return state;
   264 	}
   265 
   266 	protected CellContent getContent(Cell cell) {
   267 		if (cell == null)
   268 			return null;
   269 		return cell.getContent();
   270 	}
   271 
   272 	public Collection<Cell> findCells(CellContent content) {
   273 		Collection<Cell> finded = new HashSet<Cell>();
   274 		for (int y = sizeY - 1; y >= 0; y--) {
   275 			for (int x = 0; x < sizeX - 1; x++) {
   276 				if (cells[x][y].getContent() == content)
   277 					finded.add(cells[x][y]);
   278 			}
   279 		}
   280 		return finded;
   281 	}
   282 
   283 	public int getLastScoreChange() {
   284 		return lastScoreChange;
   285 	}
   286 
   287 }