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