2 * The MIT License (MIT)
4 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 package org.apidesign.html.demo.chess;
26 /** Common chess rules.
28 * @author Jaroslav Tulach <jtulach@netbeans.org>
31 static void computeAccessible(Board b, Square s) {
32 for (Row r : b.getRows()) {
33 for (Square ts : r.getColumns()) {
34 ts.setAccessible(false);
41 switch (s.getPiece()) {
46 computeAccessible(b, s, 1, 1, 1);
47 computeAccessible(b, s, 1, -1, 1);
48 computeAccessible(b, s, -1, -1, 1);
49 computeAccessible(b, s, -1, 1, 1);
50 computeAccessible(b, s, 1, 0, 1);
51 computeAccessible(b, s, 0, -1, 1);
52 computeAccessible(b, s, 0, 1, 1);
53 computeAccessible(b, s, -1, 0, 1);
63 computeAccessible(b, s, 2, 1, 1);
64 computeAccessible(b, s, 2, -1, 1);
65 computeAccessible(b, s, -2, -1, 1);
66 computeAccessible(b, s, -2, 1, 1);
67 computeAccessible(b, s, 1, 2, 1);
68 computeAccessible(b, s, -1, 2, 1);
69 computeAccessible(b, s, -1, -2, 1);
70 computeAccessible(b, s, 1, -2, 1);
78 private static void moveRock(Board b, Square s) {
79 computeAccessible(b, s, 1, 0, 8);
80 computeAccessible(b, s, 0, -1, 8);
81 computeAccessible(b, s, -1, 0, 8);
82 computeAccessible(b, s, 0, 1, 8);
85 private static void moveBishop(Board b, Square s) {
86 computeAccessible(b, s, 1, 1, 8);
87 computeAccessible(b, s, 1, -1, 8);
88 computeAccessible(b, s, -1, -1, 8);
89 computeAccessible(b, s, -1, 1, 8);
92 private static void computeAccessible(
93 Board b, Square s, int dx, int dy,
102 Square next = BoardModel.findSquare(b, (char)x, y);
106 if (next.getPieceColor() == s.getPieceColor()) {
109 next.setAccessible(true);
110 if (next.getPieceColor() != null) {
116 private static void pawns(Board b, Square s) {
117 final boolean white = s.getPieceColor() == BoardModel.ColorType.WHITE;
118 int dy = white ? 1 : -1;
119 Square step = BoardModel.findSquare(b, (char)s.getX(), s.getY() + dy);
120 if (step != null && step.getPiece() == null) {
121 step.setAccessible(true);
122 if ((s.getY() == 2 && white) || (s.getY() == 7 && !white)) {
123 Square nextSTep = BoardModel.findSquare(b, (char)s.getX(), step.getY() + dy);
124 if (nextSTep != null && step.getPiece() == null) {
125 nextSTep.setAccessible(true);
129 BoardModel.ColorType opposite = white ? BoardModel.ColorType.BLACK : BoardModel.ColorType.WHITE;
130 Square takeLeft = BoardModel.findSquare(b, (char)(s.getX() - 1), s.getY() + dy);
131 if (takeLeft != null && takeLeft.getPieceColor() == opposite) {
132 takeLeft.setAccessible(true);
134 Square takeRight = BoardModel.findSquare(b, (char)(s.getX() + 1), s.getY() + dy);
135 if (takeRight != null && takeRight.getPieceColor() == opposite) {
136 takeRight.setAccessible(true);