1.1 --- a/quoridor/src/main/java/cz/xelfi/quoridor/Board.java Mon May 11 10:41:52 2009 +0200
1.2 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Board.java Mon May 11 11:18:37 2009 +0200
1.3 @@ -26,9 +26,12 @@
1.4
1.5 package cz.xelfi.quoridor;
1.6
1.7 +import java.io.BufferedReader;
1.8 +import java.io.EOFException;
1.9 import java.io.IOException;
1.10 import java.io.Reader;
1.11 import java.io.Writer;
1.12 +import java.util.ArrayList;
1.13 import java.util.Arrays;
1.14 import java.util.BitSet;
1.15 import java.util.Collection;
1.16 @@ -36,6 +39,8 @@
1.17 import java.util.HashSet;
1.18 import java.util.List;
1.19 import java.util.Set;
1.20 +import java.util.regex.Matcher;
1.21 +import java.util.regex.Pattern;
1.22
1.23 /**
1.24 * This is a class that represents a snapshot of the game position,
1.25 @@ -227,17 +232,73 @@
1.26 // Serialization
1.27 //
1.28
1.29 - public static Board read(Reader r) throws IOException {
1.30 + public static Board read(Reader r) throws IOException, IllegalPositionException {
1.31 + BufferedReader b = new BufferedReader(r);
1.32 + Pattern p = Pattern.compile("(\\+-*\\+)");
1.33 + for (;;) {
1.34 + String s = b.readLine();
1.35 + if (s == null) {
1.36 + throw new IOException("No board found!");
1.37 + }
1.38 + Matcher m = p.matcher(s);
1.39 + if (m.find()) {
1.40 + return readFromBetween(b, m.start(1), m.end(1));
1.41 + }
1.42 + }
1.43 + }
1.44 +
1.45 + private static void assertChar(String s, int pos, char ch) throws IOException {
1.46 + if (s.length() < pos || s.charAt(pos) != ch) {
1.47 + throw new IOException("Not found " + ch + " at " + pos + " in" + s);
1.48 + }
1.49 + }
1.50 +
1.51 + private static void findPlayer(
1.52 + String line, int y, char ch, List<Player> toAdd, int spaceX,
1.53 + Player.Direction dir
1.54 + ) {
1.55 + int index = line.indexOf(ch);
1.56 + if (index == -1) {
1.57 + return;
1.58 + }
1.59 +
1.60 + int x = (index - 1) / (spaceX + 1) * 2;
1.61 + toAdd.add(new Player(x, y - 1, 0, dir));
1.62 + }
1.63 +
1.64 + private static Board readFromBetween(BufferedReader b, int from, int to)
1.65 + throws IOException, IllegalPositionException {
1.66 + final int spaceX = (to - from - 1) / 9 - 1;
1.67 + final int spaceY = 1;
1.68 +
1.69 + List<Player> players = new ArrayList<Player>();
1.70 +
1.71 StringBuffer sb = new StringBuffer();
1.72 - sb.append('\n');
1.73 - for (;;) {
1.74 - int c = r.read();
1.75 - if (c == -1) {
1.76 - break;
1.77 + for (int y = 1; y < (spaceY + 1) * 9; y++) {
1.78 + String s = b.readLine();
1.79 + if (s == null) {
1.80 + throw new EOFException();
1.81 }
1.82 - sb.append((char)c);
1.83 + sb.append(s);
1.84 + sb.append('\n');
1.85 + if (s.length() < to) {
1.86 + throw new IOException("Too short line: " + s); // NOI18N
1.87 + }
1.88 + assertChar(s, from, '|');
1.89 + assertChar(s, to - 1, '|');
1.90 +
1.91 + if (y % (spaceY + 1) == 0) {
1.92 + for (int x = 1; x < 9; x++) {
1.93 + assertChar(s, from + (spaceX + 1) * x, '+');
1.94 + }
1.95 + } else {
1.96 + String line = s.substring(from, to);
1.97 + findPlayer(line, y, 'P', players, spaceX, Player.Direction.SOUTH);
1.98 + findPlayer(line, y, 'Q', players, spaceX, Player.Direction.NORTH);
1.99 + }
1.100 }
1.101 - throw new IOException(sb.toString());
1.102 +
1.103 + return new Board(0, players, Collections.<Fence>emptySet());
1.104 }
1.105
1.106
1.107 @@ -551,7 +612,7 @@
1.108 /** the direction of players end line */
1.109 private final Direction endDirection;
1.110
1.111 - Player (int x, int y, int f, Player.Direction endDir) {
1.112 + Player (int x, int y, int f, Player.Direction endDir) {
1.113 this.x = x;
1.114 this.y = y;
1.115 this.f = f;
2.1 --- a/quoridor/src/test/java/cz/xelfi/quoridor/SerializeTest.java Mon May 11 10:41:52 2009 +0200
2.2 +++ b/quoridor/src/test/java/cz/xelfi/quoridor/SerializeTest.java Mon May 11 11:18:37 2009 +0200
2.3 @@ -29,8 +29,6 @@
2.4 import java.io.IOException;
2.5 import java.io.StringReader;
2.6 import java.io.StringWriter;
2.7 -import java.util.logging.Level;
2.8 -import java.util.logging.Logger;
2.9
2.10 /** Basic tests in simple configuration.
2.11 *
2.12 @@ -45,7 +43,7 @@
2.13 protected Board move(Board b, int player, Direction... where) throws IllegalPositionException {
2.14 try {
2.15 Board read = reread(b);
2.16 - return read.move(b.getPlayers().get(player), where);
2.17 + return read.move(read.getPlayers().get(player), where);
2.18 } catch (IOException ex) {
2.19 throw new IllegalStateException(ex);
2.20 }
2.21 @@ -54,13 +52,13 @@
2.22 throws IllegalPositionException {
2.23 try {
2.24 Board read = reread(b);
2.25 - return read.fence(b.getPlayers().get(player), x, y, orie);
2.26 + return read.fence(read.getPlayers().get(player), x, y, orie);
2.27 } catch (IOException ex) {
2.28 throw new IllegalStateException(ex);
2.29 }
2.30 }
2.31
2.32 - private Board reread(Board b) throws IOException {
2.33 + private Board reread(Board b) throws IOException, IllegalPositionException {
2.34 StringWriter w = new StringWriter();
2.35 b.write(w);
2.36 w.close();