2 * The MIT License (MIT)
4 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 package com.oracle.chess.client.htmljava;
26 import com.oracle.chess.client.htmljava.Communication.AlertType;
27 import java.util.Arrays;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.Timer;
31 import java.util.TimerTask;
32 import net.java.html.json.ComputedProperty;
33 import net.java.html.json.Function;
34 import net.java.html.json.Model;
35 import net.java.html.json.ModelOperation;
36 import net.java.html.json.OnPropertyChange;
37 import net.java.html.json.Property;
38 import net.java.html.sound.AudioClip;
40 @Model(className="Board", properties={
41 @Property(name = "player", type = String.class),
42 @Property(name = "gameId", type = String.class),
43 @Property(name = "whitePlayer", type = String.class),
44 @Property(name = "blackPlayer", type = String.class),
45 @Property(name = "alert", type = Alert.class),
46 @Property(name = "alertMessage", type = String.class),
47 @Property(name = "status", type = String.class),
48 @Property(name = "rows", type = Row.class, array = true),
49 @Property(name = "turn", type = Color.class),
50 @Property(name = "moves", type = Move.class, array = true),
51 @Property(name = "pendingMove", type = Move.class),
52 @Property(name = "active", type = boolean.class),
54 public class BoardModel {
55 @ComputedProperty static String title(String status, String gameId) {
56 String t = status != null ? status : gameId;
57 if (t != null && t.length() > 10) {
58 return t.substring(0, 10);
63 @ModelOperation static void updateSummary(Board b, String summary) {
64 if (summary != null) {
69 @ComputedProperty static boolean myTurn(String player, String whitePlayer, String blackPlayer, Color turn) {
70 if (turn != null && player != null) switch (turn) {
71 case B: return player.equals(blackPlayer);
72 case W: return player.equals(whitePlayer);
77 @ComputedProperty static boolean justObserving(String player) {
78 return player == null;
81 private static final AudioClip MOVE = AudioClip.create("sounds/move.mp3");
82 @OnPropertyChange("moves") static void playMove() {
86 private static final AudioClip CHECK = AudioClip.create("sounds/check.mp3");
87 private static final AudioClip CHECKMATE = AudioClip.create("sounds/checkmate.mp3");
88 @OnPropertyChange("alert") static void warnCheckAndMate(Board b) {
89 if (b.getAlert() == null) {
92 if (b.getAlert().getType() == AlertType.CHECK) {
95 if (b.getAlert().getType() == AlertType.CHECKMATE) {
100 @Function static void selected(Board b, Square data) {
102 b.setAlertMessage("Not your turn!");
106 Square previoslySelected = findSelectedSquare(b);
107 if (previoslySelected == null) {
108 if (data.getPiece() != null && data.getPieceColor() == b.getTurn()) {
109 data.setSelected(true);
110 Rules.computeAccessible(b, data);
113 if (previoslySelected == data) {
114 data.setSelected(false);
115 Rules.computeAccessible(b, null);
118 if (data.getPiece() != null && data.getPieceColor() == previoslySelected.getPieceColor()) {
119 previoslySelected.setSelected(false);
120 data.setSelected(true);
121 Rules.computeAccessible(b, data);
124 if (data.isAccessible()) {
125 previoslySelected.setSelected(false);
127 Move newMove = new Move();
128 newMove.setFrom(previoslySelected.getPosition());
129 newMove.setTo(data.getPosition());
130 newMove.setRound(b.getMoves().size() / 2 + 1);
131 newMove.setPiece(previoslySelected.getPiece());
132 newMove.setTurn(previoslySelected.getPieceColor());
133 newMove.setTakes(data.getPiece() != null);
134 b.getMoves().add(newMove);
135 b.setPendingMove(newMove);
137 data.setPending(true);
138 data.setPieceColor(previoslySelected.getPieceColor());
139 data.setPiece(previoslySelected.getPiece());
141 previoslySelected.setPiece(null);
142 previoslySelected.setPieceColor(null);
143 Rules.computeAccessible(b, null);
145 Request sm = new Request();
146 sm.setMsg(MsgType.SendMove);
147 sm.setGameId(b.getGameId());
148 sm.setFrom(newMove.getFrom().getLocation());
149 sm.setTo(newMove.getTo().getLocation());
150 sm.setColor(newMove.getTurn());
151 final UI ui = UIModel.findUI(b);
159 static class NextMove extends TimerTask {
160 private static final Timer T = new Timer("Animate moves");
161 private final Board b;
162 private final Move m;
164 public NextMove(Board b, Move m) {
167 T.schedule(this, 1000);
177 @ModelOperation @Function static void showPosition(Board b, Move data) {
179 boolean found = false;
180 for (Move m : b.getMoves()) {
186 Square from = findSquare(b, (char)m.getFrom().getX(), m.getFrom().getY());
187 Square to = findSquare(b, (char)m.getTo().getX(), m.getTo().getY());
188 to.setPiece(from.getPiece());
189 to.setPieceColor(from.getPieceColor());
191 from.setPieceColor(null);
196 b.setTurn(b.getMoves().size() % 2 == 0 ? Color.W : Color.B);
201 @Function static void rotateBoard(Board b) {
202 Collections.reverse(b.getRows());
203 for (Row r : b.getRows()) {
204 Collections.reverse(r.getColumns());
206 Square sq = findSelectedSquare(b);
208 sq.setSelected(false);
209 Rules.computeAccessible(b, null);
213 @ComputedProperty static boolean whiteTurn(Color turn) {
214 return turn == Color.W;
217 @ComputedProperty static boolean blackTurn(Color turn) {
218 return turn == Color.B;
221 @ComputedProperty static List<String> columnNames(List<Row> rows) {
222 boolean whiteDown = rows.isEmpty() || rows.get(0).getY() == 8;
223 String[] arr = new String[8];
224 for (int i = 0; i < 8; i++) {
227 s = "" + (char)('A' + i);
229 s = "" + (char)('H' - i);
233 return Arrays.asList(arr);
236 static Square findSquare(Board b, char column, int row) {
237 for (Row r : b.getRows()) {
238 for (Square square : r.getColumns()) {
239 if (square.getPosition().getX() == column && square.getPosition().getY() == row) {
247 private static Square findSquare(Board b, Position to) {
248 return findSquare(b, (char)to.getX(), to.getY());
251 static Square findSelectedSquare(Board b) {
252 for (Row row : b.getRows()) {
253 for (Square square : row.getColumns()) {
254 if (square.isSelected()) {
262 static void moveResponse(final Board b, final String errMsg, final List<String> whites, final List<String> blacks, final Color turn,
265 if (errMsg != null) {
266 b.getMoves().remove(b.getPendingMove());
267 b.setPendingMove(null);
268 b.setAlertMessage(errMsg);
271 b.setAlertMessage(null);
274 Rules.initBoard(b, whites, blacks, turn);
277 static void moveUpdate(final Board b, final Move move, final List<String> whites, final List<String> blacks, final Color turn, Alert alert) {
278 final Square from = BoardModel.findSquare(b, move.getFrom());
279 final Square to = BoardModel.findSquare(b, move.getTo());
280 move.setPiece(from.getPiece());
281 move.setTurn(from.getPieceColor());
282 if (to.getPiece() != null) {
286 b.setAlertMessage(alert == null ? null : alert.getMessage());
287 move.setRound(b.getMoves().size() / 2 + 1);
288 b.getMoves().add(move);
289 Rules.initBoard(b, whites, blacks, turn);
292 @Model(className="Row", properties = {
293 @Property(name = "columns", type = Square.class, array = true)
295 static class RowsImpl {
296 @ComputedProperty static int y(List<Square> columns) {
297 return columns.isEmpty() ? 0 : columns.get(0).getY();
302 PAWN(5), ROCK(2), KNIGHT(4), BISHOP(3), QUEEN(1), KING(0);
304 final int entityIndex;
307 this.entityIndex = ei;
310 static PieceType fromNotation(char notation) {
312 case 'R': return ROCK;
313 case 'N': return KNIGHT;
314 case 'B': return BISHOP;
315 case 'Q': return QUEEN;
316 case 'K': return KING;
317 case 'P': return PAWN;
319 throw new IllegalStateException("Unexpected: " + notation);
322 String computeEntity(Color color) {
328 case W: base = 12; break;
329 case B: base = 18; break;
331 throw new AssertionError();
333 return "b" + String.valueOf(base + entityIndex) + ";";
337 @Model(className="Position", properties = {
338 @Property(name = "x", type = char.class),
339 @Property(name = "y", type = int.class),
341 static class PositionImpl {
342 @ComputedProperty static String location(int x, int y) {
343 return "" + (char)(x - 'A' + 'a') + y;
347 @Model(className="Square", properties = {
348 @Property(name = "position", type = Position.class),
349 @Property(name = "color", type = Color.class),
350 @Property(name = "piece", type = PieceType.class),
351 @Property(name = "pieceColor", type = Color.class),
352 @Property(name = "selected", type = boolean.class),
353 @Property(name = "accessible", type = boolean.class),
354 @Property(name = "pending", type = boolean.class),
356 static class SquareModel {
357 @ComputedProperty static String pieceEntity(
358 PieceType piece, Color pieceColor
363 return piece.computeEntity(pieceColor);
366 @ComputedProperty static String squareColor(
367 Color color, boolean selected, boolean accessible, boolean pending
382 if (color == Color.W) {
390 @ComputedProperty static char x(Position position) {
391 return position == null ? 'A' : position.getX();
394 @ComputedProperty static int y(Position position) {
395 return position == null ? 1 : position.getY();
399 @Model(className = "Move", properties = {
400 @Property(name = "round", type = int.class),
401 @Property(name = "turn", type = Color.class),
402 @Property(name = "piece", type = PieceType.class),
403 @Property(name = "from", type = Position.class),
404 @Property(name = "to", type = Position.class),
405 @Property(name = "promoted", type = PieceType.class),
406 @Property(name = "takes", type = boolean.class),
407 @Property(name = "check", type = boolean.class),
409 static class MoveImpl {
410 @ComputedProperty static boolean whiteMove(Color turn) {
411 return turn == Color.W;
414 @ComputedProperty static String html(
415 Position from, Position to, boolean takes, PieceType piece, Color turn
417 if (from == null || to == null) {
420 StringBuilder sb = new StringBuilder();
421 if (piece != null && piece != PieceType.PAWN) {
422 sb.append(piece.computeEntity(turn));
425 sb.append(from.getLocation());
429 sb.append(to.getLocation());
430 return sb.toString();
433 static Move valueOf(String move) {
434 move = move.toUpperCase();
437 Position p = new Position();
438 p.setX(move.charAt(0));
439 p.setY(move.charAt(1) - '0');
443 Position p = new Position();
444 p.setX(move.charAt(2));
445 p.setY(move.charAt(3) - '0');