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 org.apidesign.html.demo.chess;
26 import net.java.html.json.ComputedProperty;
27 import net.java.html.json.Function;
28 import net.java.html.json.Model;
29 import net.java.html.json.Property;
31 @Model(className="Board", properties={
32 @Property(name = "rows", type = Row.class, array = true)
34 public class BoardModel {
35 @Model(className="Row", properties = {
36 @Property(name = "columns", type = Square.class, array = true)
38 static class RowsImpl {
42 PAWN, ROCK, KNIGHT, BISHOP, QUEEN, KING;
48 @Model(className="Square", properties = {
49 @Property(name = "piece", type = PieceType.class),
50 @Property(name = "pieceColor", type = ColorType.class),
51 @Property(name = "color", type = ColorType.class),
52 @Property(name = "x", type = int.class),
53 @Property(name = "y", type = int.class),
55 static class PieceImpl {
56 @Function static void changeToPawn(Square s) {
57 s.setPiece(PieceType.PAWN);
60 @ComputedProperty static String imageURL(PieceType piece) {
64 if (piece == PieceType.BISHOP) {
71 public static void initialize(String[] args) {
72 Board b = createBoard();
76 private static Board createBoard() {
77 Board b = new Board();
78 for (int i = 8; i > 0; i--) {
81 for (int j = 'A'; j <= 'H'; j++) {
82 Square s = new Square();
85 s.setColor((i + j) % 2 == 0 ? ColorType.WHITE : ColorType.BLACK);
86 r.getColumns().add(s);
88 s.setPiece(PieceType.PAWN);
89 s.setPieceColor(ColorType.WHITE);
91 s.setPiece(PieceType.PAWN);
92 s.setPieceColor(ColorType.BLACK);
93 } else if (i == 8 || i == 1) {
94 s.setColor(i == 1 ? ColorType.WHITE : ColorType.BLACK);
97 case 'A': case 'H': t = PieceType.ROCK; break;
98 case 'B': case 'G': t = PieceType.KNIGHT; break;
99 case 'C': case 'F': t = PieceType.BISHOP; break;
100 case 'D': t = PieceType.QUEEN; break;
101 default: t = PieceType.KING; break;