| •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.util.ArrayList; |
| 23 |
import java.util.Iterator; |
| 24 |
import java.util.regex.Pattern; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public class GridPattern extends TextGrid { |
| 78 |
|
| 79 |
private ArrayList<Pattern> regExps = new ArrayList<Pattern>(); |
| 80 |
private boolean regExpsAreValid = false; |
| 81 |
|
| 82 |
private static final boolean DEBUG = false; |
| 83 |
|
| 84 |
private boolean usesStandardSyntax = false; |
| 85 |
|
| 86 |
public GridPattern(){ |
| 87 |
super(3, 3); |
| 88 |
} |
| 89 |
|
| 90 |
public GridPattern(String row1, String row2, String row3){ |
| 91 |
super(Math.max(Math.max(row1.length(), row2.length()), row3.length()), 3); |
| 92 |
setTo(row1, row2, row3); |
| 93 |
regExpsAreValid = false; |
| 94 |
} |
| 95 |
|
| 96 |
public boolean usesStandardSyntax() { |
| 97 |
return usesStandardSyntax; |
| 98 |
} |
| 99 |
|
| 100 |
public void setUsesStandardSyntax(boolean b) { |
| 101 |
usesStandardSyntax = b; |
| 102 |
regExpsAreValid = false; |
| 103 |
} |
| 104 |
|
| 105 |
public boolean isMatchedBy(TextGrid grid){ |
| 106 |
|
| 107 |
|
| 108 |
if(!regExpsAreValid) prepareRegExps(); |
| 109 |
|
| 110 |
for(int i = 0; i < grid.getHeight(); i++) { |
| 111 |
String row = grid.getRow(i).toString(); |
| 112 |
Pattern regexp = regExps.get(i); |
| 113 |
if(!regexp.matcher(row).matches()) { |
| 114 |
if(DEBUG) |
| 115 |
System.out.println(row+" does not match "+regexp); |
| 116 |
return false; |
| 117 |
} |
| 118 |
} |
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
private void prepareRegExps(){ |
| 123 |
regExpsAreValid = true; |
| 124 |
regExps.clear(); |
| 125 |
if (DEBUG) |
| 126 |
System.out.println("Trying to match:"); |
| 127 |
if(!usesStandardSyntax){ |
| •128 |
Iterator<StringBuilder> it = getRows().iterator(); |
| 129 |
while (it.hasNext()) { |
| 130 |
String row = it.next().toString(); |
| 131 |
regExps.add(Pattern.compile(makeRegExp(row))); |
| 132 |
if(DEBUG) |
| 133 |
System.out.println(row+" becomes "+makeRegExp(row)); |
| 134 |
} |
| 135 |
} else { |
| •136 |
Iterator<StringBuilder> it = getRows().iterator(); |
| 137 |
while (it.hasNext()) { |
| 138 |
String row = it.next().toString(); |
| 139 |
regExps.add(Pattern.compile(row)); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
private String makeRegExp(String pattern){ |
| 145 |
StringBuilder result = new StringBuilder(); |
| 146 |
int tokensHandled = 0; |
| 147 |
for(int i = 0; i < pattern.length() && tokensHandled < 3; i++){ |
| 148 |
char c = pattern.charAt(i); |
| 149 |
if(c == '[') { |
| 150 |
result.append("[^|:]"); |
| 151 |
} else if(c == '|') { |
| 152 |
result.append("[|:]"); |
| 153 |
} else if(c == '-') { |
| •154 |
result.append("[-=]"); |
| 155 |
} else if(c == '!') { |
| 156 |
result.append("[^-=\\/\\\\+|:]"); |
| 157 |
} else if(c == 'b') { |
| 158 |
result.append("[-=\\/\\\\+|:]"); |
| 159 |
} else if(c == '^') { |
| 160 |
result.append("[\\/\\\\+|:]"); |
| 161 |
} else if(c == '(') { |
| 162 |
result.append("[-=\\/\\\\+]"); |
| 163 |
} else if(c == '~') { |
| 164 |
result.append("."); |
| 165 |
} else if(c == '+') { |
| 166 |
result.append("\\+"); |
| 167 |
} else if(c == '\\') { |
| 168 |
result.append("\\\\"); |
| 169 |
} else if(c == 's') { |
| 170 |
result.append("[-=+|:]"); |
| 171 |
} else if(c == 'S') { |
| 172 |
result.append("[\\/\\\\]"); |
| 173 |
} else if(c == '*') { |
| 174 |
result.append("\\*"); |
| 175 |
|
| 176 |
|
| 177 |
} else if(c == '1') { |
| 178 |
result.append("[\\\\]"); |
| 179 |
|
| 180 |
} else if(c == '2') { |
| 181 |
result.append("[|:+\\/\\\\]"); |
| 182 |
|
| 183 |
} else if(c == '3') { |
| 184 |
result.append("[\\/]"); |
| 185 |
|
| 186 |
} else if(c == '4') { |
| 187 |
result.append("[-=+\\/\\\\]"); |
| 188 |
|
| 189 |
} else if(c == '5') { |
| 190 |
result.append("[\\\\]"); |
| 191 |
|
| 192 |
} else if(c == '6') { |
| 193 |
result.append("[|:+\\/\\\\]"); |
| 194 |
|
| 195 |
} else if(c == '7') { |
| 196 |
result.append("[\\/]"); |
| 197 |
|
| 198 |
} else if(c == '8') { |
| 199 |
result.append("[-=+\\/\\\\]"); |
| 200 |
|
| 201 |
|
| 202 |
} else if(c == '%') { |
| 203 |
if(i+1 > pattern.length()){ |
| 204 |
throw new RuntimeException("Invalid pattern, found % at the end"); |
| 205 |
} |
| 206 |
c = pattern.charAt(++i); |
| 207 |
|
| 208 |
if(c == '1') { |
| 209 |
result.append("[^\\\\]"); |
| 210 |
|
| 211 |
} else if(c == '2') { |
| 212 |
result.append("[^|:+\\/\\\\]"); |
| 213 |
|
| 214 |
} else if(c == '3') { |
| 215 |
result.append("[^\\/]"); |
| 216 |
|
| 217 |
} else if(c == '4') { |
| 218 |
result.append("[^-=+\\/\\\\]"); |
| 219 |
|
| 220 |
} else if(c == '5') { |
| 221 |
result.append("[^\\\\]"); |
| 222 |
|
| 223 |
} else if(c == '6') { |
| 224 |
result.append("[^|:+\\/\\\\]"); |
| 225 |
|
| 226 |
} else if(c == '7') { |
| 227 |
result.append("[^\\/]"); |
| 228 |
|
| 229 |
} else if(c == '8') { |
| 230 |
result.append("[^-=+\\/\\\\]"); |
| 231 |
} |
| 232 |
} else result.append(String.valueOf(c)); |
| 233 |
tokensHandled++; |
| 234 |
} |
| 235 |
return result.toString(); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
public void setTo(String row1, String row2, String row3){ |
| 240 |
if(getHeight() != 3) throw new RuntimeException("This method can only be called for GridPatternS with height 3"); |
| 241 |
regExpsAreValid = false; |
| 242 |
writeStringTo(0, 0, row1); |
| 243 |
writeStringTo(0, 1, row2); |
| 244 |
writeStringTo(0, 2, row3); |
| 245 |
|
| 246 |
} |
| 247 |
|
| 248 |
public static void main(String[] args) { |
| 249 |
TextGrid grid = new TextGrid(3, 3); |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
grid.setRow(0, "---"); |
| 284 |
grid.setRow(1, " / "); |
| 285 |
grid.setRow(2, "---"); |
| 286 |
grid.printDebug(); |
| 287 |
if(GridPatternGroup.loneDiagonalCriteria.isAnyMatchedBy(grid)){ |
| 288 |
System.out.println("Grid is lone diagonal"); |
| 289 |
} else { |
| 290 |
System.out.println("Grid is not lone diagonal"); |
| 291 |
} |
| 292 |
|
| 293 |
grid.setRow(0, "--/"); |
| 294 |
grid.setRow(1, " / "); |
| 295 |
grid.setRow(2, "---"); |
| 296 |
grid.printDebug(); |
| 297 |
if(GridPatternGroup.loneDiagonalCriteria.isAnyMatchedBy(grid)){ |
| 298 |
System.out.println("Grid is lone diagonal"); |
| 299 |
} else { |
| 300 |
System.out.println("Grid is not lone diagonal"); |
| 301 |
} |
| 302 |
|
| 303 |
grid.setRow(0, "-- "); |
| 304 |
grid.setRow(1, " \\ "); |
| 305 |
grid.setRow(2, "---"); |
| 306 |
grid.printDebug(); |
| 307 |
if(GridPatternGroup.loneDiagonalCriteria.isAnyMatchedBy(grid)){ |
| 308 |
System.out.println("Grid is lone diagonal"); |
| 309 |
} else { |
| 310 |
System.out.println("Grid is not lone diagonal"); |
| 311 |
} |
| 312 |
|
| 313 |
grid.setRow(0, "-- "); |
| 314 |
grid.setRow(1, " \\ "); |
| 315 |
grid.setRow(2, "--\\"); |
| 316 |
grid.printDebug(); |
| 317 |
if(GridPatternGroup.loneDiagonalCriteria.isAnyMatchedBy(grid)){ |
| 318 |
System.out.println("Grid is lone diagonal"); |
| 319 |
} else { |
| 320 |
System.out.println("Grid is not lone diagonal"); |
| 321 |
} |
| 322 |
|
| 323 |
grid.setRow(0, " "); |
| 324 |
grid.setRow(1, "-\\/"); |
| 325 |
grid.setRow(2, " ||"); |
| 326 |
grid.printDebug(); |
| 327 |
if(GridPatternGroup.loneDiagonalCriteria.isAnyMatchedBy(grid)){ |
| 328 |
System.out.println("Grid is lone diagonal"); |
| 329 |
} else { |
| 330 |
System.out.println("Grid is not lone diagonal"); |
| 331 |
} |
| 332 |
} |
| 333 |
} |