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