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