# HG changeset patch # User Jaroslav Tulach # Date 1248896104 -7200 # Node ID e45bc8ad2eaf8f117aa6400788ff761ecf01e6b1 # Parent 6b889f2717e9ca5fdce429d062117f13d6d1f025 Rotating the display of the board - north is on top. Enhancing the display with row ids and column numbers and compass directions. diff -r 6b889f2717e9 -r e45bc8ad2eaf quoridor/src/main/java/cz/xelfi/quoridor/Board.java --- a/quoridor/src/main/java/cz/xelfi/quoridor/Board.java Wed Jul 29 18:48:04 2009 +0200 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Board.java Wed Jul 29 21:35:04 2009 +0200 @@ -261,6 +261,18 @@ return fence(player, new Fence ((x - 'A') * 2 + 1, y * 2 - 1, orientation)); } + private void columnLine(int width, int spaceX, Writer w) throws IOException { + char ch = 'A'; + for (int x = 0; x < width - 1; x++) { + if (x % (spaceX + 1) == 0 && x > 0) { + w.write(ch); + ch = (char) (ch + 1); + } else { + w.write(' '); + } + } + } + private Board fence(Player player, Fence fence) throws IllegalPositionException { if (player.getFences () == 0) { throw new IllegalPositionException ("Not enough fences: " + player); // NOI18N @@ -330,8 +342,8 @@ Set fences = new HashSet(); StringBuffer sb = new StringBuffer(); - int row = 0; - for (int y = 1; y < (spaceY + 1) * 9; y++) { + int row = 7; + for (int y = (spaceY + 1) * 9 - 1; y > 0; y--) { String s = b.readLine(); if (s == null) { throw new EOFException(); @@ -359,7 +371,7 @@ assert false; } } - row++; + row--; } else { String line = s.substring(from, to); p = findPlayer(p, line, y, spaceX, Player.Direction.NORTH, -1); @@ -387,33 +399,41 @@ - /** Writes the board to the provided writer. - * + /** Writes the board to the provided writer. P denotes position + * of the first player and Q of the second. Number of remaining + * fences of each player are shown in left bottom and left top corner + * as appropriate number of ||| - one | per fence. The + * player that is supposed to move in this turn has * right after + * the set of its fences. * *
-            H   G   F   E   D   C   B   A   
+                         [N]
+
+            A   B   C   D   E   F   G   H
             |   |   |   |   |   |   |   |
         +|||||------------------------------+
-        |                                   |          
-     1--|   +   +   +   +   +   +   +   +   |--1        
+        |                 Q                 |
+     8--|   +   +   +   +   +   +   +   +   |--8
         |       |                           |           
-     2--|   +   |   +   +-------+-------+   |--2        
+     7--|   +   |   +   +-------+-------+   |--7
         |       |       |                   |           
-     3--|-------+-------|-------+-------+   |--3        
+     6--|-------+-------|-------+-------+   |--6
         |               |                   |          
-     4--|   +   +   +   +   +   +   +   +   |--4        
-[E]     |               |                   |     [W]
-     5--|   +   +   +   |   +   +   +-------|--5        
-        |               |     y             |          
-     6--|   +   +   +   +-------+   +   +   |--6        
-        |                 a   O   a         |           
-     7--|   +   +   +   +   +   +   +   +   |--7       
-        |                 b   X |           |            
-     8--|   +   +   +   +   +   |   +   +   |--8        
-        |                     z |           |          
-        +|||--------------------------------+
+     5--|   +   +   +   +   +   +   +   +   |--5
+[W]     |               |                   |     [E]
+     4--|   +   +   +   |   +   +   +-------|--4
+        |               |                   |
+     3--|   +   +   +   +-------+   +   +   |--3
+        |                                   |
+     2--|   +   +   +   +   +   +   +   +   |--2
+        |                       |           |
+     1--|   +   +   +   +   +   |   +   +   |--1
+        |                 P     |           |
+        +|||*-------------------------------+
             |   |   |   |   |   |   |   |
-            H   G   F   E   D   C   B   A
+            A   B   C   D   E   F   G   H
+
+                         [S]
      
* @param w writer to write the board to * @exception IOException if communiction with writer fails @@ -421,6 +441,34 @@ public void write (Writer w) throws IOException { write(w, 3, 1); } + + private void northSouthSign(int width, char sign, Writer w) throws IOException { + int middle = width / 2; + for (int x = 0; x < width; x++) { + char ch = ' '; + if (x == middle - 1) { + ch = '['; + } + if (x == middle) { + ch = sign; + } + if (x == middle + 1) { + ch = ']'; + } + w.write(ch); + } + w.write(System.getProperty("line.separator")); // NOI18N + } + + private void subColumnLine(int width, int spaceX, Writer w) throws IOException { + for (int x = 0; x < width - 1; x++) { + if (x % (spaceX + 1) == 0 && x > 0) { + w.write('|'); + } else { + w.write(' '); + } + } + } /** This will print the board with provided spacing. * This is example of 3:1 spacing: @@ -491,20 +539,60 @@ throw new IllegalStateException ("Unknown orientation: " + f.getOrientation ()); // NOI18N } } - - for (int y = 0; y < height; y++) { + w.write(" "); + northSouthSign(width, 'N', w); + w.write(System.getProperty("line.separator")); // NOI18N + w.write(" "); + columnLine(width, spaceX, w); + w.write(System.getProperty("line.separator")); // NOI18N + w.write(" "); + subColumnLine(width, spaceX, w); + w.write(System.getProperty("line.separator")); // NOI18N + int cnt = 9; + for (int y = height - 1; y >= 0; y--) { + if (y == height / 2) { + w.write("[W] "); + } else { + w.write(" "); + } + boolean number = y % (spaceY + 1) == 0 && y > 0 && y < height - 1; + if (number) { + cnt--; + w.write(Integer.toString(cnt)); + w.write("--"); + } else { + w.write(" "); + } for (int x = 0; x < width; x++) { w.write(desk[y][x]); } + if (number) { + w.write("--"); + w.write(Integer.toString(cnt)); + } else { + w.write(" "); + } + if (y == height / 2) { + w.write(" [E]"); + } w.write(System.getProperty("line.separator")); // NOI18N } + w.write(" "); + subColumnLine(width, spaceX, w); + w.write(System.getProperty("line.separator")); // NOI18N + w.write(" "); + columnLine(width, spaceX, w); + w.write(System.getProperty("line.separator")); // NOI18N + w.write(System.getProperty("line.separator")); // NOI18N + w.write(" "); + northSouthSign(width, 'S', w); } private void paintLeftFences(char[][] desk, Direction endDirection, int fences, boolean currentTurn) { assert fences >= 0 && fences <= 10 : "Players: " + players; switch (endDirection) { - case NORTH: { + case SOUTH: { for (int i = 0; i < fences; i++) { desk[desk.length - 1][i + 1] = '|'; } @@ -513,7 +601,7 @@ } break; } - case SOUTH: { + case NORTH: { for (int i = 0; i < fences; i++) { desk[0][i + 1] = '|'; } diff -r 6b889f2717e9 -r e45bc8ad2eaf quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java --- a/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java Wed Jul 29 18:48:04 2009 +0200 +++ b/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java Wed Jul 29 21:35:04 2009 +0200 @@ -28,8 +28,6 @@ import cz.xelfi.quoridor.Fence.Orientation; import cz.xelfi.quoridor.Player.Direction; import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; import junit.framework.TestCase; /**