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 -}
2.1 --- a/chess/src/main/java/org/apidesign/html/demo/chess/Main.java Tue Sep 24 22:20:24 2013 +0200
2.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
2.3 @@ -1,40 +0,0 @@
2.4 -/**
2.5 - * The MIT License (MIT)
2.6 - *
2.7 - * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
2.8 - *
2.9 - * Permission is hereby granted, free of charge, to any person obtaining a copy
2.10 - * of this software and associated documentation files (the "Software"), to deal
2.11 - * in the Software without restriction, including without limitation the rights
2.12 - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
2.13 - * copies of the Software, and to permit persons to whom the Software is
2.14 - * furnished to do so, subject to the following conditions:
2.15 - *
2.16 - * The above copyright notice and this permission notice shall be included in
2.17 - * all copies or substantial portions of the Software.
2.18 - *
2.19 - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2.20 - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2.21 - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2.22 - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2.23 - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2.24 - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2.25 - * THE SOFTWARE.
2.26 - */
2.27 -package org.apidesign.html.demo.chess;
2.28 -
2.29 -import net.java.html.boot.BrowserBuilder;
2.30 -
2.31 -public final class Main {
2.32 - private Main() {
2.33 - }
2.34 -
2.35 - public static void main(String... args) throws Exception {
2.36 - BrowserBuilder.newBrowser().
2.37 - loadPage("pages/index.html").
2.38 - loadClass(BoardModel.class).
2.39 - invoke("initialize", args).
2.40 - showAndWait();
2.41 - System.exit(0);
2.42 - }
2.43 -}
3.1 --- a/chess/src/main/java/org/apidesign/html/demo/chess/Rules.java Tue Sep 24 22:20:24 2013 +0200
3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
3.3 @@ -1,139 +0,0 @@
3.4 -/**
3.5 - * The MIT License (MIT)
3.6 - *
3.7 - * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
3.8 - *
3.9 - * Permission is hereby granted, free of charge, to any person obtaining a copy
3.10 - * of this software and associated documentation files (the "Software"), to deal
3.11 - * in the Software without restriction, including without limitation the rights
3.12 - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
3.13 - * copies of the Software, and to permit persons to whom the Software is
3.14 - * furnished to do so, subject to the following conditions:
3.15 - *
3.16 - * The above copyright notice and this permission notice shall be included in
3.17 - * all copies or substantial portions of the Software.
3.18 - *
3.19 - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
3.20 - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
3.21 - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
3.22 - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
3.23 - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3.24 - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
3.25 - * THE SOFTWARE.
3.26 - */
3.27 -package org.apidesign.html.demo.chess;
3.28 -
3.29 -/** Common chess rules.
3.30 - *
3.31 - * @author Jaroslav Tulach <jtulach@netbeans.org>
3.32 - */
3.33 -class Rules {
3.34 - static void computeAccessible(Board b, Square s) {
3.35 - for (Row r : b.getRows()) {
3.36 - for (Square ts : r.getColumns()) {
3.37 - ts.setAccessible(false);
3.38 - }
3.39 - }
3.40 - if (s == null) {
3.41 - return;
3.42 - }
3.43 -
3.44 - switch (s.getPiece()) {
3.45 - case BISHOP:
3.46 - moveBishop(b, s);
3.47 - break;
3.48 - case KING:
3.49 - computeAccessible(b, s, 1, 1, 1);
3.50 - computeAccessible(b, s, 1, -1, 1);
3.51 - computeAccessible(b, s, -1, -1, 1);
3.52 - computeAccessible(b, s, -1, 1, 1);
3.53 - computeAccessible(b, s, 1, 0, 1);
3.54 - computeAccessible(b, s, 0, -1, 1);
3.55 - computeAccessible(b, s, 0, 1, 1);
3.56 - computeAccessible(b, s, -1, 0, 1);
3.57 - break;
3.58 - case ROCK:
3.59 - moveRock(b, s);
3.60 - break;
3.61 - case QUEEN:
3.62 - moveRock(b, s);
3.63 - moveBishop(b, s);
3.64 - break;
3.65 - case KNIGHT:
3.66 - computeAccessible(b, s, 2, 1, 1);
3.67 - computeAccessible(b, s, 2, -1, 1);
3.68 - computeAccessible(b, s, -2, -1, 1);
3.69 - computeAccessible(b, s, -2, 1, 1);
3.70 - computeAccessible(b, s, 1, 2, 1);
3.71 - computeAccessible(b, s, -1, 2, 1);
3.72 - computeAccessible(b, s, -1, -2, 1);
3.73 - computeAccessible(b, s, 1, -2, 1);
3.74 - break;
3.75 - case PAWN:
3.76 - pawns(b, s);
3.77 - break;
3.78 - }
3.79 - }
3.80 -
3.81 - private static void moveRock(Board b, Square s) {
3.82 - computeAccessible(b, s, 1, 0, 8);
3.83 - computeAccessible(b, s, 0, -1, 8);
3.84 - computeAccessible(b, s, -1, 0, 8);
3.85 - computeAccessible(b, s, 0, 1, 8);
3.86 - }
3.87 -
3.88 - private static void moveBishop(Board b, Square s) {
3.89 - computeAccessible(b, s, 1, 1, 8);
3.90 - computeAccessible(b, s, 1, -1, 8);
3.91 - computeAccessible(b, s, -1, -1, 8);
3.92 - computeAccessible(b, s, -1, 1, 8);
3.93 - }
3.94 -
3.95 - private static void computeAccessible(
3.96 - Board b, Square s, int dx, int dy,
3.97 - int limit
3.98 - ) {
3.99 - int x = s.getX();
3.100 - int y = s.getY();
3.101 -
3.102 - while (limit-- > 0) {
3.103 - x += dx;
3.104 - y += dy;
3.105 - Square next = BoardModel.findSquare(b, (char)x, y);
3.106 - if (next == null) {
3.107 - break;
3.108 - }
3.109 - if (next.getPieceColor() == s.getPieceColor()) {
3.110 - break;
3.111 - }
3.112 - next.setAccessible(true);
3.113 - if (next.getPieceColor() != null) {
3.114 - break;
3.115 - }
3.116 - }
3.117 - }
3.118 -
3.119 - private static void pawns(Board b, Square s) {
3.120 - final boolean white = s.getPieceColor() == BoardModel.ColorType.WHITE;
3.121 - int dy = white ? 1 : -1;
3.122 - Square step = BoardModel.findSquare(b, (char)s.getX(), s.getY() + dy);
3.123 - if (step != null && step.getPiece() == null) {
3.124 - step.setAccessible(true);
3.125 - if ((s.getY() == 2 && white) || (s.getY() == 7 && !white)) {
3.126 - Square nextSTep = BoardModel.findSquare(b, (char)s.getX(), step.getY() + dy);
3.127 - if (nextSTep != null && step.getPiece() == null) {
3.128 - nextSTep.setAccessible(true);
3.129 - }
3.130 - }
3.131 - }
3.132 - BoardModel.ColorType opposite = white ? BoardModel.ColorType.BLACK : BoardModel.ColorType.WHITE;
3.133 - Square takeLeft = BoardModel.findSquare(b, (char)(s.getX() - 1), s.getY() + dy);
3.134 - if (takeLeft != null && takeLeft.getPieceColor() == opposite) {
3.135 - takeLeft.setAccessible(true);
3.136 - }
3.137 - Square takeRight = BoardModel.findSquare(b, (char)(s.getX() + 1), s.getY() + dy);
3.138 - if (takeRight != null && takeRight.getPieceColor() == opposite) {
3.139 - takeRight.setAccessible(true);
3.140 - }
3.141 - }
3.142 -}