quoridor/src/main/java/cz/xelfi/quoridor/Board.java
changeset 48 69e897fe8140
parent 40 e45bc8ad2eaf
child 75 6802034b7a6f
     1.1 --- a/quoridor/src/main/java/cz/xelfi/quoridor/Board.java	Wed Jul 29 21:35:04 2009 +0200
     1.2 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Board.java	Sun Aug 30 14:37:47 2009 +0200
     1.3 @@ -31,6 +31,7 @@
     1.4  import java.io.EOFException;
     1.5  import java.io.IOException;
     1.6  import java.io.Reader;
     1.7 +import java.io.StringReader;
     1.8  import java.io.StringWriter;
     1.9  import java.io.Writer;
    1.10  import java.util.ArrayList;
    1.11 @@ -293,6 +294,15 @@
    1.12      //
    1.13  
    1.14      private static final Pattern northSouthPattern = Pattern.compile("(\\+(\\|*)(\\*)?-+\\+)");
    1.15 +
    1.16 +    /** Reads the board from a reader. Opposite operation to {@link #write(java.io.Writer)}.
    1.17 +     *
    1.18 +     * @param r the reader
    1.19 +     * @return the read board
    1.20 +     * @throws IOException if I/O error occurs
    1.21 +     * @throws IllegalPositionException if the reader does not contain description
    1.22 +     *   of the board
    1.23 +     */
    1.24      public static Board read(Reader r) throws IOException, IllegalPositionException {
    1.25          BufferedReader b = new BufferedReader(r);
    1.26          for (;;) {
    1.27 @@ -307,6 +317,24 @@
    1.28          }
    1.29      }
    1.30  
    1.31 +    /** Translates the string into board, if possible. String created by
    1.32 +     * use of {@link #toString()} is accepted, more information about the
    1.33 +     * format is avaliable in the description of {@link #write(java.io.Writer)}
    1.34 +     * method.
    1.35 +     *
    1.36 +     * @param board string to analyze
    1.37 +     * @return board object, if the string can be read
    1.38 +     * @throws IllegalPositionException if the string does not represent the board
    1.39 +     */
    1.40 +    public static Board valueOf(String board) throws IllegalPositionException {
    1.41 +        try {
    1.42 +            return read(new StringReader(board));
    1.43 +        } catch (IOException ex) {
    1.44 +            // shall not happen, StringReader does not throw IOException
    1.45 +            throw (IllegalPositionException)new IllegalPositionException(ex.getMessage()).initCause(ex);
    1.46 +        }
    1.47 +    }
    1.48 +
    1.49      private static int assertChar(String s, int pos, char... ch) throws IOException {
    1.50          if (s.length() >= pos) {
    1.51              for (int i = 0; i < ch.length; i++) {
    1.52 @@ -635,6 +663,12 @@
    1.53          return false;
    1.54      }
    1.55  
    1.56 +    /** Converts the board into string representation. For more information
    1.57 +     * about the format see {@link #write(java.io.Writer)}. To read the
    1.58 +     * string back use {@link #valueOf(java.lang.String)}.
    1.59 +     *
    1.60 +     * @return string representing the board
    1.61 +     */
    1.62      @Override
    1.63      public String toString() {
    1.64          StringWriter w = new StringWriter();