webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 04 Sep 2009 21:14:33 +0200
changeset 57 fa12b02023a0
parent 55 830e0ba29c04
child 60 74d333da1725
permissions -rw-r--r--
Separating games by their current state
     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.List;
    35 import javax.xml.bind.annotation.XmlAccessType;
    36 import javax.xml.bind.annotation.XmlAccessorType;
    37 import javax.xml.bind.annotation.XmlAttribute;
    38 import javax.xml.bind.annotation.XmlElement;
    39 import javax.xml.bind.annotation.XmlRootElement;
    40 import javax.xml.bind.annotation.adapters.XmlAdapter;
    41 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    42 
    43 /**
    44  *
    45  * @author Jaroslav Tulach <jtulach@netbeans.org>
    46  */
    47 @XmlRootElement
    48 @XmlAccessorType(XmlAccessType.FIELD)
    49 public final class Game extends Object {
    50     @XmlElement
    51     private GameId id;
    52     @XmlElement
    53     @XmlJavaTypeAdapter(BoardAdapter.class)
    54     private Board board;
    55     @XmlElement
    56     @XmlJavaTypeAdapter(MoveAdapter.class)
    57     private List<Move> moves;
    58 
    59     Game() {
    60     }
    61 
    62     public Game(String first, String second) {
    63         this.id = new GameId(first, second);
    64         this.board = Board.empty();
    65     }
    66 
    67     public Game(GameId id) {
    68         this.id = id;
    69         this.board = Board.empty();
    70     }
    71 
    72     public GameId getId() {
    73         return id;
    74     }
    75     
    76     public Board getBoard() {
    77         return board;
    78     }
    79 
    80     @XmlAttribute
    81     final String getCurrentPlayer() {
    82         if (board.getCurrentPlayer() == board.getPlayers().get(0)) {
    83             return id.getWhite();
    84         } else {
    85             return id.getBlack();
    86         }
    87     }
    88 
    89     public void apply(String player, Move m) throws IllegalPositionException {
    90         Player p = null;
    91         if (id.getWhite().equals(player)) {
    92             p = getBoard().getPlayers().get(0);
    93         } else {
    94             if (id.getBlack().equals(player)) {
    95                 p = getBoard().getPlayers().get(1);
    96             }
    97         }
    98         if (p != getBoard().getCurrentPlayer()) {
    99             throw new IllegalArgumentException("Wrong player: " + player);
   100         }
   101 
   102         board = getBoard().apply(m);
   103         if (board.getWinner() != null) {
   104             GameResult r = board.getWinner() == board.getPlayers().get(0) ? GameResult.WHITE_WON : GameResult.BLACK_WON;
   105             id = new GameId(id.getId(), id.getWhite(), id.getBlack(), id.getStarted(), r);
   106         }
   107         getMoves().add(m);
   108     }
   109 
   110     public List<Move> getMoves() {
   111         if (moves == null) {
   112             moves = new ArrayList<Move>();
   113         }
   114         return moves;
   115     }
   116 
   117     private static final class MoveAdapter extends XmlAdapter<String[],List<Move>> {
   118         @Override
   119         public List<Move> unmarshal(String[] arr) throws Exception {
   120             List<Move> res = new ArrayList<Move>();
   121             if (arr != null) {
   122                 for (String v : arr) {
   123                     res.add(Move.valueOf(v));
   124                 }
   125             }
   126             return res;
   127         }
   128 
   129         @Override
   130         public String[] marshal(List<Move> arr) throws Exception {
   131             List<String> res = new ArrayList<String>();
   132             if (arr != null) {
   133                 for (Move m : arr) {
   134                     res.add(m.toString());
   135                 }
   136             }
   137             return res.toArray(new String[0]);
   138         }
   139     } // end of MoveAdapter
   140 
   141     private static final class BoardAdapter extends XmlAdapter<String,Board> {
   142 
   143         @Override
   144         public Board unmarshal(String v) throws Exception {
   145             return v == null ? null : Board.valueOf(v);
   146         }
   147 
   148         @Override
   149         public String marshal(Board v) throws Exception {
   150             return v == null ? null : v.toString();
   151         }
   152 
   153     }
   154 }