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 com.oracle.chess.client.htmljava;
26 import java.util.ArrayList;
27 import java.util.List;
29 /** Common chess rules.
31 * @author Jaroslav Tulach <jtulach@netbeans.org>
34 static void computeAccessible(Board b, Square s) {
35 for (Row r : b.getRows()) {
36 for (Square ts : r.getColumns()) {
37 ts.setAccessible(false);
44 switch (s.getPiece()) {
49 computeAccessible(b, s, 1, 1, 1);
50 computeAccessible(b, s, 1, -1, 1);
51 computeAccessible(b, s, -1, -1, 1);
52 computeAccessible(b, s, -1, 1, 1);
53 computeAccessible(b, s, 1, 0, 1);
54 computeAccessible(b, s, 0, -1, 1);
55 computeAccessible(b, s, 0, 1, 1);
56 computeAccessible(b, s, -1, 0, 1);
67 computeAccessible(b, s, 2, 1, 1);
68 computeAccessible(b, s, 2, -1, 1);
69 computeAccessible(b, s, -2, -1, 1);
70 computeAccessible(b, s, -2, 1, 1);
71 computeAccessible(b, s, 1, 2, 1);
72 computeAccessible(b, s, -1, 2, 1);
73 computeAccessible(b, s, -1, -2, 1);
74 computeAccessible(b, s, 1, -2, 1);
82 private static void moveRock(Board b, Square s) {
83 computeAccessible(b, s, 1, 0, 8);
84 computeAccessible(b, s, 0, -1, 8);
85 computeAccessible(b, s, -1, 0, 8);
86 computeAccessible(b, s, 0, 1, 8);
89 private static void moveBishop(Board b, Square s) {
90 computeAccessible(b, s, 1, 1, 8);
91 computeAccessible(b, s, 1, -1, 8);
92 computeAccessible(b, s, -1, -1, 8);
93 computeAccessible(b, s, -1, 1, 8);
96 private static void computeAccessible(
97 Board b, Square s, int dx, int dy,
103 while (limit-- > 0) {
106 Square next = BoardModel.findSquare(b, (char)x, y);
110 if (next.getPieceColor() == s.getPieceColor()) {
113 next.setAccessible(true);
114 if (next.getPieceColor() != null) {
120 private static void pawns(Board b, Square s) {
121 final boolean white = s.getPieceColor() == Color.W;
122 int dy = white ? 1 : -1;
123 Square step = BoardModel.findSquare(b, (char)s.getX(), s.getY() + dy);
124 if (step != null && step.getPiece() == null) {
125 step.setAccessible(true);
126 if ((s.getY() == 2 && white) || (s.getY() == 7 && !white)) {
127 Square nextSTep = BoardModel.findSquare(b, (char)s.getX(), step.getY() + dy);
128 if (nextSTep != null && step.getPiece() == null && nextSTep.getPiece() == null) {
129 nextSTep.setAccessible(true);
133 Color opposite = white ? Color.B : Color.W;
134 Square takeLeft = BoardModel.findSquare(b, (char)(s.getX() - 1), s.getY() + dy);
135 if (takeLeft != null && takeLeft.getPieceColor() == opposite) {
136 takeLeft.setAccessible(true);
138 Square takeRight = BoardModel.findSquare(b, (char)(s.getX() + 1), s.getY() + dy);
139 if (takeRight != null && takeRight.getPieceColor() == opposite) {
140 takeRight.setAccessible(true);
142 if ((white && s.getY() == 5) || (!white && s.getY() == 4)) {
143 int enPassantFrom = white ? 7 : 2;
144 int enPassantTo = white ? 5 : 4;
145 if (!b.getMoves().isEmpty()) {
146 Move last = b.getMoves().get(b.getMoves().size() - 1);
148 last.getPiece() == BoardModel.PieceType.PAWN &&
149 last.getFrom().getY() == enPassantFrom &&
150 last.getTo().getY() == enPassantTo
152 if (takeLeft != null && last.getFrom().getX() == s.getX() - 1) {
153 takeLeft.setAccessible(true);
155 if (takeRight != null && last.getFrom().getX() == s.getX() + 1) {
156 takeRight.setAccessible(true);
163 static Board createBoard() {
164 Board b = new Board();
170 private static void initBoardField(Board b) {
171 List<Row> addRows = new ArrayList<>(8);
172 for (int i = 8; i > 0; i--) {
173 Square[] arr = new Square[8];
174 for (char j = 'A'; j <= 'H'; j++) {
175 arr[j - 'A'] = new Square(
177 (i + j) % 2 == 1 ? Color.W : Color.B,
178 null, null, // figure
182 addRows.add(new Row(arr));
184 b.getRows().addAll(addRows);
187 static void initBoard(Board b) {
190 private static void initBoard(Board b, boolean init) {
194 if (b.getRows().isEmpty()) {
195 for (int i = 8; i > 0; i--) {
196 Row r = b.getRows().get(8 - i);
197 for (char j = 'A'; j <= 'H'; j++) {
198 Square s = r.getColumns().get(j - 'A');
199 s.setAccessible(false);
201 r.getColumns().set(j - 'A', s);
202 initialPosition(s, init);
206 for (Row r : b.getRows()) {
207 for (Square square : r.getColumns()) {
208 square.setAccessible(false);
209 square.setPending(false);
210 square.setPiece(null);
211 square.setPieceColor(null);
212 square.setSelected(false);
213 initialPosition(square, init);
217 b.setPendingMove(null);
220 private static void initialPosition(Square s, boolean init) {
221 int row = s.getPosition().getY();
222 char column = s.getPosition().getX();
224 s.setPieceColor(null);
227 s.setPiece(BoardModel.PieceType.PAWN);
228 s.setPieceColor(Color.W);
229 } else if (row == 7) {
230 s.setPiece(BoardModel.PieceType.PAWN);
231 s.setPieceColor(Color.B);
232 } else if (row == 8 || row == 1) {
233 s.setPieceColor(row == 1 ? Color.W : Color.B);
234 BoardModel.PieceType t;
238 t = BoardModel.PieceType.ROCK;
242 t = BoardModel.PieceType.KNIGHT;
246 t = BoardModel.PieceType.BISHOP;
249 t = BoardModel.PieceType.QUEEN;
252 t = BoardModel.PieceType.KING;
260 static void initBoard(
261 Board board, List<String> whites, List<String> blacks, Color turn
263 initBoard(board, false);
264 for (String w : whites) {
265 assert w.length() == 3 : "Expecting three letter string: '" + w + "'";
267 char column = w.charAt(1);
268 int row = (w.charAt(2) - '0');
270 Square s = BoardModel.findSquare(board, column, row);
271 s.setPieceColor(Color.W);
272 s.setPiece(BoardModel.PieceType.fromNotation(w.charAt(0)));
274 for (String w : blacks) {
275 assert w.length() == 3 : "Expecting three letter string: '" + w + "'";
277 char column = w.charAt(1);
278 int row = (w.charAt(2) - '0');
280 Square s = BoardModel.findSquare(board, column, row);
281 s.setPieceColor(Color.B);
282 s.setPiece(BoardModel.PieceType.fromNotation(w.charAt(0)));
287 private static void checkRochade(Board b, Square s) {
288 if (s.getPosition().getX() == 'E') {
289 int y = s.getPosition().getY();
290 final Square gRow = BoardModel.findSquare(b, 'G', y);
292 BoardModel.findSquare(b, 'H', y).getPiece() == BoardModel.PieceType.ROCK
294 BoardModel.findSquare(b, 'F', y).getPiece() == null
296 gRow.getPiece() == null
298 gRow.setAccessible(true);
300 final Square cRow = BoardModel.findSquare(b, 'C', y);
302 BoardModel.findSquare(b, 'A', y).getPiece() == BoardModel.PieceType.ROCK
304 BoardModel.findSquare(b, 'B', y).getPiece() == null
306 BoardModel.findSquare(b, 'D', y).getPiece() == null
308 cRow.getPiece() == null
310 cRow.setAccessible(true);