1.1 --- a/chess/src/main/java/org/apidesign/html/demo/chess/BoardModel.java Tue Sep 24 22:20:24 2013 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,202 +0,0 @@
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 java.util.Locale;
1.30 -import net.java.html.json.ComputedProperty;
1.31 -import net.java.html.json.Function;
1.32 -import net.java.html.json.Model;
1.33 -import net.java.html.json.Property;
1.34 -
1.35 -@Model(className="Board", properties={
1.36 - @Property(name = "rows", type = Row.class, array = true),
1.37 - @Property(name = "turn", type = BoardModel.ColorType.class)
1.38 -})
1.39 -public class BoardModel {
1.40 - @Function static void selected(Board b, Square data) {
1.41 - Square previoslySelected = findSelectedSquare(b);
1.42 - if (previoslySelected == null) {
1.43 - if (data.getPiece() != null && data.getPieceColor() == b.getTurn()) {
1.44 - data.setSelected(true);
1.45 - Rules.computeAccessible(b, data);
1.46 - }
1.47 - } else {
1.48 - if (data.getPiece() != null && data.getPieceColor() == previoslySelected.getPieceColor()) {
1.49 - previoslySelected.setSelected(false);
1.50 - data.setSelected(true);
1.51 - Rules.computeAccessible(b, data);
1.52 - return;
1.53 - }
1.54 - if (data.isAccessible()) {
1.55 - previoslySelected.setSelected(false);
1.56 - data.setPieceColor(previoslySelected.getPieceColor());
1.57 - data.setPiece(previoslySelected.getPiece());
1.58 - b.setTurn(b.getTurn() == ColorType.WHITE ? ColorType.BLACK : ColorType.WHITE);
1.59 - previoslySelected.setPiece(null);
1.60 - previoslySelected.setPieceColor(null);
1.61 - Rules.computeAccessible(b, null);
1.62 - }
1.63 - }
1.64 - }
1.65 -
1.66 - @ComputedProperty static boolean whiteTurn(ColorType turn) {
1.67 - return turn == ColorType.WHITE;
1.68 - }
1.69 -
1.70 - @ComputedProperty static boolean blackTurn(ColorType turn) {
1.71 - return turn == ColorType.BLACK;
1.72 - }
1.73 -
1.74 - static Square findSquare(Board b, char column, int row) {
1.75 - for (Row r : b.getRows()) {
1.76 - for (Square square : r.getColumns()) {
1.77 - if (square.getX() == column && square.getY() == row) {
1.78 - return square;
1.79 - }
1.80 - }
1.81 - }
1.82 - return null;
1.83 - }
1.84 -
1.85 - static Square findSelectedSquare(Board b) {
1.86 - for (Row row : b.getRows()) {
1.87 - for (Square square : row.getColumns()) {
1.88 - if (square.isSelected()) {
1.89 - return square;
1.90 - }
1.91 - }
1.92 - }
1.93 - return null;
1.94 - }
1.95 -
1.96 - @Model(className="Row", properties = {
1.97 - @Property(name = "columns", type = Square.class, array = true)
1.98 - })
1.99 - static class RowsImpl {
1.100 - }
1.101 -
1.102 - enum PieceType {
1.103 - PAWN(5), ROCK(2), KNIGHT(4), BISHOP(3), QUEEN(1), KING(0);
1.104 -
1.105 - final int entityIndex;
1.106 -
1.107 - PieceType(int ei) {
1.108 - this.entityIndex = ei;
1.109 - }
1.110 -
1.111 - String computeEntity(ColorType color) {
1.112 - if (color == null) {
1.113 - color = ColorType.WHITE;
1.114 - }
1.115 - int base;
1.116 - switch (color) {
1.117 - case WHITE: base = 12; break;
1.118 - case BLACK: base = 18; break;
1.119 - default:
1.120 - throw new AssertionError();
1.121 - }
1.122 - return "b" + String.valueOf(base + entityIndex) + ";";
1.123 - }
1.124 - }
1.125 - enum ColorType {
1.126 - WHITE, BLACK;
1.127 - }
1.128 -
1.129 - @Model(className="Square", properties = {
1.130 - @Property(name = "piece", type = PieceType.class),
1.131 - @Property(name = "pieceColor", type = ColorType.class),
1.132 - @Property(name = "color", type = ColorType.class),
1.133 - @Property(name = "x", type = int.class),
1.134 - @Property(name = "y", type = int.class),
1.135 - @Property(name = "selected", type = boolean.class),
1.136 - @Property(name = "accessible", type = boolean.class),
1.137 - })
1.138 - static class PieceImpl {
1.139 - @ComputedProperty static String pieceEntity(
1.140 - PieceType piece, ColorType pieceColor
1.141 - ) {
1.142 - if (piece == null) {
1.143 - return "";
1.144 - }
1.145 - return piece.computeEntity(pieceColor);
1.146 - }
1.147 -
1.148 - @ComputedProperty static String squareColor(
1.149 - ColorType color, boolean selected, boolean accessible
1.150 - ) {
1.151 - if (selected) {
1.152 - return "selected";
1.153 - }
1.154 - if (accessible) {
1.155 - return "accessible";
1.156 - }
1.157 -
1.158 - if (color == null) {
1.159 - return "";
1.160 - } else {
1.161 - return color.toString().toLowerCase(Locale.US);
1.162 - }
1.163 - }
1.164 - }
1.165 -
1.166 - public static void initialize(String[] args) {
1.167 - Board b = createBoard();
1.168 - b.applyBindings();
1.169 - }
1.170 -
1.171 - static Board createBoard() {
1.172 - Board b = new Board();
1.173 - b.setTurn(ColorType.WHITE);
1.174 - for (int i = 8; i > 0; i--) {
1.175 - Row r = new Row();
1.176 - b.getRows().add(r);
1.177 - for (int j = 'A'; j <= 'H'; j++) {
1.178 - Square s = new Square();
1.179 - s.setX(j);
1.180 - s.setY(i);
1.181 - s.setColor((i + j) % 2 == 1 ? ColorType.WHITE : ColorType.BLACK);
1.182 - r.getColumns().add(s);
1.183 - if (i == 2) {
1.184 - s.setPiece(PieceType.PAWN);
1.185 - s.setPieceColor(ColorType.WHITE);
1.186 - } else if (i == 7) {
1.187 - s.setPiece(PieceType.PAWN);
1.188 - s.setPieceColor(ColorType.BLACK);
1.189 - } else if (i == 8 || i == 1) {
1.190 - s.setPieceColor(i == 1 ? ColorType.WHITE : ColorType.BLACK);
1.191 - PieceType t;
1.192 - switch (j) {
1.193 - case 'A': case 'H': t = PieceType.ROCK; break;
1.194 - case 'B': case 'G': t = PieceType.KNIGHT; break;
1.195 - case 'C': case 'F': t = PieceType.BISHOP; break;
1.196 - case 'D': t = PieceType.QUEEN; break;
1.197 - default: t = PieceType.KING; break;
1.198 - }
1.199 - s.setPiece(t);
1.200 - }
1.201 - }
1.202 - }
1.203 - return b;
1.204 - }
1.205 -}