Basic implementation of printing board to stream
authorJaroslav Tulach <jtulach@netbeans.org>
Mon, 11 May 2009 10:41:52 +0200
changeset 9db37c60e9a72
parent 8 d646ee3ce41b
child 10 0b73ae14f907
Basic implementation of printing board to stream
quoridor/src/main/java/cz/xelfi/quoridor/Board.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 09:21:54 2009 +0200
     1.2 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Board.java	Mon May 11 10:41:52 2009 +0200
     1.3 @@ -26,6 +26,9 @@
     1.4  
     1.5  package cz.xelfi.quoridor;
     1.6  
     1.7 +import java.io.IOException;
     1.8 +import java.io.Reader;
     1.9 +import java.io.Writer;
    1.10  import java.util.Arrays;
    1.11  import java.util.BitSet;
    1.12  import java.util.Collection;
    1.13 @@ -219,10 +222,30 @@
    1.14          
    1.15          return new Board (turn + 1, Arrays.asList (arr), fen);
    1.16      }
    1.17 +
    1.18 +    //
    1.19 +    // Serialization
    1.20 +    //
    1.21 +
    1.22 +    public static Board read(Reader r) throws IOException {
    1.23 +        StringBuffer sb = new StringBuffer();
    1.24 +        sb.append('\n');
    1.25 +        for (;;) {
    1.26 +            int c = r.read();
    1.27 +            if (c == -1) {
    1.28 +                break;
    1.29 +            }
    1.30 +            sb.append((char)c);
    1.31 +        }
    1.32 +        throw new IOException(sb.toString());
    1.33 +    }
    1.34 +
    1.35 +
    1.36      
    1.37      /** Writes the board to the provided writer. 
    1.38       *
    1.39       *
    1.40 +     * <pre>
    1.41              H   G   F   E   D   C   B   A   
    1.42              |   |   |   |   |   |   |   |
    1.43          +-----------------------------------+
    1.44 @@ -245,11 +268,13 @@
    1.45          |                     z |           |          
    1.46          +-----------------------------------+
    1.47              |   |   |   |   |   |   |   |
    1.48 -            H   G   F   E   D   C   B   A        *
    1.49 +            H   G   F   E   D   C   B   A
    1.50 +     </pre>
    1.51       * @param w writer to write the board to
    1.52       * @exception IOException if communiction with writer fails
    1.53       */
    1.54 -    public void write (java.io.Writer w) throws java.io.IOException {
    1.55 +    public void write (Writer w) throws IOException {
    1.56 +        write(w, 3, 1);
    1.57      }
    1.58      
    1.59      /** This will print the board with provided spacing.
    1.60 @@ -267,46 +292,65 @@
    1.61       *  +----+
    1.62       * </pre>
    1.63       */
    1.64 -    private void write (StringBuffer sb, int spaceX, int spaceY) {
    1.65 -        
    1.66 -        // TODO rethink
    1.67 -        int cellSizeX = 1 + spaceX;
    1.68 -        int cellSizeY = 1 + spaceY;
    1.69 -        
    1.70 -        StringBuffer[] data = new StringBuffer[1 + cellSizeX * 9];
    1.71 -        for (int i = 0; i < data.length; i++) {
    1.72 -            data[i] = new StringBuffer (34);
    1.73 -            
    1.74 -            for (int grid = 1; grid < 34; grid += 4) {
    1.75 -                data[i].setCharAt (grid, '+');
    1.76 +    private void write (Writer w, int spaceX, int spaceY) throws IOException {
    1.77 +        int width = spaceX * 9 + 10;
    1.78 +        int height = spaceY * 9 + 10;
    1.79 +        char[][] desk = new char[height][];
    1.80 +        for (int i = 0; i < height; i++) {
    1.81 +            desk[i] = new char[width];
    1.82 +            for (int j = 0; j < width; j++) {
    1.83 +                if (i % (spaceY + 1) == 0 && j % (spaceX + 1) == 0) {
    1.84 +                    desk[i][j] = '+';
    1.85 +                } else {
    1.86 +                    desk[i][j] = ' ';
    1.87 +                }
    1.88              }
    1.89 +            desk[i][0] = '|';
    1.90 +            desk[i][width - 1] = '|';
    1.91          }
    1.92 -        
    1.93 -        for (Player p : players) {
    1.94 -            data[p.getY()].setCharAt(p.getY() * 2, 'P');
    1.95 +        for (int i = 1; i < width - 1; i++) {
    1.96 +            desk[0][i] = '-';
    1.97 +            desk[height -1][i] = '-';
    1.98 +        }
    1.99 +        desk[0][0] = '+';
   1.100 +        desk[0][width - 1] = '+';
   1.101 +        desk[height - 1][0] = '+';
   1.102 +        desk[height - 1][width - 1] = '+';
   1.103 +
   1.104 +        {
   1.105 +            int player = 'P';
   1.106 +            for (Player p : players) {
   1.107 +                int px = p.getX() / 2;
   1.108 +                int py = p.getY() / 2;
   1.109 +                desk
   1.110 +                    [1 + py * (spaceY + 1) + spaceY / 2]
   1.111 +                    [1 + px * (spaceX + 1) + spaceX / 2] = (char) player++;
   1.112 +            }
   1.113          }
   1.114          
   1.115          for (Fence f : fences) {
   1.116              switch (f.getOrientation()) {
   1.117                  case HORIZONTAL:
   1.118                      for (int i = -1; i <= 1; i++) {
   1.119 -                        data[f.getY()].setCharAt (f.getX () + i, '-');
   1.120 +//                        data[f.getY()].setCharAt (f.getX () + i, '-');
   1.121                      }
   1.122                      break;
   1.123                  case VERTICAL:
   1.124                      for (int i = -1; i <= 1; i++) {
   1.125 -                        data[f.getY() + i].setCharAt (f.getX (), '|');
   1.126 +//                        data[f.getY() + i].setCharAt (f.getX (), '|');
   1.127                      }
   1.128                      break;
   1.129                  default: 
   1.130                      throw new IllegalStateException ("Unkown orientation: " + f.getOrientation ()); // NOI18N
   1.131              }
   1.132          }
   1.133 -        
   1.134 -        StringBuffer result = new StringBuffer (20 * 20);
   1.135 -        /*
   1.136 -        for (int i = 0; i < )
   1.137 -         */
   1.138 +
   1.139 +        for (int y = 0; y < height; y++) {
   1.140 +            for (int x = 0; x < width; x++) {
   1.141 +                w.write(desk[y][x]);
   1.142 +            }
   1.143 +            w.write(System.getProperty("line.separator")); // NOI18N
   1.144 +        }
   1.145      }
   1.146      
   1.147      //
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/quoridor/src/test/java/cz/xelfi/quoridor/SerializeTest.java	Mon May 11 10:41:52 2009 +0200
     2.3 @@ -0,0 +1,69 @@
     2.4 +/*
     2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 + *
     2.7 + * The contents of this file are subject to the terms of either the GNU
     2.8 + * General Public License Version 2 only ("GPL") or the Common
     2.9 + * Development and Distribution License("CDDL") (collectively, the
    2.10 + * "License"). You may not use this file except in compliance with the
    2.11 + * License. You can obtain a copy of the License at
    2.12 + * http://www.netbeans.org/cddl-gplv2.html
    2.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    2.14 + * specific language governing permissions and limitations under the
    2.15 + * License.  When distributing the software, include this License Header
    2.16 + * Notice in each file and include the License file at
    2.17 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    2.18 + * particular file as subject to the "Classpath" exception as provided
    2.19 + * by Sun in the GPL Version 2 section of the License file that
    2.20 + * accompanied this code. If applicable, add the following below the
    2.21 + * License Header, with the fields enclosed by brackets [] replaced by
    2.22 + * your own identifying information:
    2.23 + * "Portions Copyrighted [year] [name of copyright owner]"
    2.24 + *
    2.25 + * Contributor(s):
    2.26 + *
    2.27 + * Portions Copyrighted 2009 Jaroslav Tulach
    2.28 + */
    2.29 +package cz.xelfi.quoridor;
    2.30 +
    2.31 +import cz.xelfi.quoridor.Board.Player.Direction;
    2.32 +import java.io.IOException;
    2.33 +import java.io.StringReader;
    2.34 +import java.io.StringWriter;
    2.35 +import java.util.logging.Level;
    2.36 +import java.util.logging.Logger;
    2.37 +
    2.38 +/** Basic tests in simple configuration.
    2.39 + *
    2.40 + * @author Jaroslav Tulach
    2.41 + */
    2.42 +public class SerializeTest extends BoardCase {
    2.43 +    public SerializeTest (String testName) {
    2.44 +        super (testName);
    2.45 +    }
    2.46 +
    2.47 +    @Override
    2.48 +    protected Board move(Board b, int player, Direction... where) throws IllegalPositionException {
    2.49 +        try {
    2.50 +            Board read = reread(b);
    2.51 +            return read.move(b.getPlayers().get(player), where);
    2.52 +        } catch (IOException ex) {
    2.53 +            throw new IllegalStateException(ex);
    2.54 +        }
    2.55 +    }
    2.56 +    protected Board fence(Board b, int player, char x, int y, Board.Fence.Orientation orie)
    2.57 +    throws IllegalPositionException {
    2.58 +        try {
    2.59 +            Board read = reread(b);
    2.60 +            return read.fence(b.getPlayers().get(player), x, y, orie);
    2.61 +        } catch (IOException ex) {
    2.62 +            throw new IllegalStateException(ex);
    2.63 +        }
    2.64 +    }
    2.65 +
    2.66 +    private Board reread(Board b) throws IOException {
    2.67 +        StringWriter w = new StringWriter();
    2.68 +        b.write(w);
    2.69 +        w.close();
    2.70 +        return Board.read(new StringReader(w.toString()));
    2.71 +    }
    2.72 +}