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