webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 12 Jul 2009 13:59:07 +0200
changeset 35 2e85dd878f04
child 37 782d925cb5a1
permissions -rw-r--r--
Converting the webidor into Jersey based REST server
jtulach@35
     1
/*
jtulach@35
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jtulach@35
     3
 *
jtulach@35
     4
 * The contents of this file are subject to the terms of either the GNU
jtulach@35
     5
 * General Public License Version 2 only ("GPL") or the Common
jtulach@35
     6
 * Development and Distribution License("CDDL") (collectively, the
jtulach@35
     7
 * "License"). You may not use this file except in compliance with the
jtulach@35
     8
 * License. You can obtain a copy of the License at
jtulach@35
     9
 * http://www.netbeans.org/cddl-gplv2.html
jtulach@35
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jtulach@35
    11
 * specific language governing permissions and limitations under the
jtulach@35
    12
 * License.  When distributing the software, include this License Header
jtulach@35
    13
 * Notice in each file and include the License file at
jtulach@35
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jtulach@35
    15
 * particular file as subject to the "Classpath" exception as provided
jtulach@35
    16
 * by Sun in the GPL Version 2 section of the License file that
jtulach@35
    17
 * accompanied this code. If applicable, add the following below the
jtulach@35
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jtulach@35
    19
 * your own identifying information:
jtulach@35
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jtulach@35
    21
 *
jtulach@35
    22
 * Contributor(s):
jtulach@35
    23
 *
jtulach@35
    24
 * Portions Copyrighted 2009 Jaroslav Tulach
jtulach@35
    25
 */
jtulach@35
    26
jtulach@35
    27
package cz.xelfi.quoridor.webidor;
jtulach@35
    28
jtulach@35
    29
import cz.xelfi.quoridor.Board;
jtulach@35
    30
import cz.xelfi.quoridor.IllegalPositionException;
jtulach@35
    31
import cz.xelfi.quoridor.Move;
jtulach@35
    32
import cz.xelfi.quoridor.Player;
jtulach@35
    33
import java.util.UUID;
jtulach@35
    34
import javax.xml.bind.annotation.XmlAccessType;
jtulach@35
    35
import javax.xml.bind.annotation.XmlAccessorType;
jtulach@35
    36
import javax.xml.bind.annotation.XmlAttribute;
jtulach@35
    37
import javax.xml.bind.annotation.XmlID;
jtulach@35
    38
import javax.xml.bind.annotation.XmlRootElement;
jtulach@35
    39
jtulach@35
    40
/**
jtulach@35
    41
 *
jtulach@35
    42
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@35
    43
 */
jtulach@35
    44
@XmlRootElement
jtulach@35
    45
@XmlAccessorType(XmlAccessType.FIELD)
jtulach@35
    46
public final class Game {
jtulach@35
    47
    @XmlAttribute
jtulach@35
    48
    private String white;
jtulach@35
    49
    @XmlAttribute
jtulach@35
    50
    private String black;
jtulach@35
    51
    @XmlID
jtulach@35
    52
    private String id;
jtulach@35
    53
jtulach@35
    54
    private transient Board board;
jtulach@35
    55
jtulach@35
    56
    Game() {
jtulach@35
    57
    }
jtulach@35
    58
jtulach@35
    59
    public Game(String first, String second) {
jtulach@35
    60
        this.white = first;
jtulach@35
    61
        this.black = second;
jtulach@35
    62
        this.id = UUID.randomUUID().toString();
jtulach@35
    63
    }
jtulach@35
    64
jtulach@35
    65
    public String getId() {
jtulach@35
    66
        return id;
jtulach@35
    67
    }
jtulach@35
    68
jtulach@35
    69
    public String getWhite() {
jtulach@35
    70
        return white;
jtulach@35
    71
    }
jtulach@35
    72
jtulach@35
    73
    public String getBlack() {
jtulach@35
    74
        return black;
jtulach@35
    75
    }
jtulach@35
    76
jtulach@35
    77
    public Board getBoard() {
jtulach@35
    78
        if (board == null) {
jtulach@35
    79
            board = Board.empty();
jtulach@35
    80
        }
jtulach@35
    81
        return board;
jtulach@35
    82
    }
jtulach@35
    83
jtulach@35
    84
    public void apply(String player, Move m) throws IllegalPositionException {
jtulach@35
    85
        Player p = null;
jtulach@35
    86
        if (getWhite().equals(player)) {
jtulach@35
    87
            p = getBoard().getPlayers().get(0);
jtulach@35
    88
        } else {
jtulach@35
    89
            if (getBlack().equals(player)) {
jtulach@35
    90
                p = getBoard().getPlayers().get(1);
jtulach@35
    91
            }
jtulach@35
    92
        }
jtulach@35
    93
        if (p != getBoard().getCurrentPlayer()) {
jtulach@35
    94
            throw new IllegalArgumentException("Wrong player: " + player);
jtulach@35
    95
        }
jtulach@35
    96
jtulach@35
    97
        board = getBoard().apply(m);
jtulach@35
    98
    }
jtulach@35
    99
}