icfp-model/src/main/java/ru/bosony/model/game/Game.java
author indvdum (gotoindvdum[at]gmail[dot]com)
Mon, 16 Jul 2012 00:05:50 +0400
changeset 29 66aa96e8fd92
parent 28 9c68562715ed
child 30 666821484adc
permissions -rw-r--r--
Add beards and razors
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@29
    10
import static ru.bosony.model.cellscontents.CellContent.HuttonsRazor;
indvdum@29
    11
import static ru.bosony.model.cellscontents.CellContent.WadlersBeard;
indvdum@9
    12
indvdum@16
    13
import java.util.ArrayList;
indvdum@16
    14
import java.util.List;
indvdum@9
    15
indvdum@9
    16
import ru.bosony.model.cellscontents.CellContent;
indvdum@9
    17
import ru.bosony.model.mine.Cell;
indvdum@9
    18
import ru.bosony.model.mine.Mine;
indvdum@22
    19
import ru.bosony.model.moving.Coordinate;
indvdum@9
    20
import ru.bosony.model.moving.Movement;
indvdum@9
    21
indvdum@9
    22
/**
indvdum@9
    23
 * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
indvdum@9
    24
 *         14.07.2012 22:51:20
indvdum@9
    25
 * 
indvdum@9
    26
 */
indvdum@9
    27
public class Game {
indvdum@9
    28
indvdum@22
    29
	protected Mine				mine					= null;
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@25
    35
	protected int				underwater				= 0;
indvdum@9
    36
indvdum@9
    37
	public Game(Mine mine) {
indvdum@9
    38
		this.mine = mine;
indvdum@9
    39
	}
indvdum@9
    40
indvdum@9
    41
	public Mine getMine() {
indvdum@9
    42
		return mine;
indvdum@9
    43
	}
indvdum@9
    44
indvdum@9
    45
	protected Cell getCell(Coordinate coordinate, Cell[][] cells) {
indvdum@9
    46
		if (coordinate == null)
indvdum@9
    47
			return null;
indvdum@9
    48
		int x = coordinate.x;
indvdum@9
    49
		int y = coordinate.y;
indvdum@9
    50
		if (cells == null)
indvdum@9
    51
			return null;
indvdum@9
    52
		if (x < 1 || x > mine.getSizeX() || y < 1 || y > mine.getSizeY())
indvdum@9
    53
			return null;
indvdum@9
    54
		return cells[x - 1][mine.getSizeY() - y];
indvdum@9
    55
	}
indvdum@9
    56
indvdum@9
    57
	protected void moveRobot(Cell nextRobotCell) {
indvdum@9
    58
		mine.getRobotCell().setContent(Empty);
indvdum@9
    59
		nextRobotCell.setContent(MiningRobot);
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
indvdum@9
    69
		// Moving and scoring
indvdum@20
    70
		route.add(mov);
indvdum@29
    71
		Cell robotCell = mine.getRobotCell();
indvdum@29
    72
		Coordinate robotCoord = robotCell.getCoordinate();
indvdum@9
    73
		Coordinate nextRobotCoord = null;
indvdum@9
    74
		switch (mov) {
indvdum@9
    75
		case ABORT:
indvdum@9
    76
			score += 25 * lambdaCollectedCount;
indvdum@9
    77
indvdum@9
    78
			lastScoreChange = score - prevScore;
indvdum@9
    79
			state = GameState.Abort;
indvdum@9
    80
			return state;
indvdum@9
    81
		case WAIT:
indvdum@9
    82
			break;
indvdum@29
    83
		case RAZOR:
indvdum@29
    84
			if (mine.getRazorsCount() > 0) {
indvdum@29
    85
				for (Cell cell : mine.getNeighboringCells(robotCell)) {
indvdum@29
    86
					if (cell.getContent() == WadlersBeard) {
indvdum@29
    87
						cell.setContent(Empty);
indvdum@29
    88
					}
indvdum@29
    89
				}
indvdum@29
    90
				mine.setRazorsCount(mine.getRazorsCount() - 1);
indvdum@29
    91
			}
indvdum@29
    92
			break;
indvdum@9
    93
		case LEFT:
indvdum@22
    94
			nextRobotCoord = robotCoord.left();
indvdum@9
    95
			break;
indvdum@9
    96
		case UP:
indvdum@22
    97
			nextRobotCoord = robotCoord.up();
indvdum@9
    98
			break;
indvdum@9
    99
		case RIGHT:
indvdum@22
   100
			nextRobotCoord = robotCoord.right();
indvdum@9
   101
			break;
indvdum@9
   102
		case DOWN:
indvdum@22
   103
			nextRobotCoord = robotCoord.down();
indvdum@9
   104
			break;
indvdum@9
   105
		}
indvdum@23
   106
		score--;
indvdum@9
   107
		Cell nextRobotCell = mine.getCell(nextRobotCoord);
indvdum@9
   108
		CellContent nextRobotCellContent = getContent(nextRobotCell);
indvdum@9
   109
		if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) {
indvdum@9
   110
			moveRobot(nextRobotCell);
indvdum@9
   111
		} else if (nextRobotCellContent == Lambda) {
indvdum@9
   112
			moveRobot(nextRobotCell);
indvdum@9
   113
			lambdaCollectedCount++;
indvdum@9
   114
			score += 25;
indvdum@9
   115
		} else if (nextRobotCellContent == OpenLambdaLift) {
indvdum@9
   116
			moveRobot(nextRobotCell);
indvdum@9
   117
			score += 50 * lambdaCollectedCount;
indvdum@9
   118
indvdum@9
   119
			lastScoreChange = score - prevScore;
indvdum@9
   120
			state = GameState.Win;
indvdum@9
   121
			return state;
indvdum@29
   122
		} else if (nextRobotCellContent == HuttonsRazor) {
indvdum@29
   123
			moveRobot(nextRobotCell);
indvdum@29
   124
			mine.setRazorsCount(mine.getRazorsCount() + 1);
indvdum@23
   125
		} else if (robotCoord.right().equals(nextRobotCoord) && nextRobotCellContent == Rock
indvdum@9
   126
				&& getContent(mine.getCell(nextRobotCoord.right())) == Empty) {
indvdum@9
   127
			moveRobot(nextRobotCell);
indvdum@9
   128
			mine.getCell(nextRobotCoord.right()).setContent(Rock);
indvdum@23
   129
		} else if (robotCoord.left().equals(nextRobotCoord) && nextRobotCellContent == Rock
indvdum@9
   130
				&& getContent(mine.getCell(nextRobotCoord.left())) == Empty) {
indvdum@9
   131
			moveRobot(nextRobotCell);
indvdum@9
   132
			mine.getCell(nextRobotCoord.left()).setContent(Rock);
indvdum@27
   133
		} else if (CellContent.getTrampolines().contains(nextRobotCellContent)
indvdum@27
   134
				&& CellContent.getTargets().contains(nextRobotCellContent.getTrampolineTarget())) {
indvdum@27
   135
			CellContent trampoline = nextRobotCellContent;
indvdum@27
   136
			CellContent target = trampoline.getTrampolineTarget();
indvdum@27
   137
			for (Cell targetCell : mine.findCells(target)) {
indvdum@27
   138
				moveRobot(targetCell);
indvdum@27
   139
			}
indvdum@27
   140
			for (CellContent tramp : CellContent.getTrampolines()) {
indvdum@27
   141
				if (tramp.getTrampolineTarget() == target) {
indvdum@27
   142
					for (Cell trampolineCell : mine.findCells(tramp)) {
indvdum@27
   143
						trampolineCell.setContent(Empty);
indvdum@27
   144
					}
indvdum@27
   145
				}
indvdum@27
   146
			}
indvdum@9
   147
		} else {
indvdum@9
   148
			mov = Movement.WAIT;
indvdum@9
   149
		}
indvdum@9
   150
indvdum@9
   151
		// World changes
indvdum@22
   152
		Cell[][] newCells = mine.cloneCells();
indvdum@9
   153
		for (int y = 1; y <= mine.getSizeY(); y++) {
indvdum@9
   154
			for (int x = 1; x <= mine.getSizeX(); x++) {
indvdum@9
   155
				Coordinate curCoord = new Coordinate(x, y);
indvdum@9
   156
				Coordinate leftCoord = curCoord.left();
indvdum@9
   157
				Coordinate upCoord = curCoord.up();
indvdum@9
   158
				Coordinate rightCoord = curCoord.right();
indvdum@9
   159
				Coordinate downCoord = curCoord.down();
indvdum@9
   160
				Coordinate leftAndUpCoord = curCoord.leftAndUp();
indvdum@9
   161
				Coordinate rightAndUpCoord = curCoord.rightAndUp();
indvdum@9
   162
				Coordinate rightAndDownCoord = curCoord.rightAndDown();
indvdum@9
   163
				Coordinate leftAndDownCoord = curCoord.leftAndDown();
indvdum@9
   164
				Cell curCell = mine.getCell(curCoord);
indvdum@9
   165
				Cell leftCell = mine.getCell(leftCoord);
indvdum@9
   166
				Cell upCell = mine.getCell(upCoord);
indvdum@9
   167
				Cell rightCell = mine.getCell(rightCoord);
indvdum@9
   168
				Cell downCell = mine.getCell(downCoord);
indvdum@9
   169
				Cell leftAndUpCell = mine.getCell(leftAndUpCoord);
indvdum@9
   170
				Cell rightAndUpCell = mine.getCell(rightAndUpCoord);
indvdum@9
   171
				Cell rightAndDownCell = mine.getCell(rightAndDownCoord);
indvdum@9
   172
				Cell leftAndDownCell = mine.getCell(leftAndDownCoord);
indvdum@9
   173
indvdum@9
   174
				if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
indvdum@9
   175
					getCell(curCoord, newCells).setContent(Empty);
indvdum@9
   176
					getCell(downCoord, newCells).setContent(Rock);
indvdum@28
   177
				} else if (getContent(curCell) == Rock && getContent(downCell) == Rock
indvdum@9
   178
						&& getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
indvdum@9
   179
					getCell(curCoord, newCells).setContent(Empty);
indvdum@9
   180
					getCell(rightAndDownCoord, newCells).setContent(Rock);
indvdum@9
   181
				} else if (getContent(curCell) == Rock && getContent(downCell) == Rock
indvdum@9
   182
						&& (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
indvdum@9
   183
						&& getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
indvdum@9
   184
					getCell(curCoord, newCells).setContent(Empty);
indvdum@9
   185
					getCell(leftAndDownCoord, newCells).setContent(Rock);
indvdum@9
   186
				} else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
indvdum@9
   187
						&& getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
indvdum@9
   188
					getCell(curCoord, newCells).setContent(Empty);
indvdum@9
   189
					getCell(rightAndDownCoord, newCells).setContent(Rock);
indvdum@9
   190
				} else if (getContent(curCell) == ClosedLambdaLift && mine.findCells(Lambda).size() == 0) {
indvdum@9
   191
					getCell(curCoord, newCells).setContent(OpenLambdaLift);
indvdum@29
   192
				} else if (getContent(curCell) == WadlersBeard && mine.getGrowth() > 0
indvdum@29
   193
						&& route.size() % mine.getGrowth() == 0) {
indvdum@29
   194
					if (getContent(leftCell) == Empty)
indvdum@29
   195
						getCell(leftCoord, newCells).setContent(WadlersBeard);
indvdum@29
   196
					if (getContent(upCell) == Empty)
indvdum@29
   197
						getCell(upCoord, newCells).setContent(WadlersBeard);
indvdum@29
   198
					if (getContent(rightCell) == Empty)
indvdum@29
   199
						getCell(rightCoord, newCells).setContent(WadlersBeard);
indvdum@29
   200
					if (getContent(downCell) == Empty)
indvdum@29
   201
						getCell(downCoord, newCells).setContent(WadlersBeard);
indvdum@29
   202
					if (getContent(leftAndUpCell) == Empty)
indvdum@29
   203
						getCell(leftAndUpCoord, newCells).setContent(WadlersBeard);
indvdum@29
   204
					if (getContent(rightAndUpCell) == Empty)
indvdum@29
   205
						getCell(rightAndUpCoord, newCells).setContent(WadlersBeard);
indvdum@29
   206
					if (getContent(leftAndDownCell) == Empty)
indvdum@29
   207
						getCell(leftAndDownCoord, newCells).setContent(WadlersBeard);
indvdum@29
   208
					if (getContent(rightAndDownCell) == Empty)
indvdum@29
   209
						getCell(rightAndDownCoord, newCells).setContent(WadlersBeard);
indvdum@9
   210
				}
indvdum@9
   211
			}
indvdum@9
   212
		}
indvdum@22
   213
indvdum@22
   214
		// Other ending conditions
indvdum@22
   215
		robotCoord = mine.getRobotCell().getCoordinate();
indvdum@22
   216
		if (getContent(mine.getCell(robotCoord.up())) != Rock && getContent(getCell(robotCoord.up(), newCells)) == Rock) {
indvdum@22
   217
			mine.setCells(newCells);
indvdum@25
   218
indvdum@22
   219
			lastScoreChange = score - prevScore;
indvdum@22
   220
			state = GameState.Lose;
indvdum@22
   221
			return state;
indvdum@22
   222
		}
indvdum@22
   223
indvdum@9
   224
		mine.setCells(newCells);
indvdum@9
   225
indvdum@25
   226
		// Water flooding
indvdum@25
   227
		robotCoord = mine.getRobotCell().getCoordinate();
indvdum@25
   228
		if (robotCoord.y <= mine.getWaterLevel()) {
indvdum@25
   229
			underwater++;
indvdum@25
   230
		} else {
indvdum@25
   231
			underwater = 0;
indvdum@25
   232
		}
indvdum@25
   233
		if (underwater > mine.getRobotWaterproof()) {
indvdum@25
   234
			lastScoreChange = score - prevScore;
indvdum@25
   235
			state = GameState.Lose;
indvdum@25
   236
			return state;
indvdum@25
   237
		}
indvdum@25
   238
		if (mine.getFlooding() > 0 && route.size() % mine.getFlooding() == 0) {
indvdum@25
   239
			mine.setWaterLevel(mine.getWaterLevel() + 1);
indvdum@25
   240
		}
indvdum@25
   241
indvdum@9
   242
		lastScoreChange = score - prevScore;
indvdum@9
   243
		state = GameState.Game;
indvdum@9
   244
		return state;
indvdum@9
   245
	}
indvdum@9
   246
indvdum@16
   247
	public GameState move(List<Movement> movs) {
indvdum@16
   248
		for (Movement mov : movs) {
indvdum@16
   249
			if (move(mov) != GameState.Game)
indvdum@16
   250
				return state;
indvdum@16
   251
		}
indvdum@16
   252
		return state;
indvdum@16
   253
	}
indvdum@16
   254
indvdum@19
   255
	public GameState move(String route) {
indvdum@16
   256
		List<Movement> movs = new ArrayList<Movement>();
indvdum@19
   257
		for (int i = 0; i < route.length(); i++) {
indvdum@19
   258
			String s = "" + route.charAt(i);
indvdum@16
   259
			for (Movement mov : Movement.values()) {
indvdum@16
   260
				if (mov.toText().equals(s)) {
indvdum@16
   261
					movs.add(mov);
indvdum@16
   262
					break;
indvdum@16
   263
				}
indvdum@16
   264
			}
indvdum@16
   265
		}
indvdum@16
   266
		return move(movs);
indvdum@16
   267
	}
indvdum@16
   268
indvdum@9
   269
	protected CellContent getContent(Cell cell) {
indvdum@9
   270
		if (cell == null)
indvdum@9
   271
			return null;
indvdum@9
   272
		return cell.getContent();
indvdum@9
   273
	}
indvdum@9
   274
indvdum@9
   275
	public int getLastScoreChange() {
indvdum@9
   276
		return lastScoreChange;
indvdum@9
   277
	}
indvdum@9
   278
indvdum@9
   279
	public int getScore() {
indvdum@9
   280
		return score;
indvdum@9
   281
	}
indvdum@9
   282
indvdum@9
   283
	public GameState getState() {
indvdum@9
   284
		return state;
indvdum@9
   285
	}
indvdum@9
   286
indvdum@9
   287
	public int getLambdaCollectedCount() {
indvdum@9
   288
		return lambdaCollectedCount;
indvdum@9
   289
	}
indvdum@20
   290
indvdum@20
   291
	public List<Movement> getRoute() {
indvdum@20
   292
		return route;
indvdum@20
   293
	}
indvdum@20
   294
indvdum@20
   295
	public String getStringRoute() {
indvdum@20
   296
		String movs = "";
indvdum@20
   297
		for (Movement mov : route) {
indvdum@20
   298
			movs += mov.toText();
indvdum@20
   299
		}
indvdum@20
   300
		return movs;
indvdum@20
   301
	}
indvdum@25
   302
indvdum@25
   303
	public int getUnderwater() {
indvdum@25
   304
		return underwater;
indvdum@25
   305
	}
indvdum@9
   306
}