Moving Fence and Player into outer level. This looks more natural in javadoc and is shorter in code.
1.1 --- a/quoridor/src/main/java/cz/xelfi/quoridor/Board.java Mon May 11 18:23:20 2009 +0200
1.2 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Board.java Mon May 11 18:35:16 2009 +0200
1.3 @@ -26,7 +26,7 @@
1.4
1.5 package cz.xelfi.quoridor;
1.6
1.7 -import cz.xelfi.quoridor.Board.Player.Direction;
1.8 +import cz.xelfi.quoridor.Player.Direction;
1.9 import java.io.BufferedReader;
1.10 import java.io.EOFException;
1.11 import java.io.IOException;
1.12 @@ -698,143 +698,5 @@
1.13 int baseOnY0 = max * (max + 1) / 2;
1.14 return baseOnY0 + y;
1.15 }
1.16 -
1.17 - /** Defines properties (mostly position) of a player.
1.18 - */
1.19 - public static final class Player extends Object {
1.20 - private final int x;
1.21 - private final int y;
1.22 - /** number of fences this player has */
1.23 - private final int f;
1.24 - /** the direction of players end line */
1.25 - private final Direction endDirection;
1.26 -
1.27 - Player (int x, int y, int f, Player.Direction endDir) {
1.28 - this.x = x;
1.29 - this.y = y;
1.30 - this.f = f;
1.31 - this.endDirection = endDir;
1.32 - }
1.33 -
1.34 -
1.35 - /** Returns the x-coordinate of the player.
1.36 - * @return number from 0 to 8
1.37 - */
1.38 - int getX () {
1.39 - return x;
1.40 - }
1.41 -
1.42 - /** Returns the y-coordinate of the player.
1.43 - * @return number from 0 to 8
1.44 - */
1.45 - int getY () {
1.46 - return y;
1.47 - }
1.48 -
1.49 - /** @return number of fences this player still has
1.50 - */
1.51 - int getFences () {
1.52 - return f;
1.53 - }
1.54 -
1.55 - @Override
1.56 - public String toString () {
1.57 - return "Player[" + x + "," + y + "," + f + "," + endDirection + "]"; // NOI18N
1.58 - }
1.59 -
1.60 - @Override
1.61 - public int hashCode () {
1.62 - return 8 * x + 2 * y + 4 * f + 7 + endDirection.hashCode ();
1.63 - }
1.64 -
1.65 - @Override
1.66 - public boolean equals (Object o) {
1.67 - if (o instanceof Player) {
1.68 - Player p = (Player)o;
1.69 - return x == p.x && y == p.y && f == p.f && endDirection.equals (p.endDirection);
1.70 - }
1.71 - return false;
1.72 - }
1.73 -
1.74 - /** Possible directions of player's moves.
1.75 - */
1.76 - public enum Direction {
1.77 - NORTH('P', 16), WEST((char) -1, -1), EAST((char)-1, -1), SOUTH('Q', 0);
1.78 -
1.79 - final char player;
1.80 - final int finalLine;
1.81 - private Direction(char ch, int fl) {
1.82 - this.player = ch;
1.83 - this.finalLine = fl;
1.84 - }
1.85 -
1.86 - private boolean reached(Player p) {
1.87 - return p.getY() == finalLine;
1.88 - }
1.89 - }
1.90 - } // end of Player
1.91
1.92 - /** Defines a fence. Its position is defined as position of its
1.93 - * center (index from 0-15 in both directions) and its orientation
1.94 - * either horizontal or vertical.
1.95 - */
1.96 - public static final class Fence extends Object {
1.97 - private final int x, y;
1.98 - private final Orientation o;
1.99 -
1.100 - Fence (int x, int y, Orientation o) {
1.101 - this.x = x;
1.102 - this.y = y;
1.103 - this.o = o;
1.104 - }
1.105 -
1.106 - /** Midle coordinate of the fence.
1.107 - * @return 1-15
1.108 - */
1.109 - int getX () {
1.110 - return x;
1.111 - }
1.112 -
1.113 - /** Midle coordinate of the fence.
1.114 - * @return 1-15
1.115 - */
1.116 - int getY () {
1.117 - return y;
1.118 - }
1.119 -
1.120 - /** The orientation of the fence.
1.121 - */
1.122 - Orientation getOrientation () {
1.123 - return o;
1.124 - }
1.125 -
1.126 - @Override
1.127 - public String toString () {
1.128 - return "Fence[" + x + "," + y + "," + o + "]"; // NOI18N
1.129 - }
1.130 -
1.131 - @Override
1.132 - public int hashCode () {
1.133 - return 8 * x + 4 * y + 13 + o.hashCode ();
1.134 - }
1.135 -
1.136 - @Override
1.137 - public boolean equals (Object o) {
1.138 - if (o instanceof Fence) {
1.139 - Fence f = (Fence)o;
1.140 -
1.141 - return x == f.x && y == f.y && getOrientation ().equals (f.getOrientation ());
1.142 - }
1.143 - return false;
1.144 - }
1.145 -
1.146 - /** The fences orientation. Either horizontal of vertical.
1.147 - */
1.148 - public static enum Orientation {
1.149 - HORIZONTAL, VERTICAL;
1.150 -
1.151 - private Orientation () {
1.152 - }
1.153 - }
1.154 - }
1.155 }
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Fence.java Mon May 11 18:35:16 2009 +0200
2.3 @@ -0,0 +1,94 @@
2.4 +/*
2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2.6 + *
2.7 + * The contents of this file are subject to the terms of either the GNU
2.8 + * General Public License Version 2 only ("GPL") or the Common
2.9 + * Development and Distribution License("CDDL") (collectively, the
2.10 + * "License"). You may not use this file except in compliance with the
2.11 + * License. You can obtain a copy of the License at
2.12 + * http://www.netbeans.org/cddl-gplv2.html
2.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
2.14 + * specific language governing permissions and limitations under the
2.15 + * License. When distributing the software, include this License Header
2.16 + * Notice in each file and include the License file at
2.17 + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
2.18 + * particular file as subject to the "Classpath" exception as provided
2.19 + * by Sun in the GPL Version 2 section of the License file that
2.20 + * accompanied this code. If applicable, add the following below the
2.21 + * License Header, with the fields enclosed by brackets [] replaced by
2.22 + * your own identifying information:
2.23 + * "Portions Copyrighted [year] [name of copyright owner]"
2.24 + *
2.25 + * Contributor(s):
2.26 + *
2.27 + * Portions Copyrighted 2009 Jaroslav Tulach
2.28 + */
2.29 +package cz.xelfi.quoridor;
2.30 +
2.31 +/**
2.32 + * Defines a fence. Its position is defined as position of its
2.33 + * center (index from 0-15 in both directions) and its orientation
2.34 + * either horizontal or vertical.
2.35 + */
2.36 +public final class Fence extends Object {
2.37 +
2.38 + private final int x;
2.39 + private final int y;
2.40 + private final Orientation o;
2.41 +
2.42 + Fence(int x, int y, Orientation o) {
2.43 + super();
2.44 + this.x = x;
2.45 + this.y = y;
2.46 + this.o = o;
2.47 + }
2.48 +
2.49 + /** Midle coordinate of the fence.
2.50 + * @return 1-15
2.51 + */
2.52 + int getX() {
2.53 + return x;
2.54 + }
2.55 +
2.56 + /** Midle coordinate of the fence.
2.57 + * @return 1-15
2.58 + */
2.59 + int getY() {
2.60 + return y;
2.61 + }
2.62 +
2.63 + /** The orientation of the fence.
2.64 + */
2.65 + Orientation getOrientation() {
2.66 + return o;
2.67 + }
2.68 +
2.69 + @Override
2.70 + public String toString() {
2.71 + return "Fence[" + x + "," + y + "," + o + "]";
2.72 + }
2.73 +
2.74 + @Override
2.75 + public int hashCode() {
2.76 + return 8 * x + 4 * y + 13 + o.hashCode();
2.77 + }
2.78 +
2.79 + @Override
2.80 + public boolean equals(Object o) {
2.81 + if (o instanceof Fence) {
2.82 + Fence f = (Fence) o;
2.83 + return x == f.x && y == f.y && getOrientation().equals(f.getOrientation());
2.84 + }
2.85 + return false;
2.86 + }
2.87 +
2.88 + /** The fences orientation. Either horizontal of vertical.
2.89 + */
2.90 + public static enum Orientation {
2.91 +
2.92 + HORIZONTAL, VERTICAL;
2.93 +
2.94 + private Orientation() {
2.95 + }
2.96 + }
2.97 +}
3.1 --- a/quoridor/src/main/java/cz/xelfi/quoridor/Move.java Mon May 11 18:23:20 2009 +0200
3.2 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Move.java Mon May 11 18:35:16 2009 +0200
3.3 @@ -26,8 +26,7 @@
3.4
3.5 package cz.xelfi.quoridor;
3.6
3.7 -import cz.xelfi.quoridor.Board.Fence;
3.8 -import cz.xelfi.quoridor.Board.Player.Direction;
3.9 +import cz.xelfi.quoridor.Player.Direction;
3.10
3.11 /**
3.12 *
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Player.java Mon May 11 18:35:16 2009 +0200
4.3 @@ -0,0 +1,104 @@
4.4 +/*
4.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4.6 + *
4.7 + * The contents of this file are subject to the terms of either the GNU
4.8 + * General Public License Version 2 only ("GPL") or the Common
4.9 + * Development and Distribution License("CDDL") (collectively, the
4.10 + * "License"). You may not use this file except in compliance with the
4.11 + * License. You can obtain a copy of the License at
4.12 + * http://www.netbeans.org/cddl-gplv2.html
4.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
4.14 + * specific language governing permissions and limitations under the
4.15 + * License. When distributing the software, include this License Header
4.16 + * Notice in each file and include the License file at
4.17 + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
4.18 + * particular file as subject to the "Classpath" exception as provided
4.19 + * by Sun in the GPL Version 2 section of the License file that
4.20 + * accompanied this code. If applicable, add the following below the
4.21 + * License Header, with the fields enclosed by brackets [] replaced by
4.22 + * your own identifying information:
4.23 + * "Portions Copyrighted [year] [name of copyright owner]"
4.24 + *
4.25 + * Contributor(s):
4.26 + *
4.27 + * Portions Copyrighted 2009 Jaroslav Tulach
4.28 + */
4.29 +package cz.xelfi.quoridor;
4.30 +
4.31 +/**
4.32 + * Defines properties (mostly position) of a player.
4.33 + */
4.34 +public final class Player extends Object {
4.35 +
4.36 + final int x;
4.37 + final int y;
4.38 + /** number of fences this player has */
4.39 + final int f;
4.40 + /** the direction of players end line */
4.41 + final Direction endDirection;
4.42 +
4.43 + Player(int x, int y, int f, Player.Direction endDir) {
4.44 + super();
4.45 + this.x = x;
4.46 + this.y = y;
4.47 + this.f = f;
4.48 + this.endDirection = endDir;
4.49 + }
4.50 +
4.51 + /** Returns the x-coordinate of the player.
4.52 + * @return number from 0 to 8
4.53 + */
4.54 + int getX() {
4.55 + return x;
4.56 + }
4.57 +
4.58 + /** Returns the y-coordinate of the player.
4.59 + * @return number from 0 to 8
4.60 + */
4.61 + int getY() {
4.62 + return y;
4.63 + }
4.64 +
4.65 + /** @return number of fences this player still has
4.66 + */
4.67 + int getFences() {
4.68 + return f;
4.69 + }
4.70 +
4.71 + @Override
4.72 + public String toString() {
4.73 + return "Player[" + x + "," + y + "," + f + "," + endDirection + "]";
4.74 + }
4.75 +
4.76 + @Override
4.77 + public int hashCode() {
4.78 + return 8 * x + 2 * y + 4 * f + 7 + endDirection.hashCode();
4.79 + }
4.80 +
4.81 + @Override
4.82 + public boolean equals(Object o) {
4.83 + if (o instanceof Player) {
4.84 + Player p = (Player) o;
4.85 + return x == p.x && y == p.y && f == p.f && endDirection.equals(p.endDirection);
4.86 + }
4.87 + return false;
4.88 + }
4.89 +
4.90 + /** Possible directions of player's moves.
4.91 + */
4.92 + public enum Direction {
4.93 +
4.94 + NORTH('P', 16), WEST((char) -1, -1), EAST((char) -1, -1), SOUTH('Q', 0);
4.95 + final char player;
4.96 + final int finalLine;
4.97 +
4.98 + private Direction(char ch, int fl) {
4.99 + this.player = ch;
4.100 + this.finalLine = fl;
4.101 + }
4.102 +
4.103 + final boolean reached(Player p) {
4.104 + return p.getY() == finalLine;
4.105 + }
4.106 + }
4.107 +}
5.1 --- a/quoridor/src/main/java/cz/xelfi/quoridor/View.java Mon May 11 18:23:20 2009 +0200
5.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
5.3 @@ -1,265 +0,0 @@
5.4 -/*
5.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5.6 - *
5.7 - * The contents of this file are subject to the terms of either the GNU
5.8 - * General Public License Version 2 only ("GPL") or the Common
5.9 - * Development and Distribution License("CDDL") (collectively, the
5.10 - * "License"). You may not use this file except in compliance with the
5.11 - * License. You can obtain a copy of the License at
5.12 - * http://www.netbeans.org/cddl-gplv2.html
5.13 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
5.14 - * specific language governing permissions and limitations under the
5.15 - * License. When distributing the software, include this License Header
5.16 - * Notice in each file and include the License file at
5.17 - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
5.18 - * particular file as subject to the "Classpath" exception as provided
5.19 - * by Sun in the GPL Version 2 section of the License file that
5.20 - * accompanied this code. If applicable, add the following below the
5.21 - * License Header, with the fields enclosed by brackets [] replaced by
5.22 - * your own identifying information:
5.23 - * "Portions Copyrighted [year] [name of copyright owner]"
5.24 - *
5.25 - * Contributor(s):
5.26 - *
5.27 - * Portions Copyrighted 2009 Jaroslav Tulach
5.28 - */
5.29 -
5.30 -package cz.xelfi.quoridor;
5.31 -
5.32 -import java.awt.BorderLayout;
5.33 -import java.awt.Dimension;
5.34 -import java.awt.Graphics2D;
5.35 -import java.awt.Point;
5.36 -import java.awt.event.MouseEvent;
5.37 -import java.awt.event.MouseListener;
5.38 -import java.awt.event.MouseMotionListener;
5.39 -import javax.swing.JComponent;
5.40 -
5.41 -/**
5.42 - *
5.43 - * @author Jaroslav Tulach
5.44 - */
5.45 -class View extends JComponent {
5.46 - /** fence size */
5.47 - private final static int SIZE_FENCE = 20;
5.48 - private final static int SIZE_FIELD = 50;
5.49 -
5.50 - private static final java.awt.Color[] PLAYER_COLORS = {
5.51 - java.awt.Color.WHITE,
5.52 - java.awt.Color.BLACK,
5.53 - java.awt.Color.RED,
5.54 - java.awt.Color.BLUE,
5.55 - };
5.56 -
5.57 - /** board to view */
5.58 - private Board board = Board.createEmptyBoard ();
5.59 - {
5.60 - try {
5.61 - board = board.fence (board.getPlayers ().get (0), 'A', 5, Board.Fence.Orientation.HORIZONTAL);
5.62 - board = board.fence (board.getPlayers ().get (0), 'A', 6, Board.Fence.Orientation.VERTICAL);
5.63 - board = board.fence (board.getPlayers ().get (0), 'C', 5, Board.Fence.Orientation.HORIZONTAL);
5.64 - } catch (IllegalPositionException ex) {
5.65 - ex.printStackTrace();
5.66 - }
5.67 - }
5.68 - /** number of player that can do the move */
5.69 - private int onMove;
5.70 - private Listener listener;
5.71 - /** non-null if we do drag and is the delta of the lefttop corner
5.72 - * from the position of the mouse */
5.73 - private Point dragDelta;
5.74 - /** draw circle */
5.75 - private Point move;
5.76 -
5.77 - /** Creates a new instance of View */
5.78 - public View () {
5.79 - int s = (SIZE_FIELD + SIZE_FENCE) * 9;
5.80 -
5.81 - setPreferredSize (new Dimension (s, s));
5.82 - setBackground (java.awt.Color.GRAY.darker ().darker ());
5.83 -
5.84 - listener = new Listener ();
5.85 - addMouseListener(listener);
5.86 - addMouseMotionListener(listener);
5.87 - }
5.88 -
5.89 - protected void paintComponent (java.awt.Graphics g) {
5.90 - Graphics2D g2 = (Graphics2D)g;
5.91 -
5.92 - g.setColor (getBackground ());
5.93 - g.fillRect (0, 0, getSize ().width, getSize ().height);
5.94 -
5.95 - // draw the empty boards
5.96 - g.setColor (java.awt.Color.GRAY);
5.97 - for (int i = 0; i < 9; i++) {
5.98 - for (int j = 0; j < 9; j++) {
5.99 - g.fillRect (
5.100 - i * (SIZE_FIELD + SIZE_FENCE) + SIZE_FENCE / 2,
5.101 - j * (SIZE_FIELD + SIZE_FENCE) + SIZE_FENCE / 2,
5.102 - SIZE_FIELD,
5.103 - SIZE_FIELD
5.104 - );
5.105 - } }
5.106 -
5.107 - // draw fences
5.108 - g.setColor (java.awt.Color.YELLOW.darker ());
5.109 - for (Board.Fence f : board.getFences ()) {
5.110 - Point b = findFence(f);
5.111 - switch (f.getOrientation ()) {
5.112 - case HORIZONTAL:
5.113 - g.fillRect (b.x - SIZE_FIELD + 1, b.y + 1, SIZE_FIELD * 2 + SIZE_FENCE - 2, SIZE_FENCE - 2);
5.114 - break;
5.115 - case VERTICAL:
5.116 - g.fillRect (b.x + 1, b.y - SIZE_FIELD + 1, SIZE_FENCE - 2, SIZE_FIELD * 2 + SIZE_FENCE - 2);
5.117 - break;
5.118 - }
5.119 - }
5.120 -
5.121 - int pindex = 0;
5.122 - for (Board.Player p : board.getPlayers ()) {
5.123 - Point place = findPlayer (p);
5.124 -
5.125 - g.setColor (PLAYER_COLORS[pindex++]);
5.126 - g.fillArc (place.x, place.y, SIZE_FIELD, SIZE_FIELD, 0, 360);
5.127 -
5.128 - }
5.129 - if (move != null) {
5.130 - g.setColor (PLAYER_COLORS[onMove]);
5.131 - g.drawArc(move.x, move.y, SIZE_FIELD, SIZE_FIELD, 0, 360);
5.132 - }
5.133 - }
5.134 -
5.135 - /** Tries to move a player to given coordinates.
5.136 - */
5.137 - final Board tryMove (int player, int newX, int newY) {
5.138 - Point p = findPlayer (board.getPlayers().get (player));
5.139 -
5.140 - int cmpX = compare (p.x, SIZE_FIELD, newX);
5.141 - int cmpY = compare (p.y, SIZE_FIELD, newY);
5.142 -
5.143 - if (cmpX == 0 && cmpY == 0) {
5.144 - return null;
5.145 - }
5.146 - try {
5.147 - if (cmpX == 0) {
5.148 - if (cmpY < 0 && cmpY > -3) {
5.149 - return board.move (board.getPlayers().get (player), Board.Player.Direction.SOUTH);
5.150 - }
5.151 - if (cmpY > 0 && cmpY < 3) {
5.152 - return board.move (board.getPlayers().get (player), Board.Player.Direction.NORTH);
5.153 - }
5.154 - return null;
5.155 - }
5.156 -
5.157 - if (cmpY == 0) {
5.158 - if (cmpX < 0 && cmpX > -3) {
5.159 - return board.move (board.getPlayers().get (player), Board.Player.Direction.WEST);
5.160 - }
5.161 - if (cmpX > 0 && cmpX < 3) {
5.162 - return board.move (board.getPlayers().get (player), Board.Player.Direction.EAST);
5.163 - }
5.164 - return null;
5.165 - }
5.166 - } catch (IllegalPositionException ex) {
5.167 - return null;
5.168 - }
5.169 -
5.170 - return null;
5.171 - }
5.172 -
5.173 - private static int compare (int from, int length, int where) {
5.174 - return (where - from) / length;
5.175 - }
5.176 -
5.177 - /** computes middle point of a fence.
5.178 - */
5.179 - final Point findFence (Board.Fence f) {
5.180 - int bx = (f.getX () / 2 + 1) * (SIZE_FIELD + SIZE_FENCE) - SIZE_FENCE;
5.181 - int by = (f.getY () / 2 + 1) * (SIZE_FIELD + SIZE_FENCE) - SIZE_FENCE;
5.182 -
5.183 - return new Point (bx + SIZE_FENCE / 2, by + SIZE_FENCE / 2);
5.184 - }
5.185 -
5.186 - /** For a player computes left-top point of its square.
5.187 - */
5.188 - final Point findPlayer (Board.Player p) {
5.189 - int px = (p.getX () / 2 + 0) * (SIZE_FIELD + SIZE_FENCE);
5.190 - int py = (p.getY () / 2 + 0) * (SIZE_FIELD + SIZE_FENCE);
5.191 -
5.192 - return new Point (px + SIZE_FENCE / 2, py + SIZE_FENCE / 2);
5.193 - }
5.194 -
5.195 - /** Finds if the click on the component is activates a player.
5.196 - * @return null if not
5.197 - */
5.198 - final Board.Player findPlayer (int x, int y) {
5.199 - for (Board.Player p : board.getPlayers ()) {
5.200 - Point z = findPlayer(p);
5.201 -
5.202 - if (
5.203 - z.x < x && x < z.x + SIZE_FIELD &&
5.204 - z.y < y && y < z.y + SIZE_FIELD
5.205 - ) {
5.206 - return p;
5.207 - }
5.208 - }
5.209 - return null;
5.210 - }
5.211 -
5.212 - private final class Listener implements MouseListener, MouseMotionListener {
5.213 - public void mouseClicked(java.awt.event.MouseEvent mouseEvent) {
5.214 - }
5.215 -
5.216 - public void mouseEntered(java.awt.event.MouseEvent mouseEvent) {
5.217 - }
5.218 -
5.219 - public void mouseExited(java.awt.event.MouseEvent mouseEvent) {
5.220 - }
5.221 -
5.222 - public void mousePressed(java.awt.event.MouseEvent mouseEvent) {
5.223 - Board.Player p = board.getPlayers ().get (onMove);
5.224 - Board.Player clicked = findPlayer (mouseEvent.getX (), mouseEvent.getY ());
5.225 -
5.226 - if (p.equals (clicked)) {
5.227 - // start drag
5.228 - Point delta = findPlayer (p);
5.229 - delta.x = mouseEvent.getX () - delta.x;
5.230 - delta.y = mouseEvent.getY () - delta.y;
5.231 - dragDelta = delta;
5.232 - repaint();
5.233 - }
5.234 - }
5.235 -
5.236 - public void mouseReleased(java.awt.event.MouseEvent mouseEvent) {
5.237 - Board newBoard = tryMove (onMove, mouseEvent.getX(), mouseEvent.getY());
5.238 - if (newBoard != null) {
5.239 - board = newBoard;
5.240 - onMove = (onMove + 1) % newBoard.getPlayers().size();
5.241 - }
5.242 - dragDelta = null;
5.243 - move = null;
5.244 - repaint();
5.245 - }
5.246 -
5.247 - public void mouseMoved(MouseEvent e) {
5.248 - }
5.249 -
5.250 - public void mouseDragged(MouseEvent e) {
5.251 - if (dragDelta != null) {
5.252 - move = new Point (e.getX () - dragDelta.x, e.getY () - dragDelta.y);
5.253 - repaint ();
5.254 - }
5.255 - }
5.256 -
5.257 - }
5.258 -
5.259 -
5.260 - public static void main (String[] args) {
5.261 - javax.swing.JFrame f = new javax.swing.JFrame ();
5.262 - View v = new View ();
5.263 - f.getContentPane ().add (BorderLayout.CENTER, v);
5.264 - f.setDefaultCloseOperation (f.EXIT_ON_CLOSE);
5.265 - f.pack ();
5.266 - f.setVisible (true);
5.267 - }
5.268 -}
6.1 --- a/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java Mon May 11 18:23:20 2009 +0200
6.2 +++ b/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java Mon May 11 18:35:16 2009 +0200
6.3 @@ -40,17 +40,17 @@
6.4 board = Board.createEmptyBoard();
6.5 }
6.6
6.7 - protected abstract Board move(Board b, int player, Board.Player.Direction... where)
6.8 + protected abstract Board move(Board b, int player, Player.Direction... where)
6.9 throws IllegalPositionException;
6.10
6.11 - protected abstract Board fence(Board b, int player, char x, int y, Board.Fence.Orientation orie)
6.12 + protected abstract Board fence(Board b, int player, char x, int y, Fence.Orientation orie)
6.13 throws IllegalPositionException;
6.14
6.15 protected abstract Board apply(Board b, Move move) throws IllegalPositionException;
6.16
6.17
6.18 public void testTwoPlayers () {
6.19 - List<Board.Player> list = board.getPlayers();
6.20 + List<Player> list = board.getPlayers();
6.21 assertEquals ("Two", 2, list.size ());
6.22 assertFalse ("Both are non-null", list.contains (null));
6.23 try {
6.24 @@ -89,13 +89,13 @@
6.25 Board b = board;
6.26
6.27 for (int i = 0; i < 3; i++) {
6.28 - b = move(b, 0, Board.Player.Direction.NORTH);
6.29 - b = move(b, 1, Board.Player.Direction.SOUTH);
6.30 + b = move(b, 0, Player.Direction.NORTH);
6.31 + b = move(b, 1, Player.Direction.SOUTH);
6.32 }
6.33
6.34 - b = move(b, 0, Board.Player.Direction.NORTH);
6.35 + b = move(b, 0, Player.Direction.NORTH);
6.36 try {
6.37 - b = move(b, 1, Board.Player.Direction.SOUTH);
6.38 + b = move(b, 1, Player.Direction.SOUTH);
6.39 fail ("Now the positions of two players are supposed to be the same, this results in exception");
6.40 } catch (IllegalPositionException ex) {
6.41 // ok
6.42 @@ -104,10 +104,10 @@
6.43 }
6.44
6.45 public void testCrossFences () throws Exception {
6.46 - Board b1 = board.fence (board.getPlayers ().get(0), 'A', 1, Board.Fence.Orientation.HORIZONTAL);
6.47 + Board b1 = board.fence (board.getPlayers ().get(0), 'A', 1, Fence.Orientation.HORIZONTAL);
6.48
6.49 try {
6.50 - b1.fence (b1.getPlayers ().get(1), 'A', 1, Board.Fence.Orientation.VERTICAL);
6.51 + b1.fence (b1.getPlayers ().get(1), 'A', 1, Fence.Orientation.VERTICAL);
6.52 fail ("This must fail, as the fences overlap");
6.53 } catch (IllegalPositionException ex) {
6.54 // ok
6.55 @@ -118,20 +118,20 @@
6.56 Board b = board;
6.57
6.58 for (int i = 0; i < 3; i++) {
6.59 - b = move(b, 0, Board.Player.Direction.NORTH);
6.60 - b = move(b, 1, Board.Player.Direction.SOUTH);
6.61 + b = move(b, 0, Player.Direction.NORTH);
6.62 + b = move(b, 1, Player.Direction.SOUTH);
6.63 }
6.64
6.65 - b = move(b, 0, Board.Player.Direction.NORTH);
6.66 + b = move(b, 0, Player.Direction.NORTH);
6.67
6.68 // jump over
6.69 - b = move(b, 1, Board.Player.Direction.SOUTH, Board.Player.Direction.SOUTH);
6.70 + b = move(b, 1, Player.Direction.SOUTH, Player.Direction.SOUTH);
6.71 }
6.72
6.73 public void testCannotJumpOverFence () throws Exception {
6.74 - Board b = fence (board, 0, 'D', 8, Board.Fence.Orientation.HORIZONTAL);
6.75 + Board b = fence (board, 0, 'D', 8, Fence.Orientation.HORIZONTAL);
6.76 try {
6.77 - move(b, 1, Board.Player.Direction.SOUTH);
6.78 + move(b, 1, Player.Direction.SOUTH);
6.79 fail ("This shall not be allowed, as there is the fence");
6.80 } catch (IllegalPositionException ex) {
6.81 // ok
6.82 @@ -143,14 +143,14 @@
6.83 Board b = board;
6.84
6.85 for (int i = 0; i < 3; i++) {
6.86 - b = move(b, 0, Board.Player.Direction.NORTH);
6.87 - b = move(b, 1, Board.Player.Direction.SOUTH);
6.88 + b = move(b, 0, Player.Direction.NORTH);
6.89 + b = move(b, 1, Player.Direction.SOUTH);
6.90 }
6.91
6.92 - b = move(b, 0, Board.Player.Direction.NORTH);
6.93 + b = move(b, 0, Player.Direction.NORTH);
6.94
6.95 try {
6.96 - b = move(b, 1, Board.Player.Direction.SOUTH, Board.Player.Direction.WEST);
6.97 + b = move(b, 1, Player.Direction.SOUTH, Player.Direction.WEST);
6.98 fail ("Cannot just jump aside");
6.99 } catch (IllegalPositionException ex) {
6.100 // ok
6.101 @@ -161,21 +161,21 @@
6.102 Board b = board;
6.103
6.104 for (int i = 0; i < 3; i++) {
6.105 - b = move(b, 0, Board.Player.Direction.NORTH);
6.106 - b = move(b, 1, Board.Player.Direction.SOUTH);
6.107 + b = move(b, 0, Player.Direction.NORTH);
6.108 + b = move(b, 1, Player.Direction.SOUTH);
6.109 }
6.110
6.111 - b = move(b, 0, Board.Player.Direction.NORTH);
6.112 + b = move(b, 0, Player.Direction.NORTH);
6.113
6.114 assertEquals (8, b.getPlayers ().get (0).getX ());
6.115 assertEquals (8, b.getPlayers ().get (0).getY ());
6.116
6.117 - b = fence(b, 0, 'D', 4, Board.Fence.Orientation.HORIZONTAL);
6.118 + b = fence(b, 0, 'D', 4, Fence.Orientation.HORIZONTAL);
6.119
6.120 // we can over jump to west
6.121 - move(b, 1, Board.Player.Direction.SOUTH, Board.Player.Direction.WEST);
6.122 + move(b, 1, Player.Direction.SOUTH, Player.Direction.WEST);
6.123 // as well as east
6.124 - move(b, 1, Board.Player.Direction.SOUTH, Board.Player.Direction.EAST);
6.125 + move(b, 1, Player.Direction.SOUTH, Player.Direction.EAST);
6.126 }
6.127
6.128 public void testPlaceAllFences() throws Exception {
6.129 @@ -190,14 +190,14 @@
6.130
6.131 int cnt = 10;
6.132 for (int i = 1; i <= 5; i++) {
6.133 - b = fence(b, player, 'A', i, Board.Fence.Orientation.HORIZONTAL);
6.134 - b = fence(b, player, 'D', i, Board.Fence.Orientation.HORIZONTAL);
6.135 + b = fence(b, player, 'A', i, Fence.Orientation.HORIZONTAL);
6.136 + b = fence(b, player, 'D', i, Fence.Orientation.HORIZONTAL);
6.137 cnt -= 2;
6.138 assertEquals("Two less" + i, cnt, b.getPlayers().get(player).getFences());
6.139 }
6.140
6.141 try {
6.142 - fence(b, player, 'F', 7, Board.Fence.Orientation.VERTICAL);
6.143 + fence(b, player, 'F', 7, Fence.Orientation.VERTICAL);
6.144 fail("We shall run out of fences");
6.145 } catch (IllegalPositionException ex) {
6.146 // OK
6.147 @@ -207,11 +207,11 @@
6.148 public void testAlwaysHasToHaveAccessToEndLine () throws Exception {
6.149 Board b = board;
6.150
6.151 - b = b.fence (b.getPlayers ().get (0), 'E', 1, Board.Fence.Orientation.HORIZONTAL);
6.152 - b = b.fence (b.getPlayers ().get (0), 'F', 1, Board.Fence.Orientation.VERTICAL);
6.153 + b = b.fence (b.getPlayers ().get (0), 'E', 1, Fence.Orientation.HORIZONTAL);
6.154 + b = b.fence (b.getPlayers ().get (0), 'F', 1, Fence.Orientation.VERTICAL);
6.155
6.156 try {
6.157 - b = b.fence (b.getPlayers ().get (0), 'D', 1, Board.Fence.Orientation.VERTICAL);
6.158 + b = b.fence (b.getPlayers ().get (0), 'D', 1, Fence.Orientation.VERTICAL);
6.159 fail ("This is not allowed as player 0 cannot now reach the final line");
6.160 } catch (IllegalPositionException ex) {
6.161 // ok
6.162 @@ -220,12 +220,12 @@
6.163 }
6.164
6.165 public void testEqualsOfPlayers () throws Exception {
6.166 - Board.Player p1 = new Board.Player (1, 1, 10, Board.Player.Direction.EAST);
6.167 - Board.Player p2 = new Board.Player (1, 1, 10, Board.Player.Direction.EAST);
6.168 - Board.Player p3 = new Board.Player (2, 1, 10, Board.Player.Direction.EAST);
6.169 - Board.Player p4 = new Board.Player (1, 1, 10, Board.Player.Direction.WEST);
6.170 - Board.Player p5 = new Board.Player (1, 2, 10, Board.Player.Direction.EAST);
6.171 - Board.Player p6 = new Board.Player (1, 1, 5, Board.Player.Direction.EAST);
6.172 + Player p1 = new Player (1, 1, 10, Player.Direction.EAST);
6.173 + Player p2 = new Player (1, 1, 10, Player.Direction.EAST);
6.174 + Player p3 = new Player (2, 1, 10, Player.Direction.EAST);
6.175 + Player p4 = new Player (1, 1, 10, Player.Direction.WEST);
6.176 + Player p5 = new Player (1, 2, 10, Player.Direction.EAST);
6.177 + Player p6 = new Player (1, 1, 5, Player.Direction.EAST);
6.178
6.179 assertEquals ("p1 == p2", p1, p2);
6.180 if (p2.equals (p3)) fail ();
6.181 @@ -246,11 +246,11 @@
6.182 }
6.183
6.184 public void testEqualsOfFences () throws Exception {
6.185 - Board.Fence f1 = new Board.Fence (1, 1, Board.Fence.Orientation.HORIZONTAL);
6.186 - Board.Fence f2 = new Board.Fence (1, 1, Board.Fence.Orientation.HORIZONTAL);
6.187 - Board.Fence f3 = new Board.Fence (3, 1, Board.Fence.Orientation.HORIZONTAL);
6.188 - Board.Fence f4 = new Board.Fence (1, 3, Board.Fence.Orientation.HORIZONTAL);
6.189 - Board.Fence f5 = new Board.Fence (1, 1, Board.Fence.Orientation.VERTICAL);
6.190 + Fence f1 = new Fence (1, 1, Fence.Orientation.HORIZONTAL);
6.191 + Fence f2 = new Fence (1, 1, Fence.Orientation.HORIZONTAL);
6.192 + Fence f3 = new Fence (3, 1, Fence.Orientation.HORIZONTAL);
6.193 + Fence f4 = new Fence (1, 3, Fence.Orientation.HORIZONTAL);
6.194 + Fence f5 = new Fence (1, 1, Fence.Orientation.VERTICAL);
6.195
6.196 assertEquals ("f1 == f2", f1, f2);
6.197 if (f1.equals (f3)) fail ();
6.198 @@ -262,9 +262,9 @@
6.199 }
6.200
6.201 public void testEqualsOfBoards1 () throws Exception {
6.202 - Board b1 = board.move (board.getPlayers ().get (0), Board.Player.Direction.NORTH);
6.203 - Board b2 = board.move (board.getPlayers ().get (0), Board.Player.Direction.NORTH);
6.204 - Board b3 = board.move (board.getPlayers ().get (0), Board.Player.Direction.EAST);
6.205 + Board b1 = board.move (board.getPlayers ().get (0), Player.Direction.NORTH);
6.206 + Board b2 = board.move (board.getPlayers ().get (0), Player.Direction.NORTH);
6.207 + Board b3 = board.move (board.getPlayers ().get (0), Player.Direction.EAST);
6.208
6.209 if (b1.equals (b3)) fail ();
6.210 if (b3.equals (b1)) fail ();
6.211 @@ -272,11 +272,11 @@
6.212 assertEquals ("b1 == b2", b1, b2);
6.213 }
6.214 public void testEqualsOfBoards2 () throws Exception {
6.215 - Board b1 = board.fence (board.getPlayers ().get (0), 'E', 3, Board.Fence.Orientation.HORIZONTAL);
6.216 - Board b2 = board.fence (board.getPlayers ().get (0), 'E', 3, Board.Fence.Orientation.HORIZONTAL);
6.217 - Board b3 = board.fence (board.getPlayers ().get (0), 'D', 3, Board.Fence.Orientation.HORIZONTAL);
6.218 - Board b4 = board.fence (board.getPlayers ().get (0), 'E', 4, Board.Fence.Orientation.HORIZONTAL);
6.219 - Board b5 = board.fence (board.getPlayers ().get (0), 'E', 3, Board.Fence.Orientation.VERTICAL);
6.220 + Board b1 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.HORIZONTAL);
6.221 + Board b2 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.HORIZONTAL);
6.222 + Board b3 = board.fence (board.getPlayers ().get (0), 'D', 3, Fence.Orientation.HORIZONTAL);
6.223 + Board b4 = board.fence (board.getPlayers ().get (0), 'E', 4, Fence.Orientation.HORIZONTAL);
6.224 + Board b5 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.VERTICAL);
6.225
6.226 if (b1.equals (b3)) fail ();
6.227 if (b3.equals (b1)) fail ();
6.228 @@ -291,14 +291,14 @@
6.229 Board b = board;
6.230
6.231 for (int i = 0; i < 3; i++) {
6.232 - b = move(b, 0, Board.Player.Direction.NORTH);
6.233 - b = move(b, 1, Board.Player.Direction.SOUTH);
6.234 + b = move(b, 0, Player.Direction.NORTH);
6.235 + b = move(b, 1, Player.Direction.SOUTH);
6.236 }
6.237
6.238 - Board b1 = move(b, 0, Board.Player.Direction.NORTH);
6.239 + Board b1 = move(b, 0, Player.Direction.NORTH);
6.240
6.241 - Board b2 = move(b, 1, Board.Player.Direction.SOUTH);
6.242 - b2 = b2.move (b2.getPlayers ().get (0), Board.Player.Direction.NORTH, Board.Player.Direction.NORTH);
6.243 + Board b2 = move(b, 1, Player.Direction.SOUTH);
6.244 + b2 = b2.move (b2.getPlayers ().get (0), Player.Direction.NORTH, Player.Direction.NORTH);
6.245
6.246 if (b1.equals (b2)) fail ("Not the same, pawns are reverted");
6.247 if (b2.equals (b1)) fail ("Not the same, pawns are reverted");
7.1 --- a/quoridor/src/test/java/cz/xelfi/quoridor/BoardTest.java Mon May 11 18:23:20 2009 +0200
7.2 +++ b/quoridor/src/test/java/cz/xelfi/quoridor/BoardTest.java Mon May 11 18:35:16 2009 +0200
7.3 @@ -26,7 +26,7 @@
7.4
7.5 package cz.xelfi.quoridor;
7.6
7.7 -import cz.xelfi.quoridor.Board.Player.Direction;
7.8 +import cz.xelfi.quoridor.Player.Direction;
7.9
7.10 /** Basic tests in simple configuration.
7.11 *
7.12 @@ -41,7 +41,7 @@
7.13 protected Board move(Board b, int player, Direction... where) throws IllegalPositionException {
7.14 return b.move(b.getPlayers().get(player), where);
7.15 }
7.16 - protected Board fence(Board b, int player, char x, int y, Board.Fence.Orientation orie)
7.17 + protected Board fence(Board b, int player, char x, int y, Fence.Orientation orie)
7.18 throws IllegalPositionException {
7.19 return b.fence(b.getPlayers().get(player), x, y, orie);
7.20 }
8.1 --- a/quoridor/src/test/java/cz/xelfi/quoridor/SerializeTest.java Mon May 11 18:23:20 2009 +0200
8.2 +++ b/quoridor/src/test/java/cz/xelfi/quoridor/SerializeTest.java Mon May 11 18:35:16 2009 +0200
8.3 @@ -25,7 +25,7 @@
8.4 */
8.5 package cz.xelfi.quoridor;
8.6
8.7 -import cz.xelfi.quoridor.Board.Player.Direction;
8.8 +import cz.xelfi.quoridor.Player.Direction;
8.9 import java.io.IOException;
8.10 import java.io.StringReader;
8.11 import java.io.StringWriter;
8.12 @@ -48,7 +48,7 @@
8.13 throw new IllegalStateException(ex);
8.14 }
8.15 }
8.16 - protected Board fence(Board b, int player, char x, int y, Board.Fence.Orientation orie)
8.17 + protected Board fence(Board b, int player, char x, int y, Fence.Orientation orie)
8.18 throws IllegalPositionException {
8.19 try {
8.20 Board read = reread(b);