webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java
changeset 35 2e85dd878f04
child 37 782d925cb5a1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java	Sun Jul 12 13:59:07 2009 +0200
     1.3 @@ -0,0 +1,99 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * The contents of this file are subject to the terms of either the GNU
     1.8 + * General Public License Version 2 only ("GPL") or the Common
     1.9 + * Development and Distribution License("CDDL") (collectively, the
    1.10 + * "License"). You may not use this file except in compliance with the
    1.11 + * License. You can obtain a copy of the License at
    1.12 + * http://www.netbeans.org/cddl-gplv2.html
    1.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.14 + * specific language governing permissions and limitations under the
    1.15 + * License.  When distributing the software, include this License Header
    1.16 + * Notice in each file and include the License file at
    1.17 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    1.18 + * particular file as subject to the "Classpath" exception as provided
    1.19 + * by Sun in the GPL Version 2 section of the License file that
    1.20 + * accompanied this code. If applicable, add the following below the
    1.21 + * License Header, with the fields enclosed by brackets [] replaced by
    1.22 + * your own identifying information:
    1.23 + * "Portions Copyrighted [year] [name of copyright owner]"
    1.24 + *
    1.25 + * Contributor(s):
    1.26 + *
    1.27 + * Portions Copyrighted 2009 Jaroslav Tulach
    1.28 + */
    1.29 +
    1.30 +package cz.xelfi.quoridor.webidor;
    1.31 +
    1.32 +import cz.xelfi.quoridor.Board;
    1.33 +import cz.xelfi.quoridor.IllegalPositionException;
    1.34 +import cz.xelfi.quoridor.Move;
    1.35 +import cz.xelfi.quoridor.Player;
    1.36 +import java.util.UUID;
    1.37 +import javax.xml.bind.annotation.XmlAccessType;
    1.38 +import javax.xml.bind.annotation.XmlAccessorType;
    1.39 +import javax.xml.bind.annotation.XmlAttribute;
    1.40 +import javax.xml.bind.annotation.XmlID;
    1.41 +import javax.xml.bind.annotation.XmlRootElement;
    1.42 +
    1.43 +/**
    1.44 + *
    1.45 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.46 + */
    1.47 +@XmlRootElement
    1.48 +@XmlAccessorType(XmlAccessType.FIELD)
    1.49 +public final class Game {
    1.50 +    @XmlAttribute
    1.51 +    private String white;
    1.52 +    @XmlAttribute
    1.53 +    private String black;
    1.54 +    @XmlID
    1.55 +    private String id;
    1.56 +
    1.57 +    private transient Board board;
    1.58 +
    1.59 +    Game() {
    1.60 +    }
    1.61 +
    1.62 +    public Game(String first, String second) {
    1.63 +        this.white = first;
    1.64 +        this.black = second;
    1.65 +        this.id = UUID.randomUUID().toString();
    1.66 +    }
    1.67 +
    1.68 +    public String getId() {
    1.69 +        return id;
    1.70 +    }
    1.71 +
    1.72 +    public String getWhite() {
    1.73 +        return white;
    1.74 +    }
    1.75 +
    1.76 +    public String getBlack() {
    1.77 +        return black;
    1.78 +    }
    1.79 +
    1.80 +    public Board getBoard() {
    1.81 +        if (board == null) {
    1.82 +            board = Board.empty();
    1.83 +        }
    1.84 +        return board;
    1.85 +    }
    1.86 +
    1.87 +    public void apply(String player, Move m) throws IllegalPositionException {
    1.88 +        Player p = null;
    1.89 +        if (getWhite().equals(player)) {
    1.90 +            p = getBoard().getPlayers().get(0);
    1.91 +        } else {
    1.92 +            if (getBlack().equals(player)) {
    1.93 +                p = getBoard().getPlayers().get(1);
    1.94 +            }
    1.95 +        }
    1.96 +        if (p != getBoard().getCurrentPlayer()) {
    1.97 +            throw new IllegalArgumentException("Wrong player: " + player);
    1.98 +        }
    1.99 +
   1.100 +        board = getBoard().apply(m);
   1.101 +    }
   1.102 +}