| author | indvdum (gotoindvdum[at]gmail[dot]com) |
| Sun, 15 Jul 2012 19:26:28 +0400 | |
| changeset 26 | 964aa8102804 |
| parent 25 | d6a75f29ba9b |
| child 27 | 505c25412b16 |
| permissions | -rw-r--r-- |
| indvdum@6 | 1 |
package ru.bosony.model.mine; |
| indvdum@6 | 2 |
|
| indvdum@9 | 3 |
import static ru.bosony.model.cellscontents.CellContent.ClosedLambdaLift; |
| indvdum@9 | 4 |
import static ru.bosony.model.cellscontents.CellContent.Earth; |
| indvdum@9 | 5 |
import static ru.bosony.model.cellscontents.CellContent.Empty; |
| indvdum@9 | 6 |
import static ru.bosony.model.cellscontents.CellContent.Lambda; |
| indvdum@9 | 7 |
import static ru.bosony.model.cellscontents.CellContent.MiningRobot; |
| indvdum@9 | 8 |
import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift; |
| indvdum@9 | 9 |
import static ru.bosony.model.cellscontents.CellContent.Rock; |
| indvdum@9 | 10 |
|
| indvdum@7 | 11 |
import java.util.HashSet; |
| indvdum@7 | 12 |
import java.util.Set; |
| indvdum@25 | 13 |
import java.util.regex.Matcher; |
| indvdum@25 | 14 |
import java.util.regex.Pattern; |
| indvdum@7 | 15 |
|
| indvdum@6 | 16 |
import ru.bosony.model.cellscontents.CellContent; |
| indvdum@6 | 17 |
import ru.bosony.model.io.TextRepresentable; |
| indvdum@22 | 18 |
import ru.bosony.model.moving.Coordinate; |
| indvdum@7 | 19 |
import ru.bosony.model.moving.Movement; |
| indvdum@6 | 20 |
|
| indvdum@7 | 21 |
/** |
| indvdum@7 | 22 |
* @author indvdum (gotoindvdum[at]gmail[dot]com) <br> |
| indvdum@7 | 23 |
* 14.07.2012 2:02:18 |
| indvdum@7 | 24 |
* |
| indvdum@7 | 25 |
*/ |
| indvdum@6 | 26 |
public class Mine implements TextRepresentable {
|
| indvdum@6 | 27 |
|
| indvdum@25 | 28 |
protected Cell[][] cells; |
| indvdum@25 | 29 |
protected int sizeX; |
| indvdum@25 | 30 |
protected int sizeY; |
| indvdum@26 | 31 |
protected int waterLevel = 0; |
| indvdum@26 | 32 |
protected int flooding = 0; |
| indvdum@26 | 33 |
protected int robotWaterproof = 10; |
| indvdum@26 | 34 |
protected static Pattern minePattern = Pattern |
| indvdum@26 | 35 |
.compile("(?s)(.*?)(\n\n(Water.*|Flooding.*|Waterproof.*)|$)");
|
| indvdum@26 | 36 |
protected static Pattern waterPattern = Pattern.compile("(?s).*(?<=.*Water )(\\d+).*");
|
| indvdum@26 | 37 |
protected static Pattern floodingPattern = Pattern.compile("(?s).*(?<=.*Flooding )(\\d+).*");
|
| indvdum@26 | 38 |
protected static Pattern waterproofPattern = Pattern.compile("(?s).*(?<=.*Waterproof )(\\d+).*");
|
| indvdum@6 | 39 |
|
| indvdum@6 | 40 |
public Mine(String text) {
|
| indvdum@6 | 41 |
fromText(text); |
| indvdum@8 | 42 |
if (findCells(OpenLambdaLift).size() != 0) |
| indvdum@8 | 43 |
throw new RuntimeException("Detected OpenLambdaLift in new map!");
|
| indvdum@8 | 44 |
if (findCells(MiningRobot).size() != 1) |
| indvdum@8 | 45 |
throw new RuntimeException("New map does not contain a robot!");
|
| indvdum@8 | 46 |
if (findCells(ClosedLambdaLift).size() != 1) |
| indvdum@8 | 47 |
throw new RuntimeException("New map does not contain a ClosedLambdaLift!");
|
| indvdum@6 | 48 |
} |
| indvdum@6 | 49 |
|
| indvdum@6 | 50 |
@Override |
| indvdum@6 | 51 |
public String toText() {
|
| indvdum@6 | 52 |
String text = ""; |
| indvdum@6 | 53 |
for (int y = 0; y < sizeY; y++) {
|
| indvdum@6 | 54 |
for (int x = 0; x < sizeX; x++) {
|
| indvdum@6 | 55 |
text += cells[x][y].getContent().toText(); |
| indvdum@6 | 56 |
} |
| indvdum@6 | 57 |
text += "\n"; |
| indvdum@6 | 58 |
} |
| indvdum@25 | 59 |
text = text.replaceAll("\n*$", "");
|
| indvdum@25 | 60 |
if (waterLevel != 0 || flooding != 0 || robotWaterproof != 10) {
|
| indvdum@25 | 61 |
text += "\n\n"; |
| indvdum@25 | 62 |
text += "Water " + waterLevel + "\n"; |
| indvdum@25 | 63 |
text += "Flooding " + flooding + "\n"; |
| indvdum@25 | 64 |
text += "Waterproof " + robotWaterproof; |
| indvdum@25 | 65 |
} |
| indvdum@25 | 66 |
return text; |
| indvdum@6 | 67 |
} |
| indvdum@6 | 68 |
|
| indvdum@25 | 69 |
protected void fromText(String str) {
|
| indvdum@26 | 70 |
Matcher matcher = minePattern.matcher(str); |
| indvdum@25 | 71 |
if (!matcher.matches()) |
| indvdum@25 | 72 |
throw new RuntimeException("Can't parse mine from string");
|
| indvdum@25 | 73 |
String body = matcher.group(1); |
| indvdum@25 | 74 |
String[] rows = body.split("\n+");
|
| indvdum@6 | 75 |
int y = rows.length; |
| indvdum@6 | 76 |
int x = 0; |
| indvdum@6 | 77 |
for (String row : rows) {
|
| indvdum@6 | 78 |
if (x < row.length()) |
| indvdum@6 | 79 |
x = row.length(); |
| indvdum@6 | 80 |
} |
| indvdum@6 | 81 |
sizeX = x; |
| indvdum@6 | 82 |
sizeY = y; |
| indvdum@6 | 83 |
cells = new Cell[x][y]; |
| indvdum@6 | 84 |
for (int curY = 0; curY < rows.length; curY++) {
|
| indvdum@6 | 85 |
String row = rows[curY]; |
| indvdum@6 | 86 |
for (int curX = 0; curX < row.length() || curX < x; curX++) {
|
| indvdum@6 | 87 |
CellContent content = null; |
| indvdum@6 | 88 |
if (curX < row.length()) |
| indvdum@6 | 89 |
content = CellContent.fromText("" + row.charAt(curX));
|
| indvdum@6 | 90 |
else |
| indvdum@8 | 91 |
content = Empty; |
| indvdum@22 | 92 |
if (content == null) |
| indvdum@22 | 93 |
content = Empty; |
| indvdum@7 | 94 |
Cell cell = new Cell(new Coordinate(curX + 1, rows.length - curY), content); |
| indvdum@6 | 95 |
cells[curX][curY] = cell; |
| indvdum@6 | 96 |
} |
| indvdum@6 | 97 |
} |
| indvdum@26 | 98 |
|
| indvdum@26 | 99 |
String metadata = matcher.group(3); |
| indvdum@26 | 100 |
String sWaterLevel = ""; |
| indvdum@26 | 101 |
String sFlooding = ""; |
| indvdum@26 | 102 |
String sRobotWaterproof = ""; |
| indvdum@26 | 103 |
Matcher waterMatcher = waterPattern.matcher(metadata); |
| indvdum@26 | 104 |
if (waterMatcher.matches()) {
|
| indvdum@26 | 105 |
sWaterLevel = waterMatcher.group(1); |
| indvdum@26 | 106 |
} |
| indvdum@26 | 107 |
Matcher floodingMatcher = floodingPattern.matcher(metadata); |
| indvdum@26 | 108 |
if (floodingMatcher.matches()) {
|
| indvdum@26 | 109 |
sFlooding = floodingMatcher.group(1); |
| indvdum@26 | 110 |
} |
| indvdum@26 | 111 |
Matcher waterproofMatcher = waterproofPattern.matcher(metadata); |
| indvdum@26 | 112 |
if (waterproofMatcher.matches()) {
|
| indvdum@26 | 113 |
sRobotWaterproof = waterproofMatcher.group(1); |
| indvdum@26 | 114 |
} |
| indvdum@25 | 115 |
if (sWaterLevel.length() > 0) |
| indvdum@25 | 116 |
waterLevel = Integer.parseInt(sWaterLevel); |
| indvdum@25 | 117 |
if (sFlooding.length() > 0) |
| indvdum@25 | 118 |
flooding = Integer.parseInt(sFlooding); |
| indvdum@25 | 119 |
if (sRobotWaterproof.length() > 0) |
| indvdum@25 | 120 |
robotWaterproof = Integer.parseInt(sRobotWaterproof); |
| indvdum@6 | 121 |
} |
| indvdum@6 | 122 |
|
| indvdum@7 | 123 |
public Cell getCell(Coordinate coordinate) {
|
| indvdum@8 | 124 |
if (coordinate == null) |
| indvdum@8 | 125 |
return null; |
| indvdum@7 | 126 |
int x = coordinate.x; |
| indvdum@7 | 127 |
int y = coordinate.y; |
| indvdum@7 | 128 |
if (x < 1 || x > sizeX || y < 1 || y > sizeY) |
| indvdum@7 | 129 |
return null; |
| indvdum@7 | 130 |
return cells[x - 1][sizeY - y]; |
| indvdum@7 | 131 |
} |
| indvdum@7 | 132 |
|
| indvdum@6 | 133 |
@Override |
| indvdum@6 | 134 |
public String toString() {
|
| indvdum@6 | 135 |
return toText(); |
| indvdum@6 | 136 |
} |
| indvdum@6 | 137 |
|
| indvdum@7 | 138 |
public int getSizeX() {
|
| indvdum@7 | 139 |
return sizeX; |
| indvdum@7 | 140 |
} |
| indvdum@7 | 141 |
|
| indvdum@7 | 142 |
public int getSizeY() {
|
| indvdum@7 | 143 |
return sizeY; |
| indvdum@7 | 144 |
} |
| indvdum@7 | 145 |
|
| indvdum@7 | 146 |
public Set<Movement> getAvailableRobotMovements() {
|
| indvdum@7 | 147 |
Set<Movement> movs = new HashSet<Movement>(); |
| indvdum@22 | 148 |
Cell robotCell = getRobotCell(); |
| indvdum@7 | 149 |
Set<Cell> nCells = getNeighboringCells(robotCell); |
| indvdum@7 | 150 |
for (Cell nCell : nCells) {
|
| indvdum@7 | 151 |
CellContent nContent = nCell.getContent(); |
| indvdum@7 | 152 |
Coordinate nCoordinate = nCell.getCoordinate(); |
| indvdum@7 | 153 |
Cell rightNCell = getCell(nCoordinate.right()); |
| indvdum@7 | 154 |
Cell leftNCell = getCell(nCoordinate.left()); |
| indvdum@8 | 155 |
if (nContent == Empty |
| indvdum@8 | 156 |
|| nContent == Earth |
| indvdum@8 | 157 |
|| nContent == Lambda |
| indvdum@8 | 158 |
|| nContent == OpenLambdaLift |
| indvdum@8 | 159 |
|| (nContent == Rock && robotCell.getCoordinate().right().equals(nCoordinate) && rightNCell != null && rightNCell |
| indvdum@8 | 160 |
.getContent() == Empty) |
| indvdum@8 | 161 |
|| (nContent == Rock && robotCell.getCoordinate().left().equals(nCoordinate) && leftNCell != null && leftNCell |
| indvdum@8 | 162 |
.getContent() == Empty)) {
|
| indvdum@7 | 163 |
Movement mov = robotCell.getCoordinate().getNecessaryMovement(nCoordinate); |
| indvdum@7 | 164 |
movs.add(mov); |
| indvdum@7 | 165 |
} |
| indvdum@7 | 166 |
} |
| indvdum@7 | 167 |
movs.add(Movement.ABORT); |
| indvdum@7 | 168 |
movs.add(Movement.WAIT); |
| indvdum@7 | 169 |
return movs; |
| indvdum@7 | 170 |
} |
| indvdum@7 | 171 |
|
| indvdum@7 | 172 |
public Set<Cell> getNeighboringCells(Cell cell) {
|
| indvdum@7 | 173 |
Set<Cell> cells = new HashSet<Cell>(); |
| indvdum@7 | 174 |
for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates()) |
| indvdum@7 | 175 |
cells.add(getCell(coordinate)); |
| indvdum@7 | 176 |
return cells; |
| indvdum@7 | 177 |
} |
| indvdum@7 | 178 |
|
| indvdum@9 | 179 |
public Set<Cell> findCells(CellContent content) {
|
| indvdum@9 | 180 |
Set<Cell> finded = new HashSet<Cell>(); |
| indvdum@8 | 181 |
for (int y = sizeY - 1; y >= 0; y--) {
|
| indvdum@15 | 182 |
for (int x = 0; x < sizeX; x++) {
|
| indvdum@8 | 183 |
if (cells[x][y].getContent() == content) |
| indvdum@8 | 184 |
finded.add(cells[x][y]); |
| indvdum@8 | 185 |
} |
| indvdum@8 | 186 |
} |
| indvdum@8 | 187 |
return finded; |
| indvdum@8 | 188 |
} |
| indvdum@8 | 189 |
|
| indvdum@9 | 190 |
public void setCells(Cell[][] cells) {
|
| indvdum@9 | 191 |
this.cells = cells; |
| indvdum@9 | 192 |
} |
| indvdum@9 | 193 |
|
| indvdum@9 | 194 |
public Cell getRobotCell() {
|
| indvdum@22 | 195 |
for (Cell cell : findCells(MiningRobot)) {
|
| indvdum@22 | 196 |
return cell; |
| indvdum@22 | 197 |
} |
| indvdum@22 | 198 |
throw new RuntimeException("Where is robot!?");
|
| indvdum@9 | 199 |
} |
| indvdum@9 | 200 |
|
| indvdum@22 | 201 |
public Cell[][] cloneCells() {
|
| indvdum@22 | 202 |
Cell[][] clone = new Cell[sizeX][sizeY]; |
| indvdum@22 | 203 |
for (int y = sizeY - 1; y >= 0; y--) {
|
| indvdum@22 | 204 |
for (int x = 0; x < sizeX; x++) {
|
| indvdum@22 | 205 |
try {
|
| indvdum@22 | 206 |
clone[x][y] = cells[x][y].clone(); |
| indvdum@22 | 207 |
} catch (CloneNotSupportedException e) {
|
| indvdum@22 | 208 |
throw new RuntimeException(e); |
| indvdum@22 | 209 |
} |
| indvdum@22 | 210 |
} |
| indvdum@22 | 211 |
} |
| indvdum@22 | 212 |
return clone; |
| indvdum@8 | 213 |
} |
| indvdum@8 | 214 |
|
| indvdum@25 | 215 |
public int getWaterLevel() {
|
| indvdum@25 | 216 |
return waterLevel; |
| indvdum@25 | 217 |
} |
| indvdum@25 | 218 |
|
| indvdum@25 | 219 |
public void setWaterLevel(int waterLevel) {
|
| indvdum@25 | 220 |
this.waterLevel = waterLevel; |
| indvdum@25 | 221 |
} |
| indvdum@25 | 222 |
|
| indvdum@25 | 223 |
public int getFlooding() {
|
| indvdum@25 | 224 |
return flooding; |
| indvdum@25 | 225 |
} |
| indvdum@25 | 226 |
|
| indvdum@25 | 227 |
public int getRobotWaterproof() {
|
| indvdum@25 | 228 |
return robotWaterproof; |
| indvdum@25 | 229 |
} |
| indvdum@25 | 230 |
|
| indvdum@6 | 231 |
} |