Serialization and deserialization of fences seems to be OK
authorJaroslav Tulach <jtulach@netbeans.org>
Mon, 11 May 2009 16:17:14 +0200
changeset 1240177b10ed87
parent 11 05783fbf342c
child 13 9602f7fc5fcd
Serialization and deserialization of fences seems to be OK
quoridor/TEST-cz.xelfi.quoridor.BoardCase.xml
quoridor/src/main/java/cz/xelfi/quoridor/Board.java
quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/quoridor/TEST-cz.xelfi.quoridor.BoardCase.xml	Mon May 11 16:17:14 2009 +0200
     1.3 @@ -0,0 +1,64 @@
     1.4 +<?xml version="1.0" encoding="UTF-8" ?>
     1.5 +<testsuite errors="0" failures="1" hostname="logouticek" name="cz.xelfi.quoridor.BoardCase" tests="1" time="0.076" timestamp="2009-05-11T13:38:10">
     1.6 +  <properties>
     1.7 +    <property name="java.runtime.name" value="Java(TM) SE Runtime Environment" />
     1.8 +    <property name="sun.boot.library.path" value="/usr/lib/jvm/java-1.6.0-sun-1.6.0.06/jre/lib/i386" />
     1.9 +    <property name="java.vm.version" value="10.0-b22" />
    1.10 +    <property name="java.vm.vendor" value="Sun Microsystems Inc." />
    1.11 +    <property name="java.vendor.url" value="http://java.sun.com/" />
    1.12 +    <property name="path.separator" value=":" />
    1.13 +    <property name="java.vm.name" value="Java HotSpot(TM) Server VM" />
    1.14 +    <property name="file.encoding.pkg" value="sun.io" />
    1.15 +    <property name="user.country" value="CZ" />
    1.16 +    <property name="sun.java.launcher" value="SUN_STANDARD" />
    1.17 +    <property name="sun.os.patch.level" value="unknown" />
    1.18 +    <property name="test" value="BoardCase" />
    1.19 +    <property name="java.vm.specification.name" value="Java Virtual Machine Specification" />
    1.20 +    <property name="user.dir" value="/home/jarda/src/quoridor/quoridor" />
    1.21 +    <property name="java.runtime.version" value="1.6.0_06-b02" />
    1.22 +    <property name="java.awt.graphicsenv" value="sun.awt.X11GraphicsEnvironment" />
    1.23 +    <property name="basedir" value="/home/jarda/src/quoridor/quoridor" />
    1.24 +    <property name="java.endorsed.dirs" value="/usr/lib/jvm/java-1.6.0-sun-1.6.0.06/jre/lib/endorsed" />
    1.25 +    <property name="os.arch" value="i386" />
    1.26 +    <property name="java.io.tmpdir" value="/tmp" />
    1.27 +    <property name="line.separator" value="
    1.28 +" />
    1.29 +    <property name="java.vm.specification.vendor" value="Sun Microsystems Inc." />
    1.30 +    <property name="os.name" value="Linux" />
    1.31 +    <property name="sun.jnu.encoding" value="UTF-8" />
    1.32 +    <property name="java.library.path" value="/usr/lib/jvm/java-1.6.0-sun-1.6.0.06/jre/lib/i386/server:/usr/lib/jvm/java-1.6.0-sun-1.6.0.06/jre/lib/i386:/usr/lib/jvm/java-1.6.0-sun-1.6.0.06/jre/../lib/i386:/usr/lib/jvm/java-1.6.0-sun-1.6.0.06/jre/lib/i386/client:/usr/lib/jvm/java-1.6.0-sun-1.6.0.06/jre/lib/i386:/usr/lib/jvm/java-1.6.0-sun-1.6.0.06/jre/../lib/i386:/usr/java/packages/lib/i386:/lib:/usr/lib" />
    1.33 +    <property name="java.specification.name" value="Java Platform API Specification" />
    1.34 +    <property name="java.class.version" value="50.0" />
    1.35 +    <property name="sun.management.compiler" value="HotSpot Tiered Compilers" />
    1.36 +    <property name="os.version" value="2.6.24.7-laptop-1mnb" />
    1.37 +    <property name="user.home" value="/home/jarda" />
    1.38 +    <property name="user.timezone" value="" />
    1.39 +    <property name="java.awt.printerjob" value="sun.print.PSPrinterJob" />
    1.40 +    <property name="file.encoding" value="UTF-8" />
    1.41 +    <property name="java.specification.version" value="1.6" />
    1.42 +    <property name="user.name" value="jarda" />
    1.43 +    <property name="java.class.path" value="/home/jarda/bin/netbeans/java2/ant/patches/72080.jar:/home/jarda/bin/netbeans/java2/ant/patches/ant-42275.jar:/home/jarda/bin/netbeans/java2/ant/lib/ant.jar:/home/jarda/bin/netbeans/java2/ant/lib/ant-junit.jar:/home/jarda/src/quoridor/quoridor/target/classes:/home/jarda/src/quoridor/quoridor/src/main/java:/home/jarda/src/quoridor/quoridor/src/main/resources:/home/jarda/src/quoridor/quoridor/target/test-classes:/home/jarda/src/quoridor/quoridor/src/test/java:/home/jarda/.m2/repository/org/netbeans/api/org-netbeans-modules-nbjunit/RELEASE65/org-netbeans-modules-nbjunit-RELEASE65.jar:/home/jarda/.m2/repository/org/netbeans/api/org-netbeans-libs-junit4/RELEASE65/org-netbeans-libs-junit4-RELEASE65.jar:/home/jarda/.m2/repository/junit/junit/4.5/junit-4.5.jar:/home/jarda/.m2/repository/org/netbeans/modules/org-netbeans-insane/RELEASE65/org-netbeans-insane-RELEASE65.jar" />
    1.44 +    <property name="java.vm.specification.version" value="1.0" />
    1.45 +    <property name="sun.arch.data.model" value="32" />
    1.46 +    <property name="java.home" value="/usr/lib/jvm/java-1.6.0-sun-1.6.0.06/jre" />
    1.47 +    <property name="java.specification.vendor" value="Sun Microsystems Inc." />
    1.48 +    <property name="user.language" value="cs" />
    1.49 +    <property name="java.vm.info" value="mixed mode" />
    1.50 +    <property name="java.version" value="1.6.0_06" />
    1.51 +    <property name="java.ext.dirs" value="/usr/lib/jvm/java-1.6.0-sun-1.6.0.06/jre/lib/ext:/usr/java/packages/lib/ext" />
    1.52 +    <property name="sun.boot.class.path" value="/usr/lib/jvm/java-1.6.0-sun-1.6.0.06/jre/lib/resources.jar:/usr/lib/jvm/java-1.6.0-sun-1.6.0.06/jre/lib/rt.jar:/usr/lib/jvm/java-1.6.0-sun-1.6.0.06/jre/lib/sunrsasign.jar:/usr/lib/jvm/java-1.6.0-sun-1.6.0.06/jre/lib/jsse.jar:/usr/lib/jvm/java-1.6.0-sun-1.6.0.06/jre/lib/jce.jar:/usr/lib/jvm/java-1.6.0-sun-1.6.0.06/jre/lib/charsets.jar:/usr/lib/jvm/java-1.6.0-sun-1.6.0.06/jre/classes" />
    1.53 +    <property name="java.vendor" value="Sun Microsystems Inc." />
    1.54 +    <property name="file.separator" value="/" />
    1.55 +    <property name="java.vendor.url.bug" value="http://java.sun.com/cgi-bin/bugreport.cgi" />
    1.56 +    <property name="forkMode" value="once" />
    1.57 +    <property name="sun.cpu.endian" value="little" />
    1.58 +    <property name="sun.io.unicode.encoding" value="UnicodeLittle" />
    1.59 +    <property name="sun.cpu.isalist" value="" />
    1.60 +  </properties>
    1.61 +  <testcase classname="junit.framework.TestSuite$1" name="warning" time="0.0070">
    1.62 +    <failure message="Class cz.xelfi.quoridor.BoardCase has no public constructor TestCase(String name) or TestCase()" type="junit.framework.AssertionFailedError">junit.framework.AssertionFailedError: Class cz.xelfi.quoridor.BoardCase has no public constructor TestCase(String name) or TestCase()
    1.63 +</failure>
    1.64 +  </testcase>
    1.65 +  <system-out><![CDATA[]]></system-out>
    1.66 +  <system-err><![CDATA[]]></system-err>
    1.67 +</testsuite>
     2.1 --- a/quoridor/src/main/java/cz/xelfi/quoridor/Board.java	Mon May 11 12:09:34 2009 +0200
     2.2 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Board.java	Mon May 11 16:17:14 2009 +0200
     2.3 @@ -31,6 +31,7 @@
     2.4  import java.io.EOFException;
     2.5  import java.io.IOException;
     2.6  import java.io.Reader;
     2.7 +import java.io.StringWriter;
     2.8  import java.io.Writer;
     2.9  import java.util.ArrayList;
    2.10  import java.util.Arrays;
    2.11 @@ -40,6 +41,8 @@
    2.12  import java.util.HashSet;
    2.13  import java.util.List;
    2.14  import java.util.Set;
    2.15 +import java.util.logging.Level;
    2.16 +import java.util.logging.Logger;
    2.17  import java.util.regex.Matcher;
    2.18  import java.util.regex.Pattern;
    2.19  
    2.20 @@ -248,10 +251,15 @@
    2.21          }
    2.22      }
    2.23  
    2.24 -    private static void assertChar(String s, int pos, char ch) throws IOException {
    2.25 -        if (s.length() < pos || s.charAt(pos) != ch) {
    2.26 -            throw new IOException("Not found " + ch + " at " + pos + " in" + s);
    2.27 +    private static int assertChar(String s, int pos, char... ch) throws IOException {
    2.28 +        if (s.length() >= pos) {
    2.29 +            for (int i = 0; i < ch.length; i++) {
    2.30 +                if (ch[i] == s.charAt(pos)) {
    2.31 +                    return i;
    2.32 +                }
    2.33 +            }
    2.34          }
    2.35 +        throw new IOException("Not found " + ch[0] + " at " + pos + " in" + s);
    2.36      }
    2.37  
    2.38      private static void findPlayer(
    2.39 @@ -273,8 +281,10 @@
    2.40          final int spaceY = 1;
    2.41  
    2.42          List<Player> players = new ArrayList<Player>();
    2.43 +        Set<Fence> fences = new HashSet<Fence>();
    2.44  
    2.45          StringBuffer sb = new StringBuffer();
    2.46 +        int row = 0;
    2.47          for (int y = 1; y < (spaceY + 1) * 9; y++) {
    2.48              String s = b.readLine();
    2.49              if (s == null) {
    2.50 @@ -290,8 +300,20 @@
    2.51  
    2.52              if (y % (spaceY + 1) == 0) {
    2.53                  for (int x = 1; x < 9; x++) {
    2.54 -                    assertChar(s, from + (spaceX + 1) * x, '+');
    2.55 +                    switch (assertChar(s, from + (spaceX + 1) * x, '+', '-', '|')) {
    2.56 +                        case 1:
    2.57 +                            fences.add(new Fence(x * 2 - 1, row * 2 + 1, Fence.Orientation.HORIZONTAL));
    2.58 +                            break;
    2.59 +                        case 2:
    2.60 +                            fences.add(new Fence(x * 2 - 1, row * 2 + 1, Fence.Orientation.VERTICAL));
    2.61 +                            break;
    2.62 +                        case 0:
    2.63 +                            break;
    2.64 +                        default:
    2.65 +                            assert false;
    2.66 +                    }
    2.67                  }
    2.68 +                row++;
    2.69              } else {
    2.70                  String line = s.substring(from, to);
    2.71                  findPlayer(line, y, 'P', players, spaceX, Player.Direction.SOUTH, northFences);
    2.72 @@ -309,10 +331,10 @@
    2.73          }
    2.74          int index = players.get(0).getFences() == -1 ? 0 : 1;
    2.75          Player p = players.get(index);
    2.76 -        int fences = m.end(2) - m.start(2);
    2.77 -        players.set(index, new Player(p.getX(), p.getY(), fences, p.endDirection));
    2.78 +        int southFences = m.end(2) - m.start(2);
    2.79 +        players.set(index, new Player(p.getX(), p.getY(), southFences, p.endDirection));
    2.80  
    2.81 -        return new Board(0, players, Collections.<Fence>emptySet());
    2.82 +        return new Board(0, players, fences);
    2.83      }
    2.84  
    2.85  
    2.86 @@ -405,15 +427,17 @@
    2.87          }
    2.88          
    2.89          for (Fence f : fences) {
    2.90 +            int fx = (f.getX() / 2 + 1) * (spaceX + 1);
    2.91 +            int fy = (f.getY() / 2 + 1) * (spaceY + 1);
    2.92              switch (f.getOrientation()) {
    2.93                  case HORIZONTAL:
    2.94 -                    for (int i = -1; i <= 1; i++) {
    2.95 -//                        data[f.getY()].setCharAt (f.getX () + i, '-');
    2.96 +                    for (int i = -spaceX; i <= spaceX; i++) {
    2.97 +                        desk[fy][fx + i] = '-';
    2.98                      }
    2.99                      break;
   2.100                  case VERTICAL:
   2.101 -                    for (int i = -1; i <= 1; i++) {
   2.102 -//                        data[f.getY() + i].setCharAt (f.getX (), '|');
   2.103 +                    for (int i = -spaceY; i <= spaceY; i++) {
   2.104 +                        desk[fy + i][fx] = '|';
   2.105                      }
   2.106                      break;
   2.107                  default: 
   2.108 @@ -468,6 +492,18 @@
   2.109          }
   2.110          return false;
   2.111      }
   2.112 +
   2.113 +    @Override
   2.114 +    public String toString() {
   2.115 +        StringWriter w = new StringWriter();
   2.116 +        try {
   2.117 +            write(w);
   2.118 +        } catch (IOException ex) {
   2.119 +            return ex.toString();
   2.120 +        }
   2.121 +        return w.toString();
   2.122 +    }
   2.123 +
   2.124      
   2.125      //
   2.126      // Validation methods
   2.127 @@ -703,7 +739,7 @@
   2.128      } // end of Player
   2.129      
   2.130      /** Defines a fence. Its position is defined as position of its
   2.131 -     * center (index from 0-8 in both directions) and its orientation
   2.132 +     * center (index from 0-15 in both directions) and its orientation
   2.133       * either horizontal or vertical.
   2.134       */
   2.135      public static final class Fence extends Object {
     3.1 --- a/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java	Mon May 11 12:09:34 2009 +0200
     3.2 +++ b/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java	Mon May 11 16:17:14 2009 +0200
     3.3 @@ -127,7 +127,7 @@
     3.4      }
     3.5  
     3.6      public void testCannotJumpOverFence () throws Exception {
     3.7 -        Board b = board.fence (board.getPlayers ().get (0), 'D', 8, Board.Fence.Orientation.HORIZONTAL);
     3.8 +        Board b = fence (board, 0, 'D', 8, Board.Fence.Orientation.HORIZONTAL);
     3.9          try {
    3.10              move(b, 1, Board.Player.Direction.SOUTH);
    3.11              fail ("This shall not be allowed, as there is the fence");