# HG changeset patch # User Jaroslav Tulach # Date 1374760672 -7200 # Node ID 827f30fbdeabdedece8e7a8e84a6a389ac1af1c0 # Parent fb06534ab8dbfdd4932ddcd160bead3ab270da72 Rendering the board with knockout diff -r fb06534ab8db -r 827f30fbdeab chess/src/main/java/org/apidesign/html/demo/chess/BoardModel.java --- a/chess/src/main/java/org/apidesign/html/demo/chess/BoardModel.java Thu Jul 25 15:09:49 2013 +0200 +++ b/chess/src/main/java/org/apidesign/html/demo/chess/BoardModel.java Thu Jul 25 15:57:52 2013 +0200 @@ -23,6 +23,7 @@ */ 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; @@ -39,7 +40,27 @@ } enum PieceType { - PAWN, ROCK, KNIGHT, BISHOP, QUEEN, KING; + 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; @@ -57,14 +78,21 @@ s.setPiece(PieceType.PAWN); } - @ComputedProperty static String imageURL(PieceType piece) { + @ComputedProperty static String pieceEntity( + PieceType piece, ColorType pieceColor + ) { if (piece == null) { return ""; } - if (piece == PieceType.BISHOP) { - return "bishop.png"; + return piece.computeEntity(pieceColor); + } + + @ComputedProperty static String squareColor(ColorType color) { + if (color == null) { + return ""; + } else { + return color.toString().toLowerCase(Locale.US); } - return "knight.jpg"; } } @@ -82,7 +110,7 @@ Square s = new Square(); s.setX(j); s.setY(i); - s.setColor((i + j) % 2 == 0 ? ColorType.WHITE : ColorType.BLACK); + s.setColor((i + j) % 2 == 1 ? ColorType.WHITE : ColorType.BLACK); r.getColumns().add(s); if (i == 2) { s.setPiece(PieceType.PAWN); @@ -91,7 +119,7 @@ s.setPiece(PieceType.PAWN); s.setPieceColor(ColorType.BLACK); } else if (i == 8 || i == 1) { - s.setColor(i == 1 ? ColorType.WHITE : ColorType.BLACK); + s.setPieceColor(i == 1 ? ColorType.WHITE : ColorType.BLACK); PieceType t; switch (j) { case 'A': case 'H': t = PieceType.ROCK; break; diff -r fb06534ab8db -r 827f30fbdeab chess/src/main/web/pages/index.html --- a/chess/src/main/web/pages/index.html Thu Jul 25 15:09:49 2013 +0200 +++ b/chess/src/main/web/pages/index.html Thu Jul 25 15:57:52 2013 +0200 @@ -64,86 +64,15 @@

My game

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + +
+ +