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