2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * The contents of this file are subject to the terms of either the GNU
5 * General Public License Version 2 only ("GPL") or the Common
6 * Development and Distribution License("CDDL") (collectively, the
7 * "License"). You may not use this file except in compliance with the
8 * License. You can obtain a copy of the License at
9 * http://www.netbeans.org/cddl-gplv2.html
10 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
11 * specific language governing permissions and limitations under the
12 * License. When distributing the software, include this License Header
13 * Notice in each file and include the License file at
14 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
15 * particular file as subject to the "Classpath" exception as provided
16 * by Sun in the GPL Version 2 section of the License file that
17 * accompanied this code. If applicable, add the following below the
18 * License Header, with the fields enclosed by brackets [] replaced by
19 * your own identifying information:
20 * "Portions Copyrighted [year] [name of copyright owner]"
24 * Portions Copyrighted 2009 Jaroslav Tulach
27 package cz.xelfi.quoridor.webidor;
29 import cz.xelfi.quoridor.Board;
30 import cz.xelfi.quoridor.IllegalPositionException;
31 import cz.xelfi.quoridor.Move;
32 import cz.xelfi.quoridor.Player;
33 import java.util.ArrayList;
34 import java.util.Date;
35 import java.util.List;
36 import javax.xml.bind.annotation.XmlAccessType;
37 import javax.xml.bind.annotation.XmlAccessorType;
38 import javax.xml.bind.annotation.XmlAttribute;
39 import javax.xml.bind.annotation.XmlElement;
40 import javax.xml.bind.annotation.XmlElementWrapper;
41 import javax.xml.bind.annotation.XmlRootElement;
42 import javax.xml.bind.annotation.adapters.XmlAdapter;
43 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
47 * @author Jaroslav Tulach <jtulach@netbeans.org>
50 @XmlAccessorType(XmlAccessType.FIELD)
51 public final class Game extends Object {
55 @XmlJavaTypeAdapter(BoardAdapter.class)
57 @XmlElementWrapper(name="moves")
58 @XmlElement(name="item")
59 private List<CommentedMove> moves;
65 public Game(String first, String second) {
66 this.id = new GameId(first, second);
67 this.board = Board.empty();
70 public Game(GameId id) {
72 this.board = Board.empty();
75 public GameId getId() {
79 public Board getBoard() {
84 final String getCurrentPlayer() {
85 if (board.getCurrentPlayer() == board.getPlayers().get(0)) {
92 public void apply(String player, Move m, Date when) throws IllegalPositionException {
94 if (id.getWhite().equals(player)) {
95 p = getBoard().getPlayers().get(0);
97 if (id.getBlack().equals(player)) {
98 p = getBoard().getPlayers().get(1);
101 if (p != getBoard().getCurrentPlayer()) {
102 throw new IllegalArgumentException("Wrong player: " + player);
105 board = getBoard().apply(m);
107 when = new Date(id.getModified());
110 id.getId(), id.getWhite(), id.getBlack(),
111 new Date(id.getStarted()), when,
112 GameStatus.valueOf(board), id.getComments()
114 getMoves().add(new CommentedMove(m, getMoves().size() + 1));
117 public void comment(String player, String comment, Date date) {
118 Note n = new Note(comment, date, player);
120 id.getId(), id.getWhite(), id.getBlack(),
121 new Date(id.getStarted()), new Date(id.getModified()),
122 GameStatus.valueOf(board), id.getComments() + 1
124 getMoves().get(getMoves().size() - 1).addNote(n);
128 public List<CommentedMove> getMoves() {
130 moves = new ArrayList<CommentedMove>();
136 public int getCurrentMove() {
137 return move == null ? getMoves().size() : move;
140 public Game snapshot(int move) throws IllegalPositionException {
141 Board b = Board.empty();
143 for (CommentedMove m : getMoves()) {
147 b = b.apply(m.getMove());
153 id.getId(), id.getWhite(), id.getBlack(),
154 new Date(id.getStarted()), new Date(id.getModified()),
155 GameStatus.history, id.getComments()
160 g.moves = new ArrayList<CommentedMove>(getMoves());
164 private static final class MoveAdapter extends XmlAdapter<CommentedMove[],List<Move>> {
166 public List<Move> unmarshal(CommentedMove[] arr) throws Exception {
167 List<Move> res = new ArrayList<Move>();
169 for (CommentedMove v : arr) {
170 res.add(Move.valueOf(v.move));
177 public CommentedMove[] marshal(List<Move> arr) throws Exception {
178 List<CommentedMove> res = new ArrayList<CommentedMove>();
182 res.add(new CommentedMove(m.toString(), ++index));
185 return res.toArray(new CommentedMove[0]);
187 } // end of MoveAdapter
189 private static final class BoardAdapter extends XmlAdapter<String,Board> {
192 public Board unmarshal(String v) throws Exception {
193 return v == null ? null : Board.valueOf(v);
197 public String marshal(Board v) throws Exception {
198 return v == null ? null : v.toString();