1.1 --- a/quoridor/src/main/java/cz/xelfi/quoridor/Board.java Tue Sep 22 23:06:16 2009 +0200
1.2 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Board.java Thu Jan 07 22:34:17 2010 +0100
1.3 @@ -42,6 +42,7 @@
1.4 import java.util.HashSet;
1.5 import java.util.List;
1.6 import java.util.Set;
1.7 +import java.util.TreeSet;
1.8 import java.util.regex.Matcher;
1.9 import java.util.regex.Pattern;
1.10
1.11 @@ -352,7 +353,11 @@
1.12 * @return board object, if the string can be read
1.13 * @throws IllegalPositionException if the string does not represent the board
1.14 */
1.15 - public static Board valueOf(String board) throws IllegalPositionException {
1.16 + public static Board valueOf(String board) {
1.17 + return new Board(board);
1.18 + }
1.19 +
1.20 + public static Board picture2board(String board) throws IllegalPositionException {
1.21 try {
1.22 return read(new StringReader(board));
1.23 } catch (IOException ex) {
1.24 @@ -697,6 +702,10 @@
1.25 */
1.26 @Override
1.27 public String toString() {
1.28 + return Board.board2HashCode(this);
1.29 + }
1.30 +
1.31 + public String boardToPicture() {
1.32 StringWriter w = new StringWriter();
1.33 try {
1.34 write(w);
1.35 @@ -863,4 +872,91 @@
1.36 return 17 * y + x;
1.37 }
1.38
1.39 + public Board(String hashCode) throws IllegalStateException{
1.40 + this.fences = new HashSet<Fence>();
1.41 + if((hashCode != null) && (hashCode.length() > 6)){
1.42 + char[]c = hashCode.toCharArray();
1.43 + this.players = Collections.unmodifiableList (Arrays.asList (new Player[] {
1.44 + new Player ((c[0]-'A')*2, (c[1]-'0')*2, c[2]-'a', Player.Direction.NORTH),
1.45 + new Player ((c[3]-'A')*2, (c[4]-'0')*2, c[5]-'a', Player.Direction.SOUTH),
1.46 + }));
1.47 + if(c[6]=='w'){
1.48 + this.turn = 0;
1.49 + this.winner = null;
1.50 + }else if(c[6]=='b'){
1.51 + this.turn = 1;
1.52 + this.winner = null;
1.53 + }else if(c[6]=='W'){
1.54 + this.turn = 0;
1.55 + this.winner = this.players.get(0);
1.56 + }else if(c[6]=='B'){
1.57 + this.turn = 1;
1.58 + this.winner = this.players.get(1);
1.59 + }else{
1.60 + this.turn = 0;
1.61 + this.winner = null;
1.62 + }
1.63 + for(int i=7; i<c.length;i+=2){
1.64 + int f = Integer.parseInt(hashCode.substring(i, i+2),16);
1.65 + Fence.Orientation o = Fence.Orientation.HORIZONTAL;
1.66 + if(f > 64){
1.67 + o = Fence.Orientation.VERTICAL;
1.68 + f -= 64;
1.69 + }
1.70 + fences.add(new Fence((f/8)*2+1, (f%8)*2+1,o));
1.71 + }
1.72 + }else{
1.73 + this.players = Collections.unmodifiableList (Arrays.asList (new Player[] {
1.74 + new Player (8,0,10,Player.Direction.NORTH),
1.75 + new Player (8,16,10,Player.Direction.SOUTH),
1.76 + }));
1.77 + this.winner = null;
1.78 + this.turn = 0;
1.79 + }
1.80 + try {
1.81 + this.occupied = computeOccupied (players, fences);
1.82 + } catch (IllegalPositionException ex) {
1.83 + throw new IllegalStateException (ex.getMessage ());
1.84 + }
1.85 + }
1.86 +
1.87 + public static String board2HashCode(Board b){
1.88 + StringBuilder sb = new StringBuilder();
1.89 + for(Player p: b.getPlayers()){
1.90 + sb.append((char)(p.getColumn() + 'A'));
1.91 + sb.append((char)(p.getRow() + '0'));
1.92 + sb.append((char)(p.getFences() + 'a'));
1.93 + }
1.94 + Player winner = b.getWinner();
1.95 + if(winner == null){
1.96 + if(b.players.indexOf(b.getCurrentPlayer())==0)
1.97 + sb.append('w');
1.98 + else if(b.players.indexOf(b.getCurrentPlayer())==1)
1.99 + sb.append('b');
1.100 + else
1.101 + sb.append('n');
1.102 + }else{
1.103 + if(b.players.indexOf(winner)==0)
1.104 + sb.append('W');
1.105 + else if(b.players.indexOf(winner)==1)
1.106 + sb.append('B');
1.107 + else
1.108 + sb.append('N');
1.109 + }
1.110 +
1.111 + TreeSet<Integer> fences = new TreeSet<Integer>();
1.112 + for(Fence f: b.getFences()){
1.113 + int a = (f.getColumn() - 'A')*8 + (f.getRow()-1);
1.114 + if(f.getOrientation().equals(Fence.Orientation.VERTICAL))
1.115 + a+=64;
1.116 + fences.add(a);
1.117 + }
1.118 + for(int f: fences){
1.119 + if(f<16)
1.120 + sb.append('0');
1.121 + sb.append(Integer.toHexString(f));
1.122 + }
1.123 + return sb.toString();
1.124 + }
1.125 +
1.126 }