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