コード例 #1
0
ファイル: Board.java プロジェクト: mnylen/johtek
  private void addValidMoves(
      ChessPieceMoveSet moveSet,
      ChessPiece pc,
      ChessPieceColor opponentColor,
      ArrayList<Byte> to,
      boolean breakIfNotAdded) {
    for (byte moveTo : moveSet) {
      Square sq = this.get(moveTo);
      if (sq.isEmpty()) {
        to.add(moveTo);
      } else if (sq.getPiece().getColor() == opponentColor) {
        /*
           pc is attacking the piece in this square
        */

        ChessPiece attacked = sq.getPiece();
        attacked.setAttackedValue(attacked.getAttackedValue() + pc.getActionValue());

      } else if (sq.getPiece().getColor() != opponentColor) {
        /*
           pc is protecting the same colored piece in this square
        */

        ChessPiece defended = sq.getPiece();
        defended.setDefendedValue(defended.getDefendedValue() + pc.getActionValue());
      } else {
        if (breakIfNotAdded) {
          break;
        }
      }
    }
  }
コード例 #2
0
ファイル: Board.java プロジェクト: mnylen/johtek
  public Board(int[][] boardValues) {
    this.squares = new Square[ROWS * COLUMNS];

    byte piecesOnBoard = 0;
    byte whitePiecesOnBoard = 0;
    byte blackPiecesOnBoard = 0;

    for (byte x = 0; x < COLUMNS; x++) {
      for (byte y = 0; y < ROWS; y++) {
        byte squareValue = (byte) boardValues[x][y];
        byte index = index(x, y);
        Square sq = this.squares[index] = new Square(squareValue);

        if (!(sq.isEmpty())) {
          piecesOnBoard++;

          if (sq.getPiece().getColor() == ChessPieceColor.WHITE) {
            whitePiecesOnBoard++;
          } else {
            blackPiecesOnBoard++;
          }
        }
      }
    }

    this.piecesOnBoard = piecesOnBoard;
    this.whitePiecesOnBoard = whitePiecesOnBoard;
    this.blackPiecesOnBoard = blackPiecesOnBoard;
  }
コード例 #3
0
ファイル: Board.java プロジェクト: mnylen/johtek
  public ArrayList<Byte> validMoves(int index) {
    Square square = this.get(index);
    if (square.isEmpty()) {
      return new ArrayList<Byte>();
    }

    ChessPiece pc = square.getPiece();
    ChessPieceColor opponentColor =
        (pc.getColor() == ChessPieceColor.WHITE) ? ChessPieceColor.BLACK : ChessPieceColor.WHITE;
    ArrayList<Byte> validMoves = new ArrayList<Byte>(24);

    switch (pc.getType()) {
      case KNIGHT:
        addValidMoves(ChessPieceMoves.KNIGHT_MOVES[index], pc, opponentColor, validMoves, false);
        break;

      case KING:
        addValidMoves(ChessPieceMoves.KING_MOVES[index], pc, opponentColor, validMoves, false);
        break;

      case PAWN:
        if (pc.getColor() == ChessPieceColor.WHITE) {
          addValidMoves(
              ChessPieceMoves.WHITE_PAWN_MOVES[index], pc, opponentColor, validMoves, false);
        } else {
          addValidMoves(
              ChessPieceMoves.BLACK_PAWN_MOVES[index], pc, opponentColor, validMoves, false);
        }
        break;

      case ROOK:
        addValidMoves(ChessPieceMoves.ROOK_MOVES_DOWN[index], pc, opponentColor, validMoves, true);
        addValidMoves(ChessPieceMoves.ROOK_MOVES_LEFT[index], pc, opponentColor, validMoves, true);
        addValidMoves(ChessPieceMoves.ROOK_MOVES_RIGHT[index], pc, opponentColor, validMoves, true);
        addValidMoves(ChessPieceMoves.ROOK_MOVES_UP[index], pc, opponentColor, validMoves, true);
        break;

      case QUEEN:
        addValidMoves(ChessPieceMoves.QUEEN_MOVES_DOWN[index], pc, opponentColor, validMoves, true);
        addValidMoves(ChessPieceMoves.QUEEN_MOVES_LEFT[index], pc, opponentColor, validMoves, true);
        addValidMoves(
            ChessPieceMoves.QUEEN_MOVES_RIGHT[index], pc, opponentColor, validMoves, true);
        addValidMoves(ChessPieceMoves.QUEEN_MOVES_UP[index], pc, opponentColor, validMoves, true);
        addValidMoves(
            ChessPieceMoves.QUEEN_MOVES_DOWN_LEFT[index], pc, opponentColor, validMoves, true);
        addValidMoves(
            ChessPieceMoves.QUEEN_MOVES_DOWN_RIGHT[index], pc, opponentColor, validMoves, true);
        addValidMoves(
            ChessPieceMoves.QUEEN_MOVES_UP_LEFT[index], pc, opponentColor, validMoves, true);
        addValidMoves(
            ChessPieceMoves.QUEEN_MOVES_UP_RIGHT[index], pc, opponentColor, validMoves, true);
        break;
    }

    return validMoves;
  }
コード例 #4
0
 /**
  * Returns the list of pieces that are attacking the king of <code>color</code> or
  * <code>null</null> if king is not in check.
  *
  * @param color King's color.
  * @param toKing King's location on the board.
  * @return The list of pieces attacking king or <code>null</code> if not in check.
  */
 public List<Square> kingAttackers(Color color, Point toKing) {
   List<Square> result = null;
   final Color opponent = color.getOpponentColor();
   Iterator<Square> iter = getIterator(opponent);
   while (iter.hasNext()) {
     final Square square = iter.next();
     if (square.getPiece().isLegalMove(square.getPoint(), toKing, this)) {
       if (result == null) {
         result = new ArrayList<>();
       }
       result.add(square);
     }
   }
   return result;
 }
コード例 #5
0
ファイル: Piece.java プロジェクト: routhcr/chess
  /**
   * Add a legal destination to the ArrayList.
   *
   * @param dest The Square to be added to the List
   * @return If the Square was successfully added to the ArrayList
   */
  public boolean addLegalDest(Square dest) {
    // If the Square is not habitable, don't add it, but return true so we
    // can move past it.
    if (!dest.isHabitable()) return true;

    if (dest.getRow() == m_curSquare.getRow() && dest.getCol() == m_curSquare.getCol())
      return false;

    if (dest.isOccupied() && dest.getPiece().isBlack() == isBlack()) {
      // If the destination has a Piece from the same team, we must be
      // guarding that Piece
      getGuardSquares().add(dest);
      return false;
    }

    // Otherwise, add the Piece and return true.
    getLegalDests().add(dest);
    return true;
  }
コード例 #6
0
ファイル: Piece.java プロジェクト: routhcr/chess
  /**
   * Generate the ArrayList of legal destinations for this Piece
   *
   * @param board The Board on which to look for legal destinations
   * @return The number of legal destinations for this Piece
   */
  public int genLegalDests(Board board) {
    // Clear both ArrayLists
    getLegalDests().clear();
    getGuardSquares().clear();
    setPinnedBy(null);
    // Boolean to tell when we are done generating destinations

    /*
     * Special genLegalDests for Pawns, to incorporate enPassant, special
     * initial movement, and diagonal capturing.
     */
    if (m_name.equals("Pawn")) {
      Square dest = null;
      int dir, row, col;
      setPinnedBy(null);
      if (m_captured) return 0;

      dir = (isBlack()) ? -1 : 1;

      // Take one step forward
      if (board.isRowValid(m_curSquare.getRow() + dir)) {
        dest = board.getSquare(m_curSquare.getRow() + dir, m_curSquare.getCol());
        if (!dest.isOccupied() && !getLegalDests().contains(dest)) addLegalDest(dest);
      }

      // Take an opposing piece
      if (board.isRowValid(row = (m_curSquare.getRow() + dir))) {
        col = m_curSquare.getCol();

        // if valid row
        // and the square is occupied (by the other team)
        // or it's not my move (so I'm threatening the square)
        if (board.isColValid((col + 1))
            && ((board.getSquare(row, col + 1).isOccupied()
                && isBlack() != board.getSquare(row, col + 1).getPiece().isBlack()))) {
          addLegalDest(board.getSquare(row, col + 1));
        }
        if (board.isColValid((col - 1))
            && ((board.getSquare(row, col - 1).isOccupied()
                && isBlack() != board.getSquare(row, col - 1).getPiece().isBlack()))) {
          addLegalDest(board.getSquare(row, col - 1));
        }
      }

      // two step
      if (getMoveCount() == 0 && board.isRowValid((m_curSquare.getRow() + (2 * dir)))) {
        dest = board.getSquare((m_curSquare.getRow() + (2 * dir)), m_curSquare.getCol());
        if (!dest.isOccupied()
            && !board.getSquare((m_curSquare.getRow() + dir), m_curSquare.getCol()).isOccupied()
            && !getLegalDests().contains(dest)) {
          addLegalDest(dest);
        }
      }

      if (board.getGame().isClassicChess()) {
        // enPassant
        if (isBlack() == board.isBlackTurn()
            && ((!isBlack() && m_curSquare.getRow() == 5)
                || (isBlack() && m_curSquare.getRow() == 4))) {
          col = m_curSquare.getCol();
          row = isBlack() ? m_curSquare.getRow() - 1 : m_curSquare.getRow() + 1;
          if (board.isColValid(col + 1) && board.getEnpassantCol() == (col + 1))
            addLegalDest(board.getSquare(row, (col + 1)));

          if (board.isColValid(col - 1) && board.getEnpassantCol() == (col - 1))
            addLegalDest(board.getSquare(row, (col - 1)));
        }
      }
      return getLegalDests().size();
    }

    boolean done = false;
    Square dest;
    boolean wraparound = board.isWrapAround();
    /*
     * East
     */
    if (m_movements.containsKey('E')) {
      int northMax = m_movements.get('E') + m_curSquare.getCol();
      if (northMax > board.getMaxCol() || m_movements.get('E') == -1) {
        if (!wraparound) northMax = board.getMaxCol();
      }
      for (int c = m_curSquare.getCol() + 1;
          ((m_movements.get('E') == -1 && wraparound) ? true : c <= northMax) && !done;
          c++) {
        int j = c;
        if (wraparound) {
          if (j > board.getMaxCol()) j = j % board.getMaxCol();
        }

        if (j == 0) break;

        dest = board.getSquare(m_curSquare.getRow(), j);
        done = !addLegalDest(dest);
        done =
            m_isLeaper
                ? false
                : (done
                    || (dest.isOccupied()
                        && !(board.isBlackTurn() != isBlack()
                            && dest.getPiece()
                                .equals(board.getGame().getOtherObjectivePiece(isBlack())))));
      }
    }
    done = false;
    /*
     * West
     */
    if (m_movements.containsKey('W')) {
      int southMax = m_curSquare.getCol() - m_movements.get('W');
      if (southMax < 1 || m_movements.get('W') == -1) {
        if (!wraparound) southMax = 1;
      }
      for (int c = m_curSquare.getCol() - 1;
          ((m_movements.get('W') == -1 && wraparound) ? true : c >= southMax) && !done;
          c--) {
        int j = c;
        if (wraparound) {
          if (j < 1) j = board.getMaxCol() + j;
        }

        dest = board.getSquare(m_curSquare.getRow(), j);
        done = !addLegalDest(dest);
        done =
            m_isLeaper
                ? false
                : (done
                    || (dest.isOccupied()
                        && !(board.isBlackTurn() != isBlack()
                            && dest.getPiece()
                                .equals(board.getGame().getOtherObjectivePiece(isBlack())))));
      }
    }
    done = false;
    /*
     * North
     */
    if (m_movements.containsKey('N')) {
      int eastMax = m_movements.get('N') + m_curSquare.getRow();

      if (eastMax >= board.getMaxRow() || m_movements.get('N') == -1) eastMax = board.getMaxRow();

      for (int r = m_curSquare.getRow() + 1; (r <= eastMax) && !done; r++) {
        int j = r;
        dest = board.getSquare(j, m_curSquare.getCol());
        done = !addLegalDest(dest);
        done =
            m_isLeaper
                ? false
                : (done
                    || (dest.isOccupied()
                        && !(board.isBlackTurn() != isBlack()
                            && dest.getPiece()
                                .equals(board.getGame().getOtherObjectivePiece(isBlack())))));
      }
    }
    done = false;
    /*
     * South
     */
    if (m_movements.containsKey('S')) {
      int westMax = m_curSquare.getRow() - m_movements.get('S');

      if (westMax < 1 || m_movements.get('S') == -1) westMax = 1;

      for (int r = m_curSquare.getRow() - 1; (r >= westMax) && !done; r--) {
        int j = r;
        dest = board.getSquare(j, m_curSquare.getCol());
        done = !addLegalDest(dest);
        done =
            m_isLeaper
                ? false
                : (done
                    || (dest.isOccupied()
                        && !(board.isBlackTurn() != isBlack()
                            && dest.getPiece()
                                .equals(board.getGame().getOtherObjectivePiece(isBlack())))));
      }
    }
    /*
     * NorthEast
     */
    done = false;
    if (m_movements.containsKey('R')) {
      int neMax =
          ((m_curSquare.getRow() >= m_curSquare.getCol())
                  ? m_curSquare.getRow()
                  : m_curSquare.getCol())
              + m_movements.get('R');

      if (neMax >= board.getMaxCol() || m_movements.get('R') == -1) neMax = board.getMaxCol();
      if (neMax >= board.getMaxRow() || m_movements.get('R') == -1) neMax = board.getMaxRow();

      for (int r = m_curSquare.getRow() + 1, c = m_curSquare.getCol() + 1;
          r <= neMax && c <= neMax && !done;
          r++, c++) {
        dest = board.getSquare(r, c);
        done = !addLegalDest(dest);
        done =
            m_isLeaper
                ? false
                : (done
                    || (dest.isOccupied()
                        && !(board.isBlackTurn() != isBlack()
                            && dest.getPiece()
                                .equals(board.getGame().getOtherObjectivePiece(isBlack())))));
      }
    }

    /*
     * SouthEast
     */
    done = false;
    if (m_movements.containsKey('r')) {
      int eastMax = m_curSquare.getCol() + m_movements.get('r');

      if (eastMax >= board.getMaxCol() || m_movements.get('r') == -1) eastMax = board.getMaxCol();

      int southMin = m_curSquare.getRow() - m_movements.get('r');

      if (southMin <= 1 || m_movements.get('R') == -1) southMin = 1;

      for (int r = m_curSquare.getRow() - 1, c = m_curSquare.getCol() + 1;
          r >= southMin && c <= eastMax && !done;
          r--, c++) {
        dest = board.getSquare(r, c);
        done = !addLegalDest(dest);
        done =
            m_isLeaper
                ? false
                : (done
                    || (dest.isOccupied()
                        && !(board.isBlackTurn() != isBlack()
                            && dest.getPiece()
                                .equals(board.getGame().getOtherObjectivePiece(isBlack())))));
      }
    }
    /*
     * NorthWest
     */
    done = false;
    if (m_movements.containsKey('L')) {
      int westMin = m_curSquare.getCol() - m_movements.get('L');
      if (westMin <= 1 || m_movements.get('L') == -1) westMin = 1;

      int NorthMax = m_curSquare.getRow() + m_movements.get('L');
      if (NorthMax >= board.getMaxRow() || m_movements.get('L') == -1) NorthMax = board.getMaxRow();

      for (int r = m_curSquare.getRow() + 1, c = m_curSquare.getCol() - 1;
          r <= NorthMax && c >= westMin && !done;
          r++, c--) {
        dest = board.getSquare(r, c);
        done = !addLegalDest(dest);
        done =
            m_isLeaper
                ? false
                : (done
                    || (dest.isOccupied()
                        && !(board.isBlackTurn() != isBlack()
                            && dest.getPiece()
                                .equals(board.getGame().getOtherObjectivePiece(isBlack())))));
      }
    }
    /*
     * SouthWest
     */
    done = false;
    if (m_movements.containsKey('l')) {
      int westMin = m_curSquare.getCol() - m_movements.get('l');
      if (westMin <= 1 || m_movements.get('l') == -1) westMin = 1;

      int southMin = m_curSquare.getRow() - m_movements.get('l');
      if (southMin <= 1 || m_movements.get('l') == -1) southMin = 1;

      for (int r = m_curSquare.getRow() - 1, c = m_curSquare.getCol() - 1;
          r >= southMin && c >= westMin && !done;
          r--, c--) {
        dest = board.getSquare(r, c);
        done = !addLegalDest(dest);
        done =
            m_isLeaper
                ? false
                : (done
                    || (dest.isOccupied()
                        && !(board.isBlackTurn() != isBlack()
                            && dest.getPiece()
                                .equals(board.getGame().getOtherObjectivePiece(isBlack())))));
      }
    }

    /*
     * Knight / Leaper Movements
     *
     *
     * Store of Knight Movements are as followed:
     *
     * A Piece can move x File by y Rank squares at a time.
     *
     * IE: A knight can move 1 by 2 or 2 by 1, but not 1 by 1 or 2 by 2
     */
    if (m_movements.containsKey('x')) {
      int f, r;
      int Rank = m_movements.get('x');
      int File = m_movements.get('y');
      f = (m_curSquare.getRow() + File);
      r = (m_curSquare.getCol() + Rank);
      if (wraparound) {
        if (r > board.getMaxCol() + 1) r = r % board.getMaxCol();
      }

      if (board.isRowValid(f) && board.isColValid(r)) addLegalDest(board.getSquare(f, r));

      // two o'clock
      f = (m_curSquare.getRow() + Rank);
      r = (m_curSquare.getCol() + File);
      if (wraparound) {
        if (r > board.getMaxCol() + 1) r = r % board.getMaxCol();
      }

      if (board.isRowValid(f) && board.isColValid(r)) addLegalDest(board.getSquare(f, r));

      // four o'clock
      f = (m_curSquare.getRow() + File);
      r = (m_curSquare.getCol() - Rank);
      if (wraparound) {
        if (r < 1) r = board.getMaxCol() + r;
      }

      if (board.isRowValid(f) && board.isColValid(r)) addLegalDest(board.getSquare(f, r));

      // five o'clock
      f = (m_curSquare.getRow() + Rank);
      r = (m_curSquare.getCol() - File);
      if (wraparound) {
        if (r < 1) r = board.getMaxCol() + r;
      }

      if (board.isRowValid(f) && board.isColValid(r)) addLegalDest(board.getSquare(f, r));

      // seven o'clock
      f = (m_curSquare.getRow() - File);
      r = (m_curSquare.getCol() - Rank);
      if (wraparound) {
        if (r < 1) r = board.getMaxCol() + r;
      }

      if (board.isRowValid(f) && board.isColValid(r)) addLegalDest(board.getSquare(f, r));

      // eight o'clock
      f = (m_curSquare.getRow() - Rank);
      r = (m_curSquare.getCol() - File);
      if (wraparound) {
        if (r < 1) r = board.getMaxCol() + r;
      }

      if (board.isRowValid(f) && board.isColValid(r)) addLegalDest(board.getSquare(f, r));

      // ten o'clock
      f = (m_curSquare.getRow() - File);
      r = (m_curSquare.getCol() + Rank);
      if (wraparound) {
        if (r > board.getMaxCol() + 1) r = r % board.getMaxCol();
      }

      if (board.isRowValid(f) && board.isColValid(r)) addLegalDest(board.getSquare(f, r));

      // eleven o'clock
      f = (m_curSquare.getRow() - Rank);
      r = (m_curSquare.getCol() + File);
      if (wraparound) {
        if (r > board.getMaxCol() + 1) r = r % board.getMaxCol();
      }

      if (board.isRowValid(f) && board.isColValid(r)) addLegalDest(board.getSquare(f, r));
    }
    return getLegalDests().size();
  }