Movements and Coordinates indvdum
authorindvdum (gotoindvdum[at]gmail[dot]com)
Sat, 14 Jul 2012 02:07:51 +0400
branchindvdum
changeset 7251961e44b5f
parent 6 0dbbc8983926
child 8 ad884dc6877c
Movements and Coordinates
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/Movement.java
icfp-model/src/test/java/ru/bosony/tests/TestMineParsing.java
     1.1 --- a/icfp-model/src/main/java/ru/bosony/model/mine/Cell.java	Sat Jul 14 00:36:43 2012 +0400
     1.2 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Cell.java	Sat Jul 14 02:07:51 2012 +0400
     1.3 @@ -9,13 +9,11 @@
     1.4   */
     1.5  public class Cell {
     1.6  
     1.7 -	protected int			x;
     1.8 -	protected int			y;
     1.9 +	protected Coordinate	coordinate;
    1.10  	protected CellContent	content;
    1.11  
    1.12 -	public Cell(int x, int y, CellContent content) {
    1.13 -		this.x = x;
    1.14 -		this.y = y;
    1.15 +	public Cell(Coordinate coordinate, CellContent content) {
    1.16 +		this.coordinate = coordinate;
    1.17  		this.content = content;
    1.18  	}
    1.19  
    1.20 @@ -27,12 +25,8 @@
    1.21  		this.content = content;
    1.22  	}
    1.23  
    1.24 -	public int getX() {
    1.25 -		return x;
    1.26 -	}
    1.27 -
    1.28 -	public int getY() {
    1.29 -		return y;
    1.30 +	public Coordinate getCoordinate() {
    1.31 +		return coordinate;
    1.32  	}
    1.33  
    1.34  }
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Coordinate.java	Sat Jul 14 02:07:51 2012 +0400
     2.3 @@ -0,0 +1,92 @@
     2.4 +package ru.bosony.model.mine;
     2.5 +
     2.6 +import java.util.HashSet;
     2.7 +import java.util.Set;
     2.8 +
     2.9 +import ru.bosony.model.moving.Movement;
    2.10 +
    2.11 +/**
    2.12 + * @author 	indvdum (gotoindvdum[at]gmail[dot]com) <br>
    2.13 + * 			14.07.2012 1:07:51
    2.14 + *
    2.15 + */
    2.16 +public class Coordinate {
    2.17 +	
    2.18 +	public Coordinate(int x, int y) {
    2.19 +		this.x = x;
    2.20 +		this.y = y;
    2.21 +	}
    2.22 +	
    2.23 +	public int x;
    2.24 +	public int y;
    2.25 +	
    2.26 +	public Coordinate left() {
    2.27 +		return new Coordinate(x - 1, y);
    2.28 +	}
    2.29 +	
    2.30 +	public Coordinate top() {
    2.31 +		return new Coordinate(x, y + 1);
    2.32 +	}
    2.33 +	
    2.34 +	public Coordinate right() {
    2.35 +		return new Coordinate(x + 1, y);
    2.36 +	}
    2.37 +	
    2.38 +	public Coordinate bottom() {
    2.39 +		return new Coordinate(x, y - 1);
    2.40 +	}
    2.41 +	
    2.42 +	public Coordinate leftAndTop() {
    2.43 +		return new Coordinate(x - 1, y + 1);
    2.44 +	}
    2.45 +	
    2.46 +	public Coordinate rightAndTop() {
    2.47 +		return new Coordinate(x + 1, y + 1);
    2.48 +	}
    2.49 +	
    2.50 +	public Coordinate rightAndBottom() {
    2.51 +		return new Coordinate(x + 1, y - 1);
    2.52 +	}
    2.53 +	
    2.54 +	public Coordinate leftAndBottom() {
    2.55 +		return new Coordinate(x - 1, y - 1);
    2.56 +	}
    2.57 +	
    2.58 +	public Set<Coordinate> getNeighboringCoordinates() {
    2.59 +		Set<Coordinate> coords = new HashSet<Coordinate>();
    2.60 +		coords.add(left());
    2.61 +		coords.add(top());
    2.62 +		coords.add(right());
    2.63 +		coords.add(bottom());
    2.64 +		coords.add(leftAndTop());
    2.65 +		coords.add(rightAndTop());
    2.66 +		coords.add(rightAndBottom());
    2.67 +		coords.add(leftAndBottom());
    2.68 +		return coords;
    2.69 +	}
    2.70 +	
    2.71 +	public Movement getNecessaryMovement(Coordinate nCoordinate) {
    2.72 +		if (left().equals(nCoordinate))
    2.73 +			return Movement.LEFT;
    2.74 +		if (top().equals(nCoordinate))
    2.75 +			return Movement.UP;
    2.76 +		if (right().equals(nCoordinate))
    2.77 +			return Movement.RIGHT;
    2.78 +		if (bottom().equals(nCoordinate))
    2.79 +			return Movement.DOWN;
    2.80 +		return null;
    2.81 +	}
    2.82 +
    2.83 +	@Override
    2.84 +	public int hashCode() {
    2.85 +		return x ^ y;
    2.86 +	}
    2.87 +
    2.88 +	@Override
    2.89 +	public boolean equals(Object obj) {
    2.90 +		if (obj instanceof Coordinate)
    2.91 +			return x == ((Coordinate)obj).x && y == ((Coordinate)obj).y;
    2.92 +		return super.equals(obj);
    2.93 +	}
    2.94 +	
    2.95 +}
     3.1 --- a/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java	Sat Jul 14 00:36:43 2012 +0400
     3.2 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java	Sat Jul 14 02:07:51 2012 +0400
     3.3 @@ -1,13 +1,23 @@
     3.4  package ru.bosony.model.mine;
     3.5  
     3.6 +import java.util.HashSet;
     3.7 +import java.util.Set;
     3.8 +
     3.9  import ru.bosony.model.cellscontents.CellContent;
    3.10  import ru.bosony.model.io.TextRepresentable;
    3.11 +import ru.bosony.model.moving.Movement;
    3.12  
    3.13 +/**
    3.14 + * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
    3.15 + *         14.07.2012 2:02:18
    3.16 + * 
    3.17 + */
    3.18  public class Mine implements TextRepresentable {
    3.19  
    3.20 -	public Cell[][]	cells;
    3.21 -	protected int	sizeX;
    3.22 -	protected int	sizeY;
    3.23 +	private Cell[][]	cells;
    3.24 +	protected int		sizeX;
    3.25 +	protected int		sizeY;
    3.26 +	protected Cell		robotCell;
    3.27  
    3.28  	public Mine(String text) {
    3.29  		fromText(text);
    3.30 @@ -44,15 +54,65 @@
    3.31  					content = CellContent.fromText("" + row.charAt(curX));
    3.32  				else
    3.33  					content = CellContent.Empty;
    3.34 -				Cell cell = new Cell(curX + 1, rows.length - curY, content);
    3.35 +				Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
    3.36  				cells[curX][curY] = cell;
    3.37 +				if (content == CellContent.MiningRobot)
    3.38 +					robotCell = cell;
    3.39  			}
    3.40  		}
    3.41  	}
    3.42  
    3.43 +	public Cell getCell(Coordinate coordinate) {
    3.44 +		int x = coordinate.x;
    3.45 +		int y = coordinate.y;
    3.46 +		if (x < 1 || x > sizeX || y < 1 || y > sizeY)
    3.47 +			return null;
    3.48 +		return cells[x - 1][sizeY - y];
    3.49 +	}
    3.50 +
    3.51  	@Override
    3.52  	public String toString() {
    3.53  		return toText();
    3.54  	}
    3.55  
    3.56 +	public int getSizeX() {
    3.57 +		return sizeX;
    3.58 +	}
    3.59 +
    3.60 +	public int getSizeY() {
    3.61 +		return sizeY;
    3.62 +	}
    3.63 +
    3.64 +	public Set<Movement> getAvailableRobotMovements() {
    3.65 +		Set<Movement> movs = new HashSet<Movement>();
    3.66 +		Set<Cell> nCells = getNeighboringCells(robotCell);
    3.67 +		for (Cell nCell : nCells) {
    3.68 +			CellContent nContent = nCell.getContent();
    3.69 +			Coordinate nCoordinate = nCell.getCoordinate();
    3.70 +			Cell rightNCell = getCell(nCoordinate.right());
    3.71 +			Cell leftNCell = getCell(nCoordinate.left());
    3.72 +			if (nContent == CellContent.Empty
    3.73 +					|| nContent == CellContent.Earth
    3.74 +					|| nContent == CellContent.Lambda
    3.75 +					|| nContent == CellContent.OpenLambdaLift
    3.76 +					|| (nContent == CellContent.Rock && robotCell.getCoordinate().right().equals(nCoordinate)
    3.77 +							&& rightNCell != null && rightNCell.getContent() == CellContent.Empty)
    3.78 +					|| (nContent == CellContent.Rock && robotCell.getCoordinate().left().equals(nCoordinate)
    3.79 +							&& leftNCell != null && leftNCell.getContent() == CellContent.Empty)) {
    3.80 +				Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate);
    3.81 +				movs.add(mov);
    3.82 +			}
    3.83 +		}
    3.84 +		movs.add(Movement.ABORT);
    3.85 +		movs.add(Movement.WAIT);
    3.86 +		return movs;
    3.87 +	}
    3.88 +
    3.89 +	public Set<Cell> getNeighboringCells(Cell cell) {
    3.90 +		Set<Cell> cells = new HashSet<Cell>();
    3.91 +		for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates())
    3.92 +			cells.add(getCell(coordinate));
    3.93 +		return cells;
    3.94 +	}
    3.95 +
    3.96  }
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/icfp-model/src/main/java/ru/bosony/model/moving/Movement.java	Sat Jul 14 02:07:51 2012 +0400
     4.3 @@ -0,0 +1,38 @@
     4.4 +package ru.bosony.model.moving;
     4.5 +
     4.6 +import ru.bosony.model.io.TextRepresentable;
     4.7 +
     4.8 +/**
     4.9 + * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
    4.10 + *         14.07.2012 0:40:35
    4.11 + * 
    4.12 + */
    4.13 +public enum Movement implements TextRepresentable {
    4.14 +	
    4.15 +	LEFT("L"),
    4.16 +	RIGHT("R"),
    4.17 +	UP("U"),
    4.18 +	DOWN("D"),
    4.19 +	WAIT("W"),
    4.20 +	ABORT("A");
    4.21 +
    4.22 +	private String	text;
    4.23 +
    4.24 +	private Movement(String text) {
    4.25 +		this.text = text;
    4.26 +	}
    4.27 +
    4.28 +	@Override
    4.29 +	public String toText() {
    4.30 +		return text;
    4.31 +	}
    4.32 +
    4.33 +	public static Movement fromText(String text) {
    4.34 +		for (Movement content : values()) {
    4.35 +			if (text != null && text.equals(content.toText()))
    4.36 +				return content;
    4.37 +		}
    4.38 +		return null;
    4.39 +	}
    4.40 +
    4.41 +}
     5.1 --- a/icfp-model/src/test/java/ru/bosony/tests/TestMineParsing.java	Sat Jul 14 00:36:43 2012 +0400
     5.2 +++ b/icfp-model/src/test/java/ru/bosony/tests/TestMineParsing.java	Sat Jul 14 02:07:51 2012 +0400
     5.3 @@ -8,6 +8,11 @@
     5.4  import ru.bosony.model.mine.Cell;
     5.5  import ru.bosony.model.mine.Mine;
     5.6  
     5.7 +/**
     5.8 + * @author 	indvdum (gotoindvdum[at]gmail[dot]com) <br>
     5.9 + * 			14.07.2012 0:37:14
    5.10 + *
    5.11 + */
    5.12  public class TestMineParsing {
    5.13  	
    5.14  	protected static String alignMap(String map) {