icfp-model/src/main/java/ru/bosony/model/game/Game.java
author indvdum (gotoindvdum[at]gmail[dot]com)
Sun, 15 Jul 2012 04:40:38 +0400
changeset 23 606bba9825c8
parent 22 a46d0179df7a
child 25 d6a75f29ba9b
permissions -rw-r--r--
Fixing scores and coordinates
     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 
    34 	public Game(Mine mine) {
    35 		this.mine = mine;
    36 	}
    37 
    38 	public Mine getMine() {
    39 		return mine;
    40 	}
    41 
    42 	protected Cell getCell(Coordinate coordinate, Cell[][] cells) {
    43 		if (coordinate == null)
    44 			return null;
    45 		int x = coordinate.x;
    46 		int y = coordinate.y;
    47 		if (cells == null)
    48 			return null;
    49 		if (x < 1 || x > mine.getSizeX() || y < 1 || y > mine.getSizeY())
    50 			return null;
    51 		return cells[x - 1][mine.getSizeY() - y];
    52 	}
    53 
    54 	protected void moveRobot(Cell nextRobotCell) {
    55 		mine.getRobotCell().setContent(Empty);
    56 		nextRobotCell.setContent(MiningRobot);
    57 	}
    58 
    59 	public GameState move(Movement mov) {
    60 		if (state != GameState.Game)
    61 			throw new RuntimeException("Game over!");
    62 
    63 		// Score
    64 		int prevScore = score;
    65 
    66 		// Moving and scoring
    67 		route.add(mov);
    68 		Coordinate robotCoord = mine.getRobotCell().getCoordinate();
    69 		Coordinate nextRobotCoord = null;
    70 		switch (mov) {
    71 		case ABORT:
    72 			score += 25 * lambdaCollectedCount;
    73 
    74 			lastScoreChange = score - prevScore;
    75 			state = GameState.Abort;
    76 			return state;
    77 		case WAIT:
    78 			break;
    79 		case LEFT:
    80 			nextRobotCoord = robotCoord.left();
    81 			break;
    82 		case UP:
    83 			nextRobotCoord = robotCoord.up();
    84 			break;
    85 		case RIGHT:
    86 			nextRobotCoord = robotCoord.right();
    87 			break;
    88 		case DOWN:
    89 			nextRobotCoord = robotCoord.down();
    90 			break;
    91 		}
    92 		score--;
    93 		Cell nextRobotCell = mine.getCell(nextRobotCoord);
    94 		CellContent nextRobotCellContent = getContent(nextRobotCell);
    95 		if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) {
    96 			moveRobot(nextRobotCell);
    97 		} else if (nextRobotCellContent == Lambda) {
    98 			moveRobot(nextRobotCell);
    99 			lambdaCollectedCount++;
   100 			score += 25;
   101 		} else if (nextRobotCellContent == OpenLambdaLift) {
   102 			moveRobot(nextRobotCell);
   103 			score += 50 * lambdaCollectedCount;
   104 
   105 			lastScoreChange = score - prevScore;
   106 			state = GameState.Win;
   107 			return state;
   108 		} else if (robotCoord.right().equals(nextRobotCoord) && nextRobotCellContent == Rock
   109 				&& getContent(mine.getCell(nextRobotCoord.right())) == Empty) {
   110 			moveRobot(nextRobotCell);
   111 			mine.getCell(nextRobotCoord.right()).setContent(Rock);
   112 		} else if (robotCoord.left().equals(nextRobotCoord) && nextRobotCellContent == Rock
   113 				&& getContent(mine.getCell(nextRobotCoord.left())) == Empty) {
   114 			moveRobot(nextRobotCell);
   115 			mine.getCell(nextRobotCoord.left()).setContent(Rock);
   116 		} else {
   117 			mov = Movement.WAIT;
   118 		}
   119 
   120 		// World changes
   121 		Cell[][] newCells = mine.cloneCells();
   122 		for (int y = 1; y <= mine.getSizeY(); y++) {
   123 			for (int x = 1; x <= mine.getSizeX(); x++) {
   124 				Coordinate curCoord = new Coordinate(x, y);
   125 				Coordinate leftCoord = curCoord.left();
   126 				Coordinate upCoord = curCoord.up();
   127 				Coordinate rightCoord = curCoord.right();
   128 				Coordinate downCoord = curCoord.down();
   129 				Coordinate leftAndUpCoord = curCoord.leftAndUp();
   130 				Coordinate rightAndUpCoord = curCoord.rightAndUp();
   131 				Coordinate rightAndDownCoord = curCoord.rightAndDown();
   132 				Coordinate leftAndDownCoord = curCoord.leftAndDown();
   133 				Cell curCell = mine.getCell(curCoord);
   134 				Cell leftCell = mine.getCell(leftCoord);
   135 				Cell upCell = mine.getCell(upCoord);
   136 				Cell rightCell = mine.getCell(rightCoord);
   137 				Cell downCell = mine.getCell(downCoord);
   138 				Cell leftAndUpCell = mine.getCell(leftAndUpCoord);
   139 				Cell rightAndUpCell = mine.getCell(rightAndUpCoord);
   140 				Cell rightAndDownCell = mine.getCell(rightAndDownCoord);
   141 				Cell leftAndDownCell = mine.getCell(leftAndDownCoord);
   142 
   143 				if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
   144 					getCell(curCoord, newCells).setContent(Empty);
   145 					getCell(downCoord, newCells).setContent(Rock);
   146 				} else if (getContent(curCell) == Rock && getContent(downCell) == Empty
   147 						&& getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
   148 					getCell(curCoord, newCells).setContent(Empty);
   149 					getCell(rightAndDownCoord, newCells).setContent(Rock);
   150 				} else if (getContent(curCell) == Rock && getContent(downCell) == Rock
   151 						&& (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
   152 						&& getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
   153 					getCell(curCoord, newCells).setContent(Empty);
   154 					getCell(leftAndDownCoord, newCells).setContent(Rock);
   155 				} else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
   156 						&& getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
   157 					getCell(curCoord, newCells).setContent(Empty);
   158 					getCell(rightAndDownCoord, newCells).setContent(Rock);
   159 				} else if (getContent(curCell) == ClosedLambdaLift && mine.findCells(Lambda).size() == 0) {
   160 					getCell(curCoord, newCells).setContent(OpenLambdaLift);
   161 				}
   162 			}
   163 		}
   164 
   165 		// Other ending conditions
   166 		robotCoord = mine.getRobotCell().getCoordinate();
   167 		if (getContent(mine.getCell(robotCoord.up())) != Rock && getContent(getCell(robotCoord.up(), newCells)) == Rock) {
   168 			mine.setCells(newCells);
   169 			lastScoreChange = score - prevScore;
   170 			state = GameState.Lose;
   171 			return state;
   172 		}
   173 
   174 		mine.setCells(newCells);
   175 
   176 		lastScoreChange = score - prevScore;
   177 		state = GameState.Game;
   178 		return state;
   179 	}
   180 
   181 	public GameState move(List<Movement> movs) {
   182 		for (Movement mov : movs) {
   183 			if (move(mov) != GameState.Game)
   184 				return state;
   185 		}
   186 		return state;
   187 	}
   188 
   189 	public GameState move(String route) {
   190 		List<Movement> movs = new ArrayList<Movement>();
   191 		for (int i = 0; i < route.length(); i++) {
   192 			String s = "" + route.charAt(i);
   193 			for (Movement mov : Movement.values()) {
   194 				if (mov.toText().equals(s)) {
   195 					movs.add(mov);
   196 					break;
   197 				}
   198 			}
   199 		}
   200 		return move(movs);
   201 	}
   202 
   203 	protected CellContent getContent(Cell cell) {
   204 		if (cell == null)
   205 			return null;
   206 		return cell.getContent();
   207 	}
   208 
   209 	public int getLastScoreChange() {
   210 		return lastScoreChange;
   211 	}
   212 
   213 	public int getScore() {
   214 		return score;
   215 	}
   216 
   217 	public GameState getState() {
   218 		return state;
   219 	}
   220 
   221 	public int getLambdaCollectedCount() {
   222 		return lambdaCollectedCount;
   223 	}
   224 
   225 	public List<Movement> getRoute() {
   226 		return route;
   227 	}
   228 
   229 	public String getStringRoute() {
   230 		String movs = "";
   231 		for (Movement mov : route) {
   232 			movs += mov.toText();
   233 		}
   234 		return movs;
   235 	}
   236 }