1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/chess/src/main/java/org/apidesign/html/demo/chess/Rules.java Fri Jul 26 17:14:25 2013 +0200
1.3 @@ -0,0 +1,130 @@
1.4 +/**
1.5 + * The MIT License (MIT)
1.6 + *
1.7 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.8 + *
1.9 + * Permission is hereby granted, free of charge, to any person obtaining a copy
1.10 + * of this software and associated documentation files (the "Software"), to deal
1.11 + * in the Software without restriction, including without limitation the rights
1.12 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1.13 + * copies of the Software, and to permit persons to whom the Software is
1.14 + * furnished to do so, subject to the following conditions:
1.15 + *
1.16 + * The above copyright notice and this permission notice shall be included in
1.17 + * all copies or substantial portions of the Software.
1.18 + *
1.19 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1.20 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1.21 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1.22 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1.23 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1.24 + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1.25 + * THE SOFTWARE.
1.26 + */
1.27 +package org.apidesign.html.demo.chess;
1.28 +
1.29 +/** Common chess rules.
1.30 + *
1.31 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.32 + */
1.33 +class Rules {
1.34 + static void computeAccessible(Board b, Square s) {
1.35 + for (Row r : b.getRows()) {
1.36 + for (Square ts : r.getColumns()) {
1.37 + ts.setAccessible(false);
1.38 + }
1.39 + }
1.40 + if (s == null) {
1.41 + return;
1.42 + }
1.43 +
1.44 + switch (s.getPiece()) {
1.45 + case BISHOP:
1.46 + moveBishop(b, s);
1.47 + break;
1.48 + case KING:
1.49 + computeAccessible(b, s, 1, 1, 1);
1.50 + computeAccessible(b, s, 1, -1, 1);
1.51 + computeAccessible(b, s, -1, -1, 1);
1.52 + computeAccessible(b, s, -1, 1, 1);
1.53 + computeAccessible(b, s, 1, 0, 1);
1.54 + computeAccessible(b, s, 0, -1, 1);
1.55 + computeAccessible(b, s, 0, 1, 1);
1.56 + computeAccessible(b, s, -1, 0, 1);
1.57 + break;
1.58 + case ROCK:
1.59 + moveRock(b, s);
1.60 + break;
1.61 + case QUEEN:
1.62 + moveRock(b, s);
1.63 + moveBishop(b, s);
1.64 + break;
1.65 + case KNIGHT:
1.66 + computeAccessible(b, s, 2, 1, 1);
1.67 + computeAccessible(b, s, 2, -1, 1);
1.68 + computeAccessible(b, s, -2, -1, 1);
1.69 + computeAccessible(b, s, -2, 1, 1);
1.70 + computeAccessible(b, s, 1, 2, 1);
1.71 + computeAccessible(b, s, -1, 2, 1);
1.72 + computeAccessible(b, s, -1, -2, 1);
1.73 + computeAccessible(b, s, 1, -2, 1);
1.74 + break;
1.75 + case PAWN:
1.76 + pawns(b, s);
1.77 + break;
1.78 + }
1.79 + }
1.80 +
1.81 + private static void moveRock(Board b, Square s) {
1.82 + computeAccessible(b, s, 1, 0, 8);
1.83 + computeAccessible(b, s, 0, -1, 8);
1.84 + computeAccessible(b, s, -1, 0, 8);
1.85 + computeAccessible(b, s, 0, 1, 8);
1.86 + }
1.87 +
1.88 + private static void moveBishop(Board b, Square s) {
1.89 + computeAccessible(b, s, 1, 1, 8);
1.90 + computeAccessible(b, s, 1, -1, 8);
1.91 + computeAccessible(b, s, -1, -1, 8);
1.92 + computeAccessible(b, s, -1, 1, 8);
1.93 + }
1.94 +
1.95 + private static void computeAccessible(
1.96 + Board b, Square s, int dx, int dy,
1.97 + int limit
1.98 + ) {
1.99 + int x = s.getX();
1.100 + int y = s.getY();
1.101 +
1.102 + while (limit-- > 0) {
1.103 + x += dx;
1.104 + y += dy;
1.105 + Square next = BoardModel.findSquare(b, (char)x, y);
1.106 + if (next == null) {
1.107 + break;
1.108 + }
1.109 + if (next.getPieceColor() == s.getPieceColor()) {
1.110 + break;
1.111 + }
1.112 + next.setAccessible(true);
1.113 + if (next.getPieceColor() != null) {
1.114 + break;
1.115 + }
1.116 + }
1.117 + }
1.118 +
1.119 + private static void pawns(Board b, Square s) {
1.120 + final boolean white = s.getPieceColor() == BoardModel.ColorType.WHITE;
1.121 + int dy = white ? 1 : -1;
1.122 + Square step = BoardModel.findSquare(b, (char)s.getX(), s.getY() + dy);
1.123 + if (step != null && step.getPiece() == null) {
1.124 + step.setAccessible(true);
1.125 + if ((s.getY() == 2 && white) || (s.getY() == 7 && !white)) {
1.126 + Square nextSTep = BoardModel.findSquare(b, (char)s.getX(), step.getY() + dy);
1.127 + if (nextSTep != null && step.getPiece() == null) {
1.128 + nextSTep.setAccessible(true);
1.129 + }
1.130 + }
1.131 + }
1.132 + }
1.133 +}