1.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java Sun Sep 05 22:34:43 2010 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,213 +0,0 @@
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.ArrayList;
1.37 -import java.util.Date;
1.38 -import java.util.List;
1.39 -import javax.xml.bind.annotation.XmlAccessType;
1.40 -import javax.xml.bind.annotation.XmlAccessorType;
1.41 -import javax.xml.bind.annotation.XmlAttribute;
1.42 -import javax.xml.bind.annotation.XmlElement;
1.43 -import javax.xml.bind.annotation.XmlElementWrapper;
1.44 -import javax.xml.bind.annotation.XmlRootElement;
1.45 -import javax.xml.bind.annotation.adapters.XmlAdapter;
1.46 -import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
1.47 -
1.48 -/**
1.49 - *
1.50 - * @author Jaroslav Tulach <jtulach@netbeans.org>
1.51 - */
1.52 -@XmlRootElement
1.53 -@XmlAccessorType(XmlAccessType.FIELD)
1.54 -public final class Game extends Object {
1.55 - @XmlElement
1.56 - private GameId id;
1.57 - @XmlElement
1.58 - @XmlJavaTypeAdapter(BoardAdapter.class)
1.59 - private Board board;
1.60 - @XmlElementWrapper(name="moves")
1.61 - @XmlElement(name="item")
1.62 - private List<CommentedMove> moves;
1.63 - private Integer move;
1.64 -
1.65 - Game() {
1.66 - }
1.67 -
1.68 - public Game(String first, String second) {
1.69 - this.id = new GameId(first, second);
1.70 - this.board = Board.empty();
1.71 - }
1.72 -
1.73 - public Game(GameId id) {
1.74 - this.id = id;
1.75 - this.board = Board.empty();
1.76 - }
1.77 -
1.78 - public GameId getId() {
1.79 - return id;
1.80 - }
1.81 -
1.82 - public Board getBoard() {
1.83 - return board;
1.84 - }
1.85 -
1.86 - @XmlAttribute
1.87 - final String getCurrentPlayer() {
1.88 - Player w = board.getWinner();
1.89 - if(w==null){
1.90 - if (board.getCurrentPlayer() == board.getPlayers().get(0)) {
1.91 - return id.getWhite();
1.92 - } else {
1.93 - return id.getBlack();
1.94 - }
1.95 - }else{
1.96 - if (w == board.getPlayers().get(0)) {
1.97 - return id.getWhite();
1.98 - } else {
1.99 - return id.getBlack();
1.100 - }
1.101 - }
1.102 - }
1.103 -
1.104 - public void apply(String player, Move m, Date when) throws IllegalPositionException {
1.105 - Player p = null;
1.106 - if (id.getWhite().equals(player)) {
1.107 - p = getBoard().getPlayers().get(0);
1.108 - } else {
1.109 - if (id.getBlack().equals(player)) {
1.110 - p = getBoard().getPlayers().get(1);
1.111 - }
1.112 - }
1.113 - if (p != getBoard().getCurrentPlayer()) {
1.114 - throw new IllegalArgumentException("Wrong player: " + player);
1.115 - }
1.116 -
1.117 - board = getBoard().apply(m);
1.118 - if (when == null) {
1.119 - when = new Date(id.getModified());
1.120 - }
1.121 - final GameStatus status = GameStatus.valueOf(board);
1.122 - id = new GameId(
1.123 - id.getId(), id.getWhite(), id.getBlack(),
1.124 - new Date(id.getStarted()), when, status,
1.125 - id.getComments(), !status.isInProgress());
1.126 - getMoves().add(new CommentedMove(m, getMoves().size() + 1));
1.127 - }
1.128 -
1.129 - public void comment(String player, String comment, Date date) {
1.130 - Note n = new Note(comment, date, player);
1.131 - id = new GameId(
1.132 - id.getId(), id.getWhite(), id.getBlack(),
1.133 - new Date(id.getStarted()), new Date(id.getModified()),
1.134 - GameStatus.valueOf(board), id.getComments() + 1, id.isFinished()
1.135 - );
1.136 - getMoves().get(getMoves().size() - 1).addNote(n);
1.137 - }
1.138 -
1.139 -
1.140 - public List<CommentedMove> getMoves() {
1.141 - if (moves == null) {
1.142 - moves = new ArrayList<CommentedMove>();
1.143 - }
1.144 - return moves;
1.145 - }
1.146 -
1.147 - @XmlAttribute
1.148 - public int getCurrentMove() {
1.149 - return move == null ? getMoves().size() : move;
1.150 - }
1.151 -
1.152 - public Game snapshot(int move) throws IllegalPositionException {
1.153 - Board b = Board.empty();
1.154 - int cnt = 0;
1.155 - for (CommentedMove m : getMoves()) {
1.156 - if (move-- <= 0) {
1.157 - break;
1.158 - }
1.159 - b = b.apply(m.getMove());
1.160 - cnt++;
1.161 - }
1.162 -
1.163 - Game g = new Game(
1.164 - new GameId(
1.165 - id.getId(), id.getWhite(), id.getBlack(),
1.166 - new Date(id.getStarted()), new Date(id.getModified()),
1.167 - GameStatus.history, id.getComments(), id.isFinished()
1.168 - )
1.169 - );
1.170 - g.board = b;
1.171 - g.move = cnt;
1.172 - g.moves = new ArrayList<CommentedMove>(getMoves());
1.173 - return g;
1.174 - }
1.175 -/*
1.176 - private static final class MoveAdapter extends XmlAdapter<CommentedMove[],List<Move>> {
1.177 - @Override
1.178 - public List<Move> unmarshal(CommentedMove[] arr) throws Exception {
1.179 - List<Move> res = new ArrayList<Move>();
1.180 - if (arr != null) {
1.181 - for (CommentedMove v : arr) {
1.182 - res.add(Move.valueOf(v.move));
1.183 - }
1.184 - }
1.185 - return res;
1.186 - }
1.187 -
1.188 - @Override
1.189 - public CommentedMove[] marshal(List<Move> arr) throws Exception {
1.190 - List<CommentedMove> res = new ArrayList<CommentedMove>();
1.191 - if (arr != null) {
1.192 - int index = 0;
1.193 - for (Move m : arr) {
1.194 - res.add(new CommentedMove(m.toString(), ++index));
1.195 - }
1.196 - }
1.197 - return res.toArray(new CommentedMove[0]);
1.198 - }
1.199 - } // end of MoveAdapter
1.200 -*/
1.201 - private static final class BoardAdapter extends XmlAdapter<String,Board> {
1.202 -
1.203 - @Override
1.204 - public Board unmarshal(String v) throws Exception {
1.205 - return v == null ? null : Board.valueOf(v);
1.206 -// return v == null ? null : new Board(v);
1.207 - }
1.208 -
1.209 - @Override
1.210 - public String marshal(Board v) throws Exception {
1.211 - return v == null ? null : v.toString();
1.212 -// return v == null ? null : Board.board2HashCode(v);
1.213 - }
1.214 -
1.215 - }
1.216 -}