quoridor/src/main/java/cz/xelfi/quoridor/Player.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 08:56:13 +0200
changeset 264 d60370059c3c
parent 34 34baf57f2d4e
permissions -rw-r--r--
Changing headers to GPLv3
     1 /*
     2  * Quoridor server and related libraries
     3  * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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/.
    17  */
    18 package cz.xelfi.quoridor;
    19 
    20 /**
    21  * Represents position and number of available fences of a player on
    22  * a {@link Board}.
    23  */
    24 public final class Player extends Object {
    25 
    26     final int x;
    27     final int y;
    28     /** number of fences this player has */
    29     final int f;
    30     /** the direction of players end line */
    31     final Direction endDirection;
    32 
    33     Player(int x, int y, int f, Player.Direction endDir) {
    34         super();
    35         this.x = x;
    36         this.y = y;
    37         this.f = f;
    38         this.endDirection = endDir;
    39     }
    40 
    41     /** Returns the x-coordinate of the player.
    42      * @return  number from 0 to 16
    43      */
    44     int getXInternal() {
    45         return x;
    46     }
    47 
    48     /** Returns the y-coordinate of the player.
    49      * @return  number from 0 to 16
    50      */
    51     int getYInternal() {
    52         return y;
    53     }
    54 
    55     /** Returns position of the player from 0-8.
    56      *
    57      * @return integer from 0-8
    58      */
    59     public int getColumn() {
    60         return x / 2;
    61     }
    62 
    63     /** The y-position of the player.
    64      * @return integer from 0-8
    65      */
    66     public int getRow() {
    67         return y / 2;
    68     }
    69 
    70     /** How much fences is still available for the player.
    71      * 
    72      * @return number of fences this player still has
    73      */
    74     public int getFences() {
    75         return f;
    76     }
    77 
    78     @Override
    79     public String toString() {
    80         return "Player[" + getColumn() + "," + getRow() + "," + f + "," + endDirection + "]";
    81     }
    82 
    83     @Override
    84     public int hashCode() {
    85         return 8 * x + 2 * y + 4 * f + 7 + endDirection.hashCode();
    86     }
    87 
    88     @Override
    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);
    93         }
    94         return false;
    95     }
    96 
    97     /** Possible directions of player's moves.
    98      */
    99     public enum Direction {
   100 
   101         NORTH('P', 16), WEST((char) -1, -1), EAST((char) -1, -1), SOUTH('Q', 0);
   102         final char player;
   103         final int finalLine;
   104 
   105         private Direction(char ch, int fl) {
   106             this.player = ch;
   107             this.finalLine = fl;
   108         }
   109 
   110         final boolean reached(Player p) {
   111             return p.getYInternal() == finalLine;
   112         }
   113 
   114         static Direction valueOf(char ch) throws IllegalPositionException {
   115             for (Direction direction : values()) {
   116                 if (direction.name().charAt(0) == ch) {
   117                     return direction;
   118                 }
   119             }
   120             throw new IllegalPositionException("No direction " + ch);
   121         }
   122     }
   123 }