1.1 --- a/quoridor/src/main/java/cz/xelfi/quoridor/Board.java Mon May 11 11:18:37 2009 +0200
1.2 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Board.java Mon May 11 12:09:34 2009 +0200
1.3 @@ -26,6 +26,7 @@
1.4
1.5 package cz.xelfi.quoridor;
1.6
1.7 +import cz.xelfi.quoridor.Board.Player.Direction;
1.8 import java.io.BufferedReader;
1.9 import java.io.EOFException;
1.10 import java.io.IOException;
1.11 @@ -232,17 +233,17 @@
1.12 // Serialization
1.13 //
1.14
1.15 + private static final Pattern northSouthPattern = Pattern.compile("(\\+(\\|*)-+\\+)");
1.16 public static Board read(Reader r) throws IOException, IllegalPositionException {
1.17 BufferedReader b = new BufferedReader(r);
1.18 - Pattern p = Pattern.compile("(\\+-*\\+)");
1.19 for (;;) {
1.20 String s = b.readLine();
1.21 if (s == null) {
1.22 throw new IOException("No board found!");
1.23 }
1.24 - Matcher m = p.matcher(s);
1.25 + Matcher m = northSouthPattern.matcher(s);
1.26 if (m.find()) {
1.27 - return readFromBetween(b, m.start(1), m.end(1));
1.28 + return readFromBetween(b, m.start(1), m.end(1), m.end(2) - m.start(2));
1.29 }
1.30 }
1.31 }
1.32 @@ -255,7 +256,7 @@
1.33
1.34 private static void findPlayer(
1.35 String line, int y, char ch, List<Player> toAdd, int spaceX,
1.36 - Player.Direction dir
1.37 + Player.Direction dir, int fences
1.38 ) {
1.39 int index = line.indexOf(ch);
1.40 if (index == -1) {
1.41 @@ -263,10 +264,10 @@
1.42 }
1.43
1.44 int x = (index - 1) / (spaceX + 1) * 2;
1.45 - toAdd.add(new Player(x, y - 1, 0, dir));
1.46 + toAdd.add(new Player(x, y - 1, fences, dir));
1.47 }
1.48
1.49 - private static Board readFromBetween(BufferedReader b, int from, int to)
1.50 + private static Board readFromBetween(BufferedReader b, int from, int to, int northFences)
1.51 throws IOException, IllegalPositionException {
1.52 final int spaceX = (to - from - 1) / 9 - 1;
1.53 final int spaceY = 1;
1.54 @@ -293,11 +294,24 @@
1.55 }
1.56 } else {
1.57 String line = s.substring(from, to);
1.58 - findPlayer(line, y, 'P', players, spaceX, Player.Direction.SOUTH);
1.59 - findPlayer(line, y, 'Q', players, spaceX, Player.Direction.NORTH);
1.60 + findPlayer(line, y, 'P', players, spaceX, Player.Direction.SOUTH, northFences);
1.61 + findPlayer(line, y, 'Q', players, spaceX, Player.Direction.NORTH, -1);
1.62 }
1.63 }
1.64
1.65 + String last = b.readLine();
1.66 + if (last == null) {
1.67 + throw new EOFException();
1.68 + }
1.69 + Matcher m = northSouthPattern.matcher(last);
1.70 + if (!m.find()) {
1.71 + throw new IOException("Unrecognized last line: " + last);
1.72 + }
1.73 + int index = players.get(0).getFences() == -1 ? 0 : 1;
1.74 + Player p = players.get(index);
1.75 + int fences = m.end(2) - m.start(2);
1.76 + players.set(index, new Player(p.getX(), p.getY(), fences, p.endDirection));
1.77 +
1.78 return new Board(0, players, Collections.<Fence>emptySet());
1.79 }
1.80
1.81 @@ -309,25 +323,25 @@
1.82 * <pre>
1.83 H G F E D C B A
1.84 | | | | | | | |
1.85 - +-----------------------------------+
1.86 + +|||||------------------------------+
1.87 | |
1.88 - 1--| + + + + + + + + |--1 playerA (O)
1.89 - | | | Fences - 5
1.90 - 2--| + | + +-------+-------+ |--2 ===========
1.91 - | | | | | | | | |
1.92 + 1--| + + + + + + + + |--1
1.93 + | | |
1.94 + 2--| + | + +-------+-------+ |--2
1.95 + | | | |
1.96 3--|-------+-------|-------+-------+ |--3
1.97 | | |
1.98 4--| + + + + + + + + |--4
1.99 [E] | | | [W]
1.100 5--| + + + | + + +-------|--5
1.101 | | y |
1.102 - 6--| + + + +-------+ + + |--6 PlayerB (X)
1.103 - | a O a | Fences - 3
1.104 - 7--| + + + + + + + + |--7 ===========
1.105 - | b X | | | | |
1.106 + 6--| + + + +-------+ + + |--6
1.107 + | a O a |
1.108 + 7--| + + + + + + + + |--7
1.109 + | b X | |
1.110 8--| + + + + + | + + |--8
1.111 | z | |
1.112 - +-----------------------------------+
1.113 + +|||--------------------------------+
1.114 | | | | | | | |
1.115 H G F E D C B A
1.116 </pre>
1.117 @@ -386,6 +400,7 @@
1.118 desk
1.119 [1 + py * (spaceY + 1) + spaceY / 2]
1.120 [1 + px * (spaceX + 1) + spaceX / 2] = (char) player++;
1.121 + paintLeftFences(desk, p.endDirection, p.getFences());
1.122 }
1.123 }
1.124
1.125 @@ -402,7 +417,7 @@
1.126 }
1.127 break;
1.128 default:
1.129 - throw new IllegalStateException ("Unkown orientation: " + f.getOrientation ()); // NOI18N
1.130 + throw new IllegalStateException ("Unknown orientation: " + f.getOrientation ()); // NOI18N
1.131 }
1.132 }
1.133
1.134 @@ -413,7 +428,28 @@
1.135 w.write(System.getProperty("line.separator")); // NOI18N
1.136 }
1.137 }
1.138 -
1.139 +
1.140 +
1.141 + private void paintLeftFences(char[][] desk, Direction endDirection, int fences) {
1.142 + switch (endDirection) {
1.143 + case NORTH: {
1.144 + for (int i = 0; i < fences; i++) {
1.145 + desk[desk.length - 1][i + 1] = '|';
1.146 + }
1.147 + break;
1.148 + }
1.149 + case SOUTH: {
1.150 + for (int i = 0; i < fences; i++) {
1.151 + desk[0][i + 1] = '|';
1.152 + }
1.153 + break;
1.154 + }
1.155 + default:
1.156 + assert false;
1.157 + }
1.158 + }
1.159 +
1.160 +
1.161 //
1.162 // Standard Methods
1.163 //
1.164 @@ -601,7 +637,7 @@
1.165 int baseOnY0 = max * (max + 1) / 2;
1.166 return baseOnY0 + y;
1.167 }
1.168 -
1.169 +
1.170 /** Defines properties (mostly position) of a player.
1.171 */
1.172 public static final class Player extends Object {
2.1 --- a/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java Mon May 11 11:18:37 2009 +0200
2.2 +++ b/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java Mon May 11 12:09:34 2009 +0200
2.3 @@ -175,6 +175,32 @@
2.4 // as well as east
2.5 move(b, 1, Board.Player.Direction.SOUTH, Board.Player.Direction.EAST);
2.6 }
2.7 +
2.8 + public void testPlaceAllFences() throws Exception {
2.9 + doPlaceAllFences(0);
2.10 + }
2.11 + public void testPlaceAllFences2() throws Exception {
2.12 + doPlaceAllFences(1);
2.13 + }
2.14 +
2.15 + private void doPlaceAllFences(int player) throws Exception {
2.16 + Board b = board;
2.17 +
2.18 + int cnt = 10;
2.19 + for (int i = 1; i <= 5; i++) {
2.20 + b = fence(b, player, 'A', i, Board.Fence.Orientation.HORIZONTAL);
2.21 + b = fence(b, player, 'D', i, Board.Fence.Orientation.HORIZONTAL);
2.22 + cnt -= 2;
2.23 + assertEquals("Two less" + i, cnt, b.getPlayers().get(player).getFences());
2.24 + }
2.25 +
2.26 + try {
2.27 + fence(b, player, 'F', 7, Board.Fence.Orientation.VERTICAL);
2.28 + fail("We shall run out of fences");
2.29 + } catch (IllegalPositionException ex) {
2.30 + // OK
2.31 + }
2.32 + }
2.33
2.34 public void testAlwaysHasToHaveAccessToEndLine () throws Exception {
2.35 Board b = board;
2.36 @@ -275,4 +301,5 @@
2.37 if (b1.equals (b2)) fail ("Not the same, pawns are reverted");
2.38 if (b2.equals (b1)) fail ("Not the same, pawns are reverted");
2.39 }
2.40 +
2.41 }