webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java
author Jaroslav Tulach <jtulach@netbeans.org>
Mon, 31 Aug 2009 22:44:47 +0200
changeset 54 f041b6570ff9
parent 53 ccc325a936cc
child 55 830e0ba29c04
permissions -rw-r--r--
Removing anything related to JSON from the HTML layer, using natural freemarker's W3C DOM processing capabilities
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@37
    33
import java.util.ArrayList;
jtulach@37
    34
import java.util.List;
jtulach@35
    35
import javax.xml.bind.annotation.XmlAccessType;
jtulach@35
    36
import javax.xml.bind.annotation.XmlAccessorType;
jtulach@35
    37
import javax.xml.bind.annotation.XmlAttribute;
jaroslav@48
    38
import javax.xml.bind.annotation.XmlElement;
jtulach@35
    39
import javax.xml.bind.annotation.XmlRootElement;
jaroslav@48
    40
import javax.xml.bind.annotation.adapters.XmlAdapter;
jaroslav@48
    41
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
jtulach@35
    42
jtulach@35
    43
/**
jtulach@35
    44
 *
jtulach@35
    45
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@35
    46
 */
jtulach@35
    47
@XmlRootElement
jtulach@35
    48
@XmlAccessorType(XmlAccessType.FIELD)
jaroslav@48
    49
public final class Game extends Object {
jaroslav@48
    50
    @XmlElement
jaroslav@48
    51
    private GameId id;
jtulach@54
    52
    @XmlElement
jaroslav@48
    53
    @XmlJavaTypeAdapter(BoardAdapter.class)
jaroslav@48
    54
    private Board board;
jaroslav@48
    55
    @XmlElement
jaroslav@48
    56
    @XmlJavaTypeAdapter(MoveAdapter.class)
jaroslav@48
    57
    private List<Move> moves;
jtulach@35
    58
jtulach@35
    59
    Game() {
jtulach@35
    60
    }
jtulach@35
    61
jtulach@35
    62
    public Game(String first, String second) {
jaroslav@48
    63
        this.id = new GameId(first, second);
jaroslav@53
    64
        this.board = Board.empty();
jtulach@37
    65
    }
jtulach@37
    66
jaroslav@48
    67
    public Game(GameId id) {
jtulach@37
    68
        this.id = id;
jaroslav@53
    69
        this.board = Board.empty();
jtulach@35
    70
    }
jtulach@35
    71
jaroslav@48
    72
    public GameId getId() {
jtulach@35
    73
        return id;
jtulach@35
    74
    }
jaroslav@48
    75
    
jtulach@35
    76
    public Board getBoard() {
jtulach@35
    77
        return board;
jtulach@35
    78
    }
jtulach@35
    79
jtulach@35
    80
    public void apply(String player, Move m) throws IllegalPositionException {
jtulach@35
    81
        Player p = null;
jaroslav@48
    82
        if (id.getWhite().equals(player)) {
jtulach@35
    83
            p = getBoard().getPlayers().get(0);
jtulach@35
    84
        } else {
jaroslav@48
    85
            if (id.getBlack().equals(player)) {
jtulach@35
    86
                p = getBoard().getPlayers().get(1);
jtulach@35
    87
            }
jtulach@35
    88
        }
jtulach@35
    89
        if (p != getBoard().getCurrentPlayer()) {
jtulach@35
    90
            throw new IllegalArgumentException("Wrong player: " + player);
jtulach@35
    91
        }
jtulach@35
    92
jtulach@35
    93
        board = getBoard().apply(m);
jtulach@37
    94
        getMoves().add(m);
jtulach@37
    95
    }
jtulach@37
    96
jtulach@37
    97
    public List<Move> getMoves() {
jtulach@37
    98
        if (moves == null) {
jtulach@37
    99
            moves = new ArrayList<Move>();
jtulach@37
   100
        }
jtulach@37
   101
        return moves;
jtulach@35
   102
    }
jaroslav@48
   103
jaroslav@48
   104
    private static final class MoveAdapter extends XmlAdapter<String[],List<Move>> {
jaroslav@48
   105
        @Override
jaroslav@48
   106
        public List<Move> unmarshal(String[] arr) throws Exception {
jaroslav@48
   107
            List<Move> res = new ArrayList<Move>();
jaroslav@52
   108
            if (arr != null) {
jaroslav@52
   109
                for (String v : arr) {
jaroslav@52
   110
                    res.add(Move.valueOf(v));
jaroslav@52
   111
                }
jaroslav@48
   112
            }
jaroslav@48
   113
            return res;
jaroslav@48
   114
        }
jaroslav@48
   115
jaroslav@48
   116
        @Override
jaroslav@48
   117
        public String[] marshal(List<Move> arr) throws Exception {
jaroslav@48
   118
            List<String> res = new ArrayList<String>();
jaroslav@52
   119
            if (arr != null) {
jaroslav@52
   120
                for (Move m : arr) {
jaroslav@52
   121
                    res.add(m.toString());
jaroslav@52
   122
                }
jaroslav@48
   123
            }
jaroslav@48
   124
            return res.toArray(new String[0]);
jaroslav@48
   125
        }
jaroslav@48
   126
    } // end of MoveAdapter
jaroslav@48
   127
jaroslav@48
   128
    private static final class BoardAdapter extends XmlAdapter<String,Board> {
jaroslav@48
   129
jaroslav@48
   130
        @Override
jaroslav@48
   131
        public Board unmarshal(String v) throws Exception {
jaroslav@52
   132
            return v == null ? null : Board.valueOf(v);
jaroslav@48
   133
        }
jaroslav@48
   134
jaroslav@48
   135
        @Override
jaroslav@48
   136
        public String marshal(Board v) throws Exception {
jaroslav@52
   137
            return v == null ? null : v.toString();
jaroslav@48
   138
        }
jaroslav@48
   139
jaroslav@48
   140
    }
jtulach@35
   141
}