Renaming Board.board2HashCode to getCode() statistics-and-elo
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 07 Jan 2010 22:51:17 +0100
branchstatistics-and-elo
changeset 179c5fbddc4c590
parent 178 4b78d4f028b3
child 180 c92831d4812c
Renaming Board.board2HashCode to getCode()
freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java
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/SerializeTest.java
statistics/src/main/java/cz/xelfi/quoridor/statistics/OpeningTree.java
webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java
     1.1 --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Thu Jan 07 22:34:17 2010 +0100
     1.2 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Thu Jan 07 22:51:17 2010 +0100
     1.3 @@ -38,6 +38,7 @@
     1.4  import cz.xelfi.quoridor.IllegalPositionException;
     1.5  import java.io.IOException;
     1.6  import java.io.InputStream;
     1.7 +import java.io.StringWriter;
     1.8  import java.net.URI;
     1.9  import java.util.Date;
    1.10  import java.util.HashMap;
    1.11 @@ -233,17 +234,27 @@
    1.12          }
    1.13          String bCode = null;
    1.14          try{
    1.15 -            bCode = stat.path("openings").path(Board.board2HashCode(b)+".check").queryParam("loginID", user.getId()).accept(MediaType.TEXT_PLAIN).get(String.class);
    1.16 +            bCode = stat.path("openings").path(b.getCode()+".check").queryParam("loginID", user.getId()).accept(MediaType.TEXT_PLAIN).get(String.class);
    1.17          }catch(Exception e){
    1.18              bCode = null;
    1.19          }
    1.20          if(bCode == null || "".equals(bCode))
    1.21 -            v = viewable("game.fmt", doc, "message", msg, "format", format, "board", b,"textPicture",b.boardToPicture());
    1.22 +            v = viewable("game.fmt", doc, "message", msg, "format", format, "board", b,"textPicture", boardToPicture(b));
    1.23          else
    1.24 -            v = viewable("game.fmt", doc, "message", msg, "format", format, "board", b,"textPicture",b.boardToPicture(),"bCode", bCode);
    1.25 +            v = viewable("game.fmt", doc, "message", msg, "format", format, "board", b,"textPicture", boardToPicture(b),"bCode", bCode);
    1.26          return resp.entity(v).build();
    1.27      }
    1.28  
    1.29 +    private static String boardToPicture(Board b) {
    1.30 +        StringWriter w = new StringWriter();
    1.31 +        try {
    1.32 +            b.write(w);
    1.33 +        } catch (IOException ex) {
    1.34 +            return ex.toString();
    1.35 +        }
    1.36 +        return w.toString();
    1.37 +    }
    1.38 +
    1.39      @GET
    1.40      @Path("games/{id}/move")
    1.41      @Produces(MediaType.TEXT_HTML)
    1.42 @@ -418,7 +429,7 @@
    1.43          @PathParam("code") String code,
    1.44          @QueryParam("fieldSize") @DefaultValue("40") int fieldSize
    1.45      ) throws IllegalPositionException {
    1.46 -        Board b = new Board(code);
    1.47 +        Board b = Board.valueOf(code);
    1.48          ResponseBuilder resp = Response.ok();
    1.49          CacheControl cc = new CacheControl();
    1.50          cc.setNoCache(true);
     2.1 --- a/quoridor/src/main/java/cz/xelfi/quoridor/Board.java	Thu Jan 07 22:34:17 2010 +0100
     2.2 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Board.java	Thu Jan 07 22:51:17 2010 +0100
     2.3 @@ -32,7 +32,6 @@
     2.4  import java.io.IOException;
     2.5  import java.io.Reader;
     2.6  import java.io.StringReader;
     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 @@ -357,15 +356,6 @@
    2.12          return new Board(board);
    2.13      }
    2.14  
    2.15 -    public static Board picture2board(String board) throws IllegalPositionException {
    2.16 -        try {
    2.17 -            return read(new StringReader(board));
    2.18 -        } catch (IOException ex) {
    2.19 -            // shall not happen, StringReader does not throw IOException
    2.20 -            throw (IllegalPositionException)new IllegalPositionException(ex.getMessage()).initCause(ex);
    2.21 -        }
    2.22 -    }
    2.23 -
    2.24      private static int assertChar(String s, int pos, char... ch) throws IOException {
    2.25          if (s.length() >= pos) {
    2.26              for (int i = 0; i < ch.length; i++) {
    2.27 @@ -702,20 +692,9 @@
    2.28       */
    2.29      @Override
    2.30      public String toString() {
    2.31 -        return Board.board2HashCode(this);
    2.32 +        return getCode();
    2.33      }
    2.34  
    2.35 -    public String boardToPicture() {
    2.36 -        StringWriter w = new StringWriter();
    2.37 -        try {
    2.38 -            write(w);
    2.39 -        } catch (IOException ex) {
    2.40 -            return ex.toString();
    2.41 -        }
    2.42 -        return w.toString();
    2.43 -    }
    2.44 -
    2.45 -    
    2.46      //
    2.47      // Validation methods
    2.48      //
    2.49 @@ -872,7 +851,7 @@
    2.50          return 17 * y + x;
    2.51      }
    2.52  
    2.53 -    public Board(String hashCode) throws IllegalStateException{
    2.54 +    Board(String hashCode) throws IllegalStateException{
    2.55          this.fences = new HashSet<Fence>();
    2.56          if((hashCode != null) && (hashCode.length() > 6)){
    2.57              char[]c = hashCode.toCharArray();
    2.58 @@ -920,7 +899,8 @@
    2.59          }
    2.60      }
    2.61  
    2.62 -    public static String board2HashCode(Board b){
    2.63 +    public final String getCode() {
    2.64 +        Board b = this;
    2.65          StringBuilder sb = new StringBuilder();
    2.66          for(Player p: b.getPlayers()){
    2.67              sb.append((char)(p.getColumn() + 'A'));
     3.1 --- a/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java	Thu Jan 07 22:34:17 2010 +0100
     3.2 +++ b/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java	Thu Jan 07 22:51:17 2010 +0100
     3.3 @@ -27,6 +27,8 @@
     3.4  
     3.5  import cz.xelfi.quoridor.Fence.Orientation;
     3.6  import cz.xelfi.quoridor.Player.Direction;
     3.7 +import java.io.IOException;
     3.8 +import java.io.StringReader;
     3.9  import java.util.List;
    3.10  import junit.framework.TestCase;
    3.11  
    3.12 @@ -200,7 +202,7 @@
    3.13  "\n" +
    3.14  "                         [S]                 \n";
    3.15  
    3.16 -        b = Board.picture2board(b).toString();
    3.17 +        b = picture2board(b).toString();
    3.18          Board begin = Board.valueOf(b);
    3.19  
    3.20          try {
    3.21 @@ -498,4 +500,8 @@
    3.22          assertFalse(f4.equals(f5));
    3.23          assertFalse(f3.equals(f5));
    3.24      }
    3.25 +    static Board picture2board(String text) throws IOException, IllegalPositionException {
    3.26 +        StringReader sr = new StringReader(text);
    3.27 +        return Board.read(sr);
    3.28 +    }
    3.29  }
     4.1 --- a/quoridor/src/test/java/cz/xelfi/quoridor/SerializeTest.java	Thu Jan 07 22:34:17 2010 +0100
     4.2 +++ b/quoridor/src/test/java/cz/xelfi/quoridor/SerializeTest.java	Thu Jan 07 22:51:17 2010 +0100
     4.3 @@ -27,7 +27,6 @@
     4.4  
     4.5  import cz.xelfi.quoridor.Player.Direction;
     4.6  import java.io.IOException;
     4.7 -import java.io.StringReader;
     4.8  import java.io.StringWriter;
     4.9  
    4.10  /** Basic tests in simple configuration.
    4.11 @@ -72,7 +71,7 @@
    4.12          StringWriter w = new StringWriter();
    4.13          b.write(w);
    4.14          w.close();
    4.15 -        return Board.picture2board(w.toString());
    4.16 +        return picture2board(w.toString());
    4.17          //return Board.valueOf(w.toString());
    4.18      }
    4.19  
     5.1 --- a/statistics/src/main/java/cz/xelfi/quoridor/statistics/OpeningTree.java	Thu Jan 07 22:34:17 2010 +0100
     5.2 +++ b/statistics/src/main/java/cz/xelfi/quoridor/statistics/OpeningTree.java	Thu Jan 07 22:51:17 2010 +0100
     5.3 @@ -69,8 +69,8 @@
     5.4                  mirrorParentNode = mirrorNode;
     5.5                  board = board.apply(move.getMove());
     5.6                  mirrorBoard = mirrorBoard.apply(move.getMove().getMirrorMove());
     5.7 -                String hashCode = Board.board2HashCode(board);
     5.8 -                String mirrorHashCode = Board.board2HashCode(mirrorBoard);
     5.9 +                String hashCode = board.getCode();
    5.10 +                String mirrorHashCode = mirrorBoard.getCode();
    5.11                  node = nodes.get(hashCode);
    5.12                  if(node == null){
    5.13                      node = new OpeningTreeNode(hashCode);
     6.1 --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java	Thu Jan 07 22:34:17 2010 +0100
     6.2 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java	Thu Jan 07 22:51:17 2010 +0100
     6.3 @@ -26,6 +26,8 @@
     6.4  
     6.5  package cz.xelfi.quoridor.webidor;
     6.6  
     6.7 +import cz.xelfi.quoridor.IllegalPositionException;
     6.8 +import java.io.StringWriter;
     6.9  import com.sun.jersey.api.client.GenericType;
    6.10  import com.sun.jersey.api.client.UniformInterfaceException;
    6.11  import com.sun.jersey.core.header.MediaTypes;
    6.12 @@ -38,6 +40,7 @@
    6.13  import java.io.FileOutputStream;
    6.14  import java.io.FileReader;
    6.15  import java.io.IOException;
    6.16 +import java.io.StringReader;
    6.17  import java.util.List;
    6.18  import java.util.Map;
    6.19  import javax.ws.rs.core.MediaType;
    6.20 @@ -201,7 +204,7 @@
    6.21  
    6.22          class GMap extends GenericType<Map<String,Object>>{}
    6.23          String text = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_PLAIN).get(String.class);
    6.24 -        text = (new Board(text)).boardToPicture();
    6.25 +        text = (boardToPicture(Board.valueOf(text)));
    6.26          if (text.indexOf("-----") == -1) {
    6.27              fail("Expecting board:\n" + text);
    6.28          }
    6.29 @@ -209,12 +212,25 @@
    6.30          String sGame = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(String.class);
    6.31          assertNotNull("Game really returned", readGame);
    6.32  //        assertEquals("Same game as in text representation", readGame.getBoard(), Board.valueOf(text));
    6.33 -        assertEquals("Same game as in text representation", readGame.getBoard(), Board.picture2board(text));
    6.34 +        assertEquals("Same game as in text representation", readGame.getBoard(), picture2board(text));
    6.35  //        assertEquals("It is same as text of our game", readGame.getBoard().toString(), text);
    6.36 -        assertEquals("It is same as text of our game", readGame.getBoard().boardToPicture(), text);
    6.37 +        assertEquals("It is same as text of our game", boardToPicture(readGame.getBoard()), text);
    6.38  
    6.39          assertEquals(Move.NORTH, readGame.getMoves().get(0).getMove());
    6.40          assertEquals(Move.SOUTH, readGame.getMoves().get(1).getMove());
    6.41      }
    6.42 +    private static String boardToPicture(Board b) {
    6.43 +        StringWriter w = new StringWriter();
    6.44 +        try {
    6.45 +            b.write(w);
    6.46 +        } catch (IOException ex) {
    6.47 +            return ex.toString();
    6.48 +        }
    6.49 +        return w.toString();
    6.50 +    }
    6.51  
    6.52 +    private static Board picture2board(String text) throws IOException, IllegalPositionException {
    6.53 +        StringReader sr = new StringReader(text);
    6.54 +        return Board.read(sr);
    6.55 +    }
    6.56  }