webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 05 Sep 2009 15:40:39 +0200
changeset 60 74d333da1725
parent 57 fa12b02023a0
child 77 d574ac6e44cc
permissions -rw-r--r--
Displaying list of moves
     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.XmlElements;
    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 
    60     Game() {
    61     }
    62 
    63     public Game(String first, String second) {
    64         this.id = new GameId(first, second);
    65         this.board = Board.empty();
    66     }
    67 
    68     public Game(GameId id) {
    69         this.id = id;
    70         this.board = Board.empty();
    71     }
    72 
    73     public GameId getId() {
    74         return id;
    75     }
    76     
    77     public Board getBoard() {
    78         return board;
    79     }
    80 
    81     @XmlAttribute
    82     final String getCurrentPlayer() {
    83         if (board.getCurrentPlayer() == board.getPlayers().get(0)) {
    84             return id.getWhite();
    85         } else {
    86             return id.getBlack();
    87         }
    88     }
    89 
    90     public void apply(String player, Move m) throws IllegalPositionException {
    91         Player p = null;
    92         if (id.getWhite().equals(player)) {
    93             p = getBoard().getPlayers().get(0);
    94         } else {
    95             if (id.getBlack().equals(player)) {
    96                 p = getBoard().getPlayers().get(1);
    97             }
    98         }
    99         if (p != getBoard().getCurrentPlayer()) {
   100             throw new IllegalArgumentException("Wrong player: " + player);
   101         }
   102 
   103         board = getBoard().apply(m);
   104         if (board.getWinner() != null) {
   105             GameResult r = board.getWinner() == board.getPlayers().get(0) ? GameResult.WHITE_WON : GameResult.BLACK_WON;
   106             id = new GameId(id.getId(), id.getWhite(), id.getBlack(), id.getStarted(), r);
   107         }
   108         getMoves().add(m);
   109     }
   110 
   111     public List<Move> getMoves() {
   112         if (moves == null) {
   113             moves = new ArrayList<Move>();
   114         }
   115         return moves;
   116     }
   117 
   118     @XmlAccessorType(XmlAccessType.FIELD)
   119     private static final class MoveTmp {
   120         @XmlAttribute
   121         String move;
   122         @XmlAttribute
   123         int index;
   124 
   125         private MoveTmp() {
   126         }
   127 
   128         public MoveTmp(String move, int index) {
   129             this.move = move;
   130             this.index = index;
   131         }
   132     }
   133 
   134     private static final class MoveAdapter extends XmlAdapter<MoveTmp[],List<Move>> {
   135         @Override
   136         public List<Move> unmarshal(MoveTmp[] arr) throws Exception {
   137             List<Move> res = new ArrayList<Move>();
   138             if (arr != null) {
   139                 for (MoveTmp v : arr) {
   140                     res.add(Move.valueOf(v.move));
   141                 }
   142             }
   143             return res;
   144         }
   145 
   146         @Override
   147         public MoveTmp[] marshal(List<Move> arr) throws Exception {
   148             List<MoveTmp> res = new ArrayList<MoveTmp>();
   149             if (arr != null) {
   150                 int index = 0;
   151                 for (Move m : arr) {
   152                     res.add(new MoveTmp(m.toString(), ++index));
   153                 }
   154             }
   155             return res.toArray(new MoveTmp[0]);
   156         }
   157     } // end of MoveAdapter
   158 
   159     private static final class BoardAdapter extends XmlAdapter<String,Board> {
   160 
   161         @Override
   162         public Board unmarshal(String v) throws Exception {
   163             return v == null ? null : Board.valueOf(v);
   164         }
   165 
   166         @Override
   167         public String marshal(Board v) throws Exception {
   168             return v == null ? null : v.toString();
   169         }
   170 
   171     }
   172 }