Rotating the display of the board - north is on top. Enhancing the display with row ids and column numbers and compass directions.
authorJaroslav Tulach <jtulach@netbeans.org>
Wed, 29 Jul 2009 21:35:04 +0200
changeset 40e45bc8ad2eaf
parent 39 6b889f2717e9
child 41 c94f68ddef59
Rotating the display of the board - north is on top. Enhancing the display with row ids and column numbers and compass directions.
quoridor/src/main/java/cz/xelfi/quoridor/Board.java
quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java
     1.1 --- a/quoridor/src/main/java/cz/xelfi/quoridor/Board.java	Wed Jul 29 18:48:04 2009 +0200
     1.2 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Board.java	Wed Jul 29 21:35:04 2009 +0200
     1.3 @@ -261,6 +261,18 @@
     1.4          return fence(player, new Fence ((x - 'A') * 2 + 1, y * 2 - 1, orientation));
     1.5      }
     1.6  
     1.7 +    private void columnLine(int width, int spaceX, Writer w) throws IOException {
     1.8 +        char ch = 'A';
     1.9 +        for (int x = 0; x < width - 1; x++) {
    1.10 +            if (x % (spaceX + 1) == 0 && x > 0) {
    1.11 +                w.write(ch);
    1.12 +                ch = (char) (ch + 1);
    1.13 +            } else {
    1.14 +                w.write(' ');
    1.15 +            }
    1.16 +        }
    1.17 +    }
    1.18 +
    1.19      private Board fence(Player player, Fence fence) throws IllegalPositionException {
    1.20          if (player.getFences () == 0) {
    1.21              throw new IllegalPositionException ("Not enough fences: " + player); // NOI18N
    1.22 @@ -330,8 +342,8 @@
    1.23          Set<Fence> fences = new HashSet<Fence>();
    1.24  
    1.25          StringBuffer sb = new StringBuffer();
    1.26 -        int row = 0;
    1.27 -        for (int y = 1; y < (spaceY + 1) * 9; y++) {
    1.28 +        int row = 7;
    1.29 +        for (int y = (spaceY + 1) * 9 - 1; y > 0; y--) {
    1.30              String s = b.readLine();
    1.31              if (s == null) {
    1.32                  throw new EOFException();
    1.33 @@ -359,7 +371,7 @@
    1.34                              assert false;
    1.35                      }
    1.36                  }
    1.37 -                row++;
    1.38 +                row--;
    1.39              } else {
    1.40                  String line = s.substring(from, to);
    1.41                  p = findPlayer(p, line, y, spaceX, Player.Direction.NORTH, -1);
    1.42 @@ -387,33 +399,41 @@
    1.43  
    1.44  
    1.45      
    1.46 -    /** Writes the board to the provided writer. 
    1.47 -     *
    1.48 +    /** Writes the board to the provided writer. <b>P</b> denotes position
    1.49 +     * of the first player and <b>Q</b> of the second. Number of remaining
    1.50 +     * fences of each player are shown in left bottom and left top corner
    1.51 +     * as appropriate number of <b>|||</b> - one <b>|</b> per fence. The
    1.52 +     * player that is supposed to move in this turn has <b>*</b> right after
    1.53 +     * the set of its fences.
    1.54       *
    1.55       * <pre>
    1.56 -            H   G   F   E   D   C   B   A   
    1.57 +                         [N]
    1.58 +
    1.59 +            A   B   C   D   E   F   G   H
    1.60              |   |   |   |   |   |   |   |
    1.61          +|||||------------------------------+
    1.62 -        |                                   |          
    1.63 -     1--|   +   +   +   +   +   +   +   +   |--1        
    1.64 +        |                 Q                 |
    1.65 +     8--|   +   +   +   +   +   +   +   +   |--8
    1.66          |       |                           |           
    1.67 -     2--|   +   |   +   +-------+-------+   |--2        
    1.68 +     7--|   +   |   +   +-------+-------+   |--7
    1.69          |       |       |                   |           
    1.70 -     3--|-------+-------|-------+-------+   |--3        
    1.71 +     6--|-------+-------|-------+-------+   |--6
    1.72          |               |                   |          
    1.73 -     4--|   +   +   +   +   +   +   +   +   |--4        
    1.74 -[E]     |               |                   |     [W]
    1.75 -     5--|   +   +   +   |   +   +   +-------|--5        
    1.76 -        |               |     y             |          
    1.77 -     6--|   +   +   +   +-------+   +   +   |--6        
    1.78 -        |                 a   O   a         |           
    1.79 -     7--|   +   +   +   +   +   +   +   +   |--7       
    1.80 -        |                 b   X |           |            
    1.81 -     8--|   +   +   +   +   +   |   +   +   |--8        
    1.82 -        |                     z |           |          
    1.83 -        +|||--------------------------------+
    1.84 +     5--|   +   +   +   +   +   +   +   +   |--5
    1.85 +[W]     |               |                   |     [E]
    1.86 +     4--|   +   +   +   |   +   +   +-------|--4
    1.87 +        |               |                   |
    1.88 +     3--|   +   +   +   +-------+   +   +   |--3
    1.89 +        |                                   |
    1.90 +     2--|   +   +   +   +   +   +   +   +   |--2
    1.91 +        |                       |           |
    1.92 +     1--|   +   +   +   +   +   |   +   +   |--1
    1.93 +        |                 P     |           |
    1.94 +        +|||*-------------------------------+
    1.95              |   |   |   |   |   |   |   |
    1.96 -            H   G   F   E   D   C   B   A
    1.97 +            A   B   C   D   E   F   G   H
    1.98 +
    1.99 +                         [S]
   1.100       </pre>
   1.101       * @param w writer to write the board to
   1.102       * @exception IOException if communiction with writer fails
   1.103 @@ -421,6 +441,34 @@
   1.104      public void write (Writer w) throws IOException {
   1.105          write(w, 3, 1);
   1.106      }
   1.107 +
   1.108 +    private void northSouthSign(int width, char sign, Writer w) throws IOException {
   1.109 +        int middle = width / 2;
   1.110 +        for (int x = 0; x < width; x++) {
   1.111 +            char ch = ' ';
   1.112 +            if (x == middle - 1) {
   1.113 +                ch = '[';
   1.114 +            }
   1.115 +            if (x == middle) {
   1.116 +                ch = sign;
   1.117 +            }
   1.118 +            if (x == middle + 1) {
   1.119 +                ch = ']';
   1.120 +            }
   1.121 +            w.write(ch);
   1.122 +        }
   1.123 +        w.write(System.getProperty("line.separator")); // NOI18N
   1.124 +    }
   1.125 +
   1.126 +    private void subColumnLine(int width, int spaceX, Writer w) throws IOException {
   1.127 +        for (int x = 0; x < width - 1; x++) {
   1.128 +            if (x % (spaceX + 1) == 0 && x > 0) {
   1.129 +                w.write('|');
   1.130 +            } else {
   1.131 +                w.write(' ');
   1.132 +            }
   1.133 +        }
   1.134 +    }
   1.135      
   1.136      /** This will print the board with provided spacing.
   1.137       * This is example of 3:1 spacing: 
   1.138 @@ -491,20 +539,60 @@
   1.139                      throw new IllegalStateException ("Unknown orientation: " + f.getOrientation ()); // NOI18N
   1.140              }
   1.141          }
   1.142 -
   1.143 -        for (int y = 0; y < height; y++) {
   1.144 +        w.write("        ");
   1.145 +        northSouthSign(width, 'N', w);
   1.146 +        w.write(System.getProperty("line.separator")); // NOI18N
   1.147 +        w.write("        ");
   1.148 +        columnLine(width, spaceX, w);
   1.149 +        w.write(System.getProperty("line.separator")); // NOI18N
   1.150 +        w.write("        ");
   1.151 +        subColumnLine(width, spaceX, w);
   1.152 +        w.write(System.getProperty("line.separator")); // NOI18N
   1.153 +        int cnt = 9;
   1.154 +        for (int y = height - 1; y >= 0; y--) {
   1.155 +            if (y == height / 2) {
   1.156 +                w.write("[W]  ");
   1.157 +            } else {
   1.158 +                w.write("     ");
   1.159 +            }
   1.160 +            boolean number = y % (spaceY + 1) == 0 && y > 0 && y < height - 1;
   1.161 +            if (number) {
   1.162 +                cnt--;
   1.163 +                w.write(Integer.toString(cnt));
   1.164 +                w.write("--");
   1.165 +            } else {
   1.166 +                w.write("   ");
   1.167 +            }
   1.168              for (int x = 0; x < width; x++) {
   1.169                  w.write(desk[y][x]);
   1.170              }
   1.171 +            if (number) {
   1.172 +                w.write("--");
   1.173 +                w.write(Integer.toString(cnt));
   1.174 +            } else {
   1.175 +                w.write("   ");
   1.176 +            }
   1.177 +            if (y == height / 2) {
   1.178 +                w.write("  [E]");
   1.179 +            }
   1.180              w.write(System.getProperty("line.separator")); // NOI18N
   1.181          }
   1.182 +        w.write("        ");
   1.183 +        subColumnLine(width, spaceX, w);
   1.184 +        w.write(System.getProperty("line.separator")); // NOI18N
   1.185 +        w.write("        ");
   1.186 +        columnLine(width, spaceX, w);
   1.187 +        w.write(System.getProperty("line.separator")); // NOI18N
   1.188 +        w.write(System.getProperty("line.separator")); // NOI18N
   1.189 +        w.write("        ");
   1.190 +        northSouthSign(width, 'S', w);
   1.191      }
   1.192  
   1.193  
   1.194      private void paintLeftFences(char[][] desk, Direction endDirection, int fences, boolean currentTurn) {
   1.195          assert fences >= 0 && fences <= 10 : "Players: " + players;
   1.196          switch (endDirection) {
   1.197 -            case NORTH: {
   1.198 +            case SOUTH: {
   1.199                  for (int i = 0; i < fences; i++) {
   1.200                      desk[desk.length - 1][i + 1] = '|';
   1.201                  }
   1.202 @@ -513,7 +601,7 @@
   1.203                  }
   1.204                  break;
   1.205              }
   1.206 -            case SOUTH: {
   1.207 +            case NORTH: {
   1.208                  for (int i = 0; i < fences; i++) {
   1.209                      desk[0][i + 1] = '|';
   1.210                  }
     2.1 --- a/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java	Wed Jul 29 18:48:04 2009 +0200
     2.2 +++ b/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java	Wed Jul 29 21:35:04 2009 +0200
     2.3 @@ -28,8 +28,6 @@
     2.4  import cz.xelfi.quoridor.Fence.Orientation;
     2.5  import cz.xelfi.quoridor.Player.Direction;
     2.6  import java.util.List;
     2.7 -import java.util.logging.Level;
     2.8 -import java.util.logging.Logger;
     2.9  import junit.framework.TestCase;
    2.10  
    2.11  /**