World processor indvdum
authorindvdum (gotoindvdum[at]gmail[dot]com)
Sat, 14 Jul 2012 22:49:59 +0400
branchindvdum
changeset 8ad884dc6877c
parent 7 251961e44b5f
child 9 336dcbecb53c
World processor
icfp-model/src/main/java/ru/bosony/model/mine/Coordinate.java
icfp-model/src/main/java/ru/bosony/model/mine/GameState.java
icfp-model/src/main/java/ru/bosony/model/mine/Mine.java
icfp-model/src/test/java/ru/bosony/tests/TestMovements.java
icfp-model/src/test/java/ru/bosony/tests/TestScore.java
     1.1 --- a/icfp-model/src/main/java/ru/bosony/model/mine/Coordinate.java	Sat Jul 14 02:07:51 2012 +0400
     1.2 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Coordinate.java	Sat Jul 14 22:49:59 2012 +0400
     1.3 @@ -24,7 +24,7 @@
     1.4  		return new Coordinate(x - 1, y);
     1.5  	}
     1.6  	
     1.7 -	public Coordinate top() {
     1.8 +	public Coordinate up() {
     1.9  		return new Coordinate(x, y + 1);
    1.10  	}
    1.11  	
    1.12 @@ -32,47 +32,47 @@
    1.13  		return new Coordinate(x + 1, y);
    1.14  	}
    1.15  	
    1.16 -	public Coordinate bottom() {
    1.17 +	public Coordinate down() {
    1.18  		return new Coordinate(x, y - 1);
    1.19  	}
    1.20  	
    1.21 -	public Coordinate leftAndTop() {
    1.22 +	public Coordinate leftAndUp() {
    1.23  		return new Coordinate(x - 1, y + 1);
    1.24  	}
    1.25  	
    1.26 -	public Coordinate rightAndTop() {
    1.27 +	public Coordinate rightAndUp() {
    1.28  		return new Coordinate(x + 1, y + 1);
    1.29  	}
    1.30  	
    1.31 -	public Coordinate rightAndBottom() {
    1.32 +	public Coordinate rightAndDown() {
    1.33  		return new Coordinate(x + 1, y - 1);
    1.34  	}
    1.35  	
    1.36 -	public Coordinate leftAndBottom() {
    1.37 +	public Coordinate leftAndDown() {
    1.38  		return new Coordinate(x - 1, y - 1);
    1.39  	}
    1.40  	
    1.41  	public Set<Coordinate> getNeighboringCoordinates() {
    1.42  		Set<Coordinate> coords = new HashSet<Coordinate>();
    1.43  		coords.add(left());
    1.44 -		coords.add(top());
    1.45 +		coords.add(up());
    1.46  		coords.add(right());
    1.47 -		coords.add(bottom());
    1.48 -		coords.add(leftAndTop());
    1.49 -		coords.add(rightAndTop());
    1.50 -		coords.add(rightAndBottom());
    1.51 -		coords.add(leftAndBottom());
    1.52 +		coords.add(down());
    1.53 +		coords.add(leftAndUp());
    1.54 +		coords.add(rightAndUp());
    1.55 +		coords.add(rightAndDown());
    1.56 +		coords.add(leftAndDown());
    1.57  		return coords;
    1.58  	}
    1.59  	
    1.60  	public Movement getNecessaryMovement(Coordinate nCoordinate) {
    1.61  		if (left().equals(nCoordinate))
    1.62  			return Movement.LEFT;
    1.63 -		if (top().equals(nCoordinate))
    1.64 +		if (up().equals(nCoordinate))
    1.65  			return Movement.UP;
    1.66  		if (right().equals(nCoordinate))
    1.67  			return Movement.RIGHT;
    1.68 -		if (bottom().equals(nCoordinate))
    1.69 +		if (down().equals(nCoordinate))
    1.70  			return Movement.DOWN;
    1.71  		return null;
    1.72  	}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/GameState.java	Sat Jul 14 22:49:59 2012 +0400
     2.3 @@ -0,0 +1,13 @@
     2.4 +package ru.bosony.model.mine;
     2.5 +
     2.6 +/**
     2.7 + * @author 	indvdum (gotoindvdum[at]gmail[dot]com) <br>
     2.8 + * 			14.07.2012 21:15:05
     2.9 + *
    2.10 + */
    2.11 +public enum GameState {
    2.12 +	Game,
    2.13 +	Win,
    2.14 +	Abort,
    2.15 +	Lose,
    2.16 +}
     3.1 --- a/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java	Sat Jul 14 02:07:51 2012 +0400
     3.2 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java	Sat Jul 14 22:49:59 2012 +0400
     3.3 @@ -1,9 +1,15 @@
     3.4  package ru.bosony.model.mine;
     3.5  
     3.6 +import java.util.Arrays;
     3.7 +import java.util.Collection;
     3.8  import java.util.HashSet;
     3.9 +import java.util.List;
    3.10  import java.util.Set;
    3.11  
    3.12 +import org.hamcrest.CoreMatchers;
    3.13 +
    3.14  import ru.bosony.model.cellscontents.CellContent;
    3.15 +import static ru.bosony.model.cellscontents.CellContent.*;
    3.16  import ru.bosony.model.io.TextRepresentable;
    3.17  import ru.bosony.model.moving.Movement;
    3.18  
    3.19 @@ -15,12 +21,23 @@
    3.20  public class Mine implements TextRepresentable {
    3.21  
    3.22  	private Cell[][]	cells;
    3.23 +	private Cell[][]	prevStepCells;
    3.24  	protected int		sizeX;
    3.25  	protected int		sizeY;
    3.26  	protected Cell		robotCell;
    3.27 +	protected int		score					= 0;
    3.28 +	protected int		lastScoreChange			= 0;
    3.29 +	protected GameState	state					= GameState.Game;
    3.30 +	protected int		lambdaCollectedCount	= 0;
    3.31  
    3.32  	public Mine(String text) {
    3.33  		fromText(text);
    3.34 +		if (findCells(OpenLambdaLift).size() != 0)
    3.35 +			throw new RuntimeException("Detected OpenLambdaLift in new map!");
    3.36 +		if (findCells(MiningRobot).size() != 1)
    3.37 +			throw new RuntimeException("New map does not contain a robot!");
    3.38 +		if (findCells(ClosedLambdaLift).size() != 1)
    3.39 +			throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
    3.40  	}
    3.41  
    3.42  	@Override
    3.43 @@ -53,18 +70,26 @@
    3.44  				if (curX < row.length())
    3.45  					content = CellContent.fromText("" + row.charAt(curX));
    3.46  				else
    3.47 -					content = CellContent.Empty;
    3.48 +					content = Empty;
    3.49  				Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content);
    3.50  				cells[curX][curY] = cell;
    3.51 -				if (content == CellContent.MiningRobot)
    3.52 +				if (content == MiningRobot)
    3.53  					robotCell = cell;
    3.54  			}
    3.55  		}
    3.56  	}
    3.57  
    3.58  	public Cell getCell(Coordinate coordinate) {
    3.59 +		return getCell(coordinate, cells);
    3.60 +	}
    3.61 +
    3.62 +	protected Cell getCell(Coordinate coordinate, Cell[][] cells) {
    3.63 +		if (coordinate == null)
    3.64 +			return null;
    3.65  		int x = coordinate.x;
    3.66  		int y = coordinate.y;
    3.67 +		if (cells == null)
    3.68 +			return null;
    3.69  		if (x < 1 || x > sizeX || y < 1 || y > sizeY)
    3.70  			return null;
    3.71  		return cells[x - 1][sizeY - y];
    3.72 @@ -91,14 +116,14 @@
    3.73  			Coordinate nCoordinate = nCell.getCoordinate();
    3.74  			Cell rightNCell = getCell(nCoordinate.right());
    3.75  			Cell leftNCell = getCell(nCoordinate.left());
    3.76 -			if (nContent == CellContent.Empty
    3.77 -					|| nContent == CellContent.Earth
    3.78 -					|| nContent == CellContent.Lambda
    3.79 -					|| nContent == CellContent.OpenLambdaLift
    3.80 -					|| (nContent == CellContent.Rock && robotCell.getCoordinate().right().equals(nCoordinate)
    3.81 -							&& rightNCell != null && rightNCell.getContent() == CellContent.Empty)
    3.82 -					|| (nContent == CellContent.Rock && robotCell.getCoordinate().left().equals(nCoordinate)
    3.83 -							&& leftNCell != null && leftNCell.getContent() == CellContent.Empty)) {
    3.84 +			if (nContent == Empty
    3.85 +					|| nContent == Earth
    3.86 +					|| nContent == Lambda
    3.87 +					|| nContent == OpenLambdaLift
    3.88 +					|| (nContent == Rock && robotCell.getCoordinate().right().equals(nCoordinate) && rightNCell != null && rightNCell
    3.89 +							.getContent() == Empty)
    3.90 +					|| (nContent == Rock && robotCell.getCoordinate().left().equals(nCoordinate) && leftNCell != null && leftNCell
    3.91 +							.getContent() == Empty)) {
    3.92  				Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate);
    3.93  				movs.add(mov);
    3.94  			}
    3.95 @@ -115,4 +140,148 @@
    3.96  		return cells;
    3.97  	}
    3.98  
    3.99 +	protected void moveRobot(Cell nextRobotCell) {
   3.100 +		robotCell.setContent(Empty);
   3.101 +		nextRobotCell.setContent(MiningRobot);
   3.102 +		robotCell = nextRobotCell;
   3.103 +	}
   3.104 +
   3.105 +	public GameState move(Movement mov) {
   3.106 +		if (state != GameState.Game)
   3.107 +			throw new RuntimeException("Game over!");
   3.108 +
   3.109 +		// Score
   3.110 +		int prevScore = score;
   3.111 +		score--;
   3.112 +
   3.113 +		// Moving and scoring
   3.114 +		Coordinate nextRobotCoord = null;
   3.115 +		switch (mov) {
   3.116 +		case ABORT:
   3.117 +			score += 25 * lambdaCollectedCount;
   3.118 +			
   3.119 +			lastScoreChange = score - prevScore;
   3.120 +			state = GameState.Abort;
   3.121 +			return state;
   3.122 +		case WAIT:
   3.123 +			break;
   3.124 +		case LEFT:
   3.125 +			nextRobotCoord = robotCell.coordinate.left();
   3.126 +			break;
   3.127 +		case UP:
   3.128 +			nextRobotCoord = robotCell.coordinate.up();
   3.129 +			break;
   3.130 +		case RIGHT:
   3.131 +			nextRobotCoord = robotCell.coordinate.right();
   3.132 +			break;
   3.133 +		case DOWN:
   3.134 +			nextRobotCoord = robotCell.coordinate.down();
   3.135 +			break;
   3.136 +		}
   3.137 +		Cell nextRobotCell = getCell(nextRobotCoord);
   3.138 +		CellContent nextRobotCellContent = getContent(nextRobotCell);
   3.139 +		if (nextRobotCellContent == Empty || nextRobotCellContent == Earth) {
   3.140 +			moveRobot(nextRobotCell);
   3.141 +		} else if (nextRobotCellContent == Lambda) {
   3.142 +			moveRobot(nextRobotCell);
   3.143 +			lambdaCollectedCount++;
   3.144 +			score += 25;
   3.145 +		} else if (nextRobotCellContent == OpenLambdaLift) {
   3.146 +			moveRobot(nextRobotCell);
   3.147 +			score += 50 * lambdaCollectedCount;
   3.148 +			
   3.149 +			lastScoreChange = score - prevScore;
   3.150 +			state = GameState.Win;
   3.151 +			return state;
   3.152 +		} else if (robotCell.coordinate.right() == nextRobotCoord && nextRobotCellContent == Rock && getContent(getCell(nextRobotCoord.right())) == Empty) {
   3.153 +			moveRobot(nextRobotCell);
   3.154 +			getCell(nextRobotCoord.right()).setContent(Rock);
   3.155 +		} else if (robotCell.coordinate.left() == nextRobotCoord && nextRobotCellContent == Rock && getContent(getCell(nextRobotCoord.left())) == Empty) {
   3.156 +			moveRobot(nextRobotCell);
   3.157 +			getCell(nextRobotCoord.left()).setContent(Rock);
   3.158 +		} else {
   3.159 +			mov = Movement.WAIT;
   3.160 +		}
   3.161 +
   3.162 +		// Other ending conditions
   3.163 +		if (getContent(getCell(robotCell.coordinate.up(), prevStepCells)) != Rock
   3.164 +				&& getContent(getCell(robotCell.coordinate.up())) == Rock) {
   3.165 +			lastScoreChange = score - prevScore;
   3.166 +			state = GameState.Lose;
   3.167 +			return state;
   3.168 +		}
   3.169 +
   3.170 +		// World changes
   3.171 +		Cell[][] newCells = Arrays.copyOf(cells, cells.length);
   3.172 +		for (int y = 1; y <= sizeY; y++) {
   3.173 +			for (int x = 1; x <= sizeX; x++) {
   3.174 +				Coordinate curCoord = new Coordinate(x, y);
   3.175 +				Coordinate leftCoord = curCoord.left();
   3.176 +				Coordinate upCoord = curCoord.up();
   3.177 +				Coordinate rightCoord = curCoord.right();
   3.178 +				Coordinate downCoord = curCoord.down();
   3.179 +				Coordinate leftAndUpCoord = curCoord.leftAndUp();
   3.180 +				Coordinate rightAndUpCoord = curCoord.rightAndUp();
   3.181 +				Coordinate rightAndDownCoord = curCoord.rightAndDown();
   3.182 +				Coordinate leftAndDownCoord = curCoord.leftAndDown();
   3.183 +				Cell curCell = getCell(curCoord);
   3.184 +				Cell leftCell = getCell(leftCoord);
   3.185 +				Cell upCell = getCell(upCoord);
   3.186 +				Cell rightCell = getCell(rightCoord);
   3.187 +				Cell downCell = getCell(downCoord);
   3.188 +				Cell leftAndUpCell = getCell(leftAndUpCoord);
   3.189 +				Cell rightAndUpCell = getCell(rightAndUpCoord);
   3.190 +				Cell rightAndDownCell = getCell(rightAndDownCoord);
   3.191 +				Cell leftAndDownCell = getCell(leftAndDownCoord);
   3.192 +
   3.193 +				if (getContent(curCell) == Rock && getContent(downCell) == Empty) {
   3.194 +					getCell(curCoord, newCells).setContent(Empty);
   3.195 +					getCell(downCoord, newCells).setContent(Rock);
   3.196 +				} else if (getContent(curCell) == Rock && getContent(downCell) == Empty
   3.197 +						&& getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
   3.198 +					getCell(curCoord, newCells).setContent(Empty);
   3.199 +					getCell(rightAndDownCoord, newCells).setContent(Rock);
   3.200 +				} else if (getContent(curCell) == Rock && getContent(downCell) == Rock
   3.201 +						&& (getContent(rightCell) != Empty || getContent(rightAndDownCell) != Empty)
   3.202 +						&& getContent(leftCell) == Empty && getContent(leftAndDownCell) == Empty) {
   3.203 +					getCell(curCoord, newCells).setContent(Empty);
   3.204 +					getCell(leftAndDownCoord, newCells).setContent(Rock);
   3.205 +				} else if (getContent(curCell) == Rock && getContent(downCell) == Lambda
   3.206 +						&& getContent(rightCell) == Empty && getContent(rightAndDownCell) == Empty) {
   3.207 +					getCell(curCoord, newCells).setContent(Empty);
   3.208 +					getCell(rightAndDownCoord, newCells).setContent(Rock);
   3.209 +				} else if (getContent(curCell) == ClosedLambdaLift && findCells(Lambda).size() == 0) {
   3.210 +					getCell(curCoord, newCells).setContent(OpenLambdaLift);
   3.211 +				}
   3.212 +			}
   3.213 +		}
   3.214 +		prevStepCells = cells;
   3.215 +		cells = newCells;
   3.216 +
   3.217 +		lastScoreChange = score - prevScore;
   3.218 +		state = GameState.Game;
   3.219 +		return state;
   3.220 +	}
   3.221 +
   3.222 +	protected CellContent getContent(Cell cell) {
   3.223 +		if (cell == null)
   3.224 +			return null;
   3.225 +		return cell.getContent();
   3.226 +	}
   3.227 +
   3.228 +	public Collection<Cell> findCells(CellContent content) {
   3.229 +		Collection<Cell> finded = new HashSet<Cell>();
   3.230 +		for (int y = sizeY - 1; y >= 0; y--) {
   3.231 +			for (int x = 0; x < sizeX - 1; x++) {
   3.232 +				if (cells[x][y].getContent() == content)
   3.233 +					finded.add(cells[x][y]);
   3.234 +			}
   3.235 +		}
   3.236 +		return finded;
   3.237 +	}
   3.238 +
   3.239 +	public int getLastScoreChange() {
   3.240 +		return lastScoreChange;
   3.241 +	}
   3.242 +
   3.243  }
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/icfp-model/src/test/java/ru/bosony/tests/TestMovements.java	Sat Jul 14 22:49:59 2012 +0400
     4.3 @@ -0,0 +1,47 @@
     4.4 +package ru.bosony.tests;
     4.5 +
     4.6 +import static org.junit.Assert.*;
     4.7 +
     4.8 +import java.util.Set;
     4.9 +
    4.10 +import org.junit.Test;
    4.11 +
    4.12 +import ru.bosony.model.mine.Mine;
    4.13 +import ru.bosony.model.moving.Movement;
    4.14 +
    4.15 +/**
    4.16 + * @author 	indvdum (gotoindvdum[at]gmail[dot]com) <br>
    4.17 + * 			14.07.2012 2:29:05
    4.18 + *
    4.19 + */
    4.20 +public class TestMovements {
    4.21 +
    4.22 +	@Test
    4.23 +	public void test() {
    4.24 +		/* Map 01 (6x6)
    4.25 +		 * 
    4.26 +			######
    4.27 +			#. *R#
    4.28 +			#  \.#
    4.29 +			#\ * #
    4.30 +			L  .\#
    4.31 +			######
    4.32 +		 */
    4.33 +		String map = 
    4.34 +				"######\n" + 
    4.35 +				"#. *R#\n" + 
    4.36 +				"#  \\.#\n" + 
    4.37 +				"#\\ * #\n" + 
    4.38 +				"L  .\\#\n" + 
    4.39 +				"######";
    4.40 +		Mine mine = new Mine(map);
    4.41 +		assertEquals(mine.getSizeX(), 6);
    4.42 +		assertEquals(mine.getSizeY(), 6);
    4.43 +		Set<Movement> movs = mine.getAvailableRobotMovements();
    4.44 +		assertFalse(movs.contains(Movement.UP));
    4.45 +		assertFalse(movs.contains(Movement.RIGHT));
    4.46 +		assertTrue(movs.contains(Movement.DOWN));
    4.47 +		assertTrue(movs.contains(Movement.LEFT));
    4.48 +	}
    4.49 +
    4.50 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/icfp-model/src/test/java/ru/bosony/tests/TestScore.java	Sat Jul 14 22:49:59 2012 +0400
     5.3 @@ -0,0 +1,34 @@
     5.4 +package ru.bosony.tests;
     5.5 +
     5.6 +import static org.junit.Assert.*;
     5.7 +
     5.8 +import org.junit.Test;
     5.9 +
    5.10 +import ru.bosony.model.mine.Mine;
    5.11 +import ru.bosony.model.moving.Movement;
    5.12 +
    5.13 +public class TestScore {
    5.14 +
    5.15 +	@Test
    5.16 +	public void test() {
    5.17 +		/* Map 01 (6x6)
    5.18 +		 * 
    5.19 +			######
    5.20 +			#. *R#
    5.21 +			#  \.#
    5.22 +			#\ * #
    5.23 +			L  .\#
    5.24 +			######
    5.25 +		 */
    5.26 +		String map = 
    5.27 +				"######\n" + 
    5.28 +				"#. *R#\n" + 
    5.29 +				"#  \\.#\n" + 
    5.30 +				"#\\ * #\n" + 
    5.31 +				"L  .\\#\n" + 
    5.32 +				"######";
    5.33 +		Mine mine = new Mine(map);
    5.34 +		mine.move(Movement.LEFT);
    5.35 +	}
    5.36 +
    5.37 +}