chess/src/main/java/org/apidesign/html/demo/chess/BoardModel.java
author Jaroslav Tulach <jtulach@netbeans.org>
Fri, 26 Jul 2013 09:28:49 +0200
branchchess
changeset 30 a46846115b83
parent 29 9fb64f6528b5
child 34 1ebd52ae8ccb
permissions -rw-r--r--
Ensure changing turns
     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             }
    43         } else {
    44             previoslySelected.setSelected(false);
    45             if (data.getPiece() != null && data.getPieceColor() == previoslySelected.getPieceColor()) {
    46                 previoslySelected.setSelected(false);
    47                 data.setSelected(true);
    48                 return;
    49             }
    50             data.setPieceColor(previoslySelected.getPieceColor());
    51             data.setPiece(previoslySelected.getPiece());
    52             b.setTurn(b.getTurn() == ColorType.WHITE ? ColorType.BLACK : ColorType.WHITE);
    53             previoslySelected.setPiece(null);
    54             previoslySelected.setPieceColor(null);
    55         }
    56     }
    57     
    58     @ComputedProperty static boolean whiteTurn(ColorType turn) {
    59         return turn == ColorType.WHITE;
    60     }
    61 
    62     @ComputedProperty static boolean blackTurn(ColorType turn) {
    63         return turn == ColorType.BLACK;
    64     }
    65     
    66     static Square findSquare(Board b, char column, int row) {
    67         for (Row r : b.getRows()) {
    68             for (Square square : r.getColumns()) {
    69                 if (square.getX() == column && square.getY() == row) {
    70                     return square;
    71                 }
    72             }
    73         }
    74         return null;
    75     }
    76     
    77     static Square findSelectedSquare(Board b) {
    78         for (Row row : b.getRows()) {
    79             for (Square square : row.getColumns()) {
    80                 if (square.isSelected()) {
    81                     return square;
    82                 }
    83             }
    84         }
    85         return null;
    86     }
    87     
    88     @Model(className="Row", properties = {
    89         @Property(name = "columns", type = Square.class, array = true)
    90     })
    91     static class RowsImpl {
    92     }
    93     
    94     enum PieceType {
    95         PAWN(5), ROCK(2), KNIGHT(4), BISHOP(3), QUEEN(1), KING(0);
    96         
    97         final int entityIndex;
    98         
    99         PieceType(int ei) {
   100             this.entityIndex = ei;
   101         }
   102         
   103         String computeEntity(ColorType color) {
   104             if (color == null) {
   105                 color = ColorType.WHITE;
   106             }
   107             int base;
   108             switch (color) {
   109                 case WHITE: base = 12; break;
   110                 case BLACK: base = 18; break;
   111                 default:
   112                     throw new AssertionError();
   113             }
   114             return "&#98" + String.valueOf(base + entityIndex) + ";";
   115         }
   116     }
   117     enum ColorType {
   118         WHITE, BLACK;
   119     }
   120     
   121     @Model(className="Square", properties = {
   122         @Property(name = "piece", type = PieceType.class),
   123         @Property(name = "pieceColor", type = ColorType.class),
   124         @Property(name = "color", type = ColorType.class),
   125         @Property(name = "x", type = int.class),
   126         @Property(name = "y", type = int.class),
   127         @Property(name = "selected", type = boolean.class)
   128     })
   129     static class PieceImpl {
   130         @ComputedProperty static String pieceEntity(
   131             PieceType piece, ColorType pieceColor
   132         ) {
   133             if (piece == null) {
   134                 return "";
   135             }
   136             return piece.computeEntity(pieceColor);
   137         }
   138         
   139         @ComputedProperty static String squareColor(ColorType color, boolean selected) {
   140             if (selected) {
   141                 return "selected";
   142             }
   143             
   144             if (color == null) {
   145                 return "";
   146             } else {
   147                 return color.toString().toLowerCase(Locale.US);
   148             }
   149         }
   150     }
   151     
   152     public static void initialize(String[] args) {
   153         Board b = createBoard();
   154         b.applyBindings();
   155     }
   156 
   157     static Board createBoard() {
   158         Board b = new Board();
   159         b.setTurn(ColorType.WHITE);
   160         for (int i = 8; i > 0; i--) {
   161             Row r = new Row();
   162             b.getRows().add(r);
   163             for (int j = 'A'; j <= 'H'; j++) {
   164                 Square s = new Square();
   165                 s.setX(j);
   166                 s.setY(i);
   167                 s.setColor((i + j) % 2 == 1 ? ColorType.WHITE : ColorType.BLACK);
   168                 r.getColumns().add(s);
   169                 if (i == 2) {
   170                     s.setPiece(PieceType.PAWN);
   171                     s.setPieceColor(ColorType.WHITE);
   172                 } else if (i == 7) {
   173                     s.setPiece(PieceType.PAWN);
   174                     s.setPieceColor(ColorType.BLACK);
   175                 } else if (i == 8 || i == 1) {
   176                     s.setPieceColor(i == 1 ? ColorType.WHITE : ColorType.BLACK);
   177                     PieceType t;
   178                     switch (j) {
   179                         case 'A': case 'H': t = PieceType.ROCK; break;
   180                         case 'B': case 'G': t = PieceType.KNIGHT; break;
   181                         case 'C': case 'F': t = PieceType.BISHOP; break;
   182                         case 'D': t = PieceType.QUEEN; break;
   183                         default: t = PieceType.KING; break;
   184                     }
   185                     s.setPiece(t);
   186                 }
   187             }
   188         }
   189         return b;
   190     }
   191 }