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