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