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
     1 /**
     2  * The MIT License (MIT)
     3  *
     4  * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     5  *
     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:
    12  *
    13  * The above copyright notice and this permission notice shall be included in
    14  * all copies or substantial portions of the Software.
    15  *
    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
    22  * THE SOFTWARE.
    23  */
    24 package org.apidesign.html.demo.chess;
    25 
    26 /** Common chess rules.
    27  *
    28  * @author Jaroslav Tulach <jtulach@netbeans.org>
    29  */
    30 class Rules {
    31     static void computeAccessible(Board b, Square s) {
    32         for (Row r : b.getRows()) {
    33             for (Square ts : r.getColumns()) {
    34                 ts.setAccessible(false);
    35             }
    36         }
    37         if (s == null) {
    38             return;
    39         }
    40         
    41         switch (s.getPiece()) {
    42             case BISHOP: 
    43                 moveBishop(b, s);
    44                 break;
    45             case KING:
    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);
    54                 break;
    55             case ROCK:
    56                 moveRock(b, s);
    57                 break;
    58             case QUEEN:
    59                 moveRock(b, s);
    60                 moveBishop(b, s);
    61                 break;
    62             case KNIGHT:
    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);
    71                 break;
    72             case PAWN:
    73                 pawns(b, s);
    74                 break;
    75         }
    76     }
    77 
    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);
    83     }
    84 
    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);
    90     }
    91     
    92     private static void computeAccessible(
    93         Board b, Square s, int dx, int dy,
    94         int limit
    95     ) {
    96         int x = s.getX();
    97         int y = s.getY();
    98         
    99         while (limit-- > 0) {
   100             x += dx;
   101             y += dy;
   102             Square next = BoardModel.findSquare(b, (char)x, y);
   103             if (next == null) {
   104                 break;
   105             }
   106             if (next.getPieceColor() == s.getPieceColor()) {
   107                 break;
   108             }
   109             next.setAccessible(true);
   110             if (next.getPieceColor() != null) {
   111                 break;
   112             }
   113         }
   114     }
   115     
   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);
   126                 }
   127             }
   128         }
   129     }
   130 }