quoridor/src/main/java/cz/xelfi/quoridor/Player.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 06 Jun 2009 15:13:04 +0200
changeset 33 6a6d1dbea99e
parent 30 f25846453907
child 34 34baf57f2d4e
permissions -rw-r--r--
Drawing players to the right position
jtulach@15
     1
/*
jtulach@15
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jtulach@15
     3
 *
jtulach@15
     4
 * The contents of this file are subject to the terms of either the GNU
jtulach@15
     5
 * General Public License Version 2 only ("GPL") or the Common
jtulach@15
     6
 * Development and Distribution License("CDDL") (collectively, the
jtulach@15
     7
 * "License"). You may not use this file except in compliance with the
jtulach@15
     8
 * License. You can obtain a copy of the License at
jtulach@15
     9
 * http://www.netbeans.org/cddl-gplv2.html
jtulach@15
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jtulach@15
    11
 * specific language governing permissions and limitations under the
jtulach@15
    12
 * License.  When distributing the software, include this License Header
jtulach@15
    13
 * Notice in each file and include the License file at
jtulach@15
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jtulach@15
    15
 * particular file as subject to the "Classpath" exception as provided
jtulach@15
    16
 * by Sun in the GPL Version 2 section of the License file that
jtulach@15
    17
 * accompanied this code. If applicable, add the following below the
jtulach@15
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jtulach@15
    19
 * your own identifying information:
jtulach@15
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jtulach@15
    21
 *
jtulach@15
    22
 * Contributor(s):
jtulach@15
    23
 *
jtulach@15
    24
 * Portions Copyrighted 2009 Jaroslav Tulach
jtulach@15
    25
 */
jtulach@15
    26
package cz.xelfi.quoridor;
jtulach@15
    27
jtulach@15
    28
/**
jtulach@16
    29
 * Represents position and number of available fences of a player on
jtulach@16
    30
 * a {@link Board}.
jtulach@15
    31
 */
jtulach@15
    32
public final class Player extends Object {
jtulach@15
    33
jtulach@15
    34
    final int x;
jtulach@15
    35
    final int y;
jtulach@15
    36
    /** number of fences this player has */
jtulach@15
    37
    final int f;
jtulach@15
    38
    /** the direction of players end line */
jtulach@15
    39
    final Direction endDirection;
jtulach@15
    40
jtulach@15
    41
    Player(int x, int y, int f, Player.Direction endDir) {
jtulach@15
    42
        super();
jtulach@15
    43
        this.x = x;
jtulach@15
    44
        this.y = y;
jtulach@15
    45
        this.f = f;
jtulach@15
    46
        this.endDirection = endDir;
jtulach@15
    47
    }
jtulach@15
    48
jtulach@15
    49
    /** Returns the x-coordinate of the player.
jtulach@26
    50
     * @return  number from 0 to 16
jtulach@15
    51
     */
jtulach@26
    52
    int getXInternal() {
jtulach@15
    53
        return x;
jtulach@15
    54
    }
jtulach@15
    55
jtulach@15
    56
    /** Returns the y-coordinate of the player.
jtulach@26
    57
     * @return  number from 0 to 16
jtulach@15
    58
     */
jtulach@26
    59
    int getYInternal() {
jtulach@15
    60
        return y;
jtulach@15
    61
    }
jtulach@15
    62
jtulach@30
    63
    /** Returns position of the player from 0-8.
jtulach@30
    64
     *
jtulach@30
    65
     * @return integer from 0-8
jtulach@30
    66
     */
jtulach@30
    67
    public int getColumn() {
jtulach@30
    68
        return x / 2;
jtulach@30
    69
    }
jtulach@30
    70
jtulach@30
    71
    /** The y-position of the player.
jtulach@30
    72
     * @return integer from 0-8
jtulach@30
    73
     */
jtulach@30
    74
    public int getRow() {
jtulach@30
    75
        return y / 2;
jtulach@30
    76
    }
jtulach@30
    77
jtulach@26
    78
    /** How much fences is still available for the player.
jtulach@26
    79
     * 
jtulach@26
    80
     * @return number of fences this player still has
jtulach@15
    81
     */
jtulach@26
    82
    public int getFences() {
jtulach@15
    83
        return f;
jtulach@15
    84
    }
jtulach@15
    85
jtulach@15
    86
    @Override
jtulach@15
    87
    public String toString() {
jtulach@33
    88
        return "Player[" + getColumn() + "," + getRow() + "," + f + "," + endDirection + "]";
jtulach@15
    89
    }
jtulach@15
    90
jtulach@15
    91
    @Override
jtulach@15
    92
    public int hashCode() {
jtulach@15
    93
        return 8 * x + 2 * y + 4 * f + 7 + endDirection.hashCode();
jtulach@15
    94
    }
jtulach@15
    95
jtulach@15
    96
    @Override
jtulach@15
    97
    public boolean equals(Object o) {
jtulach@15
    98
        if (o instanceof Player) {
jtulach@15
    99
            Player p = (Player) o;
jtulach@15
   100
            return x == p.x && y == p.y && f == p.f && endDirection.equals(p.endDirection);
jtulach@15
   101
        }
jtulach@15
   102
        return false;
jtulach@15
   103
    }
jtulach@15
   104
jtulach@15
   105
    /** Possible directions of player's moves.
jtulach@15
   106
     */
jtulach@15
   107
    public enum Direction {
jtulach@15
   108
jtulach@15
   109
        NORTH('P', 16), WEST((char) -1, -1), EAST((char) -1, -1), SOUTH('Q', 0);
jtulach@15
   110
        final char player;
jtulach@15
   111
        final int finalLine;
jtulach@15
   112
jtulach@15
   113
        private Direction(char ch, int fl) {
jtulach@15
   114
            this.player = ch;
jtulach@15
   115
            this.finalLine = fl;
jtulach@15
   116
        }
jtulach@15
   117
jtulach@15
   118
        final boolean reached(Player p) {
jtulach@26
   119
            return p.getYInternal() == finalLine;
jtulach@15
   120
        }
jtulach@15
   121
    }
jtulach@15
   122
}