wsdor/src/main/java/cz/xelfi/quoridor/webidor/Game.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 08:56:13 +0200
changeset 264 d60370059c3c
parent 256 1758a7727278
permissions -rw-r--r--
Changing headers to GPLv3
     1 /*
     2  * Quoridor server and related libraries
     3  * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://www.gnu.org/licenses/.
    17  */
    18 
    19 package cz.xelfi.quoridor.webidor;
    20 
    21 import cz.xelfi.quoridor.Board;
    22 import cz.xelfi.quoridor.IllegalPositionException;
    23 import cz.xelfi.quoridor.Move;
    24 import cz.xelfi.quoridor.Player;
    25 import java.util.ArrayList;
    26 import java.util.Date;
    27 import java.util.List;
    28 import javax.xml.bind.annotation.XmlAccessType;
    29 import javax.xml.bind.annotation.XmlAccessorType;
    30 import javax.xml.bind.annotation.XmlAttribute;
    31 import javax.xml.bind.annotation.XmlElement;
    32 import javax.xml.bind.annotation.XmlElementWrapper;
    33 import javax.xml.bind.annotation.XmlRootElement;
    34 import javax.xml.bind.annotation.adapters.XmlAdapter;
    35 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    36 
    37 /**
    38  *
    39  * @author Jaroslav Tulach <jtulach@netbeans.org>
    40  */
    41 @XmlRootElement
    42 @XmlAccessorType(XmlAccessType.FIELD)
    43 public final class Game extends Object {
    44     @XmlElement
    45     private GameId id;
    46     @XmlElement
    47     @XmlJavaTypeAdapter(BoardAdapter.class)
    48     private Board board;
    49     @XmlElementWrapper(name="moves")
    50     @XmlElement(name="item")
    51     private List<CommentedMove> moves;
    52     private Integer move;
    53 
    54     Game() {
    55     }
    56 
    57     public Game(String first, String second) {
    58         this.id = new GameId(first, second);
    59         this.board = Board.empty();
    60     }
    61 
    62     public Game(GameId id) {
    63         this.id = id;
    64         this.board = Board.empty();
    65     }
    66 
    67     public GameId getId() {
    68         return id;
    69     }
    70     
    71     public Board getBoard() {
    72         return board;
    73     }
    74 
    75     @XmlAttribute
    76     final String getCurrentPlayer() {
    77         Player w = board.getWinner();
    78         if(w==null){
    79             if (board.getCurrentPlayer() == board.getPlayers().get(0)) {
    80                 return id.getWhite();
    81             } else {
    82                 return id.getBlack();
    83             }
    84         }else{
    85             if (w == board.getPlayers().get(0)) {
    86                 return id.getWhite();
    87             } else {
    88                 return id.getBlack();
    89             }
    90         }
    91     }
    92 
    93     public void apply(String player, Move m, Date when) throws IllegalPositionException {
    94         Player p = null;
    95         if (id.getWhite().equals(player)) {
    96             p = getBoard().getPlayers().get(0);
    97         } else {
    98             if (id.getBlack().equals(player)) {
    99                 p = getBoard().getPlayers().get(1);
   100             }
   101         }
   102         if (p != getBoard().getCurrentPlayer()) {
   103             throw new IllegalArgumentException("Wrong player: " + player);
   104         }
   105 
   106         board = getBoard().apply(m);
   107         if (when == null) {
   108             when = new Date(id.getModified());
   109         }
   110         final GameStatus status = GameStatus.valueOf(board);
   111         id = new GameId(
   112             id.getId(), id.getWhite(), id.getBlack(),
   113             new Date(id.getStarted()), when, status,
   114             id.getComments(), !status.isInProgress());
   115         getMoves().add(new CommentedMove(m, getMoves().size() + 1));
   116     }
   117 
   118     public void comment(String player, String comment, Date date) {
   119         Note n = new Note(comment, date, player);
   120         id = new GameId(
   121             id.getId(), id.getWhite(), id.getBlack(),
   122             new Date(id.getStarted()), new Date(id.getModified()),
   123             GameStatus.valueOf(board), id.getComments() + 1, id.isFinished()
   124         );
   125         getMoves().get(getMoves().size() - 1).addNote(n);
   126     }
   127 
   128 
   129     public List<CommentedMove> getMoves() {
   130         if (moves == null) {
   131             moves = new ArrayList<CommentedMove>();
   132         }
   133         return moves;
   134     }
   135 
   136     @XmlAttribute
   137     public int getCurrentMove() {
   138         return move == null ? getMoves().size() : move;
   139     }
   140 
   141     public Game snapshot(int move) throws IllegalPositionException {
   142         Board b = Board.empty();
   143         int cnt = 0;
   144         for (CommentedMove m : getMoves()) {
   145             if (move-- <= 0) {
   146                 break;
   147             }
   148             b = b.apply(m.getMove());
   149             cnt++;
   150         }
   151 
   152         Game g = new Game(
   153             new GameId(
   154                 id.getId(), id.getWhite(), id.getBlack(),
   155                 new Date(id.getStarted()), new Date(id.getModified()),
   156                 GameStatus.history, id.getComments(), id.isFinished()
   157             )
   158         );
   159         g.board = b;
   160         g.move = cnt;
   161         g.moves = new ArrayList<CommentedMove>(getMoves());
   162         return g;
   163     }
   164 /*
   165     private static final class MoveAdapter extends XmlAdapter<CommentedMove[],List<Move>> {
   166         @Override
   167         public List<Move> unmarshal(CommentedMove[] arr) throws Exception {
   168             List<Move> res = new ArrayList<Move>();
   169             if (arr != null) {
   170                 for (CommentedMove v : arr) {
   171                     res.add(Move.valueOf(v.move));
   172                 }
   173             }
   174             return res;
   175         }
   176 
   177         @Override
   178         public CommentedMove[] marshal(List<Move> arr) throws Exception {
   179             List<CommentedMove> res = new ArrayList<CommentedMove>();
   180             if (arr != null) {
   181                 int index = 0;
   182                 for (Move m : arr) {
   183                     res.add(new CommentedMove(m.toString(), ++index));
   184                 }
   185             }
   186             return res.toArray(new CommentedMove[0]);
   187         }
   188     } // end of MoveAdapter
   189 */
   190     private static final class BoardAdapter extends XmlAdapter<String,Board> {
   191 
   192         @Override
   193         public Board unmarshal(String v) throws Exception {
   194             return v == null ? null : Board.valueOf(v);
   195 //            return v == null ? null : new Board(v);
   196         }
   197 
   198         @Override
   199         public String marshal(Board v) throws Exception {
   200             return v == null ? null : v.toString();
   201 //            return v == null ? null : Board.board2HashCode(v);
   202         }
   203 
   204     }
   205 }