quoridor/src/main/java/cz/xelfi/quoridor/Fence.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 08:56:13 +0200
changeset 264 d60370059c3c
parent 30 f25846453907
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 of a fence on a {@link Board}. The fence can be oriented 
    22  * either in
    23  * {@link Orientation#HORIZONTAL} or {@link Orientation#VERTICAL} direction.
    24  * The faces postions are numbered 1-8 and A-H according to following
    25  * graph: <pre>
    26             H   G   F   E   D   C   B   A
    27             |   |   |   |   |   |   |   |
    28         +-----------------------------------+
    29         |                                   |
    30      1--|   +   +   +   +   +   +   +   +   |--1
    31         |                                   |
    32      2--|   +   +   +   +   +   +   +   +   |--2
    33         |                                   |
    34      3--|   +   +   +   +   +   +   +   +   |--3
    35         |                                   |
    36      4--|   +   +   +   +   +   +   +   +   |--4
    37 [E]     |                                   |     [W]
    38      5--|   +   +   +   +   +   +   +   +   |--5
    39         |                                   |
    40      6--|   +   +   +   +   +   +   +   +   |--6
    41         |                                   |
    42      7--|   +   +   +   +   +   +   +   +   |--7
    43         |                                   |
    44      8--|   +   +   +   +   +   +   +   +   |--8
    45         |                                   |
    46         +-----------------------------------+
    47             |   |   |   |   |   |   |   |
    48             H   G   F   E   D   C   B   A
    49      * </pre>
    50  *
    51  */
    52 public final class Fence extends Object {
    53 
    54     private final int x;
    55     private final int y;
    56     private final Orientation o;
    57 
    58     Fence(int x, int y, Orientation o) {
    59         super();
    60         this.x = x;
    61         this.y = y;
    62         this.o = o;
    63     }
    64 
    65     /** The column of the center of the fence. The same
    66      * as used in {@link Move#fence(char, int, cz.xelfi.quoridor.Fence.Orientation)}.
    67      *
    68      * @return a letter between 'A' and 'H'
    69      */
    70     public char getColumn() {
    71         return (char) ('A' + (x - 1) / 2);
    72     }
    73 
    74     /** The row of the center of the fence. The same
    75      * as specified in {@link Move#fence(char, int, cz.xelfi.quoridor.Fence.Orientation)}.
    76      *
    77      * @return a number between 1 and 8
    78      */
    79     public int getRow() {
    80         return (y + 1) / 2;
    81     }
    82 
    83     /** Midle coordinate of the fence.
    84      * @return 1-15
    85      */
    86     int getX() {
    87         return x;
    88     }
    89 
    90     /** Midle coordinate of the fence.
    91      * @return 1-15
    92      */
    93     int getY() {
    94         return y;
    95     }
    96 
    97     /** The orientation of the fence.
    98      * 
    99      * @return HORIZONTAL or VERTICAL
   100      */
   101     public Orientation getOrientation() {
   102         return o;
   103     }
   104 
   105     @Override
   106     public String toString() {
   107         return "Fence[" + getColumn() + "," + getRow() + "," + o + "]";
   108     }
   109 
   110     @Override
   111     public int hashCode() {
   112         return 8 * x + 4 * y + 13 + o.hashCode();
   113     }
   114 
   115     @Override
   116     public boolean equals(Object o) {
   117         if (o instanceof Fence) {
   118             Fence f = (Fence) o;
   119             return x == f.x && y == f.y && getOrientation().equals(f.getOrientation());
   120         }
   121         return false;
   122     }
   123 
   124     /** The possible orientation of a {@link Fence}.
   125      * Either horizontal of vertical.
   126      */
   127     public static enum Orientation {
   128         HORIZONTAL, VERTICAL;
   129 
   130         private Orientation() {
   131         }
   132     }
   133 }