icfp-model/src/main/java/ru/bosony/model/game/Game.java
branchCleverSearch
changeset 47 b3d48e364f9e
parent 30 666821484adc
child 50 9530f55e9639
equal deleted inserted replaced
46:4197f736166a 47:b3d48e364f9e
    10 import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift;
    10 import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift;
    11 import static ru.bosony.model.cellscontents.CellContent.Rock;
    11 import static ru.bosony.model.cellscontents.CellContent.Rock;
    12 import static ru.bosony.model.cellscontents.CellContent.WadlersBeard;
    12 import static ru.bosony.model.cellscontents.CellContent.WadlersBeard;
    13 
    13 
    14 import java.util.ArrayList;
    14 import java.util.ArrayList;
       
    15 import java.util.HashMap;
    15 import java.util.List;
    16 import java.util.List;
       
    17 import java.util.Map;
    16 
    18 
    17 import ru.bosony.model.cellscontents.CellContent;
    19 import ru.bosony.model.cellscontents.CellContent;
    18 import ru.bosony.model.mine.Cell;
    20 import ru.bosony.model.mine.Cell;
    19 import ru.bosony.model.mine.Mine;
    21 import ru.bosony.model.mine.Mine;
    20 import ru.bosony.model.moving.Coordinate;
    22 import ru.bosony.model.moving.Coordinate;
    30 	protected Mine				mine					= null;
    32 	protected Mine				mine					= null;
    31 	protected int				score					= 0;
    33 	protected int				score					= 0;
    32 	protected int				lastScoreChange			= 0;
    34 	protected int				lastScoreChange			= 0;
    33 	protected GameState			state					= GameState.Game;
    35 	protected GameState			state					= GameState.Game;
    34 	protected int				lambdaCollectedCount	= 0;
    36 	protected int				lambdaCollectedCount	= 0;
       
    37 	protected List<Mine>		history					= new ArrayList<Mine>();
    35 	protected List<Movement>	route					= new ArrayList<Movement>();
    38 	protected List<Movement>	route					= new ArrayList<Movement>();
    36 	protected int				underwater				= 0;
    39 	protected int				underwater				= 0;
    37 
    40 
    38 	public Game(Mine mine) {
    41 	public Game(Mine mine) {
    39 		this.mine = mine;
    42 		this.mine = mine;
    67 		// Score
    70 		// Score
    68 		int prevScore = score;
    71 		int prevScore = score;
    69 
    72 
    70 		// Moving and scoring
    73 		// Moving and scoring
    71 		route.add(mov);
    74 		route.add(mov);
       
    75 		try {
       
    76 			history.add(mine.clone());
       
    77 		} catch (CloneNotSupportedException e) {
       
    78 			throw new RuntimeException(e);
       
    79 		}
    72 		Cell robotCell = mine.getRobotCell();
    80 		Cell robotCell = mine.getRobotCell();
    73 		Coordinate robotCoord = robotCell.getCoordinate();
    81 		Coordinate robotCoord = robotCell.getCoordinate();
    74 		Coordinate nextRobotCoord = null;
    82 		Coordinate nextRobotCoord = null;
    75 		switch (mov) {
    83 		switch (mov) {
    76 		case ABORT:
    84 		case ABORT:
   347 	}
   355 	}
   348 
   356 
   349 	public int getUnderwater() {
   357 	public int getUnderwater() {
   350 		return underwater;
   358 		return underwater;
   351 	}
   359 	}
       
   360 
       
   361 	public boolean hasViciousCircle() {
       
   362 		if (route.size() != history.size())
       
   363 			throw new RuntimeException("Desynchronized history");
       
   364 		Map<Mine, Movement> state = new HashMap<Mine, Movement>();
       
   365 		for (int i = route.size() - 1; i >= 0; i--) {
       
   366 			Mine mine = history.get(i);
       
   367 			Movement mov = route.get(i);
       
   368 			if (state.get(mine) == mov)
       
   369 				return true;
       
   370 			state.put(mine, mov);
       
   371 		}
       
   372 		return false;
       
   373 	}
   352 }
   374 }