Cannot place the same fence twice
authorJaroslav Tulach <jtulach@netbeans.org>
Tue, 22 Sep 2009 23:06:16 +0200
changeset 107152aedcc45d0
parent 106 7d090f2c5b91
child 108 41905c272ad9
Cannot place the same fence twice
quoridor/src/main/java/cz/xelfi/quoridor/Board.java
quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java
     1.1 --- a/quoridor/src/main/java/cz/xelfi/quoridor/Board.java	Sun Sep 20 22:33:45 2009 +0200
     1.2 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Board.java	Tue Sep 22 23:06:16 2009 +0200
     1.3 @@ -302,13 +302,15 @@
     1.4          if (player.getFences () == 0) {
     1.5              throw new IllegalPositionException ("Not enough fences: " + player); // NOI18N
     1.6          }
     1.7 -        
     1.8 +
     1.9          int index = players.indexOf (player);
    1.10          Player[] arr = players.toArray (new Player[0]);
    1.11          arr[index] = new Player (arr[index].getXInternal(), arr[index].getYInternal(), arr[index].getFences() - 1, arr[index].endDirection);
    1.12          
    1.13          HashSet<Fence> fen = new HashSet<Fence> (this.fences);
    1.14 -        fen.add (fence);
    1.15 +        if (!fen.add (fence)) {
    1.16 +            throw new IllegalPositionException ("Fence already prsent: " + fence); // NOI18N
    1.17 +        }
    1.18          
    1.19          return new Board (turn + 1, Arrays.asList (arr), fen);
    1.20      }
     2.1 --- a/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java	Sun Sep 20 22:33:45 2009 +0200
     2.2 +++ b/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java	Tue Sep 22 23:06:16 2009 +0200
     2.3 @@ -144,6 +144,16 @@
     2.4              assertEquals("Column", 'H', f.getColumn());
     2.5          }
     2.6      }
     2.7 +    public void testTwoFencesInTheSamePosition() throws IllegalPositionException {
     2.8 +        assertEquals ("No on board", 0, board.getFences ().size ());
     2.9 +        Board one = board.apply(Move.fence('A', 1, Orientation.HORIZONTAL));
    2.10 +        try {
    2.11 +            Board snd = one.apply(Move.fence('A', 1, Orientation.HORIZONTAL));
    2.12 +            fail("Cannot place fence twice to the same place");
    2.13 +        } catch (IllegalPositionException ex) {
    2.14 +            // OK
    2.15 +        }
    2.16 +    }
    2.17      
    2.18      public void testSelfDestructionForbidden() throws Exception {
    2.19          Board b = board;