2 * Quoridor server and related libraries
3 * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://www.gnu.org/licenses/.
19 package cz.xelfi.quoridor.webidor;
21 import cz.xelfi.quoridor.Board;
22 import cz.xelfi.quoridor.IllegalPositionException;
23 import cz.xelfi.quoridor.Move;
24 import cz.xelfi.quoridor.Player;
25 import java.util.ArrayList;
26 import java.util.Date;
27 import java.util.List;
28 import javax.xml.bind.annotation.XmlAccessType;
29 import javax.xml.bind.annotation.XmlAccessorType;
30 import javax.xml.bind.annotation.XmlAttribute;
31 import javax.xml.bind.annotation.XmlElement;
32 import javax.xml.bind.annotation.XmlElementWrapper;
33 import javax.xml.bind.annotation.XmlRootElement;
34 import javax.xml.bind.annotation.adapters.XmlAdapter;
35 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
39 * @author Jaroslav Tulach <jtulach@netbeans.org>
42 @XmlAccessorType(XmlAccessType.FIELD)
43 public final class Game extends Object {
47 @XmlJavaTypeAdapter(BoardAdapter.class)
49 @XmlElementWrapper(name="moves")
50 @XmlElement(name="item")
51 private List<CommentedMove> moves;
57 public Game(String first, String second) {
58 this.id = new GameId(first, second);
59 this.board = Board.empty();
62 public Game(GameId id) {
64 this.board = Board.empty();
67 public GameId getId() {
71 public Board getBoard() {
76 final String getCurrentPlayer() {
77 Player w = board.getWinner();
79 if (board.getCurrentPlayer() == board.getPlayers().get(0)) {
85 if (w == board.getPlayers().get(0)) {
93 public void apply(String player, Move m, Date when) throws IllegalPositionException {
95 if (id.getWhite().equals(player)) {
96 p = getBoard().getPlayers().get(0);
98 if (id.getBlack().equals(player)) {
99 p = getBoard().getPlayers().get(1);
102 if (p != getBoard().getCurrentPlayer()) {
103 throw new IllegalArgumentException("Wrong player: " + player);
106 board = getBoard().apply(m);
108 when = new Date(id.getModified());
110 final GameStatus status = GameStatus.valueOf(board);
112 id.getId(), id.getWhite(), id.getBlack(),
113 new Date(id.getStarted()), when, status,
114 id.getComments(), !status.isInProgress());
115 getMoves().add(new CommentedMove(m, getMoves().size() + 1));
118 public void comment(String player, String comment, Date date) {
119 Note n = new Note(comment, date, player);
121 id.getId(), id.getWhite(), id.getBlack(),
122 new Date(id.getStarted()), new Date(id.getModified()),
123 GameStatus.valueOf(board), id.getComments() + 1, id.isFinished()
125 getMoves().get(getMoves().size() - 1).addNote(n);
129 public List<CommentedMove> getMoves() {
131 moves = new ArrayList<CommentedMove>();
137 public int getCurrentMove() {
138 return move == null ? getMoves().size() : move;
141 public Game snapshot(int move) throws IllegalPositionException {
142 Board b = Board.empty();
144 for (CommentedMove m : getMoves()) {
148 b = b.apply(m.getMove());
154 id.getId(), id.getWhite(), id.getBlack(),
155 new Date(id.getStarted()), new Date(id.getModified()),
156 GameStatus.history, id.getComments(), id.isFinished()
161 g.moves = new ArrayList<CommentedMove>(getMoves());
165 private static final class MoveAdapter extends XmlAdapter<CommentedMove[],List<Move>> {
167 public List<Move> unmarshal(CommentedMove[] arr) throws Exception {
168 List<Move> res = new ArrayList<Move>();
170 for (CommentedMove v : arr) {
171 res.add(Move.valueOf(v.move));
178 public CommentedMove[] marshal(List<Move> arr) throws Exception {
179 List<CommentedMove> res = new ArrayList<CommentedMove>();
183 res.add(new CommentedMove(m.toString(), ++index));
186 return res.toArray(new CommentedMove[0]);
188 } // end of MoveAdapter
190 private static final class BoardAdapter extends XmlAdapter<String,Board> {
193 public Board unmarshal(String v) throws Exception {
194 return v == null ? null : Board.valueOf(v);
195 // return v == null ? null : new Board(v);
199 public String marshal(Board v) throws Exception {
200 return v == null ? null : v.toString();
201 // return v == null ? null : Board.board2HashCode(v);