1.1 --- a/chess/src/main/java/org/apidesign/html/demo/chess/BoardModel.java Thu Jul 25 15:09:49 2013 +0200
1.2 +++ b/chess/src/main/java/org/apidesign/html/demo/chess/BoardModel.java Thu Jul 25 15:57:52 2013 +0200
1.3 @@ -23,6 +23,7 @@
1.4 */
1.5 package org.apidesign.html.demo.chess;
1.6
1.7 +import java.util.Locale;
1.8 import net.java.html.json.ComputedProperty;
1.9 import net.java.html.json.Function;
1.10 import net.java.html.json.Model;
1.11 @@ -39,7 +40,27 @@
1.12 }
1.13
1.14 enum PieceType {
1.15 - PAWN, ROCK, KNIGHT, BISHOP, QUEEN, KING;
1.16 + PAWN(5), ROCK(2), KNIGHT(4), BISHOP(3), QUEEN(1), KING(0);
1.17 +
1.18 + final int entityIndex;
1.19 +
1.20 + PieceType(int ei) {
1.21 + this.entityIndex = ei;
1.22 + }
1.23 +
1.24 + String computeEntity(ColorType color) {
1.25 + if (color == null) {
1.26 + color = ColorType.WHITE;
1.27 + }
1.28 + int base;
1.29 + switch (color) {
1.30 + case WHITE: base = 12; break;
1.31 + case BLACK: base = 18; break;
1.32 + default:
1.33 + throw new AssertionError();
1.34 + }
1.35 + return "b" + String.valueOf(base + entityIndex) + ";";
1.36 + }
1.37 }
1.38 enum ColorType {
1.39 WHITE, BLACK;
1.40 @@ -57,14 +78,21 @@
1.41 s.setPiece(PieceType.PAWN);
1.42 }
1.43
1.44 - @ComputedProperty static String imageURL(PieceType piece) {
1.45 + @ComputedProperty static String pieceEntity(
1.46 + PieceType piece, ColorType pieceColor
1.47 + ) {
1.48 if (piece == null) {
1.49 return "";
1.50 }
1.51 - if (piece == PieceType.BISHOP) {
1.52 - return "bishop.png";
1.53 + return piece.computeEntity(pieceColor);
1.54 + }
1.55 +
1.56 + @ComputedProperty static String squareColor(ColorType color) {
1.57 + if (color == null) {
1.58 + return "";
1.59 + } else {
1.60 + return color.toString().toLowerCase(Locale.US);
1.61 }
1.62 - return "knight.jpg";
1.63 }
1.64 }
1.65
1.66 @@ -82,7 +110,7 @@
1.67 Square s = new Square();
1.68 s.setX(j);
1.69 s.setY(i);
1.70 - s.setColor((i + j) % 2 == 0 ? ColorType.WHITE : ColorType.BLACK);
1.71 + s.setColor((i + j) % 2 == 1 ? ColorType.WHITE : ColorType.BLACK);
1.72 r.getColumns().add(s);
1.73 if (i == 2) {
1.74 s.setPiece(PieceType.PAWN);
1.75 @@ -91,7 +119,7 @@
1.76 s.setPiece(PieceType.PAWN);
1.77 s.setPieceColor(ColorType.BLACK);
1.78 } else if (i == 8 || i == 1) {
1.79 - s.setColor(i == 1 ? ColorType.WHITE : ColorType.BLACK);
1.80 + s.setPieceColor(i == 1 ? ColorType.WHITE : ColorType.BLACK);
1.81 PieceType t;
1.82 switch (j) {
1.83 case 'A': case 'H': t = PieceType.ROCK; break;