1.1 --- a/chess/src/main/java/org/apidesign/html/demo/chess/BoardModel.java Tue Sep 24 22:20:24 2013 +0200
1.2 +++ b/chess/src/main/java/org/apidesign/html/demo/chess/BoardModel.java Tue Sep 24 22:37:17 2013 +0200
1.3 @@ -23,18 +23,86 @@
1.4 */
1.5 package org.apidesign.html.demo.chess;
1.6
1.7 -import java.util.Locale;
1.8 +import java.util.Arrays;
1.9 +import java.util.Collections;
1.10 +import java.util.List;
1.11 +import java.util.Timer;
1.12 +import java.util.TimerTask;
1.13 import net.java.html.json.ComputedProperty;
1.14 import net.java.html.json.Function;
1.15 import net.java.html.json.Model;
1.16 +import net.java.html.json.ModelOperation;
1.17 +import net.java.html.json.OnPropertyChange;
1.18 import net.java.html.json.Property;
1.19 +import net.java.html.sound.AudioClip;
1.20 +import org.apidesign.html.demo.chess.Communication.AlertType;
1.21
1.22 @Model(className="Board", properties={
1.23 + @Property(name = "player", type = String.class),
1.24 + @Property(name = "gameId", type = String.class),
1.25 + @Property(name = "whitePlayer", type = String.class),
1.26 + @Property(name = "blackPlayer", type = String.class),
1.27 + @Property(name = "alert", type = Alert.class),
1.28 + @Property(name = "alertMessage", type = String.class),
1.29 + @Property(name = "status", type = String.class),
1.30 @Property(name = "rows", type = Row.class, array = true),
1.31 - @Property(name = "turn", type = BoardModel.ColorType.class)
1.32 + @Property(name = "turn", type = Color.class),
1.33 + @Property(name = "moves", type = Move.class, array = true),
1.34 + @Property(name = "pendingMove", type = Move.class),
1.35 + @Property(name = "active", type = boolean.class),
1.36 })
1.37 public class BoardModel {
1.38 + @ComputedProperty static String title(String status, String gameId) {
1.39 + String t = status != null ? status : gameId;
1.40 + if (t != null && t.length() > 10) {
1.41 + return t.substring(0, 10);
1.42 + }
1.43 + return t;
1.44 + }
1.45 +
1.46 + @ModelOperation static void updateSummary(Board b, String summary) {
1.47 + if (summary != null) {
1.48 + b.setStatus(summary);
1.49 + }
1.50 + }
1.51 +
1.52 + @ComputedProperty static boolean myTurn(String player, String whitePlayer, String blackPlayer, Color turn) {
1.53 + if (turn != null && player != null) switch (turn) {
1.54 + case B: return player.equals(blackPlayer);
1.55 + case W: return player.equals(whitePlayer);
1.56 + }
1.57 + return false;
1.58 + }
1.59 +
1.60 + @ComputedProperty static boolean justObserving(String player) {
1.61 + return player == null;
1.62 + }
1.63 +
1.64 + private static final AudioClip MOVE = AudioClip.create("sounds/move.mp3");
1.65 + @OnPropertyChange("moves") static void playMove() {
1.66 + MOVE.play();
1.67 + }
1.68 +
1.69 + private static final AudioClip CHECK = AudioClip.create("sounds/check.mp3");
1.70 + private static final AudioClip CHECKMATE = AudioClip.create("sounds/checkmate.mp3");
1.71 + @OnPropertyChange("alert") static void warnCheckAndMate(Board b) {
1.72 + if (b.getAlert() == null) {
1.73 + return;
1.74 + }
1.75 + if (b.getAlert().getType() == AlertType.CHECK) {
1.76 + CHECK.play();
1.77 + }
1.78 + if (b.getAlert().getType() == AlertType.CHECKMATE) {
1.79 + CHECKMATE.play();
1.80 + }
1.81 + }
1.82 +
1.83 @Function static void selected(Board b, Square data) {
1.84 + if (!b.isMyTurn()) {
1.85 + b.setAlertMessage("Not your turn!");
1.86 + return;
1.87 + }
1.88 +
1.89 Square previoslySelected = findSelectedSquare(b);
1.90 if (previoslySelected == null) {
1.91 if (data.getPiece() != null && data.getPieceColor() == b.getTurn()) {
1.92 @@ -42,6 +110,11 @@
1.93 Rules.computeAccessible(b, data);
1.94 }
1.95 } else {
1.96 + if (previoslySelected == data) {
1.97 + data.setSelected(false);
1.98 + Rules.computeAccessible(b, null);
1.99 + return;
1.100 + }
1.101 if (data.getPiece() != null && data.getPieceColor() == previoslySelected.getPieceColor()) {
1.102 previoslySelected.setSelected(false);
1.103 data.setSelected(true);
1.104 @@ -50,34 +123,130 @@
1.105 }
1.106 if (data.isAccessible()) {
1.107 previoslySelected.setSelected(false);
1.108 +
1.109 + Move newMove = new Move();
1.110 + newMove.setFrom(previoslySelected.getPosition());
1.111 + newMove.setTo(data.getPosition());
1.112 + newMove.setRound(b.getMoves().size() / 2 + 1);
1.113 + newMove.setPiece(previoslySelected.getPiece());
1.114 + newMove.setTurn(previoslySelected.getPieceColor());
1.115 + newMove.setTakes(data.getPiece() != null);
1.116 + b.getMoves().add(newMove);
1.117 + b.setPendingMove(newMove);
1.118 +
1.119 + data.setPending(true);
1.120 data.setPieceColor(previoslySelected.getPieceColor());
1.121 data.setPiece(previoslySelected.getPiece());
1.122 - b.setTurn(b.getTurn() == ColorType.WHITE ? ColorType.BLACK : ColorType.WHITE);
1.123 + b.setTurn(null);
1.124 previoslySelected.setPiece(null);
1.125 previoslySelected.setPieceColor(null);
1.126 Rules.computeAccessible(b, null);
1.127 +
1.128 + Request sm = new Request();
1.129 + sm.setMsg(MsgType.SendMove);
1.130 + sm.setGameId(b.getGameId());
1.131 + sm.setFrom(newMove.getFrom().getLocation());
1.132 + sm.setTo(newMove.getTo().getLocation());
1.133 + sm.setColor(newMove.getTurn());
1.134 + final UI ui = UIModel.findUI(b);
1.135 + if (ui != null) {
1.136 + ui.sendMsg(sm);
1.137 + }
1.138 }
1.139 }
1.140 }
1.141
1.142 - @ComputedProperty static boolean whiteTurn(ColorType turn) {
1.143 - return turn == ColorType.WHITE;
1.144 + static class NextMove extends TimerTask {
1.145 + private static final Timer T = new Timer("Animate moves");
1.146 + private final Board b;
1.147 + private final Move m;
1.148 +
1.149 + public NextMove(Board b, Move m) {
1.150 + this.b = b;
1.151 + this.m = m;
1.152 + T.schedule(this, 1000);
1.153 + }
1.154 +
1.155 +
1.156 + @Override
1.157 + public void run() {
1.158 + b.showPosition(m);
1.159 + }
1.160 + }
1.161 +
1.162 + @ModelOperation @Function static void showPosition(Board b, Move data) {
1.163 + Rules.initBoard(b);
1.164 + boolean found = false;
1.165 + for (Move m : b.getMoves()) {
1.166 + if (found) {
1.167 + b.setTurn(null);
1.168 + new NextMove(b, m);
1.169 + return;
1.170 + }
1.171 + Square from = findSquare(b, (char)m.getFrom().getX(), m.getFrom().getY());
1.172 + Square to = findSquare(b, (char)m.getTo().getX(), m.getTo().getY());
1.173 + to.setPiece(from.getPiece());
1.174 + to.setPieceColor(from.getPieceColor());
1.175 + from.setPiece(null);
1.176 + from.setPieceColor(null);
1.177 + if (m == data) {
1.178 + found = true;
1.179 + }
1.180 + }
1.181 + b.setTurn(b.getMoves().size() % 2 == 0 ? Color.W : Color.B);
1.182 + }
1.183 +
1.184 +
1.185 +
1.186 + @Function static void rotateBoard(Board b) {
1.187 + Collections.reverse(b.getRows());
1.188 + for (Row r : b.getRows()) {
1.189 + Collections.reverse(r.getColumns());
1.190 + }
1.191 + Square sq = findSelectedSquare(b);
1.192 + if (sq != null) {
1.193 + sq.setSelected(false);
1.194 + Rules.computeAccessible(b, null);
1.195 + }
1.196 + }
1.197 +
1.198 + @ComputedProperty static boolean whiteTurn(Color turn) {
1.199 + return turn == Color.W;
1.200 }
1.201
1.202 - @ComputedProperty static boolean blackTurn(ColorType turn) {
1.203 - return turn == ColorType.BLACK;
1.204 + @ComputedProperty static boolean blackTurn(Color turn) {
1.205 + return turn == Color.B;
1.206 + }
1.207 +
1.208 + @ComputedProperty static List<String> columnNames(List<Row> rows) {
1.209 + boolean whiteDown = rows.isEmpty() || rows.get(0).getY() == 8;
1.210 + String[] arr = new String[8];
1.211 + for (int i = 0; i < 8; i++) {
1.212 + String s;
1.213 + if (whiteDown) {
1.214 + s = "" + (char)('A' + i);
1.215 + } else {
1.216 + s = "" + (char)('H' - i);
1.217 + }
1.218 + arr[i] = s;
1.219 + }
1.220 + return Arrays.asList(arr);
1.221 }
1.222
1.223 static Square findSquare(Board b, char column, int row) {
1.224 for (Row r : b.getRows()) {
1.225 for (Square square : r.getColumns()) {
1.226 - if (square.getX() == column && square.getY() == row) {
1.227 + if (square.getPosition().getX() == column && square.getPosition().getY() == row) {
1.228 return square;
1.229 }
1.230 }
1.231 }
1.232 return null;
1.233 }
1.234 +
1.235 + private static Square findSquare(Board b, Position to) {
1.236 + return findSquare(b, (char)to.getX(), to.getY());
1.237 + }
1.238
1.239 static Square findSelectedSquare(Board b) {
1.240 for (Row row : b.getRows()) {
1.241 @@ -89,52 +258,104 @@
1.242 }
1.243 return null;
1.244 }
1.245 -
1.246 +
1.247 + static void moveResponse(final Board b, final String errMsg, final List<String> whites, final List<String> blacks, final Color turn,
1.248 + Alert alert
1.249 + ) {
1.250 + if (errMsg != null) {
1.251 + b.getMoves().remove(b.getPendingMove());
1.252 + b.setPendingMove(null);
1.253 + b.setAlertMessage(errMsg);
1.254 + } else {
1.255 + b.setTurn(turn);
1.256 + b.setAlertMessage(null);
1.257 + }
1.258 + b.setAlert(alert);
1.259 + Rules.initBoard(b, whites, blacks, turn);
1.260 + }
1.261 +
1.262 + static void moveUpdate(final Board b, final Move move, final List<String> whites, final List<String> blacks, final Color turn, Alert alert) {
1.263 + final Square from = BoardModel.findSquare(b, move.getFrom());
1.264 + final Square to = BoardModel.findSquare(b, move.getTo());
1.265 + move.setPiece(from.getPiece());
1.266 + move.setTurn(from.getPieceColor());
1.267 + if (to.getPiece() != null) {
1.268 + move.setTakes(true);
1.269 + }
1.270 + b.setAlert(alert);
1.271 + b.setAlertMessage(alert == null ? null : alert.getMessage());
1.272 + move.setRound(b.getMoves().size() / 2 + 1);
1.273 + b.getMoves().add(move);
1.274 + Rules.initBoard(b, whites, blacks, turn);
1.275 + }
1.276 +
1.277 @Model(className="Row", properties = {
1.278 @Property(name = "columns", type = Square.class, array = true)
1.279 })
1.280 static class RowsImpl {
1.281 + @ComputedProperty static int y(List<Square> columns) {
1.282 + return columns.isEmpty() ? 0 : columns.get(0).getY();
1.283 + }
1.284 }
1.285
1.286 enum PieceType {
1.287 PAWN(5), ROCK(2), KNIGHT(4), BISHOP(3), QUEEN(1), KING(0);
1.288 -
1.289 +
1.290 final int entityIndex;
1.291
1.292 PieceType(int ei) {
1.293 this.entityIndex = ei;
1.294 }
1.295 +
1.296 + static PieceType fromNotation(char notation) {
1.297 + switch (notation) {
1.298 + case 'R': return ROCK;
1.299 + case 'N': return KNIGHT;
1.300 + case 'B': return BISHOP;
1.301 + case 'Q': return QUEEN;
1.302 + case 'K': return KING;
1.303 + case 'P': return PAWN;
1.304 + }
1.305 + throw new IllegalStateException("Unexpected: " + notation);
1.306 + }
1.307
1.308 - String computeEntity(ColorType color) {
1.309 + String computeEntity(Color color) {
1.310 if (color == null) {
1.311 - color = ColorType.WHITE;
1.312 + color = Color.W;
1.313 }
1.314 int base;
1.315 switch (color) {
1.316 - case WHITE: base = 12; break;
1.317 - case BLACK: base = 18; break;
1.318 + case W: base = 12; break;
1.319 + case B: base = 18; break;
1.320 default:
1.321 throw new AssertionError();
1.322 }
1.323 return "b" + String.valueOf(base + entityIndex) + ";";
1.324 }
1.325 }
1.326 - enum ColorType {
1.327 - WHITE, BLACK;
1.328 +
1.329 + @Model(className="Position", properties = {
1.330 + @Property(name = "x", type = char.class),
1.331 + @Property(name = "y", type = int.class),
1.332 + })
1.333 + static class PositionImpl {
1.334 + @ComputedProperty static String location(int x, int y) {
1.335 + return "" + (char)(x - 'A' + 'a') + y;
1.336 + }
1.337 }
1.338
1.339 @Model(className="Square", properties = {
1.340 + @Property(name = "position", type = Position.class),
1.341 + @Property(name = "color", type = Color.class),
1.342 @Property(name = "piece", type = PieceType.class),
1.343 - @Property(name = "pieceColor", type = ColorType.class),
1.344 - @Property(name = "color", type = ColorType.class),
1.345 - @Property(name = "x", type = int.class),
1.346 - @Property(name = "y", type = int.class),
1.347 + @Property(name = "pieceColor", type = Color.class),
1.348 @Property(name = "selected", type = boolean.class),
1.349 @Property(name = "accessible", type = boolean.class),
1.350 + @Property(name = "pending", type = boolean.class),
1.351 })
1.352 - static class PieceImpl {
1.353 + static class SquareModel {
1.354 @ComputedProperty static String pieceEntity(
1.355 - PieceType piece, ColorType pieceColor
1.356 + PieceType piece, Color pieceColor
1.357 ) {
1.358 if (piece == null) {
1.359 return "";
1.360 @@ -143,7 +364,7 @@
1.361 }
1.362
1.363 @ComputedProperty static String squareColor(
1.364 - ColorType color, boolean selected, boolean accessible
1.365 + Color color, boolean selected, boolean accessible, boolean pending
1.366 ) {
1.367 if (selected) {
1.368 return "selected";
1.369 @@ -151,52 +372,80 @@
1.370 if (accessible) {
1.371 return "accessible";
1.372 }
1.373 + if (pending) {
1.374 + return "pending";
1.375 + }
1.376
1.377 if (color == null) {
1.378 return "";
1.379 } else {
1.380 - return color.toString().toLowerCase(Locale.US);
1.381 + if (color == Color.W) {
1.382 + return "white";
1.383 + } else {
1.384 + return "black";
1.385 + }
1.386 }
1.387 }
1.388 +
1.389 + @ComputedProperty static char x(Position position) {
1.390 + return position == null ? 'A' : position.getX();
1.391 + }
1.392 +
1.393 + @ComputedProperty static int y(Position position) {
1.394 + return position == null ? 1 : position.getY();
1.395 + }
1.396 }
1.397
1.398 - public static void initialize(String[] args) {
1.399 - Board b = createBoard();
1.400 - b.applyBindings();
1.401 - }
1.402 + @Model(className = "Move", properties = {
1.403 + @Property(name = "round", type = int.class),
1.404 + @Property(name = "turn", type = Color.class),
1.405 + @Property(name = "piece", type = PieceType.class),
1.406 + @Property(name = "from", type = Position.class),
1.407 + @Property(name = "to", type = Position.class),
1.408 + @Property(name = "promoted", type = PieceType.class),
1.409 + @Property(name = "takes", type = boolean.class),
1.410 + @Property(name = "check", type = boolean.class),
1.411 + })
1.412 + static class MoveImpl {
1.413 + @ComputedProperty static boolean whiteMove(Color turn) {
1.414 + return turn == Color.W;
1.415 + }
1.416 +
1.417 + @ComputedProperty static String html(
1.418 + Position from, Position to, boolean takes, PieceType piece, Color turn
1.419 + ) {
1.420 + if (from == null || to == null) {
1.421 + return "";
1.422 + }
1.423 + StringBuilder sb = new StringBuilder();
1.424 + if (piece != null && piece != PieceType.PAWN) {
1.425 + sb.append(piece.computeEntity(turn));
1.426 + }
1.427 +
1.428 + sb.append(from.getLocation());
1.429 + if (takes) {
1.430 + sb.append("x");
1.431 + }
1.432 + sb.append(to.getLocation());
1.433 + return sb.toString();
1.434 + }
1.435
1.436 - static Board createBoard() {
1.437 - Board b = new Board();
1.438 - b.setTurn(ColorType.WHITE);
1.439 - for (int i = 8; i > 0; i--) {
1.440 - Row r = new Row();
1.441 - b.getRows().add(r);
1.442 - for (int j = 'A'; j <= 'H'; j++) {
1.443 - Square s = new Square();
1.444 - s.setX(j);
1.445 - s.setY(i);
1.446 - s.setColor((i + j) % 2 == 1 ? ColorType.WHITE : ColorType.BLACK);
1.447 - r.getColumns().add(s);
1.448 - if (i == 2) {
1.449 - s.setPiece(PieceType.PAWN);
1.450 - s.setPieceColor(ColorType.WHITE);
1.451 - } else if (i == 7) {
1.452 - s.setPiece(PieceType.PAWN);
1.453 - s.setPieceColor(ColorType.BLACK);
1.454 - } else if (i == 8 || i == 1) {
1.455 - s.setPieceColor(i == 1 ? ColorType.WHITE : ColorType.BLACK);
1.456 - PieceType t;
1.457 - switch (j) {
1.458 - case 'A': case 'H': t = PieceType.ROCK; break;
1.459 - case 'B': case 'G': t = PieceType.KNIGHT; break;
1.460 - case 'C': case 'F': t = PieceType.BISHOP; break;
1.461 - case 'D': t = PieceType.QUEEN; break;
1.462 - default: t = PieceType.KING; break;
1.463 - }
1.464 - s.setPiece(t);
1.465 - }
1.466 + static Move valueOf(String move) {
1.467 + move = move.toUpperCase();
1.468 + Move m = new Move();
1.469 + {
1.470 + Position p = new Position();
1.471 + p.setX(move.charAt(0));
1.472 + p.setY(move.charAt(1) - '0');
1.473 + m.setFrom(p);
1.474 }
1.475 + {
1.476 + Position p = new Position();
1.477 + p.setX(move.charAt(2));
1.478 + p.setY(move.charAt(3) - '0');
1.479 + m.setTo(p);
1.480 + }
1.481 + return m;
1.482 }
1.483 - return b;
1.484 }
1.485 }