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