1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/chess/src/main/java/org/apidesign/html/demo/chess/BoardModel.java Thu Jul 25 15:09:49 2013 +0200
1.3 @@ -0,0 +1,109 @@
1.4 +/**
1.5 + * The MIT License (MIT)
1.6 + *
1.7 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.8 + *
1.9 + * Permission is hereby granted, free of charge, to any person obtaining a copy
1.10 + * of this software and associated documentation files (the "Software"), to deal
1.11 + * in the Software without restriction, including without limitation the rights
1.12 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1.13 + * copies of the Software, and to permit persons to whom the Software is
1.14 + * furnished to do so, subject to the following conditions:
1.15 + *
1.16 + * The above copyright notice and this permission notice shall be included in
1.17 + * all copies or substantial portions of the Software.
1.18 + *
1.19 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1.20 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1.21 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1.22 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1.23 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1.24 + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1.25 + * THE SOFTWARE.
1.26 + */
1.27 +package org.apidesign.html.demo.chess;
1.28 +
1.29 +import net.java.html.json.ComputedProperty;
1.30 +import net.java.html.json.Function;
1.31 +import net.java.html.json.Model;
1.32 +import net.java.html.json.Property;
1.33 +
1.34 +@Model(className="Board", properties={
1.35 + @Property(name = "rows", type = Row.class, array = true)
1.36 +})
1.37 +public class BoardModel {
1.38 + @Model(className="Row", properties = {
1.39 + @Property(name = "columns", type = Square.class, array = true)
1.40 + })
1.41 + static class RowsImpl {
1.42 + }
1.43 +
1.44 + enum PieceType {
1.45 + PAWN, ROCK, KNIGHT, BISHOP, QUEEN, KING;
1.46 + }
1.47 + enum ColorType {
1.48 + WHITE, BLACK;
1.49 + }
1.50 +
1.51 + @Model(className="Square", properties = {
1.52 + @Property(name = "piece", type = PieceType.class),
1.53 + @Property(name = "pieceColor", type = ColorType.class),
1.54 + @Property(name = "color", type = ColorType.class),
1.55 + @Property(name = "x", type = int.class),
1.56 + @Property(name = "y", type = int.class),
1.57 + })
1.58 + static class PieceImpl {
1.59 + @Function static void changeToPawn(Square s) {
1.60 + s.setPiece(PieceType.PAWN);
1.61 + }
1.62 +
1.63 + @ComputedProperty static String imageURL(PieceType piece) {
1.64 + if (piece == null) {
1.65 + return "";
1.66 + }
1.67 + if (piece == PieceType.BISHOP) {
1.68 + return "bishop.png";
1.69 + }
1.70 + return "knight.jpg";
1.71 + }
1.72 + }
1.73 +
1.74 + public static void initialize(String[] args) {
1.75 + Board b = createBoard();
1.76 + b.applyBindings();
1.77 + }
1.78 +
1.79 + private static Board createBoard() {
1.80 + Board b = new Board();
1.81 + for (int i = 8; i > 0; i--) {
1.82 + Row r = new Row();
1.83 + b.getRows().add(r);
1.84 + for (int j = 'A'; j <= 'H'; j++) {
1.85 + Square s = new Square();
1.86 + s.setX(j);
1.87 + s.setY(i);
1.88 + s.setColor((i + j) % 2 == 0 ? ColorType.WHITE : ColorType.BLACK);
1.89 + r.getColumns().add(s);
1.90 + if (i == 2) {
1.91 + s.setPiece(PieceType.PAWN);
1.92 + s.setPieceColor(ColorType.WHITE);
1.93 + } else if (i == 7) {
1.94 + s.setPiece(PieceType.PAWN);
1.95 + s.setPieceColor(ColorType.BLACK);
1.96 + } else if (i == 8 || i == 1) {
1.97 + s.setColor(i == 1 ? ColorType.WHITE : ColorType.BLACK);
1.98 + PieceType t;
1.99 + switch (j) {
1.100 + case 'A': case 'H': t = PieceType.ROCK; break;
1.101 + case 'B': case 'G': t = PieceType.KNIGHT; break;
1.102 + case 'C': case 'F': t = PieceType.BISHOP; break;
1.103 + case 'D': t = PieceType.QUEEN; break;
1.104 + default: t = PieceType.KING; break;
1.105 + }
1.106 + s.setPiece(t);
1.107 + }
1.108 + }
1.109 + }
1.110 + return b;
1.111 + }
1.112 +}