| author | indvdum (gotoindvdum[at]gmail[dot]com) |
| Sat, 14 Jul 2012 02:07:51 +0400 | |
| branch | indvdum |
| changeset 7 | 251961e44b5f |
| parent 6 | 0dbbc8983926 |
| child 8 | ad884dc6877c |
| permissions | -rw-r--r-- |
| indvdum@6 | 1 |
package ru.bosony.model.mine; |
| indvdum@6 | 2 |
|
| indvdum@7 | 3 |
import java.util.HashSet; |
| indvdum@7 | 4 |
import java.util.Set; |
| indvdum@7 | 5 |
|
| indvdum@6 | 6 |
import ru.bosony.model.cellscontents.CellContent; |
| indvdum@6 | 7 |
import ru.bosony.model.io.TextRepresentable; |
| indvdum@7 | 8 |
import ru.bosony.model.moving.Movement; |
| indvdum@6 | 9 |
|
| indvdum@7 | 10 |
/** |
| indvdum@7 | 11 |
* @author indvdum (gotoindvdum[at]gmail[dot]com) <br> |
| indvdum@7 | 12 |
* 14.07.2012 2:02:18 |
| indvdum@7 | 13 |
* |
| indvdum@7 | 14 |
*/ |
| indvdum@6 | 15 |
public class Mine implements TextRepresentable {
|
| indvdum@6 | 16 |
|
| indvdum@7 | 17 |
private Cell[][] cells; |
| indvdum@7 | 18 |
protected int sizeX; |
| indvdum@7 | 19 |
protected int sizeY; |
| indvdum@7 | 20 |
protected Cell robotCell; |
| indvdum@6 | 21 |
|
| indvdum@6 | 22 |
public Mine(String text) {
|
| indvdum@6 | 23 |
fromText(text); |
| indvdum@6 | 24 |
} |
| indvdum@6 | 25 |
|
| indvdum@6 | 26 |
@Override |
| indvdum@6 | 27 |
public String toText() {
|
| indvdum@6 | 28 |
String text = ""; |
| indvdum@6 | 29 |
for (int y = 0; y < sizeY; y++) {
|
| indvdum@6 | 30 |
for (int x = 0; x < sizeX; x++) {
|
| indvdum@6 | 31 |
text += cells[x][y].getContent().toText(); |
| indvdum@6 | 32 |
} |
| indvdum@6 | 33 |
text += "\n"; |
| indvdum@6 | 34 |
} |
| indvdum@6 | 35 |
return text.replaceAll("\n*$", "");
|
| indvdum@6 | 36 |
} |
| indvdum@6 | 37 |
|
| indvdum@6 | 38 |
private void fromText(String text) {
|
| indvdum@6 | 39 |
String[] rows = text.split("\n+");
|
| indvdum@6 | 40 |
int y = rows.length; |
| indvdum@6 | 41 |
int x = 0; |
| indvdum@6 | 42 |
for (String row : rows) {
|
| indvdum@6 | 43 |
if (x < row.length()) |
| indvdum@6 | 44 |
x = row.length(); |
| indvdum@6 | 45 |
} |
| indvdum@6 | 46 |
sizeX = x; |
| indvdum@6 | 47 |
sizeY = y; |
| indvdum@6 | 48 |
cells = new Cell[x][y]; |
| indvdum@6 | 49 |
for (int curY = 0; curY < rows.length; curY++) {
|
| indvdum@6 | 50 |
String row = rows[curY]; |
| indvdum@6 | 51 |
for (int curX = 0; curX < row.length() || curX < x; curX++) {
|
| indvdum@6 | 52 |
CellContent content = null; |
| indvdum@6 | 53 |
if (curX < row.length()) |
| indvdum@6 | 54 |
content = CellContent.fromText("" + row.charAt(curX));
|
| indvdum@6 | 55 |
else |
| indvdum@6 | 56 |
content = CellContent.Empty; |
| indvdum@7 | 57 |
Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content); |
| indvdum@6 | 58 |
cells[curX][curY] = cell; |
| indvdum@7 | 59 |
if (content == CellContent.MiningRobot) |
| indvdum@7 | 60 |
robotCell = cell; |
| indvdum@6 | 61 |
} |
| indvdum@6 | 62 |
} |
| indvdum@6 | 63 |
} |
| indvdum@6 | 64 |
|
| indvdum@7 | 65 |
public Cell getCell(Coordinate coordinate) {
|
| indvdum@7 | 66 |
int x = coordinate.x; |
| indvdum@7 | 67 |
int y = coordinate.y; |
| indvdum@7 | 68 |
if (x < 1 || x > sizeX || y < 1 || y > sizeY) |
| indvdum@7 | 69 |
return null; |
| indvdum@7 | 70 |
return cells[x - 1][sizeY - y]; |
| indvdum@7 | 71 |
} |
| indvdum@7 | 72 |
|
| indvdum@6 | 73 |
@Override |
| indvdum@6 | 74 |
public String toString() {
|
| indvdum@6 | 75 |
return toText(); |
| indvdum@6 | 76 |
} |
| indvdum@6 | 77 |
|
| indvdum@7 | 78 |
public int getSizeX() {
|
| indvdum@7 | 79 |
return sizeX; |
| indvdum@7 | 80 |
} |
| indvdum@7 | 81 |
|
| indvdum@7 | 82 |
public int getSizeY() {
|
| indvdum@7 | 83 |
return sizeY; |
| indvdum@7 | 84 |
} |
| indvdum@7 | 85 |
|
| indvdum@7 | 86 |
public Set<Movement> getAvailableRobotMovements() {
|
| indvdum@7 | 87 |
Set<Movement> movs = new HashSet<Movement>(); |
| indvdum@7 | 88 |
Set<Cell> nCells = getNeighboringCells(robotCell); |
| indvdum@7 | 89 |
for (Cell nCell : nCells) {
|
| indvdum@7 | 90 |
CellContent nContent = nCell.getContent(); |
| indvdum@7 | 91 |
Coordinate nCoordinate = nCell.getCoordinate(); |
| indvdum@7 | 92 |
Cell rightNCell = getCell(nCoordinate.right()); |
| indvdum@7 | 93 |
Cell leftNCell = getCell(nCoordinate.left()); |
| indvdum@7 | 94 |
if (nContent == CellContent.Empty |
| indvdum@7 | 95 |
|| nContent == CellContent.Earth |
| indvdum@7 | 96 |
|| nContent == CellContent.Lambda |
| indvdum@7 | 97 |
|| nContent == CellContent.OpenLambdaLift |
| indvdum@7 | 98 |
|| (nContent == CellContent.Rock && robotCell.getCoordinate().right().equals(nCoordinate) |
| indvdum@7 | 99 |
&& rightNCell != null && rightNCell.getContent() == CellContent.Empty) |
| indvdum@7 | 100 |
|| (nContent == CellContent.Rock && robotCell.getCoordinate().left().equals(nCoordinate) |
| indvdum@7 | 101 |
&& leftNCell != null && leftNCell.getContent() == CellContent.Empty)) {
|
| indvdum@7 | 102 |
Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate); |
| indvdum@7 | 103 |
movs.add(mov); |
| indvdum@7 | 104 |
} |
| indvdum@7 | 105 |
} |
| indvdum@7 | 106 |
movs.add(Movement.ABORT); |
| indvdum@7 | 107 |
movs.add(Movement.WAIT); |
| indvdum@7 | 108 |
return movs; |
| indvdum@7 | 109 |
} |
| indvdum@7 | 110 |
|
| indvdum@7 | 111 |
public Set<Cell> getNeighboringCells(Cell cell) {
|
| indvdum@7 | 112 |
Set<Cell> cells = new HashSet<Cell>(); |
| indvdum@7 | 113 |
for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates()) |
| indvdum@7 | 114 |
cells.add(getCell(coordinate)); |
| indvdum@7 | 115 |
return cells; |
| indvdum@7 | 116 |
} |
| indvdum@7 | 117 |
|
| indvdum@6 | 118 |
} |