jtulach@15
|
1 |
/*
|
jaroslav@264
|
2 |
* Quoridor server and related libraries
|
jaroslav@264
|
3 |
* Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jtulach@15
|
4 |
*
|
jaroslav@264
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@264
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@264
|
7 |
* the Free Software Foundation, either version 3 of the License.
|
jtulach@15
|
8 |
*
|
jaroslav@264
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@264
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@264
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@264
|
12 |
* GNU General Public License for more details.
|
jtulach@15
|
13 |
*
|
jaroslav@264
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@264
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@264
|
16 |
* If not, see http://www.gnu.org/licenses/.
|
jtulach@15
|
17 |
*/
|
jtulach@15
|
18 |
package cz.xelfi.quoridor;
|
jtulach@15
|
19 |
|
jtulach@15
|
20 |
/**
|
jtulach@16
|
21 |
* Represents position and number of available fences of a player on
|
jtulach@16
|
22 |
* a {@link Board}.
|
jtulach@15
|
23 |
*/
|
jtulach@15
|
24 |
public final class Player extends Object {
|
jtulach@15
|
25 |
|
jtulach@15
|
26 |
final int x;
|
jtulach@15
|
27 |
final int y;
|
jtulach@15
|
28 |
/** number of fences this player has */
|
jtulach@15
|
29 |
final int f;
|
jtulach@15
|
30 |
/** the direction of players end line */
|
jtulach@15
|
31 |
final Direction endDirection;
|
jtulach@15
|
32 |
|
jtulach@15
|
33 |
Player(int x, int y, int f, Player.Direction endDir) {
|
jtulach@15
|
34 |
super();
|
jtulach@15
|
35 |
this.x = x;
|
jtulach@15
|
36 |
this.y = y;
|
jtulach@15
|
37 |
this.f = f;
|
jtulach@15
|
38 |
this.endDirection = endDir;
|
jtulach@15
|
39 |
}
|
jtulach@15
|
40 |
|
jtulach@15
|
41 |
/** Returns the x-coordinate of the player.
|
jtulach@26
|
42 |
* @return number from 0 to 16
|
jtulach@15
|
43 |
*/
|
jtulach@26
|
44 |
int getXInternal() {
|
jtulach@15
|
45 |
return x;
|
jtulach@15
|
46 |
}
|
jtulach@15
|
47 |
|
jtulach@15
|
48 |
/** Returns the y-coordinate of the player.
|
jtulach@26
|
49 |
* @return number from 0 to 16
|
jtulach@15
|
50 |
*/
|
jtulach@26
|
51 |
int getYInternal() {
|
jtulach@15
|
52 |
return y;
|
jtulach@15
|
53 |
}
|
jtulach@15
|
54 |
|
jtulach@30
|
55 |
/** Returns position of the player from 0-8.
|
jtulach@30
|
56 |
*
|
jtulach@30
|
57 |
* @return integer from 0-8
|
jtulach@30
|
58 |
*/
|
jtulach@30
|
59 |
public int getColumn() {
|
jtulach@30
|
60 |
return x / 2;
|
jtulach@30
|
61 |
}
|
jtulach@30
|
62 |
|
jtulach@30
|
63 |
/** The y-position of the player.
|
jtulach@30
|
64 |
* @return integer from 0-8
|
jtulach@30
|
65 |
*/
|
jtulach@30
|
66 |
public int getRow() {
|
jtulach@30
|
67 |
return y / 2;
|
jtulach@30
|
68 |
}
|
jtulach@30
|
69 |
|
jtulach@26
|
70 |
/** How much fences is still available for the player.
|
jtulach@26
|
71 |
*
|
jtulach@26
|
72 |
* @return number of fences this player still has
|
jtulach@15
|
73 |
*/
|
jtulach@26
|
74 |
public int getFences() {
|
jtulach@15
|
75 |
return f;
|
jtulach@15
|
76 |
}
|
jtulach@15
|
77 |
|
jtulach@15
|
78 |
@Override
|
jtulach@15
|
79 |
public String toString() {
|
jtulach@33
|
80 |
return "Player[" + getColumn() + "," + getRow() + "," + f + "," + endDirection + "]";
|
jtulach@15
|
81 |
}
|
jtulach@15
|
82 |
|
jtulach@15
|
83 |
@Override
|
jtulach@15
|
84 |
public int hashCode() {
|
jtulach@15
|
85 |
return 8 * x + 2 * y + 4 * f + 7 + endDirection.hashCode();
|
jtulach@15
|
86 |
}
|
jtulach@15
|
87 |
|
jtulach@15
|
88 |
@Override
|
jtulach@15
|
89 |
public boolean equals(Object o) {
|
jtulach@15
|
90 |
if (o instanceof Player) {
|
jtulach@15
|
91 |
Player p = (Player) o;
|
jtulach@15
|
92 |
return x == p.x && y == p.y && f == p.f && endDirection.equals(p.endDirection);
|
jtulach@15
|
93 |
}
|
jtulach@15
|
94 |
return false;
|
jtulach@15
|
95 |
}
|
jtulach@15
|
96 |
|
jtulach@15
|
97 |
/** Possible directions of player's moves.
|
jtulach@15
|
98 |
*/
|
jtulach@15
|
99 |
public enum Direction {
|
jtulach@15
|
100 |
|
jtulach@15
|
101 |
NORTH('P', 16), WEST((char) -1, -1), EAST((char) -1, -1), SOUTH('Q', 0);
|
jtulach@15
|
102 |
final char player;
|
jtulach@15
|
103 |
final int finalLine;
|
jtulach@15
|
104 |
|
jtulach@15
|
105 |
private Direction(char ch, int fl) {
|
jtulach@15
|
106 |
this.player = ch;
|
jtulach@15
|
107 |
this.finalLine = fl;
|
jtulach@15
|
108 |
}
|
jtulach@15
|
109 |
|
jtulach@15
|
110 |
final boolean reached(Player p) {
|
jtulach@26
|
111 |
return p.getYInternal() == finalLine;
|
jtulach@15
|
112 |
}
|
jtulach@34
|
113 |
|
jtulach@34
|
114 |
static Direction valueOf(char ch) throws IllegalPositionException {
|
jtulach@34
|
115 |
for (Direction direction : values()) {
|
jtulach@34
|
116 |
if (direction.name().charAt(0) == ch) {
|
jtulach@34
|
117 |
return direction;
|
jtulach@34
|
118 |
}
|
jtulach@34
|
119 |
}
|
jtulach@34
|
120 |
throw new IllegalPositionException("No direction " + ch);
|
jtulach@34
|
121 |
}
|
jtulach@15
|
122 |
}
|
jtulach@15
|
123 |
}
|