1.1 --- a/quoridor/src/main/java/cz/xelfi/quoridor/Board.java Mon May 11 18:07:29 2009 +0200
1.2 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Board.java Mon May 11 18:23:20 2009 +0200
1.3 @@ -156,8 +156,25 @@
1.4 public Player getCurrentPlayer() {
1.5 return players.get(turn);
1.6 }
1.7 +
1.8 + /** The play who wins on current board.
1.9 + *
1.10 + * @return the winning player or <code>null</code>
1.11 + */
1.12 + public Player getWinner() {
1.13 + for (Player p : players) {
1.14 + if (p.endDirection.reached(p)) {
1.15 + return p;
1.16 + }
1.17 + }
1.18 + return null;
1.19 + }
1.20
1.21 public Board apply(Move move) throws IllegalPositionException {
1.22 + if (getWinner() != null) {
1.23 + throw new IllegalPositionException("Game finished!"); // NOI18N
1.24 + }
1.25 +
1.26 if (move.direction != null) {
1.27 return move(getCurrentPlayer(), move.direction);
1.28 } else {
1.29 @@ -742,13 +759,18 @@
1.30 /** Possible directions of player's moves.
1.31 */
1.32 public enum Direction {
1.33 - NORTH('P'), WEST((char) -1), EAST((char)-1), SOUTH('Q');
1.34 + NORTH('P', 16), WEST((char) -1, -1), EAST((char)-1, -1), SOUTH('Q', 0);
1.35
1.36 final char player;
1.37 - private Direction(char ch) {
1.38 + final int finalLine;
1.39 + private Direction(char ch, int fl) {
1.40 this.player = ch;
1.41 + this.finalLine = fl;
1.42 }
1.43
1.44 + private boolean reached(Player p) {
1.45 + return p.getY() == finalLine;
1.46 + }
1.47 }
1.48 } // end of Player
1.49
2.1 --- a/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java Mon May 11 18:07:29 2009 +0200
2.2 +++ b/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java Mon May 11 18:23:20 2009 +0200
2.3 @@ -308,11 +308,23 @@
2.4 assertEquals("First player ready", board.getCurrentPlayer(), board.getPlayers().get(0));
2.5 Board b = apply(board, Move.EAST);
2.6
2.7 - for (int i = 0; i < 8; i++) {
2.8 + for (int i = 0; i < 7; i++) {
2.9 assertEquals("Snd player ready", b.getCurrentPlayer(), b.getPlayers().get(1));
2.10 b = apply(b, Move.SOUTH);
2.11 assertEquals("First player ready", b.getCurrentPlayer(), b.getPlayers().get(0));
2.12 b = apply(b, Move.NORTH);
2.13 }
2.14 +
2.15 + Board fin = b.apply(Move.NORTH).apply(Move.NORTH);
2.16 + assertNotNull("There is a winner", fin.getWinner());
2.17 + assertEquals("And the winner is", fin.getPlayers().get(0), fin.getWinner());
2.18 +
2.19 + try {
2.20 + fin.apply(Move.EAST);
2.21 + fail("No moves allow when we are in final position");
2.22 + } catch (IllegalPositionException ex) {
2.23 + // OK
2.24 + }
2.25 +
2.26 }
2.27 }