| •1 |
|
| 2 |
|
| 3 |
|
| •4 |
|
| 5 |
|
| •6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| •11 |
|
| 12 |
|
| 13 |
|
| •14 |
|
| 15 |
|
| •16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
package org.stathissideris.ascii2image.text; |
| 21 |
|
| 22 |
import java.awt.Color; |
| •23 |
import java.io.*; |
| 24 |
import java.util.*; |
| 25 |
import java.util.regex.Matcher; |
| 26 |
import java.util.regex.Pattern; |
| 27 |
|
| 28 |
import org.stathissideris.ascii2image.core.FileUtils; |
| 29 |
import org.stathissideris.ascii2image.core.ProcessingOptions; |
| •30 |
import org.stathissideris.ascii2image.graphics.CustomShapeDefinition; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class TextGrid { |
| 38 |
|
| 39 |
private static final boolean DEBUG = false; |
| 40 |
|
| •41 |
private ArrayList<StringBuilder> rows; |
| 42 |
|
| 43 |
private static char[] boundaries = {'/', '\\', '|', '-', '*', '=', ':'}; |
| 44 |
private static char[] undisputableBoundaries = {'|', '-', '*', '=', ':'}; |
| 45 |
private static char[] horizontalLines = {'-', '='}; |
| 46 |
private static char[] verticalLines = {'|', ':'}; |
| 47 |
private static char[] arrowHeads = {'<', '>', '^', 'v', 'V'}; |
| 48 |
private static char[] cornerChars = {'\\', '/', '+'}; |
| 49 |
private static char[] pointMarkers = {'*'}; |
| 50 |
private static char[] dashedLines = {':', '~', '='}; |
| 51 |
|
| 52 |
private static char[] entryPoints1 = {'\\'}; |
| 53 |
private static char[] entryPoints2 = {'|', ':', '+', '\\', '/'}; |
| 54 |
private static char[] entryPoints3 = {'/'}; |
| 55 |
private static char[] entryPoints4 = {'-', '=', '+', '\\', '/'}; |
| 56 |
private static char[] entryPoints5 = {'\\'}; |
| 57 |
private static char[] entryPoints6 = {'|', ':', '+', '\\', '/'}; |
| 58 |
private static char[] entryPoints7 = {'/'}; |
| 59 |
private static char[] entryPoints8 = {'-', '=', '+', '\\', '/'}; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
private static HashMap<String, String> humanColorCodes = new HashMap<String, String>(); |
| 64 |
static { |
| 65 |
humanColorCodes.put("GRE", "9D9"); |
| 66 |
humanColorCodes.put("BLU", "55B"); |
| 67 |
humanColorCodes.put("PNK", "FAA"); |
| 68 |
humanColorCodes.put("RED", "E32"); |
| 69 |
humanColorCodes.put("YEL", "FF3"); |
| 70 |
humanColorCodes.put("BLK", "000"); |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
private static HashSet<String> markupTags = |
| 75 |
new HashSet<String>(); |
| 76 |
|
| 77 |
static { |
| 78 |
markupTags.add("d"); |
| 79 |
markupTags.add("s"); |
| 80 |
markupTags.add("io"); |
| 81 |
markupTags.add("c"); |
| 82 |
markupTags.add("mo"); |
| 83 |
markupTags.add("tr"); |
| 84 |
markupTags.add("o"); |
| 85 |
} |
| 86 |
|
| 87 |
public void addToMarkupTags(Collection<String> tags){ |
| 88 |
markupTags.addAll(tags); |
| 89 |
} |
| 90 |
|
| 91 |
public static void main(String[] args) throws Exception { |
| 92 |
TextGrid grid = new TextGrid(); |
| 93 |
grid.loadFrom("tests/text/art10.txt"); |
| 94 |
|
| 95 |
grid.writeStringTo(grid.new Cell(28, 1), "testing"); |
| 96 |
|
| 97 |
grid.findMarkupTags(); |
| 98 |
|
| 99 |
grid.printDebug(); |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
public TextGrid(){ |
| •108 |
rows = new ArrayList<StringBuilder>(); |
| 109 |
} |
| 110 |
|
| 111 |
public TextGrid(int width, int height){ |
| 112 |
String space = StringUtils.repeatString(" ", width); |
| •113 |
rows = new ArrayList<StringBuilder>(); |
| 114 |
for(int i = 0; i < height; i++) |
| •115 |
rows.add(new StringBuilder(space)); |
| 116 |
} |
| 117 |
|
| 118 |
public static TextGrid makeSameSizeAs(TextGrid grid){ |
| 119 |
return new TextGrid(grid.getWidth(), grid.getHeight()); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
public TextGrid(TextGrid otherGrid){ |
| •124 |
rows = new ArrayList<StringBuilder>(); |
| 125 |
for(StringBuilder row : otherGrid.getRows()) { |
| 126 |
rows.add(new StringBuilder(row)); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
public void clear(){ |
| 131 |
String blank = StringUtils.repeatString(" ", getWidth()); |
| 132 |
int height = getHeight(); |
| 133 |
rows.clear(); |
| 134 |
for(int i = 0; i < height; i++) |
| •135 |
rows.add(new StringBuilder(blank)); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
public char get(int x, int y){ |
| 140 |
if(x > getWidth() - 1 |
| 141 |
|| y > getHeight() - 1 |
| 142 |
|| x < 0 |
| 143 |
|| y < 0) return 0; |
| 144 |
return rows.get(y).charAt(x); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
public char get(Cell cell){ |
| 149 |
if(cell.x > getWidth() - 1 |
| 150 |
|| cell.y > getHeight() - 1 |
| 151 |
|| cell.x < 0 |
| 152 |
|| cell.y < 0) return 0; |
| 153 |
return rows.get(cell.y).charAt(cell.x); |
| 154 |
} |
| 155 |
|
| •156 |
public StringBuilder getRow(int y){ |
| 157 |
return rows.get(y); |
| 158 |
} |
| 159 |
|
| 160 |
public TextGrid getSubGrid(int x, int y, int width, int height){ |
| 161 |
TextGrid grid = new TextGrid(width, height); |
| 162 |
for(int i = 0; i < height; i++){ |
| •163 |
grid.setRow(i, new StringBuilder(getRow(y + i).subSequence(x, x + width))); |
| 164 |
} |
| 165 |
return grid; |
| 166 |
} |
| 167 |
|
| 168 |
public TextGrid getTestingSubGrid(Cell cell){ |
| 169 |
return getSubGrid(cell.x - 1, cell.y - 1, 3, 3); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
public String getStringAt(int x, int y, int length){ |
| 174 |
return getStringAt(new Cell(x, y), length); |
| 175 |
} |
| 176 |
|
| 177 |
public String getStringAt(Cell cell, int length){ |
| 178 |
int x = cell.x; |
| 179 |
int y = cell.y; |
| 180 |
if(x > getWidth() - 1 |
| 181 |
|| y > getHeight() - 1 |
| 182 |
|| x < 0 |
| 183 |
|| y < 0) return null; |
| 184 |
return rows.get(y).substring(x, x + length); |
| 185 |
} |
| 186 |
|
| 187 |
public char getNorthOf(int x, int y){ return get(x, y - 1); } |
| 188 |
public char getSouthOf(int x, int y){ return get(x, y + 1); } |
| 189 |
public char getEastOf(int x, int y){ return get(x + 1, y); } |
| 190 |
public char getWestOf(int x, int y){ return get(x - 1, y); } |
| 191 |
|
| 192 |
public char getNorthOf(Cell cell){ return getNorthOf(cell.x, cell.y); } |
| 193 |
public char getSouthOf(Cell cell){ return getSouthOf(cell.x, cell.y); } |
| 194 |
public char getEastOf(Cell cell){ return getEastOf(cell.x, cell.y); } |
| 195 |
public char getWestOf(Cell cell){ return getWestOf(cell.x, cell.y); } |
| 196 |
|
| 197 |
public void writeStringTo(int x, int y, String str){ |
| 198 |
writeStringTo(new Cell(x, y), str); |
| 199 |
} |
| 200 |
|
| 201 |
public void writeStringTo(Cell cell, String str){ |
| 202 |
if(isOutOfBounds(cell)) return; |
| 203 |
rows.get(cell.y).replace(cell.x, cell.x + str.length(), str); |
| 204 |
} |
| 205 |
|
| 206 |
public void set(Cell cell, char c){ |
| 207 |
set(cell.x, cell.y, c); |
| 208 |
} |
| 209 |
|
| 210 |
public void set(int x, int y, char c){ |
| 211 |
if(x > getWidth() - 1 || y > getHeight() - 1) return; |
| •212 |
StringBuilder row = rows.get(y); |
| 213 |
row.setCharAt(x, c); |
| 214 |
} |
| 215 |
|
| 216 |
public void setRow(int y, String row){ |
| 217 |
if(y > getHeight() || row.length() != getWidth()) |
| 218 |
throw new IllegalArgumentException("setRow out of bounds or string wrong size"); |
| •219 |
rows.set(y, new StringBuilder(row)); |
| 220 |
} |
| 221 |
|
| •222 |
public void setRow(int y, StringBuilder row){ |
| 223 |
if(y > getHeight() || row.length() != getWidth()) |
| 224 |
throw new IllegalArgumentException("setRow out of bounds or string wrong size"); |
| 225 |
rows.set(y, row); |
| 226 |
} |
| 227 |
|
| 228 |
public int getWidth(){ |
| 229 |
if(rows.size() == 0) return 0; |
| 230 |
return rows.get(0).length(); |
| 231 |
} |
| 232 |
|
| 233 |
public int getHeight(){ |
| 234 |
return rows.size(); |
| 235 |
} |
| 236 |
|
| 237 |
public void printDebug(){ |
| •238 |
Iterator<StringBuilder> it = rows.iterator(); |
| 239 |
int i = 0; |
| 240 |
System.out.println( |
| 241 |
" " |
| 242 |
+StringUtils.repeatString("0123456789", (int) Math.floor(getWidth()/10)+1)); |
| 243 |
while(it.hasNext()){ |
| 244 |
String row = it.next().toString(); |
| 245 |
String index = new Integer(i).toString(); |
| 246 |
if(i < 10) index = " "+index; |
| 247 |
System.out.println(index+" ("+row+")"); |
| 248 |
i++; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
public String getDebugString(){ |
| •253 |
StringBuilder buffer = new StringBuilder(); |
| 254 |
Iterator<StringBuilder> it = rows.iterator(); |
| 255 |
int i = 0; |
| 256 |
buffer.append( |
| 257 |
" " |
| 258 |
+StringUtils.repeatString("0123456789", (int) Math.floor(getWidth()/10)+1)+"\n"); |
| 259 |
while(it.hasNext()){ |
| 260 |
String row = it.next().toString(); |
| 261 |
String index = new Integer(i).toString(); |
| 262 |
if(i < 10) index = " "+index; |
| 263 |
row = row.replaceAll("\n", "\\\\n"); |
| 264 |
row = row.replaceAll("\r", "\\\\r"); |
| 265 |
buffer.append(index+" ("+row+")\n"); |
| 266 |
i++; |
| 267 |
} |
| 268 |
return buffer.toString(); |
| 269 |
} |
| 270 |
|
| 271 |
public String toString(){ |
| 272 |
return getDebugString(); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
public boolean add(TextGrid grid){ |
| 284 |
if(getWidth() != grid.getWidth() |
| 285 |
|| getHeight() != grid.getHeight()) return false; |
| 286 |
int width = getWidth(); |
| 287 |
int height = getHeight(); |
| 288 |
for(int yi = 0; yi < height; yi++){ |
| 289 |
for(int xi = 0; xi < width; xi++){ |
| 290 |
if(get(xi, yi) == ' ') set(xi, yi, grid.get(xi, yi)); |
| 291 |
} |
| 292 |
} |
| 293 |
return true; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
public void replaceTypeOnLine(){ |
| 303 |
int width = getWidth(); |
| 304 |
int height = getHeight(); |
| 305 |
for(int yi = 0; yi < height; yi++){ |
| 306 |
for(int xi = 0; xi < width; xi++){ |
| 307 |
char c = get(xi, yi); |
| 308 |
if(Character.isLetterOrDigit(c)) { |
| 309 |
boolean isOnHorizontalLine = isOnHorizontalLine(xi, yi); |
| 310 |
boolean isOnVerticalLine = isOnVerticalLine(xi, yi); |
| 311 |
if(isOnHorizontalLine && isOnVerticalLine){ |
| 312 |
set(xi, yi, '+'); |
| 313 |
if(DEBUG) System.out.println("replaced type on line '"+c+"' with +"); |
| 314 |
} else if(isOnHorizontalLine){ |
| 315 |
set(xi, yi, '-'); |
| 316 |
if(DEBUG) System.out.println("replaced type on line '"+c+"' with -"); |
| 317 |
} else if(isOnVerticalLine){ |
| 318 |
set(xi, yi, '|'); |
| 319 |
if(DEBUG) System.out.println("replaced type on line '"+c+"' with |"); |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
public void replacePointMarkersOnLine(){ |
| 327 |
int width = getWidth(); |
| 328 |
int height = getHeight(); |
| 329 |
for(int yi = 0; yi < height; yi++){ |
| 330 |
for(int xi = 0; xi < width; xi++){ |
| 331 |
char c = get(xi, yi); |
| 332 |
Cell cell = new Cell(xi, yi); |
| 333 |
if(StringUtils.isOneOf(c, pointMarkers) |
| 334 |
&& isStarOnLine(cell)){ |
| 335 |
|
| 336 |
boolean isOnHorizontalLine = false; |
| 337 |
if(StringUtils.isOneOf(get(cell.getEast()), horizontalLines)) |
| 338 |
isOnHorizontalLine = true; |
| 339 |
if(StringUtils.isOneOf(get(cell.getWest()), horizontalLines)) |
| 340 |
isOnHorizontalLine = true; |
| 341 |
|
| 342 |
boolean isOnVerticalLine = false; |
| 343 |
if(StringUtils.isOneOf(get(cell.getNorth()), verticalLines)) |
| 344 |
isOnVerticalLine = true; |
| 345 |
if(StringUtils.isOneOf(get(cell.getSouth()), verticalLines)) |
| 346 |
isOnVerticalLine = true; |
| 347 |
|
| 348 |
if(isOnHorizontalLine && isOnVerticalLine){ |
| 349 |
set(xi, yi, '+'); |
| 350 |
if(DEBUG) System.out.println("replaced marker on line '"+c+"' with +"); |
| 351 |
} else if(isOnHorizontalLine){ |
| 352 |
set(xi, yi, '-'); |
| 353 |
if(DEBUG) System.out.println("replaced marker on line '"+c+"' with -"); |
| 354 |
} else if(isOnVerticalLine){ |
| 355 |
set(xi, yi, '|'); |
| 356 |
if(DEBUG) System.out.println("replaced marker on line '"+c+"' with |"); |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
public CellSet getPointMarkersOnLine(){ |
| 364 |
CellSet result = new CellSet(); |
| 365 |
int width = getWidth(); |
| 366 |
int height = getHeight(); |
| 367 |
for(int yi = 0; yi < height; yi++){ |
| 368 |
for(int xi = 0; xi < width; xi++){ |
| 369 |
char c = get(xi, yi); |
| 370 |
if(StringUtils.isOneOf(c, pointMarkers) |
| 371 |
&& isStarOnLine(new Cell(xi, yi))){ |
| 372 |
result.add(new Cell(xi, yi)); |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
return result; |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
public void replaceHumanColorCodes(){ |
| 381 |
int height = getHeight(); |
| 382 |
for(int y = 0; y < height; y++){ |
| 383 |
String row = rows.get(y).toString(); |
| 384 |
Iterator it = humanColorCodes.keySet().iterator(); |
| 385 |
while(it.hasNext()){ |
| 386 |
String humanCode = (String) it.next(); |
| 387 |
String hexCode = (String) humanColorCodes.get(humanCode); |
| 388 |
if(hexCode != null){ |
| 389 |
humanCode = "c" + humanCode; |
| 390 |
hexCode = "c" + hexCode; |
| 391 |
row = row.replaceAll(humanCode, hexCode); |
| •392 |
rows.set(y, new StringBuilder(row)); |
| 393 |
row = rows.get(y).toString(); |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| •401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
public void replaceAll(char c1, char c2){ |
| 407 |
int width = getWidth(); |
| 408 |
int height = getHeight(); |
| 409 |
for(int yi = 0; yi < height; yi++){ |
| 410 |
for(int xi = 0; xi < width; xi++){ |
| 411 |
char c = get(xi, yi); |
| 412 |
if(c == c1) set(xi, yi, c2); |
| 413 |
} |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
public boolean hasBlankCells(){ |
| 418 |
CellSet set = new CellSet(); |
| 419 |
int width = getWidth(); |
| 420 |
int height = getHeight(); |
| 421 |
for(int y = 0; y < height; y++){ |
| 422 |
for(int x = 0; x < width; x++){ |
| 423 |
Cell cell = new Cell(x, y); |
| 424 |
if(isBlank(cell)) return true; |
| 425 |
} |
| 426 |
} |
| 427 |
return false; |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
public CellSet getAllNonBlank(){ |
| 432 |
CellSet set = new CellSet(); |
| 433 |
int width = getWidth(); |
| 434 |
int height = getHeight(); |
| 435 |
for(int y = 0; y < height; y++){ |
| 436 |
for(int x = 0; x < width; x++){ |
| 437 |
Cell cell = new Cell(x, y); |
| 438 |
if(!isBlank(cell)) set.add(cell); |
| 439 |
} |
| 440 |
} |
| 441 |
return set; |
| 442 |
} |
| 443 |
|
| 444 |
public CellSet getAllBoundaries(){ |
| 445 |
CellSet set = new CellSet(); |
| 446 |
int width = getWidth(); |
| 447 |
int height = getHeight(); |
| 448 |
for(int y = 0; y < height; y++){ |
| 449 |
for(int x = 0; x < width; x++){ |
| 450 |
Cell cell = new Cell(x, y); |
| 451 |
if(isBoundary(cell)) set.add(cell); |
| 452 |
} |
| 453 |
} |
| 454 |
return set; |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
public CellSet getAllBlanksBetweenCharacters(){ |
| 459 |
CellSet set = new CellSet(); |
| 460 |
int width = getWidth(); |
| 461 |
int height = getHeight(); |
| 462 |
for(int y = 0; y < height; y++){ |
| 463 |
for(int x = 0; x < width; x++){ |
| 464 |
Cell cell = new Cell(x, y); |
| 465 |
if(isBlankBetweenCharacters(cell)) set.add(cell); |
| 466 |
} |
| 467 |
} |
| 468 |
return set; |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
public ArrayList<CellStringPair> findStrings(){ |
| 482 |
ArrayList<CellStringPair> result = new ArrayList<CellStringPair>(); |
| 483 |
int width = getWidth(); |
| 484 |
int height = getHeight(); |
| 485 |
for(int y = 0; y < height; y++){ |
| 486 |
for(int x = 0; x < width; x++){ |
| 487 |
if(!isBlank(x, y)){ |
| 488 |
Cell start = new Cell(x, y); |
| 489 |
String str = String.valueOf(get(x,y)); |
| 490 |
char c = get(++x, y); |
| 491 |
boolean finished = false; |
| 492 |
|
| 493 |
while(!finished){ |
| 494 |
str += String.valueOf(c); |
| 495 |
c = get(++x, y); |
| 496 |
char next = get(x + 1, y); |
| 497 |
if((c == ' ' || c == 0) && (next == ' ' || next == 0)) |
| 498 |
finished = true; |
| 499 |
} |
| 500 |
result.add(new CellStringPair(start, str)); |
| 501 |
} |
| 502 |
} |
| 503 |
} |
| 504 |
return result; |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
public boolean hasEntryPoint(Cell cell, int entryPointId){ |
| 516 |
String result = ""; |
| 517 |
char c = get(cell); |
| 518 |
if(entryPointId == 1) { |
| 519 |
return StringUtils.isOneOf(c, entryPoints1); |
| 520 |
|
| 521 |
} else if(entryPointId == 2) { |
| 522 |
return StringUtils.isOneOf(c, entryPoints2); |
| 523 |
|
| 524 |
} else if(entryPointId == 3) { |
| 525 |
return StringUtils.isOneOf(c, entryPoints3); |
| 526 |
|
| 527 |
} else if(entryPointId == 4) { |
| 528 |
return StringUtils.isOneOf(c, entryPoints4); |
| 529 |
|
| 530 |
} else if(entryPointId == 5) { |
| 531 |
return StringUtils.isOneOf(c, entryPoints5); |
| 532 |
|
| 533 |
} else if(entryPointId == 6) { |
| 534 |
return StringUtils.isOneOf(c, entryPoints6); |
| 535 |
|
| 536 |
} else if(entryPointId == 7) { |
| 537 |
return StringUtils.isOneOf(c, entryPoints7); |
| 538 |
|
| 539 |
} else if(entryPointId == 8) { |
| 540 |
return StringUtils.isOneOf(c, entryPoints8); |
| 541 |
} |
| 542 |
return false; |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
public boolean isBlankBetweenCharacters(Cell cell){ |
| 553 |
return (isBlank(cell) |
| 554 |
&& !isBlank(cell.getEast()) |
| 555 |
&& !isBlank(cell.getWest())); |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
public void removeNonText(){ |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
removeArrowheads(); |
| 567 |
removeColorCodes(); |
| 568 |
removeBoundaries(); |
| 569 |
removeMarkupTags(); |
| 570 |
} |
| 571 |
|
| 572 |
public void removeArrowheads(){ |
| 573 |
int width = getWidth(); |
| 574 |
int height = getHeight(); |
| 575 |
for(int yi = 0; yi < height; yi++){ |
| 576 |
for(int xi = 0; xi < width; xi++){ |
| 577 |
Cell cell = new Cell(xi, yi); |
| 578 |
if(isArrowhead(cell)) set(cell, ' '); |
| 579 |
} |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
public void removeColorCodes(){ |
| 584 |
Iterator cells = findColorCodes().iterator(); |
| 585 |
while(cells.hasNext()){ |
| 586 |
Cell cell = ((CellColorPair) cells.next()).cell; |
| 587 |
set(cell, ' '); |
| 588 |
cell = cell.getEast(); set(cell, ' '); |
| 589 |
cell = cell.getEast(); set(cell, ' '); |
| 590 |
cell = cell.getEast(); set(cell, ' '); |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
public void removeBoundaries(){ |
| 595 |
ArrayList toBeRemoved = new ArrayList(); |
| 596 |
|
| 597 |
int width = getWidth(); |
| 598 |
int height = getHeight(); |
| 599 |
for(int yi = 0; yi < height; yi++){ |
| 600 |
for(int xi = 0; xi < width; xi++){ |
| 601 |
Cell cell = new Cell(xi, yi); |
| 602 |
if(isBoundary(cell)) toBeRemoved.add(cell); |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
|
| •607 |
|
| 608 |
|
| 609 |
Iterator it = toBeRemoved.iterator(); |
| 610 |
while(it.hasNext()){ |
| 611 |
Cell cell = (Cell) it.next(); |
| 612 |
set(cell, ' '); |
| 613 |
} |
| 614 |
} |
| 615 |
|
| •616 |
public ArrayList<Cell> findArrowheads(){ |
| 617 |
ArrayList<Cell> result = new ArrayList<Cell>(); |
| 618 |
int width = getWidth(); |
| 619 |
int height = getHeight(); |
| 620 |
for(int yi = 0; yi < height; yi++){ |
| 621 |
for(int xi = 0; xi < width; xi++){ |
| 622 |
Cell cell = new Cell(xi, yi); |
| 623 |
if(isArrowhead(cell)) result.add(cell); |
| 624 |
} |
| 625 |
} |
| 626 |
if(DEBUG) System.out.println(result.size()+" arrowheads found"); |
| 627 |
return result; |
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
public ArrayList<CellColorPair> findColorCodes(){ |
| 632 |
Pattern colorCodePattern = Pattern.compile("c[A-F0-9]{3}"); |
| 633 |
ArrayList<CellColorPair> result = new ArrayList<CellColorPair>(); |
| 634 |
int width = getWidth(); |
| 635 |
int height = getHeight(); |
| 636 |
for(int yi = 0; yi < height; yi++){ |
| 637 |
for(int xi = 0; xi < width - 3; xi++){ |
| 638 |
Cell cell = new Cell(xi, yi); |
| 639 |
String s = getStringAt(cell, 4); |
| 640 |
Matcher matcher = colorCodePattern.matcher(s); |
| 641 |
if(matcher.matches()){ |
| 642 |
char cR = s.charAt(1); |
| 643 |
char cG = s.charAt(2); |
| 644 |
char cB = s.charAt(3); |
| 645 |
int r = Integer.valueOf(String.valueOf(cR), 16).intValue() * 17; |
| 646 |
int g = Integer.valueOf(String.valueOf(cG), 16).intValue() * 17; |
| 647 |
int b = Integer.valueOf(String.valueOf(cB), 16).intValue() * 17; |
| 648 |
result.add(new CellColorPair(cell, new Color(r, g, b))); |
| 649 |
} |
| 650 |
} |
| 651 |
} |
| 652 |
if(DEBUG) System.out.println(result.size()+" color codes found"); |
| 653 |
return result; |
| 654 |
} |
| 655 |
|
| 656 |
public ArrayList<CellTagPair> findMarkupTags(){ |
| 657 |
Pattern tagPattern = Pattern.compile("\\{(.+?)\\}"); |
| 658 |
ArrayList<CellTagPair> result = new ArrayList<CellTagPair>(); |
| 659 |
|
| 660 |
int width = getWidth(); |
| 661 |
int height = getHeight(); |
| 662 |
for(int y = 0; y < height; y++){ |
| 663 |
for(int x = 0; x < width - 3; x++){ |
| 664 |
Cell cell = new Cell(x, y); |
| 665 |
char c = get(cell); |
| 666 |
if(c == '{'){ |
| 667 |
String rowPart = rows.get(y).substring(x); |
| 668 |
Matcher matcher = tagPattern.matcher(rowPart); |
| 669 |
if(matcher.find()){ |
| 670 |
String tagName = matcher.group(1); |
| 671 |
if(markupTags.contains(tagName)){ |
| 672 |
if(DEBUG) System.out.println("found tag "+tagName+" at "+x+", "+y); |
| 673 |
result.add(new CellTagPair(new Cell(x, y), tagName)); |
| 674 |
} |
| 675 |
} |
| 676 |
} |
| 677 |
} |
| 678 |
} |
| 679 |
return result; |
| 680 |
} |
| 681 |
|
| 682 |
public void removeMarkupTags(){ |
| 683 |
Iterator it = findMarkupTags().iterator(); |
| 684 |
while (it.hasNext()) { |
| 685 |
CellTagPair pair = (CellTagPair) it.next(); |
| 686 |
String tagName = pair.tag; |
| 687 |
if(tagName == null) continue; |
| 688 |
int length = 2 + tagName.length(); |
| 689 |
writeStringTo(pair.cell, StringUtils.repeatString(" ", length)); |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
public boolean matchesAny(GridPatternGroup criteria){ |
| 696 |
return criteria.isAnyMatchedBy(this); |
| 697 |
} |
| 698 |
|
| 699 |
public boolean matchesAll(GridPatternGroup criteria){ |
| 700 |
return criteria.areAllMatchedBy(this); |
| 701 |
} |
| 702 |
|
| 703 |
public boolean matches(GridPattern criteria){ |
| 704 |
return criteria.isMatchedBy(this); |
| 705 |
} |
| 706 |
|
| 707 |
|
| 708 |
public boolean isOnHorizontalLine(Cell cell){ return isOnHorizontalLine(cell.x, cell.y); } |
| 709 |
private boolean isOnHorizontalLine(int x, int y){ |
| 710 |
char c1 = get(x - 1, y); |
| 711 |
char c2 = get(x + 1, y); |
| 712 |
if(isHorizontalLine(c1) && isHorizontalLine(c2)) return true; |
| 713 |
return false; |
| 714 |
} |
| 715 |
|
| 716 |
public boolean isOnVerticalLine(Cell cell){ return isOnVerticalLine(cell.x, cell.y); } |
| 717 |
private boolean isOnVerticalLine(int x, int y){ |
| 718 |
char c1 = get(x, y - 1); |
| 719 |
char c2 = get(x, y + 1); |
| 720 |
if(isVerticalLine(c1) && isVerticalLine(c2)) return true; |
| 721 |
return false; |
| 722 |
} |
| 723 |
|
| 724 |
|
| 725 |
public static boolean isBoundary(char c){ |
| 726 |
return StringUtils.isOneOf(c, boundaries); |
| 727 |
} |
| 728 |
public boolean isBoundary(int x, int y){ return isBoundary(new Cell(x, y)); } |
| 729 |
public boolean isBoundary(Cell cell){ |
| 730 |
char c = get(cell.x, cell.y); |
| 731 |
if(0 == c) return false; |
| 732 |
if('+' == c || '\\' == c || '/' == c){ |
| •733 |
System.out.print(""); |
| 734 |
if( |
| 735 |
isIntersection(cell) |
| 736 |
|| isCorner(cell) |
| 737 |
|| isStub(cell) |
| 738 |
|| isCrossOnLine(cell)){ |
| 739 |
return true; |
| 740 |
} else return false; |
| 741 |
} |
| 742 |
|
| 743 |
if(StringUtils.isOneOf(c, boundaries) && !isLoneDiagonal(cell)){ |
| 744 |
return true; |
| 745 |
} |
| 746 |
return false; |
| 747 |
} |
| 748 |
|
| 749 |
public boolean isLine(Cell cell){ |
| 750 |
return isHorizontalLine(cell) || isVerticalLine(cell); |
| 751 |
} |
| 752 |
|
| 753 |
public static boolean isHorizontalLine(char c){ |
| 754 |
return StringUtils.isOneOf(c, horizontalLines); |
| 755 |
} |
| 756 |
public boolean isHorizontalLine(Cell cell){ return isHorizontalLine(cell.x, cell.y); } |
| 757 |
public boolean isHorizontalLine(int x, int y){ |
| 758 |
char c = get(x, y); |
| 759 |
if(0 == c) return false; |
| 760 |
return StringUtils.isOneOf(c, horizontalLines); |
| 761 |
} |
| 762 |
|
| 763 |
public static boolean isVerticalLine(char c){ |
| 764 |
return StringUtils.isOneOf(c, verticalLines); |
| 765 |
} |
| 766 |
public boolean isVerticalLine(Cell cell){ return isVerticalLine(cell.x, cell.y); } |
| 767 |
public boolean isVerticalLine(int x, int y){ |
| 768 |
char c = get(x, y); |
| 769 |
if(0 == c) return false; |
| 770 |
return StringUtils.isOneOf(c, verticalLines); |
| 771 |
} |
| 772 |
|
| 773 |
public boolean isLinesEnd(int x, int y){ |
| 774 |
return isLinesEnd(new Cell(x, y)); |
| 775 |
} |
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
public boolean isLinesEnd(Cell cell){ |
| 784 |
return matchesAny(cell, GridPatternGroup.linesEndCriteria); |
| 785 |
} |
| 786 |
|
| 787 |
public boolean isVerticalLinesEnd(Cell cell){ |
| 788 |
return matchesAny(cell, GridPatternGroup.verticalLinesEndCriteria); |
| 789 |
} |
| 790 |
|
| 791 |
public boolean isHorizontalLinesEnd(Cell cell){ |
| 792 |
return matchesAny(cell, GridPatternGroup.horizontalLinesEndCriteria); |
| 793 |
} |
| 794 |
|
| 795 |
|
| 796 |
public boolean isPointCell(Cell cell){ |
| 797 |
return ( |
| 798 |
isCorner(cell) |
| 799 |
|| isIntersection(cell) |
| 800 |
|| isStub(cell) |
| 801 |
|| isLinesEnd(cell)); |
| 802 |
} |
| 803 |
|
| 804 |
|
| 805 |
public boolean containsAtLeastOneDashedLine(CellSet set){ |
| 806 |
Iterator it = set.iterator(); |
| 807 |
while(it.hasNext()) { |
| 808 |
Cell cell = (Cell) it.next(); |
| 809 |
if(StringUtils.isOneOf(get(cell), dashedLines)) return true; |
| 810 |
} |
| 811 |
return false; |
| 812 |
} |
| 813 |
|
| 814 |
public boolean exactlyOneNeighbourIsBoundary(Cell cell) { |
| 815 |
int howMany = 0; |
| 816 |
if(isBoundary(cell.getNorth())) howMany++; |
| 817 |
if(isBoundary(cell.getSouth())) howMany++; |
| 818 |
if(isBoundary(cell.getEast())) howMany++; |
| 819 |
if(isBoundary(cell.getWest())) howMany++; |
| 820 |
return (howMany == 1); |
| 821 |
} |
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
public boolean isStub(Cell cell){ |
| 839 |
return matchesAny(cell, GridPatternGroup.stubCriteria); |
| 840 |
} |
| 841 |
|
| 842 |
public boolean isCrossOnLine(Cell cell){ |
| 843 |
return matchesAny(cell, GridPatternGroup.crossOnLineCriteria); |
| 844 |
} |
| 845 |
|
| 846 |
public boolean isHorizontalCrossOnLine(Cell cell){ |
| 847 |
return matchesAny(cell, GridPatternGroup.horizontalCrossOnLineCriteria); |
| 848 |
} |
| 849 |
|
| 850 |
public boolean isVerticalCrossOnLine(Cell cell){ |
| 851 |
return matchesAny(cell, GridPatternGroup.verticalCrossOnLineCriteria); |
| 852 |
} |
| 853 |
|
| 854 |
public boolean isStarOnLine(Cell cell){ |
| 855 |
return matchesAny(cell, GridPatternGroup.starOnLineCriteria); |
| 856 |
} |
| 857 |
|
| 858 |
public boolean isLoneDiagonal(Cell cell){ |
| 859 |
return matchesAny(cell, GridPatternGroup.loneDiagonalCriteria); |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
public boolean isHorizontalStarOnLine(Cell cell){ |
| 864 |
return matchesAny(cell, GridPatternGroup.horizontalStarOnLineCriteria); |
| 865 |
} |
| 866 |
|
| 867 |
public boolean isVerticalStarOnLine(Cell cell){ |
| 868 |
return matchesAny(cell, GridPatternGroup.verticalStarOnLineCriteria); |
| 869 |
} |
| 870 |
|
| 871 |
public boolean isArrowhead(Cell cell){ |
| 872 |
return (isNorthArrowhead(cell) |
| 873 |
|| isSouthArrowhead(cell) |
| 874 |
|| isWestArrowhead(cell) |
| 875 |
|| isEastArrowhead(cell)); |
| 876 |
} |
| 877 |
|
| 878 |
public boolean isNorthArrowhead(Cell cell){ |
| 879 |
return get(cell) == '^'; |
| 880 |
} |
| 881 |
|
| 882 |
public boolean isEastArrowhead(Cell cell){ |
| 883 |
return get(cell) == '>'; |
| 884 |
} |
| 885 |
|
| 886 |
public boolean isWestArrowhead(Cell cell){ |
| 887 |
return get(cell) == '<'; |
| 888 |
} |
| 889 |
|
| 890 |
public boolean isSouthArrowhead(Cell cell){ |
| 891 |
return (get(cell) == 'v' || get(cell) == 'V') |
| 892 |
&& isVerticalLine(cell.getNorth()); |
| 893 |
} |
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
public boolean isBullet(int x, int y){ |
| 908 |
return isBullet(new Cell(x, y)); |
| 909 |
} |
| 910 |
|
| 911 |
public boolean isBullet(Cell cell){ |
| 912 |
char c = get(cell); |
| 913 |
if((c == 'o' || c == '*') |
| 914 |
&& isBlank(cell.getEast()) |
| 915 |
&& isBlank(cell.getWest()) |
| 916 |
&& Character.isLetterOrDigit(get(cell.getEast().getEast())) ) |
| 917 |
return true; |
| 918 |
return false; |
| 919 |
} |
| 920 |
|
| 921 |
public void replaceBullets(){ |
| 922 |
int width = getWidth(); |
| 923 |
int height = getHeight(); |
| 924 |
for(int yi = 0; yi < height; yi++){ |
| 925 |
for(int xi = 0; xi < width; xi++){ |
| 926 |
Cell cell = new Cell(xi, yi); |
| 927 |
if(isBullet(cell)){ |
| 928 |
set(cell, ' '); |
| 929 |
set(cell.getEast(), '\u2022'); |
| 930 |
} |
| 931 |
} |
| 932 |
} |
| 933 |
} |
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
public boolean isStringsStart(Cell cell){ |
| 943 |
return (!isBlank(cell) && isBlank(cell.getWest())); |
| 944 |
} |
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
public boolean isStringsEnd(Cell cell){ |
| 954 |
return (!isBlank(cell) |
| 955 |
|
| 956 |
&& isBlank(cell.getEast())); |
| 957 |
} |
| 958 |
|
| 959 |
public int otherStringsStartInTheSameColumn(Cell cell){ |
| 960 |
if(!isStringsStart(cell)) return 0; |
| 961 |
int result = 0; |
| 962 |
int height = getHeight(); |
| 963 |
for(int y = 0; y < height; y++){ |
| 964 |
Cell cCell = new Cell(cell.x, y); |
| 965 |
if(!cCell.equals(cell) && isStringsStart(cCell)){ |
| 966 |
result++; |
| 967 |
} |
| 968 |
} |
| 969 |
return result; |
| 970 |
} |
| 971 |
|
| 972 |
public int otherStringsEndInTheSameColumn(Cell cell){ |
| 973 |
if(!isStringsEnd(cell)) return 0; |
| 974 |
int result = 0; |
| 975 |
int height = getHeight(); |
| 976 |
for(int y = 0; y < height; y++){ |
| 977 |
Cell cCell = new Cell(cell.x, y); |
| 978 |
if(!cCell.equals(cell) && isStringsEnd(cCell)){ |
| 979 |
result++; |
| 980 |
} |
| 981 |
} |
| 982 |
return result; |
| 983 |
} |
| 984 |
|
| 985 |
public boolean isColumnBlank(int x){ |
| 986 |
int height = getHeight(); |
| 987 |
for(int y = 0; y < height; y++){ |
| 988 |
if(!isBlank(x, y)) return false; |
| 989 |
} |
| 990 |
return true; |
| 991 |
} |
| 992 |
|
| 993 |
|
| 994 |
public CellSet followLine(int x, int y){ |
| 995 |
return followLine(new Cell(x, y)); |
| 996 |
} |
| 997 |
|
| 998 |
public CellSet followIntersection(Cell cell){ |
| 999 |
return followIntersection(cell, null); |
| 1000 |
} |
| 1001 |
|
| 1002 |
public CellSet followIntersection(Cell cell, Cell blocked){ |
| 1003 |
if(!isIntersection(cell)) return null; |
| 1004 |
CellSet result = new CellSet(); |
| 1005 |
Cell cN = cell.getNorth(); |
| 1006 |
Cell cS = cell.getSouth(); |
| 1007 |
Cell cE = cell.getEast(); |
| 1008 |
Cell cW = cell.getWest(); |
| 1009 |
if(hasEntryPoint(cN, 6)) result.add(cN); |
| 1010 |
if(hasEntryPoint(cS, 2)) result.add(cS); |
| 1011 |
if(hasEntryPoint(cE, 8)) result.add(cE); |
| 1012 |
if(hasEntryPoint(cW, 4)) result.add(cW); |
| 1013 |
if(result.contains(blocked)) result.remove(blocked); |
| 1014 |
return result; |
| 1015 |
} |
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
public CellSet followLine(Cell cell){ |
| 1025 |
if(isHorizontalLine(cell)){ |
| 1026 |
CellSet result = new CellSet(); |
| 1027 |
if(isBoundary(cell.getEast())) result.add(cell.getEast()); |
| 1028 |
if(isBoundary(cell.getWest())) result.add(cell.getWest()); |
| 1029 |
return result; |
| 1030 |
} else if (isVerticalLine(cell)){ |
| 1031 |
CellSet result = new CellSet(); |
| 1032 |
if(isBoundary(cell.getNorth())) result.add(cell.getNorth()); |
| 1033 |
if(isBoundary(cell.getSouth())) result.add(cell.getSouth()); |
| 1034 |
return result; |
| 1035 |
} |
| 1036 |
return null; |
| 1037 |
} |
| 1038 |
|
| 1039 |
public CellSet followLine(Cell cell, Cell blocked){ |
| 1040 |
CellSet nextCells = followLine(cell); |
| 1041 |
if(nextCells.contains(blocked)) nextCells.remove(blocked); |
| 1042 |
return nextCells; |
| 1043 |
} |
| 1044 |
|
| 1045 |
public CellSet followCorner(Cell cell){ |
| 1046 |
return followCorner(cell, null); |
| 1047 |
} |
| 1048 |
|
| 1049 |
public CellSet followCorner(Cell cell, Cell blocked){ |
| 1050 |
if(!isCorner(cell)) return null; |
| 1051 |
if(isCorner1(cell)) return followCorner1(cell, blocked); |
| 1052 |
if(isCorner2(cell)) return followCorner2(cell, blocked); |
| 1053 |
if(isCorner3(cell)) return followCorner3(cell, blocked); |
| 1054 |
if(isCorner4(cell)) return followCorner4(cell, blocked); |
| 1055 |
return null; |
| 1056 |
} |
| 1057 |
|
| 1058 |
public CellSet followCorner1(Cell cell){ |
| 1059 |
return followCorner1(cell, null); |
| 1060 |
} |
| 1061 |
public CellSet followCorner1(Cell cell, Cell blocked){ |
| 1062 |
if(!isCorner1(cell)) return null; |
| 1063 |
CellSet result = new CellSet(); |
| 1064 |
if(!cell.getSouth().equals(blocked)) result.add(cell.getSouth()); |
| 1065 |
if(!cell.getEast().equals(blocked)) result.add(cell.getEast()); |
| 1066 |
return result; |
| 1067 |
} |
| 1068 |
|
| 1069 |
public CellSet followCorner2(Cell cell){ |
| 1070 |
return followCorner2(cell, null); |
| 1071 |
} |
| 1072 |
public CellSet followCorner2(Cell cell, Cell blocked){ |
| 1073 |
if(!isCorner2(cell)) return null; |
| 1074 |
CellSet result = new CellSet(); |
| 1075 |
if(!cell.getSouth().equals(blocked)) result.add(cell.getSouth()); |
| 1076 |
if(!cell.getWest().equals(blocked)) result.add(cell.getWest()); |
| 1077 |
return result; |
| 1078 |
} |
| 1079 |
|
| 1080 |
public CellSet followCorner3(Cell cell){ |
| 1081 |
return followCorner3(cell, null); |
| 1082 |
} |
| 1083 |
public CellSet followCorner3(Cell cell, Cell blocked){ |
| 1084 |
if(!isCorner3(cell)) return null; |
| 1085 |
CellSet result = new CellSet(); |
| 1086 |
if(!cell.getNorth().equals(blocked)) result.add(cell.getNorth()); |
| 1087 |
if(!cell.getWest().equals(blocked)) result.add(cell.getWest()); |
| 1088 |
return result; |
| 1089 |
} |
| 1090 |
|
| 1091 |
public CellSet followCorner4(Cell cell){ |
| 1092 |
return followCorner4(cell, null); |
| 1093 |
} |
| 1094 |
public CellSet followCorner4(Cell cell, Cell blocked){ |
| 1095 |
if(!isCorner4(cell)) return null; |
| 1096 |
CellSet result = new CellSet(); |
| 1097 |
if(!cell.getNorth().equals(blocked)) result.add(cell.getNorth()); |
| 1098 |
if(!cell.getEast().equals(blocked)) result.add(cell.getEast()); |
| 1099 |
return result; |
| 1100 |
} |
| 1101 |
|
| 1102 |
|
| 1103 |
public CellSet followStub(Cell cell){ |
| 1104 |
return followStub(cell, null); |
| 1105 |
} |
| 1106 |
public CellSet followStub(Cell cell, Cell blocked){ |
| 1107 |
if(!isStub(cell)) return null; |
| 1108 |
CellSet result = new CellSet(); |
| 1109 |
if(isBoundary(cell.getEast())) result.add(cell.getEast()); |
| 1110 |
else if(isBoundary(cell.getWest())) result.add(cell.getWest()); |
| 1111 |
else if(isBoundary(cell.getNorth())) result.add(cell.getNorth()); |
| 1112 |
else if(isBoundary(cell.getSouth())) result.add(cell.getSouth()); |
| 1113 |
if(result.contains(blocked)) result.remove(blocked); |
| 1114 |
return result; |
| 1115 |
} |
| 1116 |
|
| 1117 |
public CellSet followCell(Cell cell){ |
| 1118 |
return followCell(cell, null); |
| 1119 |
} |
| 1120 |
|
| 1121 |
public CellSet followCell(Cell cell, Cell blocked){ |
| 1122 |
if(isIntersection(cell)) return followIntersection(cell, blocked); |
| 1123 |
if(isCorner(cell)) return followCorner(cell, blocked); |
| 1124 |
if(isLine(cell)) return followLine(cell, blocked); |
| 1125 |
if(isStub(cell)) return followStub(cell, blocked); |
| 1126 |
if(isCrossOnLine(cell)) return followCrossOnLine(cell, blocked); |
| •1127 |
System.err.println("Ambiguous input at position "+cell+":"); |
| 1128 |
TextGrid subGrid = getTestingSubGrid(cell); |
| 1129 |
subGrid.printDebug(); |
| 1130 |
throw new RuntimeException("Cannot follow cell "+cell+": cannot determine cell type"); |
| 1131 |
} |
| 1132 |
|
| 1133 |
public String getCellTypeAsString(Cell cell){ |
| 1134 |
if(isK(cell)) return "K"; |
| 1135 |
if(isT(cell)) return "T"; |
| 1136 |
if(isInverseK(cell)) return "inverse K"; |
| 1137 |
if(isInverseT(cell)) return "inverse T"; |
| 1138 |
if(isCorner1(cell)) return "corner 1"; |
| 1139 |
if(isCorner2(cell)) return "corner 2"; |
| 1140 |
if(isCorner3(cell)) return "corner 3"; |
| 1141 |
if(isCorner4(cell)) return "corner 4"; |
| 1142 |
if(isLine(cell)) return "line"; |
| 1143 |
if(isStub(cell)) return "stub"; |
| 1144 |
if(isCrossOnLine(cell)) return "crossOnLine"; |
| 1145 |
return "unrecognisable type"; |
| 1146 |
} |
| 1147 |
|
| 1148 |
|
| 1149 |
public CellSet followCrossOnLine(Cell cell, Cell blocked){ |
| 1150 |
CellSet result = new CellSet(); |
| 1151 |
if(isHorizontalCrossOnLine(cell)){ |
| 1152 |
result.add(cell.getEast()); |
| 1153 |
result.add(cell.getWest()); |
| 1154 |
} else if(isVerticalCrossOnLine(cell)){ |
| 1155 |
result.add(cell.getNorth()); |
| 1156 |
result.add(cell.getSouth()); |
| 1157 |
} |
| 1158 |
if(result.contains(blocked)) result.remove(blocked); |
| 1159 |
return result; |
| 1160 |
} |
| 1161 |
|
| 1162 |
public boolean isOutOfBounds(Cell cell){ |
| 1163 |
if(cell.x > getWidth() - 1 |
| 1164 |
|| cell.y > getHeight() - 1 |
| 1165 |
|| cell.x < 0 |
| 1166 |
|| cell.y < 0) return true; |
| 1167 |
return false; |
| 1168 |
} |
| 1169 |
|
| 1170 |
public boolean isOutOfBounds(int x, int y){ |
| 1171 |
char c = get(x, y); |
| 1172 |
if(0 == c) return true; |
| 1173 |
return false; |
| 1174 |
} |
| 1175 |
|
| 1176 |
public boolean isBlank(Cell cell){ |
| 1177 |
char c = get(cell); |
| 1178 |
if(0 == c) return false; |
| 1179 |
return c == ' '; |
| 1180 |
} |
| 1181 |
|
| 1182 |
public boolean isBlank(int x, int y){ |
| 1183 |
char c = get(x, y); |
| 1184 |
if(0 == c) return true; |
| 1185 |
return c == ' '; |
| 1186 |
} |
| 1187 |
|
| 1188 |
public boolean isCorner(Cell cell){ |
| 1189 |
return isCorner(cell.x, cell.y); |
| 1190 |
} |
| 1191 |
public boolean isCorner(int x, int y){ |
| 1192 |
return (isNormalCorner(x,y) || isRoundCorner(x,y)); |
| 1193 |
} |
| 1194 |
|
| 1195 |
|
| 1196 |
public boolean matchesAny(Cell cell, GridPatternGroup criteria){ |
| 1197 |
TextGrid subGrid = getTestingSubGrid(cell); |
| 1198 |
return subGrid.matchesAny(criteria); |
| 1199 |
} |
| 1200 |
|
| 1201 |
public boolean isCorner1(Cell cell){ |
| 1202 |
return matchesAny(cell, GridPatternGroup.corner1Criteria); |
| 1203 |
} |
| 1204 |
|
| 1205 |
public boolean isCorner2(Cell cell){ |
| 1206 |
return matchesAny(cell, GridPatternGroup.corner2Criteria); |
| 1207 |
} |
| 1208 |
|
| 1209 |
public boolean isCorner3(Cell cell){ |
| 1210 |
return matchesAny(cell, GridPatternGroup.corner3Criteria); |
| 1211 |
} |
| 1212 |
|
| 1213 |
public boolean isCorner4(Cell cell){ |
| 1214 |
return matchesAny(cell, GridPatternGroup.corner4Criteria); |
| 1215 |
} |
| 1216 |
|
| 1217 |
public boolean isCross(Cell cell){ |
| 1218 |
return matchesAny(cell, GridPatternGroup.crossCriteria); |
| 1219 |
} |
| 1220 |
|
| 1221 |
public boolean isK(Cell cell){ |
| 1222 |
return matchesAny(cell, GridPatternGroup.KCriteria); |
| 1223 |
} |
| 1224 |
|
| 1225 |
public boolean isInverseK(Cell cell){ |
| 1226 |
return matchesAny(cell, GridPatternGroup.inverseKCriteria); |
| 1227 |
} |
| 1228 |
|
| 1229 |
public boolean isT(Cell cell){ |
| 1230 |
return matchesAny(cell, GridPatternGroup.TCriteria); |
| 1231 |
} |
| 1232 |
|
| 1233 |
public boolean isInverseT(Cell cell){ |
| 1234 |
return matchesAny(cell, GridPatternGroup.inverseTCriteria); |
| 1235 |
} |
| 1236 |
|
| 1237 |
public boolean isNormalCorner(Cell cell){ |
| 1238 |
return matchesAny(cell, GridPatternGroup.normalCornerCriteria); |
| 1239 |
} |
| 1240 |
public boolean isNormalCorner(int x, int y){ |
| 1241 |
return isNormalCorner(new Cell(x, y)); |
| 1242 |
} |
| 1243 |
|
| 1244 |
public boolean isRoundCorner(Cell cell){ |
| 1245 |
return matchesAny(cell, GridPatternGroup.roundCornerCriteria); |
| 1246 |
} |
| 1247 |
|
| 1248 |
public boolean isRoundCorner(int x, int y){ |
| 1249 |
return isRoundCorner(new Cell(x, y)); |
| 1250 |
} |
| 1251 |
|
| 1252 |
public boolean isIntersection(Cell cell){ |
| 1253 |
return matchesAny(cell, GridPatternGroup.intersectionCriteria); |
| 1254 |
} |
| 1255 |
public boolean isIntersection(int x, int y){ |
| 1256 |
return isIntersection(new Cell(x, y)); |
| 1257 |
} |
| 1258 |
|
| 1259 |
public void copyCellsTo(CellSet cells, TextGrid grid){ |
| 1260 |
Iterator it = cells.iterator(); |
| 1261 |
while(it.hasNext()){ |
| 1262 |
Cell cell = (Cell) it.next(); |
| 1263 |
grid.set(cell, this.get(cell)); |
| 1264 |
} |
| 1265 |
} |
| 1266 |
|
| 1267 |
public boolean equals(TextGrid grid){ |
| 1268 |
if(grid.getHeight() != this.getHeight() |
| 1269 |
|| grid.getWidth() != this.getWidth() |
| 1270 |
){ |
| 1271 |
return false; |
| 1272 |
} |
| 1273 |
int height = grid.getHeight(); |
| 1274 |
for(int i = 0; i < height; i++){ |
| 1275 |
String row1 = this.getRow(i).toString(); |
| 1276 |
String row2 = grid.getRow(i).toString(); |
| 1277 |
if(!row1.equals(row2)) return false; |
| 1278 |
} |
| 1279 |
return true; |
| 1280 |
} |
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
public void fillCellsWith(Iterable cells, char c){ |
| 1289 |
Iterator<Cell> it = cells.iterator(); |
| 1290 |
while(it.hasNext()){ |
| 1291 |
Cell cell = it.next(); |
| 1292 |
set(cell.x, cell.y, c); |
| 1293 |
} |
| 1294 |
} |
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
public CellSet fillContinuousArea(int x, int y, char c){ |
| 1315 |
return fillContinuousArea(new Cell(x, y), c); |
| 1316 |
} |
| 1317 |
|
| 1318 |
public CellSet fillContinuousArea(Cell cell, char c){ |
| 1319 |
if(isOutOfBounds(cell)) throw new IllegalArgumentException("Attempted to fill area out of bounds: "+cell); |
| 1320 |
return seedFillOld(cell, c); |
| 1321 |
} |
| 1322 |
|
| 1323 |
private CellSet seedFill(Cell seed, char newChar){ |
| 1324 |
CellSet cellsFilled = new CellSet(); |
| 1325 |
char oldChar = get(seed); |
| 1326 |
|
| 1327 |
if(oldChar == newChar) return cellsFilled; |
| 1328 |
if(isOutOfBounds(seed)) return cellsFilled; |
| 1329 |
|
| 1330 |
Stack<Cell> stack = new Stack<Cell>(); |
| 1331 |
|
| 1332 |
stack.push(seed); |
| 1333 |
|
| 1334 |
while(!stack.isEmpty()){ |
| 1335 |
Cell cell = (Cell) stack.pop(); |
| 1336 |
|
| 1337 |
|
| 1338 |
cellsFilled.add(cell); |
| 1339 |
|
| 1340 |
Cell nCell = cell.getNorth(); |
| 1341 |
Cell sCell = cell.getSouth(); |
| 1342 |
Cell eCell = cell.getEast(); |
| 1343 |
Cell wCell = cell.getWest(); |
| 1344 |
|
| 1345 |
if(get(nCell) == oldChar && !cellsFilled.contains(nCell)) stack.push(nCell); |
| 1346 |
if(get(sCell) == oldChar && !cellsFilled.contains(sCell)) stack.push(sCell); |
| 1347 |
if(get(eCell) == oldChar && !cellsFilled.contains(eCell)) stack.push(eCell); |
| 1348 |
if(get(wCell) == oldChar && !cellsFilled.contains(wCell)) stack.push(wCell); |
| 1349 |
} |
| 1350 |
|
| 1351 |
return cellsFilled; |
| 1352 |
} |
| 1353 |
|
| 1354 |
private CellSet seedFillOld(Cell seed, char newChar){ |
| 1355 |
CellSet cellsFilled = new CellSet(); |
| 1356 |
char oldChar = get(seed); |
| 1357 |
|
| 1358 |
if(oldChar == newChar) return cellsFilled; |
| 1359 |
if(isOutOfBounds(seed)) return cellsFilled; |
| 1360 |
|
| 1361 |
Stack<Cell> stack = new Stack<Cell>(); |
| 1362 |
|
| 1363 |
stack.push(seed); |
| 1364 |
|
| 1365 |
while(!stack.isEmpty()){ |
| 1366 |
Cell cell = (Cell) stack.pop(); |
| 1367 |
|
| 1368 |
set(cell, newChar); |
| 1369 |
cellsFilled.add(cell); |
| 1370 |
|
| 1371 |
Cell nCell = cell.getNorth(); |
| 1372 |
Cell sCell = cell.getSouth(); |
| 1373 |
Cell eCell = cell.getEast(); |
| 1374 |
Cell wCell = cell.getWest(); |
| 1375 |
|
| 1376 |
if(get(nCell) == oldChar) stack.push(nCell); |
| 1377 |
if(get(sCell) == oldChar) stack.push(sCell); |
| 1378 |
if(get(eCell) == oldChar) stack.push(eCell); |
| 1379 |
if(get(wCell) == oldChar) stack.push(wCell); |
| 1380 |
} |
| 1381 |
|
| 1382 |
return cellsFilled; |
| 1383 |
} |
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
public CellSet findBoundariesExpandingFrom(Cell seed){ |
| 1395 |
CellSet boundaries = new CellSet(); |
| 1396 |
char oldChar = get(seed); |
| 1397 |
|
| 1398 |
if(isOutOfBounds(seed)) return boundaries; |
| 1399 |
|
| 1400 |
char newChar = 1; |
| 1401 |
|
| 1402 |
Stack<Cell> stack = new Stack<Cell>(); |
| 1403 |
|
| 1404 |
stack.push(seed); |
| 1405 |
|
| 1406 |
while(!stack.isEmpty()){ |
| 1407 |
Cell cell = (Cell) stack.pop(); |
| 1408 |
|
| 1409 |
set(cell, newChar); |
| 1410 |
|
| 1411 |
Cell nCell = cell.getNorth(); |
| 1412 |
Cell sCell = cell.getSouth(); |
| 1413 |
Cell eCell = cell.getEast(); |
| 1414 |
Cell wCell = cell.getWest(); |
| 1415 |
|
| 1416 |
if(get(nCell) == oldChar) stack.push(nCell); |
| 1417 |
else if(get(nCell) == '*') boundaries.add(nCell); |
| 1418 |
|
| 1419 |
if(get(sCell) == oldChar) stack.push(sCell); |
| 1420 |
else if(get(sCell) == '*') boundaries.add(sCell); |
| 1421 |
|
| 1422 |
if(get(eCell) == oldChar) stack.push(eCell); |
| 1423 |
else if(get(eCell) == '*') boundaries.add(eCell); |
| 1424 |
|
| 1425 |
if(get(wCell) == oldChar) stack.push(wCell); |
| 1426 |
else if(get(wCell) == '*') boundaries.add(wCell); |
| 1427 |
} |
| 1428 |
|
| 1429 |
return boundaries; |
| 1430 |
} |
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
private CellSet seedFillLine(Cell cell, char newChar){ |
| 1435 |
CellSet cellsFilled = new CellSet(); |
| 1436 |
|
| 1437 |
Stack stack = new Stack(); |
| 1438 |
|
| 1439 |
char oldChar = get(cell); |
| 1440 |
|
| 1441 |
if(oldChar == newChar) return cellsFilled; |
| 1442 |
if(isOutOfBounds(cell)) return cellsFilled; |
| 1443 |
|
| 1444 |
stack.push(new LineSegment(cell.x, cell.x, cell.y, 1)); |
| 1445 |
stack.push(new LineSegment(cell.x, cell.x, cell.y + 1, -1)); |
| 1446 |
|
| 1447 |
int left; |
| 1448 |
while(!stack.isEmpty()){ |
| 1449 |
LineSegment segment = (LineSegment) stack.pop(); |
| 1450 |
int x; |
| 1451 |
|
| 1452 |
for( |
| 1453 |
x = segment.x1; |
| 1454 |
x >= 0 && get(x, segment.y) == oldChar; |
| 1455 |
--x){ |
| 1456 |
set(x, segment.y, newChar); |
| 1457 |
cellsFilled.add(new Cell(x, segment.y)); |
| 1458 |
} |
| 1459 |
|
| 1460 |
left = cell.getEast().x; |
| 1461 |
boolean skip = (x > segment.x1)? true : false; |
| 1462 |
|
| 1463 |
if(left < segment.x1){ |
| 1464 |
|
| 1465 |
stack.push( |
| 1466 |
|
| 1467 |
new LineSegment(x, left, segment.y - 1, -segment.dy)); |
| 1468 |
} |
| 1469 |
|
| 1470 |
x = segment.x1 + 1; |
| 1471 |
do { |
| 1472 |
if(!skip) { |
| 1473 |
for( ; x < getWidth() && get(x, segment.y) == oldChar; ++x){ |
| 1474 |
set(x, segment.y, newChar); |
| 1475 |
cellsFilled.add(new Cell(x, segment.y)); |
| 1476 |
} |
| 1477 |
|
| 1478 |
stack.push(new LineSegment(left, x - 1, segment.y, segment.dy)); |
| 1479 |
if(x > segment.x2 + 1) |
| 1480 |
stack.push(new LineSegment(segment.x2 + 1, x - 1, segment.y, -segment.dy)); |
| 1481 |
} |
| 1482 |
skip = false; |
| 1483 |
|
| 1484 |
for(++x; x <= segment.x2 && get(x, segment.y) != oldChar; ++x){;} |
| 1485 |
left = x; |
| 1486 |
} while( x < segment.x2); |
| 1487 |
} |
| 1488 |
|
| 1489 |
return cellsFilled; |
| 1490 |
} |
| 1491 |
|
| 1492 |
public boolean cellContainsDashedLineChar(Cell cell){ |
| 1493 |
char c = get(cell); |
| 1494 |
return StringUtils.isOneOf(c, dashedLines); |
| 1495 |
} |
| 1496 |
|
| 1497 |
public boolean loadFrom(String filename) |
| 1498 |
throws FileNotFoundException, IOException |
| 1499 |
{ |
| 1500 |
return loadFrom(filename, null); |
| 1501 |
} |
| 1502 |
|
| 1503 |
public boolean loadFrom(String filename, ProcessingOptions options) |
| 1504 |
throws IOException |
| 1505 |
{ |
| 1506 |
|
| 1507 |
String encoding = (options == null) ? null : options.getCharacterEncoding(); |
| •1508 |
ArrayList<StringBuilder> lines = new ArrayList<StringBuilder>(); |
| 1509 |
InputStream is; |
| 1510 |
if ("-".equals(filename)) |
| 1511 |
is = System.in; |
| 1512 |
else |
| 1513 |
is = new FileInputStream(filename); |
| 1514 |
String[] linesArray = FileUtils.readFile(is, filename, encoding).split("(\r)?\n"); |
| 1515 |
for(int i = 0; i < linesArray.length; i++) |
| •1516 |
lines.add(new StringBuilder(linesArray[i])); |
| 1517 |
|
| 1518 |
return initialiseWithLines(lines, options); |
| 1519 |
} |
| 1520 |
|
| 1521 |
public boolean initialiseWithText(String text, ProcessingOptions options) throws UnsupportedEncodingException { |
| 1522 |
|
| •1523 |
ArrayList<StringBuilder> lines = new ArrayList<StringBuilder>(); |
| 1524 |
String[] linesArray = text.split("(\r)?\n"); |
| 1525 |
for(int i = 0; i < linesArray.length; i++) |
| •1526 |
lines.add(new StringBuilder(linesArray[i])); |
| 1527 |
|
| 1528 |
return initialiseWithLines(lines, options); |
| 1529 |
} |
| 1530 |
|
| •1531 |
public boolean initialiseWithLines(ArrayList<StringBuilder> lines, ProcessingOptions options) throws UnsupportedEncodingException { |
| 1532 |
|
| 1533 |
|
| 1534 |
boolean done = false; |
| 1535 |
int i; |
| •1536 |
for(i = lines.size() - 1; i >= 0 && !done; i--){ |
| 1537 |
StringBuilder row = lines.get(i); |
| 1538 |
if(!StringUtils.isBlank(row.toString())) done = true; |
| 1539 |
} |
| •1540 |
rows = new ArrayList<StringBuilder>(lines.subList(0, i + 2)); |
| 1541 |
|
| 1542 |
if(options != null) fixTabs(options.getTabSize()); |
| 1543 |
else fixTabs(options.DEFAULT_TAB_SIZE); |
| 1544 |
|
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
int blankBorderSize = 2; |
| 1551 |
|
| 1552 |
int maxLength = 0; |
| 1553 |
int index = 0; |
| 1554 |
|
| 1555 |
String encoding = null; |
| •1556 |
if(options != null) encoding = options.getCharacterEncoding(); |
| 1557 |
|
| •1558 |
Iterator<StringBuilder> it = rows.iterator(); |
| 1559 |
while(it.hasNext()){ |
| 1560 |
String row = it.next().toString(); |
| 1561 |
if(encoding != null){ |
| 1562 |
byte[] bytes = row.getBytes(); |
| 1563 |
row = new String(bytes, encoding); |
| 1564 |
} |
| 1565 |
if(row.length() > maxLength) maxLength = row.length(); |
| •1566 |
rows.set(index, new StringBuilder(row)); |
| 1567 |
index++; |
| 1568 |
} |
| 1569 |
|
| 1570 |
it = rows.iterator(); |
| •1571 |
ArrayList<StringBuilder> newRows = new ArrayList<StringBuilder>(); |
| 1572 |
|
| 1573 |
|
| •1574 |
StringBuilder topBottomRow = |
| 1575 |
new StringBuilder(StringUtils.repeatString(" ", maxLength + blankBorderSize * 2)); |
| 1576 |
|
| 1577 |
newRows.add(topBottomRow); |
| 1578 |
newRows.add(topBottomRow); |
| 1579 |
while(it.hasNext()){ |
| •1580 |
StringBuilder row = it.next(); |
| 1581 |
|
| 1582 |
if(row.length() < maxLength) { |
| 1583 |
String borderString = StringUtils.repeatString(" ", blankBorderSize); |
| •1584 |
StringBuilder newRow = new StringBuilder(); |
| 1585 |
|
| 1586 |
newRow.append(borderString); |
| 1587 |
newRow.append(row); |
| 1588 |
newRow.append(StringUtils.repeatString(" ", maxLength - row.length())); |
| 1589 |
newRow.append(borderString); |
| 1590 |
|
| 1591 |
newRows.add(newRow); |
| 1592 |
} else { |
| •1593 |
newRows.add(new StringBuilder(" ").append(row).append(" ")); |
| 1594 |
} |
| 1595 |
} |
| 1596 |
|
| 1597 |
newRows.add(topBottomRow); |
| 1598 |
newRows.add(topBottomRow); |
| 1599 |
rows = newRows; |
| 1600 |
|
| 1601 |
replaceBullets(); |
| 1602 |
replaceHumanColorCodes(); |
| 1603 |
|
| 1604 |
return true; |
| 1605 |
} |
| 1606 |
|
| 1607 |
private void fixTabs(int tabSize){ |
| 1608 |
|
| 1609 |
int rowIndex = 0; |
| •1610 |
Iterator<StringBuilder> it = rows.iterator(); |
| 1611 |
|
| 1612 |
while(it.hasNext()){ |
| 1613 |
String row = it.next().toString(); |
| •1614 |
StringBuilder newRow = new StringBuilder(); |
| 1615 |
|
| 1616 |
char[] chars = row.toCharArray(); |
| 1617 |
for(int i = 0; i < chars.length; i++){ |
| 1618 |
if(chars[i] == '\t'){ |
| 1619 |
int spacesLeft = tabSize - newRow.length() % tabSize; |
| 1620 |
if(DEBUG){ |
| 1621 |
System.out.println("Found tab. Spaces left: "+spacesLeft); |
| 1622 |
} |
| 1623 |
String spaces = StringUtils.repeatString(" ", spacesLeft); |
| 1624 |
newRow.append(spaces); |
| 1625 |
} else { |
| 1626 |
String character = Character.toString(chars[i]); |
| 1627 |
newRow.append(character); |
| 1628 |
} |
| 1629 |
} |
| 1630 |
rows.set(rowIndex, newRow); |
| 1631 |
rowIndex++; |
| 1632 |
} |
| 1633 |
} |
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
|
| •1638 |
protected ArrayList<StringBuilder> getRows() { |
| 1639 |
return rows; |
| 1640 |
} |
| 1641 |
|
| 1642 |
public class CellColorPair{ |
| 1643 |
public CellColorPair(Cell cell, Color color){ |
| 1644 |
this.cell = cell; |
| 1645 |
this.color = color; |
| 1646 |
} |
| 1647 |
public Color color; |
| 1648 |
public Cell cell; |
| 1649 |
} |
| 1650 |
|
| 1651 |
public class CellStringPair{ |
| 1652 |
public CellStringPair(Cell cell, String string){ |
| 1653 |
this.cell = cell; |
| 1654 |
this.string = string; |
| 1655 |
} |
| 1656 |
public Cell cell; |
| 1657 |
public String string; |
| 1658 |
} |
| 1659 |
|
| 1660 |
public class CellTagPair{ |
| 1661 |
public CellTagPair(Cell cell, String tag){ |
| 1662 |
this.cell = cell; |
| 1663 |
this.tag = tag; |
| 1664 |
} |
| 1665 |
public Cell cell; |
| 1666 |
public String tag; |
| 1667 |
} |
| 1668 |
|
| 1669 |
|
| 1670 |
public class Cell{ |
| 1671 |
|
| 1672 |
public int x, y; |
| 1673 |
|
| 1674 |
public Cell(Cell cell){ |
| 1675 |
this(cell.x, cell.y); |
| 1676 |
} |
| 1677 |
|
| 1678 |
public Cell(int x, int y){ |
| 1679 |
this.x = x; |
| 1680 |
this.y = y; |
| 1681 |
} |
| 1682 |
|
| 1683 |
public Cell getNorth(){ return new Cell(x, y - 1); } |
| 1684 |
public Cell getSouth(){ return new Cell(x, y + 1); } |
| 1685 |
public Cell getEast(){ return new Cell(x + 1, y); } |
| 1686 |
public Cell getWest(){ return new Cell(x - 1, y); } |
| 1687 |
|
| 1688 |
public Cell getNW(){ return new Cell(x - 1, y - 1); } |
| 1689 |
public Cell getNE(){ return new Cell(x + 1, y - 1); } |
| 1690 |
public Cell getSW(){ return new Cell(x - 1, y + 1); } |
| 1691 |
public Cell getSE(){ return new Cell(x + 1, y + 1); } |
| 1692 |
|
| 1693 |
public CellSet getNeighbours4(){ |
| 1694 |
CellSet result = new CellSet(); |
| 1695 |
|
| 1696 |
result.add(getNorth()); |
| 1697 |
result.add(getSouth()); |
| 1698 |
result.add(getWest()); |
| 1699 |
result.add(getEast()); |
| 1700 |
|
| 1701 |
return result; |
| 1702 |
} |
| 1703 |
|
| 1704 |
public CellSet getNeighbours8(){ |
| 1705 |
CellSet result = new CellSet(); |
| 1706 |
|
| 1707 |
result.add(getNorth()); |
| 1708 |
result.add(getSouth()); |
| 1709 |
result.add(getWest()); |
| 1710 |
result.add(getEast()); |
| 1711 |
|
| 1712 |
result.add(getNW()); |
| 1713 |
result.add(getNE()); |
| 1714 |
result.add(getSW()); |
| 1715 |
result.add(getSE()); |
| 1716 |
|
| 1717 |
return result; |
| 1718 |
} |
| 1719 |
|
| 1720 |
|
| 1721 |
public boolean isNorthOf(Cell cell){ |
| 1722 |
if(this.y < cell.y) return true; |
| 1723 |
return false; |
| 1724 |
} |
| 1725 |
|
| 1726 |
public boolean isSouthOf(Cell cell){ |
| 1727 |
if(this.y > cell.y) return true; |
| 1728 |
return false; |
| 1729 |
} |
| 1730 |
|
| 1731 |
public boolean isWestOf(Cell cell){ |
| 1732 |
if(this.x < cell.x) return true; |
| 1733 |
return false; |
| 1734 |
} |
| 1735 |
|
| 1736 |
public boolean isEastOf(Cell cell){ |
| 1737 |
if(this.x > cell.x) return true; |
| 1738 |
return false; |
| 1739 |
} |
| 1740 |
|
| 1741 |
|
| 1742 |
public boolean equals(Object o){ |
| 1743 |
Cell cell = (Cell) o; |
| 1744 |
if(cell == null) return false; |
| 1745 |
if(x == cell.x && y == cell.y) return true; |
| 1746 |
else return false; |
| 1747 |
} |
| 1748 |
|
| 1749 |
public int hashCode() { |
| 1750 |
return (x << 16) | y; |
| 1751 |
} |
| 1752 |
|
| 1753 |
public boolean isNextTo(int x2, int y2){ |
| 1754 |
if(Math.abs(x2 - x) == 1 && Math.abs(y2 - y) == 1) return false; |
| 1755 |
if(Math.abs(x2 - x) == 1 && y2 == y) return true; |
| 1756 |
if(Math.abs(y2 - y) == 1 && x2 == x) return true; |
| 1757 |
return false; |
| 1758 |
} |
| 1759 |
|
| 1760 |
public boolean isNextTo(Cell cell){ |
| 1761 |
if(cell == null) throw new IllegalArgumentException("cell cannot be null"); |
| 1762 |
return this.isNextTo(cell.x, cell.y); |
| 1763 |
} |
| 1764 |
|
| 1765 |
public String toString(){ |
| 1766 |
return "("+x+", "+y+")"; |
| 1767 |
} |
| 1768 |
|
| 1769 |
public void scale(int s){ |
| 1770 |
x = x * s; |
| 1771 |
y = y * s; |
| 1772 |
} |
| 1773 |
|
| 1774 |
} |
| 1775 |
|
| 1776 |
private class LineSegment{ |
| 1777 |
int x1, x2, y, dy; |
| 1778 |
public LineSegment(int x1, int x2, int y, int dy){ |
| 1779 |
this.x1 = x1; |
| 1780 |
this.x2 = x2; |
| 1781 |
this.y = y; |
| 1782 |
this.dy = dy; |
| 1783 |
} |
| 1784 |
} |
| 1785 |
} |
| 1786 |
|