2 * Quoridor server and related libraries
3 * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://www.gnu.org/licenses/.
18 package cz.xelfi.quoridor;
21 * Represents position and number of available fences of a player on
24 public final class Player extends Object {
28 /** number of fences this player has */
30 /** the direction of players end line */
31 final Direction endDirection;
33 Player(int x, int y, int f, Player.Direction endDir) {
38 this.endDirection = endDir;
41 /** Returns the x-coordinate of the player.
42 * @return number from 0 to 16
48 /** Returns the y-coordinate of the player.
49 * @return number from 0 to 16
55 /** Returns position of the player from 0-8.
57 * @return integer from 0-8
59 public int getColumn() {
63 /** The y-position of the player.
64 * @return integer from 0-8
70 /** How much fences is still available for the player.
72 * @return number of fences this player still has
74 public int getFences() {
79 public String toString() {
80 return "Player[" + getColumn() + "," + getRow() + "," + f + "," + endDirection + "]";
84 public int hashCode() {
85 return 8 * x + 2 * y + 4 * f + 7 + endDirection.hashCode();
89 public boolean equals(Object o) {
90 if (o instanceof Player) {
91 Player p = (Player) o;
92 return x == p.x && y == p.y && f == p.f && endDirection.equals(p.endDirection);
97 /** Possible directions of player's moves.
99 public enum Direction {
101 NORTH('P', 16), WEST((char) -1, -1), EAST((char) -1, -1), SOUTH('Q', 0);
105 private Direction(char ch, int fl) {
110 final boolean reached(Player p) {
111 return p.getYInternal() == finalLine;
114 static Direction valueOf(char ch) throws IllegalPositionException {
115 for (Direction direction : values()) {
116 if (direction.name().charAt(0) == ch) {
120 throw new IllegalPositionException("No direction " + ch);