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@22
|
26 |
import net.java.html.json.ComputedProperty;
|
jtulach@22
|
27 |
import net.java.html.json.Function;
|
jtulach@22
|
28 |
import net.java.html.json.Model;
|
jtulach@22
|
29 |
import net.java.html.json.Property;
|
jtulach@22
|
30 |
|
jtulach@22
|
31 |
@Model(className="Board", properties={
|
jtulach@22
|
32 |
@Property(name = "rows", type = Row.class, array = true)
|
jtulach@22
|
33 |
})
|
jtulach@22
|
34 |
public class BoardModel {
|
jtulach@22
|
35 |
@Model(className="Row", properties = {
|
jtulach@22
|
36 |
@Property(name = "columns", type = Square.class, array = true)
|
jtulach@22
|
37 |
})
|
jtulach@22
|
38 |
static class RowsImpl {
|
jtulach@22
|
39 |
}
|
jtulach@22
|
40 |
|
jtulach@22
|
41 |
enum PieceType {
|
jtulach@22
|
42 |
PAWN, ROCK, KNIGHT, BISHOP, QUEEN, KING;
|
jtulach@22
|
43 |
}
|
jtulach@22
|
44 |
enum ColorType {
|
jtulach@22
|
45 |
WHITE, BLACK;
|
jtulach@22
|
46 |
}
|
jtulach@22
|
47 |
|
jtulach@22
|
48 |
@Model(className="Square", properties = {
|
jtulach@22
|
49 |
@Property(name = "piece", type = PieceType.class),
|
jtulach@22
|
50 |
@Property(name = "pieceColor", type = ColorType.class),
|
jtulach@22
|
51 |
@Property(name = "color", type = ColorType.class),
|
jtulach@22
|
52 |
@Property(name = "x", type = int.class),
|
jtulach@22
|
53 |
@Property(name = "y", type = int.class),
|
jtulach@22
|
54 |
})
|
jtulach@22
|
55 |
static class PieceImpl {
|
jtulach@22
|
56 |
@Function static void changeToPawn(Square s) {
|
jtulach@22
|
57 |
s.setPiece(PieceType.PAWN);
|
jtulach@22
|
58 |
}
|
jtulach@22
|
59 |
|
jtulach@22
|
60 |
@ComputedProperty static String imageURL(PieceType piece) {
|
jtulach@22
|
61 |
if (piece == null) {
|
jtulach@22
|
62 |
return "";
|
jtulach@22
|
63 |
}
|
jtulach@22
|
64 |
if (piece == PieceType.BISHOP) {
|
jtulach@22
|
65 |
return "bishop.png";
|
jtulach@22
|
66 |
}
|
jtulach@22
|
67 |
return "knight.jpg";
|
jtulach@22
|
68 |
}
|
jtulach@22
|
69 |
}
|
jtulach@22
|
70 |
|
jtulach@22
|
71 |
public static void initialize(String[] args) {
|
jtulach@22
|
72 |
Board b = createBoard();
|
jtulach@22
|
73 |
b.applyBindings();
|
jtulach@22
|
74 |
}
|
jtulach@22
|
75 |
|
jtulach@22
|
76 |
private static Board createBoard() {
|
jtulach@22
|
77 |
Board b = new Board();
|
jtulach@22
|
78 |
for (int i = 8; i > 0; i--) {
|
jtulach@22
|
79 |
Row r = new Row();
|
jtulach@22
|
80 |
b.getRows().add(r);
|
jtulach@22
|
81 |
for (int j = 'A'; j <= 'H'; j++) {
|
jtulach@22
|
82 |
Square s = new Square();
|
jtulach@22
|
83 |
s.setX(j);
|
jtulach@22
|
84 |
s.setY(i);
|
jtulach@22
|
85 |
s.setColor((i + j) % 2 == 0 ? ColorType.WHITE : ColorType.BLACK);
|
jtulach@22
|
86 |
r.getColumns().add(s);
|
jtulach@22
|
87 |
if (i == 2) {
|
jtulach@22
|
88 |
s.setPiece(PieceType.PAWN);
|
jtulach@22
|
89 |
s.setPieceColor(ColorType.WHITE);
|
jtulach@22
|
90 |
} else if (i == 7) {
|
jtulach@22
|
91 |
s.setPiece(PieceType.PAWN);
|
jtulach@22
|
92 |
s.setPieceColor(ColorType.BLACK);
|
jtulach@22
|
93 |
} else if (i == 8 || i == 1) {
|
jtulach@22
|
94 |
s.setColor(i == 1 ? ColorType.WHITE : ColorType.BLACK);
|
jtulach@22
|
95 |
PieceType t;
|
jtulach@22
|
96 |
switch (j) {
|
jtulach@22
|
97 |
case 'A': case 'H': t = PieceType.ROCK; break;
|
jtulach@22
|
98 |
case 'B': case 'G': t = PieceType.KNIGHT; break;
|
jtulach@22
|
99 |
case 'C': case 'F': t = PieceType.BISHOP; break;
|
jtulach@22
|
100 |
case 'D': t = PieceType.QUEEN; break;
|
jtulach@22
|
101 |
default: t = PieceType.KING; break;
|
jtulach@22
|
102 |
}
|
jtulach@22
|
103 |
s.setPiece(t);
|
jtulach@22
|
104 |
}
|
jtulach@22
|
105 |
}
|
jtulach@22
|
106 |
}
|
jtulach@22
|
107 |
return b;
|
jtulach@22
|
108 |
}
|
jtulach@22
|
109 |
}
|