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