Fixes according to FAQ
authorindvdum (gotoindvdum[at]gmail[dot]com)
Sun, 15 Jul 2012 04:09:14 +0400
changeset 22a46d0179df7a
parent 21 fade486c8eda
child 23 606bba9825c8
Fixes according to FAQ
icfp-interpreter/src/main/java/ru/bosony/interpreters/TextInterpreter.java
icfp-model/src/main/java/ru/bosony/model/game/Game.java
icfp-model/src/main/java/ru/bosony/model/mine/Cell.java
icfp-model/src/main/java/ru/bosony/model/mine/Coordinate.java
icfp-model/src/main/java/ru/bosony/model/mine/Mine.java
icfp-model/src/main/java/ru/bosony/model/moving/Coordinate.java
     1.1 --- a/icfp-interpreter/src/main/java/ru/bosony/interpreters/TextInterpreter.java	Sun Jul 15 02:08:45 2012 +0400
     1.2 +++ b/icfp-interpreter/src/main/java/ru/bosony/interpreters/TextInterpreter.java	Sun Jul 15 04:09:14 2012 +0400
     1.3 @@ -51,6 +51,8 @@
     1.4  		writer.write("Score: " + game.getScore() + " ("
     1.5  				+ (game.getLastScoreChange() >= 0 ? "+" + game.getLastScoreChange() : game.getLastScoreChange()) + ")");
     1.6  		writer.newLine();
     1.7 +		writer.write("Steps: " + game.getRoute().size());
     1.8 +		writer.newLine();
     1.9  		writer.write("Route: " + game.getStringRoute());
    1.10  		writer.newLine();
    1.11  		writer.newLine();
    1.12 @@ -60,9 +62,9 @@
    1.13  		writer.write("Available actions: ");
    1.14  		String movs = "";
    1.15  		for (Movement mov : Movement.values()) {
    1.16 -			movs += mov.toText() + ", ";
    1.17 +			movs += mov.toText() + " (" + mov.toString().toLowerCase() + "), ";
    1.18  		}
    1.19 -		movs = movs.replaceAll(",\\\\s*$", "");
    1.20 +		movs = movs.replaceAll(",\\s*$", "");
    1.21  		writer.write(movs);
    1.22  		writer.newLine();
    1.23  		writer.write("Print action(s): ");
     2.1 --- a/icfp-model/src/main/java/ru/bosony/model/game/Game.java	Sun Jul 15 02:08:45 2012 +0400
     2.2 +++ b/icfp-model/src/main/java/ru/bosony/model/game/Game.java	Sun Jul 15 04:09:14 2012 +0400
     2.3 @@ -9,13 +9,12 @@
     2.4  import static ru.bosony.model.cellscontents.CellContent.Rock;
     2.5  
     2.6  import java.util.ArrayList;
     2.7 -import java.util.Arrays;
     2.8  import java.util.List;
     2.9  
    2.10  import ru.bosony.model.cellscontents.CellContent;
    2.11  import ru.bosony.model.mine.Cell;
    2.12 -import ru.bosony.model.mine.Coordinate;
    2.13  import ru.bosony.model.mine.Mine;
    2.14 +import ru.bosony.model.moving.Coordinate;
    2.15  import ru.bosony.model.moving.Movement;
    2.16  
    2.17  /**
    2.18 @@ -25,8 +24,7 @@
    2.19   */
    2.20  public class Game {
    2.21  
    2.22 -	Mine						mine					= null;
    2.23 -	private Cell[][]			prevStepCells;
    2.24 +	protected Mine				mine					= null;
    2.25  	protected int				score					= 0;
    2.26  	protected int				lastScoreChange			= 0;
    2.27  	protected GameState			state					= GameState.Game;
    2.28 @@ -56,7 +54,6 @@
    2.29  	protected void moveRobot(Cell nextRobotCell) {
    2.30  		mine.getRobotCell().setContent(Empty);
    2.31  		nextRobotCell.setContent(MiningRobot);
    2.32 -		mine.setRobotCell(nextRobotCell);
    2.33  	}
    2.34  
    2.35  	public GameState move(Movement mov) {
    2.36 @@ -69,6 +66,7 @@
    2.37  
    2.38  		// Moving and scoring
    2.39  		route.add(mov);
    2.40 +		Coordinate robotCoord = mine.getRobotCell().getCoordinate();
    2.41  		Coordinate nextRobotCoord = null;
    2.42  		switch (mov) {
    2.43  		case ABORT:
    2.44 @@ -80,16 +78,16 @@
    2.45  		case WAIT:
    2.46  			break;
    2.47  		case LEFT:
    2.48 -			nextRobotCoord = mine.getRobotCell().getCoordinate().left();
    2.49 +			nextRobotCoord = robotCoord.left();
    2.50  			break;
    2.51  		case UP:
    2.52 -			nextRobotCoord = mine.getRobotCell().getCoordinate().up();
    2.53 +			nextRobotCoord = robotCoord.up();
    2.54  			break;
    2.55  		case RIGHT:
    2.56 -			nextRobotCoord = mine.getRobotCell().getCoordinate().right();
    2.57 +			nextRobotCoord = robotCoord.right();
    2.58  			break;
    2.59  		case DOWN:
    2.60 -			nextRobotCoord = mine.getRobotCell().getCoordinate().down();
    2.61 +			nextRobotCoord = robotCoord.down();
    2.62  			break;
    2.63  		}
    2.64  		Cell nextRobotCell = mine.getCell(nextRobotCoord);
    2.65 @@ -107,11 +105,11 @@
    2.66  			lastScoreChange = score - prevScore;
    2.67  			state = GameState.Win;
    2.68  			return state;
    2.69 -		} else if (mine.getRobotCell().getCoordinate().right() == nextRobotCoord && nextRobotCellContent == Rock
    2.70 +		} else if (robotCoord.right() == nextRobotCoord && nextRobotCellContent == Rock
    2.71  				&& getContent(mine.getCell(nextRobotCoord.right())) == Empty) {
    2.72  			moveRobot(nextRobotCell);
    2.73  			mine.getCell(nextRobotCoord.right()).setContent(Rock);
    2.74 -		} else if (mine.getRobotCell().getCoordinate().left() == nextRobotCoord && nextRobotCellContent == Rock
    2.75 +		} else if (robotCoord.left() == nextRobotCoord && nextRobotCellContent == Rock
    2.76  				&& getContent(mine.getCell(nextRobotCoord.left())) == Empty) {
    2.77  			moveRobot(nextRobotCell);
    2.78  			mine.getCell(nextRobotCoord.left()).setContent(Rock);
    2.79 @@ -119,16 +117,8 @@
    2.80  			mov = Movement.WAIT;
    2.81  		}
    2.82  
    2.83 -		// Other ending conditions
    2.84 -		if (getContent(getCell(mine.getRobotCell().getCoordinate().up(), prevStepCells)) != Rock
    2.85 -				&& getContent(mine.getCell(mine.getRobotCell().getCoordinate().up())) == Rock) {
    2.86 -			lastScoreChange = score - prevScore;
    2.87 -			state = GameState.Lose;
    2.88 -			return state;
    2.89 -		}
    2.90 -
    2.91  		// World changes
    2.92 -		Cell[][] newCells = Arrays.copyOf(mine.getCells(), mine.getCells().length);
    2.93 +		Cell[][] newCells = mine.cloneCells();
    2.94  		for (int y = 1; y <= mine.getSizeY(); y++) {
    2.95  			for (int x = 1; x <= mine.getSizeX(); x++) {
    2.96  				Coordinate curCoord = new Coordinate(x, y);
    2.97 @@ -171,7 +161,16 @@
    2.98  				}
    2.99  			}
   2.100  		}
   2.101 -		prevStepCells = mine.getCells();
   2.102 +
   2.103 +		// Other ending conditions
   2.104 +		robotCoord = mine.getRobotCell().getCoordinate();
   2.105 +		if (getContent(mine.getCell(robotCoord.up())) != Rock && getContent(getCell(robotCoord.up(), newCells)) == Rock) {
   2.106 +			mine.setCells(newCells);
   2.107 +			lastScoreChange = score - prevScore;
   2.108 +			state = GameState.Lose;
   2.109 +			return state;
   2.110 +		}
   2.111 +
   2.112  		mine.setCells(newCells);
   2.113  
   2.114  		lastScoreChange = score - prevScore;
     3.1 --- a/icfp-model/src/main/java/ru/bosony/model/mine/Cell.java	Sun Jul 15 02:08:45 2012 +0400
     3.2 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Cell.java	Sun Jul 15 04:09:14 2012 +0400
     3.3 @@ -1,13 +1,14 @@
     3.4  package ru.bosony.model.mine;
     3.5  
     3.6  import ru.bosony.model.cellscontents.CellContent;
     3.7 +import ru.bosony.model.moving.Coordinate;
     3.8  
     3.9  /**
    3.10   * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
    3.11   *         13.07.2012 22:10:18
    3.12   * 
    3.13   */
    3.14 -public class Cell {
    3.15 +public class Cell implements Cloneable {
    3.16  
    3.17  	protected Coordinate	coordinate;
    3.18  	protected CellContent	content;
    3.19 @@ -29,4 +30,10 @@
    3.20  		return coordinate;
    3.21  	}
    3.22  
    3.23 +	@Override
    3.24 +	protected Cell clone() throws CloneNotSupportedException {
    3.25 +		Cell clone = new Cell(coordinate, content);
    3.26 +		return clone;
    3.27 +	}
    3.28 +
    3.29  }
     4.1 --- a/icfp-model/src/main/java/ru/bosony/model/mine/Coordinate.java	Sun Jul 15 02:08:45 2012 +0400
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,92 +0,0 @@
     4.4 -package ru.bosony.model.mine;
     4.5 -
     4.6 -import java.util.HashSet;
     4.7 -import java.util.Set;
     4.8 -
     4.9 -import ru.bosony.model.moving.Movement;
    4.10 -
    4.11 -/**
    4.12 - * @author 	indvdum (gotoindvdum[at]gmail[dot]com) <br>
    4.13 - * 			14.07.2012 1:07:51
    4.14 - *
    4.15 - */
    4.16 -public class Coordinate {
    4.17 -	
    4.18 -	public Coordinate(int x, int y) {
    4.19 -		this.x = x;
    4.20 -		this.y = y;
    4.21 -	}
    4.22 -	
    4.23 -	public int x;
    4.24 -	public int y;
    4.25 -	
    4.26 -	public Coordinate left() {
    4.27 -		return new Coordinate(x - 1, y);
    4.28 -	}
    4.29 -	
    4.30 -	public Coordinate up() {
    4.31 -		return new Coordinate(x, y + 1);
    4.32 -	}
    4.33 -	
    4.34 -	public Coordinate right() {
    4.35 -		return new Coordinate(x + 1, y);
    4.36 -	}
    4.37 -	
    4.38 -	public Coordinate down() {
    4.39 -		return new Coordinate(x, y - 1);
    4.40 -	}
    4.41 -	
    4.42 -	public Coordinate leftAndUp() {
    4.43 -		return new Coordinate(x - 1, y + 1);
    4.44 -	}
    4.45 -	
    4.46 -	public Coordinate rightAndUp() {
    4.47 -		return new Coordinate(x + 1, y + 1);
    4.48 -	}
    4.49 -	
    4.50 -	public Coordinate rightAndDown() {
    4.51 -		return new Coordinate(x + 1, y - 1);
    4.52 -	}
    4.53 -	
    4.54 -	public Coordinate leftAndDown() {
    4.55 -		return new Coordinate(x - 1, y - 1);
    4.56 -	}
    4.57 -	
    4.58 -	public Set<Coordinate> getNeighboringCoordinates() {
    4.59 -		Set<Coordinate> coords = new HashSet<Coordinate>();
    4.60 -		coords.add(left());
    4.61 -		coords.add(up());
    4.62 -		coords.add(right());
    4.63 -		coords.add(down());
    4.64 -		coords.add(leftAndUp());
    4.65 -		coords.add(rightAndUp());
    4.66 -		coords.add(rightAndDown());
    4.67 -		coords.add(leftAndDown());
    4.68 -		return coords;
    4.69 -	}
    4.70 -	
    4.71 -	public Movement getNecessaryMovement(Coordinate nCoordinate) {
    4.72 -		if (left().equals(nCoordinate))
    4.73 -			return Movement.LEFT;
    4.74 -		if (up().equals(nCoordinate))
    4.75 -			return Movement.UP;
    4.76 -		if (right().equals(nCoordinate))
    4.77 -			return Movement.RIGHT;
    4.78 -		if (down().equals(nCoordinate))
    4.79 -			return Movement.DOWN;
    4.80 -		return null;
    4.81 -	}
    4.82 -
    4.83 -	@Override
    4.84 -	public int hashCode() {
    4.85 -		return x ^ y;
    4.86 -	}
    4.87 -
    4.88 -	@Override
    4.89 -	public boolean equals(Object obj) {
    4.90 -		if (obj instanceof Coordinate)
    4.91 -			return x == ((Coordinate)obj).x && y == ((Coordinate)obj).y;
    4.92 -		return super.equals(obj);
    4.93 -	}
    4.94 -	
    4.95 -}
     5.1 --- a/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java	Sun Jul 15 02:08:45 2012 +0400
     5.2 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java	Sun Jul 15 04:09:14 2012 +0400
     5.3 @@ -13,6 +13,7 @@
     5.4  
     5.5  import ru.bosony.model.cellscontents.CellContent;
     5.6  import ru.bosony.model.io.TextRepresentable;
     5.7 +import ru.bosony.model.moving.Coordinate;
     5.8  import ru.bosony.model.moving.Movement;
     5.9  
    5.10  /**
    5.11 @@ -25,7 +26,6 @@
    5.12  	protected Cell[][]	cells;
    5.13  	protected int		sizeX;
    5.14  	protected int		sizeY;
    5.15 -	protected Cell		robotCell;
    5.16  
    5.17  	public Mine(String text) {
    5.18  		fromText(text);
    5.19 @@ -35,8 +35,6 @@
    5.20  			throw new RuntimeException("New map does not contain a robot!");
    5.21  		if (findCells(ClosedLambdaLift).size() != 1)
    5.22  			throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
    5.23 -		for (Cell cell : findCells(MiningRobot))
    5.24 -			robotCell = cell;
    5.25  	}
    5.26  
    5.27  	@Override
    5.28 @@ -70,6 +68,8 @@
    5.29  					content = CellContent.fromText("" + row.charAt(curX));
    5.30  				else
    5.31  					content = Empty;
    5.32 +				if (content == null)
    5.33 +					content = Empty;
    5.34  				Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
    5.35  				cells[curX][curY] = cell;
    5.36  			}
    5.37 @@ -101,6 +101,7 @@
    5.38  
    5.39  	public Set<Movement> getAvailableRobotMovements() {
    5.40  		Set<Movement> movs = new HashSet<Movement>();
    5.41 +		Cell robotCell = getRobotCell();
    5.42  		Set<Cell> nCells = getNeighboringCells(robotCell);
    5.43  		for (Cell nCell : nCells) {
    5.44  			CellContent nContent = nCell.getContent();
    5.45 @@ -142,20 +143,29 @@
    5.46  		return finded;
    5.47  	}
    5.48  
    5.49 -	public Cell[][] getCells() {
    5.50 -		return cells;
    5.51 -	}
    5.52 -
    5.53  	public void setCells(Cell[][] cells) {
    5.54  		this.cells = cells;
    5.55  	}
    5.56  
    5.57  	public Cell getRobotCell() {
    5.58 -		return robotCell;
    5.59 +		for (Cell cell : findCells(MiningRobot)) {
    5.60 +			return cell;
    5.61 +		}
    5.62 +		throw new RuntimeException("Where is robot!?");
    5.63  	}
    5.64  
    5.65 -	public void setRobotCell(Cell robotCell) {
    5.66 -		this.robotCell = robotCell;
    5.67 +	public Cell[][] cloneCells() {
    5.68 +		Cell[][] clone = new Cell[sizeX][sizeY];
    5.69 +		for (int y = sizeY - 1; y >= 0; y--) {
    5.70 +			for (int x = 0; x < sizeX; x++) {
    5.71 +				try {
    5.72 +					clone[x][y] = cells[x][y].clone();
    5.73 +				} catch (CloneNotSupportedException e) {
    5.74 +					throw new RuntimeException(e);
    5.75 +				}
    5.76 +			}
    5.77 +		}
    5.78 +		return clone;
    5.79  	}
    5.80  
    5.81  }
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/icfp-model/src/main/java/ru/bosony/model/moving/Coordinate.java	Sun Jul 15 04:09:14 2012 +0400
     6.3 @@ -0,0 +1,91 @@
     6.4 +package ru.bosony.model.moving;
     6.5 +
     6.6 +import java.util.HashSet;
     6.7 +import java.util.Set;
     6.8 +
     6.9 +
    6.10 +/**
    6.11 + * @author 	indvdum (gotoindvdum[at]gmail[dot]com) <br>
    6.12 + * 			14.07.2012 1:07:51
    6.13 + *
    6.14 + */
    6.15 +public class Coordinate {
    6.16 +	
    6.17 +	public Coordinate(int x, int y) {
    6.18 +		this.x = x;
    6.19 +		this.y = y;
    6.20 +	}
    6.21 +	
    6.22 +	public int x;
    6.23 +	public int y;
    6.24 +	
    6.25 +	public Coordinate left() {
    6.26 +		return new Coordinate(x - 1, y);
    6.27 +	}
    6.28 +	
    6.29 +	public Coordinate up() {
    6.30 +		return new Coordinate(x, y + 1);
    6.31 +	}
    6.32 +	
    6.33 +	public Coordinate right() {
    6.34 +		return new Coordinate(x + 1, y);
    6.35 +	}
    6.36 +	
    6.37 +	public Coordinate down() {
    6.38 +		return new Coordinate(x, y - 1);
    6.39 +	}
    6.40 +	
    6.41 +	public Coordinate leftAndUp() {
    6.42 +		return new Coordinate(x - 1, y + 1);
    6.43 +	}
    6.44 +	
    6.45 +	public Coordinate rightAndUp() {
    6.46 +		return new Coordinate(x + 1, y + 1);
    6.47 +	}
    6.48 +	
    6.49 +	public Coordinate rightAndDown() {
    6.50 +		return new Coordinate(x + 1, y - 1);
    6.51 +	}
    6.52 +	
    6.53 +	public Coordinate leftAndDown() {
    6.54 +		return new Coordinate(x - 1, y - 1);
    6.55 +	}
    6.56 +	
    6.57 +	public Set<Coordinate> getNeighboringCoordinates() {
    6.58 +		Set<Coordinate> coords = new HashSet<Coordinate>();
    6.59 +		coords.add(left());
    6.60 +		coords.add(up());
    6.61 +		coords.add(right());
    6.62 +		coords.add(down());
    6.63 +		coords.add(leftAndUp());
    6.64 +		coords.add(rightAndUp());
    6.65 +		coords.add(rightAndDown());
    6.66 +		coords.add(leftAndDown());
    6.67 +		return coords;
    6.68 +	}
    6.69 +	
    6.70 +	public Movement getNecessaryMovement(Coordinate nCoordinate) {
    6.71 +		if (left().equals(nCoordinate))
    6.72 +			return Movement.LEFT;
    6.73 +		if (up().equals(nCoordinate))
    6.74 +			return Movement.UP;
    6.75 +		if (right().equals(nCoordinate))
    6.76 +			return Movement.RIGHT;
    6.77 +		if (down().equals(nCoordinate))
    6.78 +			return Movement.DOWN;
    6.79 +		return null;
    6.80 +	}
    6.81 +
    6.82 +	@Override
    6.83 +	public int hashCode() {
    6.84 +		return x ^ y;
    6.85 +	}
    6.86 +
    6.87 +	@Override
    6.88 +	public boolean equals(Object obj) {
    6.89 +		if (obj instanceof Coordinate)
    6.90 +			return x == ((Coordinate)obj).x && y == ((Coordinate)obj).y;
    6.91 +		return super.equals(obj);
    6.92 +	}
    6.93 +	
    6.94 +}