chess/src/main/java/org/apidesign/html/demo/chess/Rules.java
author Jaroslav Tulach <jtulach@netbeans.org>
Fri, 26 Jul 2013 17:14:25 +0200
branchchess
changeset 34 1ebd52ae8ccb
child 35 7ae0125d57b2
permissions -rw-r--r--
Show accessible squares when a figure is selected
jtulach@34
     1
/**
jtulach@34
     2
 * The MIT License (MIT)
jtulach@34
     3
 *
jtulach@34
     4
 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jtulach@34
     5
 *
jtulach@34
     6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
jtulach@34
     7
 * of this software and associated documentation files (the "Software"), to deal
jtulach@34
     8
 * in the Software without restriction, including without limitation the rights
jtulach@34
     9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
jtulach@34
    10
 * copies of the Software, and to permit persons to whom the Software is
jtulach@34
    11
 * furnished to do so, subject to the following conditions:
jtulach@34
    12
 *
jtulach@34
    13
 * The above copyright notice and this permission notice shall be included in
jtulach@34
    14
 * all copies or substantial portions of the Software.
jtulach@34
    15
 *
jtulach@34
    16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jtulach@34
    17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jtulach@34
    18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jtulach@34
    19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jtulach@34
    20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jtulach@34
    21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
jtulach@34
    22
 * THE SOFTWARE.
jtulach@34
    23
 */
jtulach@34
    24
package org.apidesign.html.demo.chess;
jtulach@34
    25
jtulach@34
    26
/** Common chess rules.
jtulach@34
    27
 *
jtulach@34
    28
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@34
    29
 */
jtulach@34
    30
class Rules {
jtulach@34
    31
    static void computeAccessible(Board b, Square s) {
jtulach@34
    32
        for (Row r : b.getRows()) {
jtulach@34
    33
            for (Square ts : r.getColumns()) {
jtulach@34
    34
                ts.setAccessible(false);
jtulach@34
    35
            }
jtulach@34
    36
        }
jtulach@34
    37
        if (s == null) {
jtulach@34
    38
            return;
jtulach@34
    39
        }
jtulach@34
    40
        
jtulach@34
    41
        switch (s.getPiece()) {
jtulach@34
    42
            case BISHOP: 
jtulach@34
    43
                moveBishop(b, s);
jtulach@34
    44
                break;
jtulach@34
    45
            case KING:
jtulach@34
    46
                computeAccessible(b, s, 1, 1, 1);
jtulach@34
    47
                computeAccessible(b, s, 1, -1, 1);
jtulach@34
    48
                computeAccessible(b, s, -1, -1, 1);
jtulach@34
    49
                computeAccessible(b, s, -1, 1, 1);
jtulach@34
    50
                computeAccessible(b, s, 1, 0, 1);
jtulach@34
    51
                computeAccessible(b, s, 0, -1, 1);
jtulach@34
    52
                computeAccessible(b, s, 0, 1, 1);
jtulach@34
    53
                computeAccessible(b, s, -1, 0, 1);
jtulach@34
    54
                break;
jtulach@34
    55
            case ROCK:
jtulach@34
    56
                moveRock(b, s);
jtulach@34
    57
                break;
jtulach@34
    58
            case QUEEN:
jtulach@34
    59
                moveRock(b, s);
jtulach@34
    60
                moveBishop(b, s);
jtulach@34
    61
                break;
jtulach@34
    62
            case KNIGHT:
jtulach@34
    63
                computeAccessible(b, s, 2, 1, 1);
jtulach@34
    64
                computeAccessible(b, s, 2, -1, 1);
jtulach@34
    65
                computeAccessible(b, s, -2, -1, 1);
jtulach@34
    66
                computeAccessible(b, s, -2, 1, 1);
jtulach@34
    67
                computeAccessible(b, s, 1, 2, 1);
jtulach@34
    68
                computeAccessible(b, s, -1, 2, 1);
jtulach@34
    69
                computeAccessible(b, s, -1, -2, 1);
jtulach@34
    70
                computeAccessible(b, s, 1, -2, 1);
jtulach@34
    71
                break;
jtulach@34
    72
            case PAWN:
jtulach@34
    73
                pawns(b, s);
jtulach@34
    74
                break;
jtulach@34
    75
        }
jtulach@34
    76
    }
jtulach@34
    77
jtulach@34
    78
    private static void moveRock(Board b, Square s) {
jtulach@34
    79
        computeAccessible(b, s, 1, 0, 8);
jtulach@34
    80
        computeAccessible(b, s, 0, -1, 8);
jtulach@34
    81
        computeAccessible(b, s, -1, 0, 8);
jtulach@34
    82
        computeAccessible(b, s, 0, 1, 8);
jtulach@34
    83
    }
jtulach@34
    84
jtulach@34
    85
    private static void moveBishop(Board b, Square s) {
jtulach@34
    86
        computeAccessible(b, s, 1, 1, 8);
jtulach@34
    87
        computeAccessible(b, s, 1, -1, 8);
jtulach@34
    88
        computeAccessible(b, s, -1, -1, 8);
jtulach@34
    89
        computeAccessible(b, s, -1, 1, 8);
jtulach@34
    90
    }
jtulach@34
    91
    
jtulach@34
    92
    private static void computeAccessible(
jtulach@34
    93
        Board b, Square s, int dx, int dy,
jtulach@34
    94
        int limit
jtulach@34
    95
    ) {
jtulach@34
    96
        int x = s.getX();
jtulach@34
    97
        int y = s.getY();
jtulach@34
    98
        
jtulach@34
    99
        while (limit-- > 0) {
jtulach@34
   100
            x += dx;
jtulach@34
   101
            y += dy;
jtulach@34
   102
            Square next = BoardModel.findSquare(b, (char)x, y);
jtulach@34
   103
            if (next == null) {
jtulach@34
   104
                break;
jtulach@34
   105
            }
jtulach@34
   106
            if (next.getPieceColor() == s.getPieceColor()) {
jtulach@34
   107
                break;
jtulach@34
   108
            }
jtulach@34
   109
            next.setAccessible(true);
jtulach@34
   110
            if (next.getPieceColor() != null) {
jtulach@34
   111
                break;
jtulach@34
   112
            }
jtulach@34
   113
        }
jtulach@34
   114
    }
jtulach@34
   115
    
jtulach@34
   116
    private static void pawns(Board b, Square s) {
jtulach@34
   117
        final boolean white = s.getPieceColor() == BoardModel.ColorType.WHITE;
jtulach@34
   118
        int dy = white ? 1 : -1;
jtulach@34
   119
        Square step = BoardModel.findSquare(b, (char)s.getX(), s.getY() + dy);
jtulach@34
   120
        if (step != null && step.getPiece() == null) {
jtulach@34
   121
            step.setAccessible(true);
jtulach@34
   122
            if ((s.getY() == 2 && white) || (s.getY() == 7 && !white)) {
jtulach@34
   123
                Square nextSTep = BoardModel.findSquare(b, (char)s.getX(), step.getY() + dy);
jtulach@34
   124
                if (nextSTep != null && step.getPiece() == null) {
jtulach@34
   125
                    nextSTep.setAccessible(true);
jtulach@34
   126
                }
jtulach@34
   127
            }
jtulach@34
   128
        }
jtulach@34
   129
    }
jtulach@34
   130
}