Serialized board indicates the player turn's by * in the top or bottom line
authorJaroslav Tulach <jtulach@netbeans.org>
Mon, 11 May 2009 18:07:29 +0200
changeset 139602f7fc5fcd
parent 12 40177b10ed87
child 14 d4c8f980978b
Serialized board indicates the player turn's by * in the top or bottom line
quoridor/src/main/java/cz/xelfi/quoridor/Board.java
quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java
quoridor/src/test/java/cz/xelfi/quoridor/BoardTest.java
quoridor/src/test/java/cz/xelfi/quoridor/SerializeTest.java
     1.1 --- a/quoridor/src/main/java/cz/xelfi/quoridor/Board.java	Mon May 11 16:17:14 2009 +0200
     1.2 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Board.java	Mon May 11 18:07:29 2009 +0200
     1.3 @@ -41,8 +41,6 @@
     1.4  import java.util.HashSet;
     1.5  import java.util.List;
     1.6  import java.util.Set;
     1.7 -import java.util.logging.Level;
     1.8 -import java.util.logging.Logger;
     1.9  import java.util.regex.Matcher;
    1.10  import java.util.regex.Pattern;
    1.11  
    1.12 @@ -236,7 +234,7 @@
    1.13      // Serialization
    1.14      //
    1.15  
    1.16 -    private static final Pattern northSouthPattern = Pattern.compile("(\\+(\\|*)-+\\+)");
    1.17 +    private static final Pattern northSouthPattern = Pattern.compile("(\\+(\\|*)(\\*)?-+\\+)");
    1.18      public static Board read(Reader r) throws IOException, IllegalPositionException {
    1.19          BufferedReader b = new BufferedReader(r);
    1.20          for (;;) {
    1.21 @@ -246,7 +244,7 @@
    1.22              }
    1.23              Matcher m = northSouthPattern.matcher(s);
    1.24              if (m.find()) {
    1.25 -                return readFromBetween(b, m.start(1), m.end(1), m.end(2) - m.start(2));
    1.26 +                return readFromBetween(b, m);
    1.27              }
    1.28          }
    1.29      }
    1.30 @@ -262,25 +260,27 @@
    1.31          throw new IOException("Not found " + ch[0] + " at " + pos + " in" + s);
    1.32      }
    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, int fences
    1.37 +    private static Player findPlayer(
    1.38 +        Player previous, String line, int y, int spaceX, Player.Direction dir, int fences
    1.39      ) {
    1.40 -        int index = line.indexOf(ch);
    1.41 +        int index = line.indexOf(dir.player);
    1.42          if (index == -1) {
    1.43 -            return;
    1.44 +            return previous;
    1.45          }
    1.46 -
    1.47          int x = (index - 1) / (spaceX + 1) * 2;
    1.48 -        toAdd.add(new Player(x, y - 1, fences, dir));
    1.49 +        return new Player(x, y - 1, fences, dir);
    1.50      }
    1.51  
    1.52 -    private static Board readFromBetween(BufferedReader b, int from, int to, int northFences)
    1.53 +    private static Board readFromBetween(BufferedReader b, Matcher firstMatcher)
    1.54      throws IOException, IllegalPositionException {
    1.55 +        final int from = firstMatcher.start(1);
    1.56 +        final int to = firstMatcher.end(1);
    1.57 +        final int northFences = firstMatcher.end(2) - firstMatcher.start(2);
    1.58          final int spaceX = (to - from - 1) / 9 - 1;
    1.59          final int spaceY = 1;
    1.60  
    1.61 -        List<Player> players = new ArrayList<Player>();
    1.62 +        Player p = null;
    1.63 +        Player q = null;
    1.64          Set<Fence> fences = new HashSet<Fence>();
    1.65  
    1.66          StringBuffer sb = new StringBuffer();
    1.67 @@ -316,8 +316,8 @@
    1.68                  row++;
    1.69              } else {
    1.70                  String line = s.substring(from, to);
    1.71 -                findPlayer(line, y, 'P', players, spaceX, Player.Direction.SOUTH, northFences);
    1.72 -                findPlayer(line, y, 'Q', players, spaceX, Player.Direction.NORTH, -1);
    1.73 +                p = findPlayer(p, line, y, spaceX, Player.Direction.NORTH, -1);
    1.74 +                q = findPlayer(q, line, y, spaceX, Player.Direction.SOUTH, northFences);
    1.75              }
    1.76          }
    1.77  
    1.78 @@ -325,16 +325,18 @@
    1.79          if (last == null) {
    1.80              throw new EOFException();
    1.81          }
    1.82 -        Matcher m = northSouthPattern.matcher(last);
    1.83 -        if (!m.find()) {
    1.84 +        Matcher lastMatcher = northSouthPattern.matcher(last);
    1.85 +        if (!lastMatcher.find()) {
    1.86              throw new IOException("Unrecognized last line: " + last);
    1.87          }
    1.88 -        int index = players.get(0).getFences() == -1 ? 0 : 1;
    1.89 -        Player p = players.get(index);
    1.90 -        int southFences = m.end(2) - m.start(2);
    1.91 -        players.set(index, new Player(p.getX(), p.getY(), southFences, p.endDirection));
    1.92  
    1.93 -        return new Board(0, players, fences);
    1.94 +        List<Player> arr = new ArrayList<Player>(2);
    1.95 +        assert p != null;
    1.96 +        int southFences = lastMatcher.end(2) - lastMatcher.start(2);
    1.97 +        arr.add(new Player(p.getX(), p.getY(), southFences, p.endDirection));
    1.98 +        arr.add(q);
    1.99 +        int turn = "*".equals(lastMatcher.group(3)) ? 0 : 1; // NOI18N
   1.100 +        return new Board(turn, arr, fences);
   1.101      }
   1.102  
   1.103  
   1.104 @@ -415,14 +417,13 @@
   1.105          desk[height - 1][width - 1] = '+';
   1.106  
   1.107          {
   1.108 -            int player = 'P';
   1.109              for (Player p : players) {
   1.110                  int px = p.getX() / 2;
   1.111                  int py = p.getY() / 2;
   1.112                  desk
   1.113                      [1 + py * (spaceY + 1) + spaceY / 2]
   1.114 -                    [1 + px * (spaceX + 1) + spaceX / 2] = (char) player++;
   1.115 -                paintLeftFences(desk, p.endDirection, p.getFences());
   1.116 +                    [1 + px * (spaceX + 1) + spaceX / 2] = p.endDirection.player;
   1.117 +                paintLeftFences(desk, p.endDirection, p.getFences(), p.equals(getCurrentPlayer()));
   1.118              }
   1.119          }
   1.120          
   1.121 @@ -454,18 +455,25 @@
   1.122      }
   1.123  
   1.124  
   1.125 -    private void paintLeftFences(char[][] desk, Direction endDirection, int fences) {
   1.126 +    private void paintLeftFences(char[][] desk, Direction endDirection, int fences, boolean currentTurn) {
   1.127 +        assert fences >= 0 && fences <= 10 : "Players: " + players;
   1.128          switch (endDirection) {
   1.129              case NORTH: {
   1.130                  for (int i = 0; i < fences; i++) {
   1.131                      desk[desk.length - 1][i + 1] = '|';
   1.132                  }
   1.133 +                if (currentTurn) {
   1.134 +                    desk[desk.length - 1][fences + 1] = '*';
   1.135 +                }
   1.136                  break;
   1.137              }
   1.138              case SOUTH: {
   1.139                  for (int i = 0; i < fences; i++) {
   1.140                      desk[0][i + 1] = '|';
   1.141                  }
   1.142 +                if (currentTurn) {
   1.143 +                    desk[0][fences + 1] = '*';
   1.144 +                }
   1.145                  break;
   1.146              }
   1.147              default:
   1.148 @@ -734,7 +742,13 @@
   1.149          /** Possible directions of player's moves.
   1.150           */
   1.151          public enum Direction {
   1.152 -            NORTH, WEST, EAST, SOUTH
   1.153 +            NORTH('P'), WEST((char) -1), EAST((char)-1), SOUTH('Q');
   1.154 +
   1.155 +            final char player;
   1.156 +            private Direction(char ch) {
   1.157 +                this.player = ch;
   1.158 +            }
   1.159 +
   1.160          }
   1.161      } // end of Player
   1.162      
     2.1 --- a/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java	Mon May 11 16:17:14 2009 +0200
     2.2 +++ b/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java	Mon May 11 18:07:29 2009 +0200
     2.3 @@ -46,6 +46,8 @@
     2.4      protected abstract Board fence(Board b, int player, char x, int y, Board.Fence.Orientation orie)
     2.5      throws IllegalPositionException;
     2.6  
     2.7 +    protected abstract Board apply(Board b, Move move) throws IllegalPositionException;
     2.8 +
     2.9  
    2.10      public void testTwoPlayers () {
    2.11          List<Board.Player> list = board.getPlayers();
    2.12 @@ -302,4 +304,15 @@
    2.13          if (b2.equals (b1)) fail ("Not the same, pawns are reverted");
    2.14      }
    2.15  
    2.16 +    public void testMoveAltersCurrentPlayer() throws Exception {
    2.17 +        assertEquals("First player ready", board.getCurrentPlayer(), board.getPlayers().get(0));
    2.18 +        Board b = apply(board, Move.EAST);
    2.19 +
    2.20 +        for (int i = 0; i < 8; i++) {
    2.21 +            assertEquals("Snd player ready", b.getCurrentPlayer(), b.getPlayers().get(1));
    2.22 +            b = apply(b, Move.SOUTH);
    2.23 +            assertEquals("First player ready", b.getCurrentPlayer(), b.getPlayers().get(0));
    2.24 +            b = apply(b, Move.NORTH);
    2.25 +        }
    2.26 +    }
    2.27  }
     3.1 --- a/quoridor/src/test/java/cz/xelfi/quoridor/BoardTest.java	Mon May 11 16:17:14 2009 +0200
     3.2 +++ b/quoridor/src/test/java/cz/xelfi/quoridor/BoardTest.java	Mon May 11 18:07:29 2009 +0200
     3.3 @@ -45,4 +45,9 @@
     3.4      throws IllegalPositionException {
     3.5          return b.fence(b.getPlayers().get(player), x, y, orie);
     3.6      }
     3.7 +
     3.8 +    @Override
     3.9 +    protected Board apply(Board b, Move move) throws IllegalPositionException {
    3.10 +        return b.apply(move);
    3.11 +    }
    3.12  }
     4.1 --- a/quoridor/src/test/java/cz/xelfi/quoridor/SerializeTest.java	Mon May 11 16:17:14 2009 +0200
     4.2 +++ b/quoridor/src/test/java/cz/xelfi/quoridor/SerializeTest.java	Mon May 11 18:07:29 2009 +0200
     4.3 @@ -58,10 +58,21 @@
     4.4          }
     4.5      }
     4.6  
     4.7 +    @Override
     4.8 +    protected Board apply(Board b, Move move) throws IllegalPositionException {
     4.9 +        try {
    4.10 +            Board read = reread(b);
    4.11 +            return read.apply(move);
    4.12 +        } catch (IOException ex) {
    4.13 +            throw new IllegalStateException(ex);
    4.14 +        }
    4.15 +    }
    4.16 +
    4.17      private Board reread(Board b) throws IOException, IllegalPositionException {
    4.18          StringWriter w = new StringWriter();
    4.19          b.write(w);
    4.20          w.close();
    4.21          return Board.read(new StringReader(w.toString()));
    4.22      }
    4.23 +
    4.24  }