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