# HG changeset patch # User Jaroslav Tulach # Date 1380054511 -7200 # Node ID 49011b4a268949b91aa0bdf7b4a66319c83e63a9 # Parent 945fbfff28f3dc4c7f947f7149c3f6cfff8a4e61 Removing old code diff -r 945fbfff28f3 -r 49011b4a2689 chess/src/main/java/org/apidesign/html/demo/chess/BoardModel.java --- a/chess/src/main/java/org/apidesign/html/demo/chess/BoardModel.java Tue Sep 24 22:20:24 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,202 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (C) 2013 Jaroslav Tulach - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package org.apidesign.html.demo.chess; - -import java.util.Locale; -import net.java.html.json.ComputedProperty; -import net.java.html.json.Function; -import net.java.html.json.Model; -import net.java.html.json.Property; - -@Model(className="Board", properties={ - @Property(name = "rows", type = Row.class, array = true), - @Property(name = "turn", type = BoardModel.ColorType.class) -}) -public class BoardModel { - @Function static void selected(Board b, Square data) { - Square previoslySelected = findSelectedSquare(b); - if (previoslySelected == null) { - if (data.getPiece() != null && data.getPieceColor() == b.getTurn()) { - data.setSelected(true); - Rules.computeAccessible(b, data); - } - } else { - if (data.getPiece() != null && data.getPieceColor() == previoslySelected.getPieceColor()) { - previoslySelected.setSelected(false); - data.setSelected(true); - Rules.computeAccessible(b, data); - return; - } - if (data.isAccessible()) { - previoslySelected.setSelected(false); - data.setPieceColor(previoslySelected.getPieceColor()); - data.setPiece(previoslySelected.getPiece()); - b.setTurn(b.getTurn() == ColorType.WHITE ? ColorType.BLACK : ColorType.WHITE); - previoslySelected.setPiece(null); - previoslySelected.setPieceColor(null); - Rules.computeAccessible(b, null); - } - } - } - - @ComputedProperty static boolean whiteTurn(ColorType turn) { - return turn == ColorType.WHITE; - } - - @ComputedProperty static boolean blackTurn(ColorType turn) { - return turn == ColorType.BLACK; - } - - static Square findSquare(Board b, char column, int row) { - for (Row r : b.getRows()) { - for (Square square : r.getColumns()) { - if (square.getX() == column && square.getY() == row) { - return square; - } - } - } - return null; - } - - static Square findSelectedSquare(Board b) { - for (Row row : b.getRows()) { - for (Square square : row.getColumns()) { - if (square.isSelected()) { - return square; - } - } - } - return null; - } - - @Model(className="Row", properties = { - @Property(name = "columns", type = Square.class, array = true) - }) - static class RowsImpl { - } - - enum PieceType { - PAWN(5), ROCK(2), KNIGHT(4), BISHOP(3), QUEEN(1), KING(0); - - final int entityIndex; - - PieceType(int ei) { - this.entityIndex = ei; - } - - String computeEntity(ColorType color) { - if (color == null) { - color = ColorType.WHITE; - } - int base; - switch (color) { - case WHITE: base = 12; break; - case BLACK: base = 18; break; - default: - throw new AssertionError(); - } - return "b" + String.valueOf(base + entityIndex) + ";"; - } - } - enum ColorType { - WHITE, BLACK; - } - - @Model(className="Square", properties = { - @Property(name = "piece", type = PieceType.class), - @Property(name = "pieceColor", type = ColorType.class), - @Property(name = "color", type = ColorType.class), - @Property(name = "x", type = int.class), - @Property(name = "y", type = int.class), - @Property(name = "selected", type = boolean.class), - @Property(name = "accessible", type = boolean.class), - }) - static class PieceImpl { - @ComputedProperty static String pieceEntity( - PieceType piece, ColorType pieceColor - ) { - if (piece == null) { - return ""; - } - return piece.computeEntity(pieceColor); - } - - @ComputedProperty static String squareColor( - ColorType color, boolean selected, boolean accessible - ) { - if (selected) { - return "selected"; - } - if (accessible) { - return "accessible"; - } - - if (color == null) { - return ""; - } else { - return color.toString().toLowerCase(Locale.US); - } - } - } - - public static void initialize(String[] args) { - Board b = createBoard(); - b.applyBindings(); - } - - static Board createBoard() { - Board b = new Board(); - b.setTurn(ColorType.WHITE); - for (int i = 8; i > 0; i--) { - Row r = new Row(); - b.getRows().add(r); - for (int j = 'A'; j <= 'H'; j++) { - Square s = new Square(); - s.setX(j); - s.setY(i); - s.setColor((i + j) % 2 == 1 ? ColorType.WHITE : ColorType.BLACK); - r.getColumns().add(s); - if (i == 2) { - s.setPiece(PieceType.PAWN); - s.setPieceColor(ColorType.WHITE); - } else if (i == 7) { - s.setPiece(PieceType.PAWN); - s.setPieceColor(ColorType.BLACK); - } else if (i == 8 || i == 1) { - s.setPieceColor(i == 1 ? ColorType.WHITE : ColorType.BLACK); - PieceType t; - switch (j) { - case 'A': case 'H': t = PieceType.ROCK; break; - case 'B': case 'G': t = PieceType.KNIGHT; break; - case 'C': case 'F': t = PieceType.BISHOP; break; - case 'D': t = PieceType.QUEEN; break; - default: t = PieceType.KING; break; - } - s.setPiece(t); - } - } - } - return b; - } -} diff -r 945fbfff28f3 -r 49011b4a2689 chess/src/main/java/org/apidesign/html/demo/chess/Main.java --- a/chess/src/main/java/org/apidesign/html/demo/chess/Main.java Tue Sep 24 22:20:24 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,40 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (C) 2013 Jaroslav Tulach - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package org.apidesign.html.demo.chess; - -import net.java.html.boot.BrowserBuilder; - -public final class Main { - private Main() { - } - - public static void main(String... args) throws Exception { - BrowserBuilder.newBrowser(). - loadPage("pages/index.html"). - loadClass(BoardModel.class). - invoke("initialize", args). - showAndWait(); - System.exit(0); - } -} diff -r 945fbfff28f3 -r 49011b4a2689 chess/src/main/java/org/apidesign/html/demo/chess/Rules.java --- a/chess/src/main/java/org/apidesign/html/demo/chess/Rules.java Tue Sep 24 22:20:24 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,139 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (C) 2013 Jaroslav Tulach - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package org.apidesign.html.demo.chess; - -/** Common chess rules. - * - * @author Jaroslav Tulach - */ -class Rules { - static void computeAccessible(Board b, Square s) { - for (Row r : b.getRows()) { - for (Square ts : r.getColumns()) { - ts.setAccessible(false); - } - } - if (s == null) { - return; - } - - switch (s.getPiece()) { - case BISHOP: - moveBishop(b, s); - break; - case KING: - computeAccessible(b, s, 1, 1, 1); - computeAccessible(b, s, 1, -1, 1); - computeAccessible(b, s, -1, -1, 1); - computeAccessible(b, s, -1, 1, 1); - computeAccessible(b, s, 1, 0, 1); - computeAccessible(b, s, 0, -1, 1); - computeAccessible(b, s, 0, 1, 1); - computeAccessible(b, s, -1, 0, 1); - break; - case ROCK: - moveRock(b, s); - break; - case QUEEN: - moveRock(b, s); - moveBishop(b, s); - break; - case KNIGHT: - computeAccessible(b, s, 2, 1, 1); - computeAccessible(b, s, 2, -1, 1); - computeAccessible(b, s, -2, -1, 1); - computeAccessible(b, s, -2, 1, 1); - computeAccessible(b, s, 1, 2, 1); - computeAccessible(b, s, -1, 2, 1); - computeAccessible(b, s, -1, -2, 1); - computeAccessible(b, s, 1, -2, 1); - break; - case PAWN: - pawns(b, s); - break; - } - } - - private static void moveRock(Board b, Square s) { - computeAccessible(b, s, 1, 0, 8); - computeAccessible(b, s, 0, -1, 8); - computeAccessible(b, s, -1, 0, 8); - computeAccessible(b, s, 0, 1, 8); - } - - private static void moveBishop(Board b, Square s) { - computeAccessible(b, s, 1, 1, 8); - computeAccessible(b, s, 1, -1, 8); - computeAccessible(b, s, -1, -1, 8); - computeAccessible(b, s, -1, 1, 8); - } - - private static void computeAccessible( - Board b, Square s, int dx, int dy, - int limit - ) { - int x = s.getX(); - int y = s.getY(); - - while (limit-- > 0) { - x += dx; - y += dy; - Square next = BoardModel.findSquare(b, (char)x, y); - if (next == null) { - break; - } - if (next.getPieceColor() == s.getPieceColor()) { - break; - } - next.setAccessible(true); - if (next.getPieceColor() != null) { - break; - } - } - } - - private static void pawns(Board b, Square s) { - final boolean white = s.getPieceColor() == BoardModel.ColorType.WHITE; - int dy = white ? 1 : -1; - Square step = BoardModel.findSquare(b, (char)s.getX(), s.getY() + dy); - if (step != null && step.getPiece() == null) { - step.setAccessible(true); - if ((s.getY() == 2 && white) || (s.getY() == 7 && !white)) { - Square nextSTep = BoardModel.findSquare(b, (char)s.getX(), step.getY() + dy); - if (nextSTep != null && step.getPiece() == null) { - nextSTep.setAccessible(true); - } - } - } - BoardModel.ColorType opposite = white ? BoardModel.ColorType.BLACK : BoardModel.ColorType.WHITE; - Square takeLeft = BoardModel.findSquare(b, (char)(s.getX() - 1), s.getY() + dy); - if (takeLeft != null && takeLeft.getPieceColor() == opposite) { - takeLeft.setAccessible(true); - } - Square takeRight = BoardModel.findSquare(b, (char)(s.getX() + 1), s.getY() + dy); - if (takeRight != null && takeRight.getPieceColor() == opposite) { - takeRight.setAccessible(true); - } - } -}