chess/src/main/java/org/apidesign/html/demo/chess/BoardModel.java
author Jaroslav Tulach <jtulach@netbeans.org>
Fri, 26 Jul 2013 17:14:25 +0200
branchchess
changeset 34 1ebd52ae8ccb
parent 30 a46846115b83
permissions -rw-r--r--
Show accessible squares when a figure is selected
     1 /**
     2  * The MIT License (MIT)
     3  *
     4  * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     5  *
     6  * Permission is hereby granted, free of charge, to any person obtaining a copy
     7  * of this software and associated documentation files (the "Software"), to deal
     8  * in the Software without restriction, including without limitation the rights
     9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    10  * copies of the Software, and to permit persons to whom the Software is
    11  * furnished to do so, subject to the following conditions:
    12  *
    13  * The above copyright notice and this permission notice shall be included in
    14  * all copies or substantial portions of the Software.
    15  *
    16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    22  * THE SOFTWARE.
    23  */
    24 package org.apidesign.html.demo.chess;
    25 
    26 import java.util.Locale;
    27 import net.java.html.json.ComputedProperty;
    28 import net.java.html.json.Function;
    29 import net.java.html.json.Model;
    30 import net.java.html.json.Property;
    31 
    32 @Model(className="Board", properties={
    33     @Property(name = "rows", type = Row.class, array = true),
    34     @Property(name = "turn", type = BoardModel.ColorType.class)
    35 })
    36 public class BoardModel {
    37     @Function static void selected(Board b, Square data) {
    38         Square previoslySelected = findSelectedSquare(b);
    39         if (previoslySelected == null) {
    40             if (data.getPiece() != null && data.getPieceColor() == b.getTurn()) {
    41                 data.setSelected(true);
    42                 Rules.computeAccessible(b, data);
    43             }
    44         } else {
    45             if (data.getPiece() != null && data.getPieceColor() == previoslySelected.getPieceColor()) {
    46                 previoslySelected.setSelected(false);
    47                 data.setSelected(true);
    48                 Rules.computeAccessible(b, data);
    49                 return;
    50             }
    51             if (data.isAccessible()) {
    52                 previoslySelected.setSelected(false);
    53                 data.setPieceColor(previoslySelected.getPieceColor());
    54                 data.setPiece(previoslySelected.getPiece());
    55                 b.setTurn(b.getTurn() == ColorType.WHITE ? ColorType.BLACK : ColorType.WHITE);
    56                 previoslySelected.setPiece(null);
    57                 previoslySelected.setPieceColor(null);
    58                 Rules.computeAccessible(b, null);
    59             }
    60         }
    61     }
    62     
    63     @ComputedProperty static boolean whiteTurn(ColorType turn) {
    64         return turn == ColorType.WHITE;
    65     }
    66 
    67     @ComputedProperty static boolean blackTurn(ColorType turn) {
    68         return turn == ColorType.BLACK;
    69     }
    70     
    71     static Square findSquare(Board b, char column, int row) {
    72         for (Row r : b.getRows()) {
    73             for (Square square : r.getColumns()) {
    74                 if (square.getX() == column && square.getY() == row) {
    75                     return square;
    76                 }
    77             }
    78         }
    79         return null;
    80     }
    81     
    82     static Square findSelectedSquare(Board b) {
    83         for (Row row : b.getRows()) {
    84             for (Square square : row.getColumns()) {
    85                 if (square.isSelected()) {
    86                     return square;
    87                 }
    88             }
    89         }
    90         return null;
    91     }
    92     
    93     @Model(className="Row", properties = {
    94         @Property(name = "columns", type = Square.class, array = true)
    95     })
    96     static class RowsImpl {
    97     }
    98     
    99     enum PieceType {
   100         PAWN(5), ROCK(2), KNIGHT(4), BISHOP(3), QUEEN(1), KING(0);
   101         
   102         final int entityIndex;
   103         
   104         PieceType(int ei) {
   105             this.entityIndex = ei;
   106         }
   107         
   108         String computeEntity(ColorType color) {
   109             if (color == null) {
   110                 color = ColorType.WHITE;
   111             }
   112             int base;
   113             switch (color) {
   114                 case WHITE: base = 12; break;
   115                 case BLACK: base = 18; break;
   116                 default:
   117                     throw new AssertionError();
   118             }
   119             return "&#98" + String.valueOf(base + entityIndex) + ";";
   120         }
   121     }
   122     enum ColorType {
   123         WHITE, BLACK;
   124     }
   125     
   126     @Model(className="Square", properties = {
   127         @Property(name = "piece", type = PieceType.class),
   128         @Property(name = "pieceColor", type = ColorType.class),
   129         @Property(name = "color", type = ColorType.class),
   130         @Property(name = "x", type = int.class),
   131         @Property(name = "y", type = int.class),
   132         @Property(name = "selected", type = boolean.class),
   133         @Property(name = "accessible", type = boolean.class),
   134     })
   135     static class PieceImpl {
   136         @ComputedProperty static String pieceEntity(
   137             PieceType piece, ColorType pieceColor
   138         ) {
   139             if (piece == null) {
   140                 return "";
   141             }
   142             return piece.computeEntity(pieceColor);
   143         }
   144         
   145         @ComputedProperty static String squareColor(
   146             ColorType color, boolean selected, boolean accessible
   147         ) {
   148             if (selected) {
   149                 return "selected";
   150             }
   151             if (accessible) {
   152                 return "accessible";
   153             }
   154             
   155             if (color == null) {
   156                 return "";
   157             } else {
   158                 return color.toString().toLowerCase(Locale.US);
   159             }
   160         }
   161     }
   162     
   163     public static void initialize(String[] args) {
   164         Board b = createBoard();
   165         b.applyBindings();
   166     }
   167 
   168     static Board createBoard() {
   169         Board b = new Board();
   170         b.setTurn(ColorType.WHITE);
   171         for (int i = 8; i > 0; i--) {
   172             Row r = new Row();
   173             b.getRows().add(r);
   174             for (int j = 'A'; j <= 'H'; j++) {
   175                 Square s = new Square();
   176                 s.setX(j);
   177                 s.setY(i);
   178                 s.setColor((i + j) % 2 == 1 ? ColorType.WHITE : ColorType.BLACK);
   179                 r.getColumns().add(s);
   180                 if (i == 2) {
   181                     s.setPiece(PieceType.PAWN);
   182                     s.setPieceColor(ColorType.WHITE);
   183                 } else if (i == 7) {
   184                     s.setPiece(PieceType.PAWN);
   185                     s.setPieceColor(ColorType.BLACK);
   186                 } else if (i == 8 || i == 1) {
   187                     s.setPieceColor(i == 1 ? ColorType.WHITE : ColorType.BLACK);
   188                     PieceType t;
   189                     switch (j) {
   190                         case 'A': case 'H': t = PieceType.ROCK; break;
   191                         case 'B': case 'G': t = PieceType.KNIGHT; break;
   192                         case 'C': case 'F': t = PieceType.BISHOP; break;
   193                         case 'D': t = PieceType.QUEEN; break;
   194                         default: t = PieceType.KING; break;
   195                     }
   196                     s.setPiece(t);
   197                 }
   198             }
   199         }
   200         return b;
   201     }
   202 }