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