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