webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 19 Sep 2009 14:38:29 +0200
changeset 100 8b899ed24f9f
parent 79 89bca098e14e
child 115 6a80463a74c0
permissions -rw-r--r--
Browsable history of each game
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@78
    34
import java.util.Date;
jtulach@37
    35
import java.util.List;
jtulach@35
    36
import javax.xml.bind.annotation.XmlAccessType;
jtulach@35
    37
import javax.xml.bind.annotation.XmlAccessorType;
jtulach@35
    38
import javax.xml.bind.annotation.XmlAttribute;
jaroslav@48
    39
import javax.xml.bind.annotation.XmlElement;
jtulach@35
    40
import javax.xml.bind.annotation.XmlRootElement;
jaroslav@48
    41
import javax.xml.bind.annotation.adapters.XmlAdapter;
jaroslav@48
    42
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
jtulach@35
    43
jtulach@35
    44
/**
jtulach@35
    45
 *
jtulach@35
    46
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@35
    47
 */
jtulach@35
    48
@XmlRootElement
jtulach@35
    49
@XmlAccessorType(XmlAccessType.FIELD)
jaroslav@48
    50
public final class Game extends Object {
jaroslav@48
    51
    @XmlElement
jaroslav@48
    52
    private GameId id;
jtulach@54
    53
    @XmlElement
jaroslav@48
    54
    @XmlJavaTypeAdapter(BoardAdapter.class)
jaroslav@48
    55
    private Board board;
jaroslav@48
    56
    @XmlElement
jaroslav@48
    57
    @XmlJavaTypeAdapter(MoveAdapter.class)
jaroslav@48
    58
    private List<Move> moves;
jaroslav@100
    59
    private Integer move;
jtulach@35
    60
jtulach@35
    61
    Game() {
jtulach@35
    62
    }
jtulach@35
    63
jtulach@35
    64
    public Game(String first, String second) {
jaroslav@48
    65
        this.id = new GameId(first, second);
jaroslav@53
    66
        this.board = Board.empty();
jtulach@37
    67
    }
jtulach@37
    68
jaroslav@48
    69
    public Game(GameId id) {
jtulach@37
    70
        this.id = id;
jaroslav@53
    71
        this.board = Board.empty();
jtulach@35
    72
    }
jtulach@35
    73
jaroslav@48
    74
    public GameId getId() {
jtulach@35
    75
        return id;
jtulach@35
    76
    }
jaroslav@48
    77
    
jtulach@35
    78
    public Board getBoard() {
jtulach@35
    79
        return board;
jtulach@35
    80
    }
jtulach@35
    81
jaroslav@55
    82
    @XmlAttribute
jaroslav@55
    83
    final String getCurrentPlayer() {
jaroslav@55
    84
        if (board.getCurrentPlayer() == board.getPlayers().get(0)) {
jaroslav@55
    85
            return id.getWhite();
jaroslav@55
    86
        } else {
jaroslav@55
    87
            return id.getBlack();
jaroslav@55
    88
        }
jaroslav@55
    89
    }
jaroslav@55
    90
jtulach@79
    91
    public void apply(String player, Move m, Date when) throws IllegalPositionException {
jtulach@35
    92
        Player p = null;
jaroslav@48
    93
        if (id.getWhite().equals(player)) {
jtulach@35
    94
            p = getBoard().getPlayers().get(0);
jtulach@35
    95
        } else {
jaroslav@48
    96
            if (id.getBlack().equals(player)) {
jtulach@35
    97
                p = getBoard().getPlayers().get(1);
jtulach@35
    98
            }
jtulach@35
    99
        }
jtulach@35
   100
        if (p != getBoard().getCurrentPlayer()) {
jtulach@35
   101
            throw new IllegalArgumentException("Wrong player: " + player);
jtulach@35
   102
        }
jtulach@35
   103
jtulach@35
   104
        board = getBoard().apply(m);
jtulach@79
   105
        if (when == null) {
jtulach@79
   106
            when = new Date(id.getModified());
jtulach@79
   107
        }
jtulach@79
   108
        id = new GameId(id.getId(), id.getWhite(), id.getBlack(), new Date(id.getStarted()), when, GameStatus.valueOf(board));
jtulach@37
   109
        getMoves().add(m);
jtulach@37
   110
    }
jtulach@37
   111
jtulach@37
   112
    public List<Move> getMoves() {
jtulach@37
   113
        if (moves == null) {
jtulach@37
   114
            moves = new ArrayList<Move>();
jtulach@37
   115
        }
jtulach@37
   116
        return moves;
jtulach@35
   117
    }
jaroslav@48
   118
jaroslav@100
   119
    @XmlAttribute
jaroslav@100
   120
    public int getCurrentMove() {
jaroslav@100
   121
        return move == null ? getMoves().size() : move;
jaroslav@100
   122
    }
jaroslav@100
   123
jaroslav@100
   124
    public Game snapshot(int move) throws IllegalPositionException {
jaroslav@100
   125
        Board b = Board.empty();
jaroslav@100
   126
        int cnt = 0;
jaroslav@100
   127
        for (Move m : getMoves()) {
jaroslav@100
   128
            if (move-- <= 0) {
jaroslav@100
   129
                break;
jaroslav@100
   130
            }
jaroslav@100
   131
            b = b.apply(m);
jaroslav@100
   132
            cnt++;
jaroslav@100
   133
        }
jaroslav@100
   134
jaroslav@100
   135
        Game g = new Game(
jaroslav@100
   136
            new GameId(
jaroslav@100
   137
                id.getId(), id.getWhite(), id.getBlack(),
jaroslav@100
   138
                new Date(id.getStarted()), new Date(id.getModified()),
jaroslav@100
   139
                GameStatus.history
jaroslav@100
   140
            )
jaroslav@100
   141
        );
jaroslav@100
   142
        g.board = b;
jaroslav@100
   143
        g.move = cnt;
jaroslav@100
   144
        g.moves = new ArrayList<Move>(getMoves());
jaroslav@100
   145
        return g;
jaroslav@100
   146
    }
jaroslav@100
   147
jaroslav@60
   148
    @XmlAccessorType(XmlAccessType.FIELD)
jaroslav@60
   149
    private static final class MoveTmp {
jaroslav@60
   150
        @XmlAttribute
jaroslav@60
   151
        String move;
jaroslav@60
   152
        @XmlAttribute
jaroslav@60
   153
        int index;
jaroslav@60
   154
jaroslav@60
   155
        private MoveTmp() {
jaroslav@60
   156
        }
jaroslav@60
   157
jaroslav@60
   158
        public MoveTmp(String move, int index) {
jaroslav@60
   159
            this.move = move;
jaroslav@60
   160
            this.index = index;
jaroslav@60
   161
        }
jaroslav@60
   162
    }
jaroslav@60
   163
jaroslav@60
   164
    private static final class MoveAdapter extends XmlAdapter<MoveTmp[],List<Move>> {
jaroslav@48
   165
        @Override
jaroslav@60
   166
        public List<Move> unmarshal(MoveTmp[] arr) throws Exception {
jaroslav@48
   167
            List<Move> res = new ArrayList<Move>();
jaroslav@52
   168
            if (arr != null) {
jaroslav@60
   169
                for (MoveTmp v : arr) {
jaroslav@60
   170
                    res.add(Move.valueOf(v.move));
jaroslav@52
   171
                }
jaroslav@48
   172
            }
jaroslav@48
   173
            return res;
jaroslav@48
   174
        }
jaroslav@48
   175
jaroslav@48
   176
        @Override
jaroslav@60
   177
        public MoveTmp[] marshal(List<Move> arr) throws Exception {
jaroslav@60
   178
            List<MoveTmp> res = new ArrayList<MoveTmp>();
jaroslav@52
   179
            if (arr != null) {
jaroslav@60
   180
                int index = 0;
jaroslav@52
   181
                for (Move m : arr) {
jaroslav@60
   182
                    res.add(new MoveTmp(m.toString(), ++index));
jaroslav@52
   183
                }
jaroslav@48
   184
            }
jaroslav@60
   185
            return res.toArray(new MoveTmp[0]);
jaroslav@48
   186
        }
jaroslav@48
   187
    } // end of MoveAdapter
jaroslav@48
   188
jaroslav@48
   189
    private static final class BoardAdapter extends XmlAdapter<String,Board> {
jaroslav@48
   190
jaroslav@48
   191
        @Override
jaroslav@48
   192
        public Board unmarshal(String v) throws Exception {
jaroslav@52
   193
            return v == null ? null : Board.valueOf(v);
jaroslav@48
   194
        }
jaroslav@48
   195
jaroslav@48
   196
        @Override
jaroslav@48
   197
        public String marshal(Board v) throws Exception {
jaroslav@52
   198
            return v == null ? null : v.toString();
jaroslav@48
   199
        }
jaroslav@48
   200
jaroslav@48
   201
    }
jtulach@35
   202
}