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