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