# HG changeset patch # User Jaroslav Tulach # Date 1253653576 -7200 # Node ID 152aedcc45d004d93d159917cd98fb1e6efa6e70 # Parent 7d090f2c5b91820985e6b3524d1bf75b9b964575 Cannot place the same fence twice diff -r 7d090f2c5b91 -r 152aedcc45d0 quoridor/src/main/java/cz/xelfi/quoridor/Board.java --- a/quoridor/src/main/java/cz/xelfi/quoridor/Board.java Sun Sep 20 22:33:45 2009 +0200 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Board.java Tue Sep 22 23:06:16 2009 +0200 @@ -302,13 +302,15 @@ if (player.getFences () == 0) { throw new IllegalPositionException ("Not enough fences: " + player); // NOI18N } - + int index = players.indexOf (player); Player[] arr = players.toArray (new Player[0]); arr[index] = new Player (arr[index].getXInternal(), arr[index].getYInternal(), arr[index].getFences() - 1, arr[index].endDirection); HashSet fen = new HashSet (this.fences); - fen.add (fence); + if (!fen.add (fence)) { + throw new IllegalPositionException ("Fence already prsent: " + fence); // NOI18N + } return new Board (turn + 1, Arrays.asList (arr), fen); } diff -r 7d090f2c5b91 -r 152aedcc45d0 quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java --- a/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java Sun Sep 20 22:33:45 2009 +0200 +++ b/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java Tue Sep 22 23:06:16 2009 +0200 @@ -144,6 +144,16 @@ assertEquals("Column", 'H', f.getColumn()); } } + public void testTwoFencesInTheSamePosition() throws IllegalPositionException { + assertEquals ("No on board", 0, board.getFences ().size ()); + Board one = board.apply(Move.fence('A', 1, Orientation.HORIZONTAL)); + try { + Board snd = one.apply(Move.fence('A', 1, Orientation.HORIZONTAL)); + fail("Cannot place fence twice to the same place"); + } catch (IllegalPositionException ex) { + // OK + } + } public void testSelfDestructionForbidden() throws Exception { Board b = board;