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