Example #1
0
  public static ArrayList<Tile> getMovements(
      Board board, Color color, Type t, Location l, boolean attacksOnly) {
    ArrayList<Location> movements = null;
    int row = l.getRow();
    int col = l.getCol();
    switch (t) {
      case PAWN:
        movements = PawnMovement.getAdjacentMoves(board, color, row, col, attacksOnly);
        break;
      case ROOK:
        movements = HorizontalMovement.getHorizontalMoves(board, row, col, attacksOnly);
        break;
      case KNIGHT:
        movements = KnightMovement.getLMoves(row, col);
        break;
      case BISHOP:
        movements = DiagonalMovement.getDiagonalMoves(board, row, col, attacksOnly);
        break;
      case KING:
        movements = KingMovement.getKingMoves(board, row, col, attacksOnly);
        break;
      case QUEEN:
        movements = HorizontalMovement.getHorizontalMoves(board, row, col, attacksOnly);
        movements.addAll(DiagonalMovement.getDiagonalMoves(board, row, col, attacksOnly));
        break;
    }

    ArrayList<Tile> ret = filterMovements(board, l, movements, color);
    if (ret != null && !ret.isEmpty()) {
      Assert.assertEquals(ret.get(0), board.getSquare(ret.get(0).getRow(), ret.get(0).getColumn()));
    }

    return ret;
  }
Example #2
0
 private boolean pathIsObstructed(Board board, Location currentLocation, Location newLocation) {
   boolean lastValidMove = false;
   if (board.getSquare(newLocation).isOccupied()) {
     if (!newLocation.equals(currentLocation)) {
       lastValidMove = true;
     }
   }
   return lastValidMove;
 }
Example #3
0
  @Override
  public ArrayList<Move> getMoves(Board board) {
    ArrayList<Move> movesList = new ArrayList<Move>();

    Location newLocation = null;
    Location currentLocation = board.getLocation(this);
    char column = currentLocation.getCharacter();
    int row = currentLocation.getInteger();
    int boardLength = 8;

    // Down Left
    for (int i = 1; i < boardLength; i++) {
      newLocation = new Location((char) (column - i), (row - i));
      if (board.isValidSquare(newLocation)) {
        Piece p = board.getPiece(newLocation);
        if (p != null && p.getColor() != this.getColor()) {
          movesList.add(new Move(currentLocation, newLocation));
        } else if (p == null) {
          movesList.add(new Move(currentLocation, newLocation));
        }

        if (pathIsObstructed(board, currentLocation, newLocation)) break;
      }
    }

    // Down Right
    for (int i = 1; i < boardLength; i++) {
      newLocation = new Location((char) (column + i), (row - i));
      if (board.isValidSquare(newLocation)) {
        Piece p = board.getPiece(newLocation);
        if (p != null && p.getColor() != this.getColor()) {
          movesList.add(new Move(currentLocation, newLocation));
        } else if (p == null) {
          movesList.add(new Move(currentLocation, newLocation));
        }

        if (pathIsObstructed(board, currentLocation, newLocation)) break;
      }
    }

    // Up Left
    for (int i = 1; i < boardLength; i++) {
      newLocation = new Location((char) (column - i), (row + i));
      if (board.isValidSquare(newLocation)) {
        Piece p = board.getPiece(newLocation);
        if (p != null && p.getColor() != this.getColor()) {
          movesList.add(new Move(currentLocation, newLocation));
        } else if (p == null) {
          movesList.add(new Move(currentLocation, newLocation));
        }

        if (pathIsObstructed(board, currentLocation, newLocation)) break;
      }
    }

    // Up Right
    for (int i = 1; i < boardLength; i++) {
      newLocation = new Location((char) (column + i), (row + i));
      if (board.isValidSquare(newLocation)) {
        Piece p = board.getPiece(newLocation);
        if (p != null && p.getColor() != this.getColor()) {
          movesList.add(new Move(currentLocation, newLocation));
        } else if (p == null) {
          movesList.add(new Move(currentLocation, newLocation));
        }

        if (pathIsObstructed(board, currentLocation, newLocation)) break;
      }
    }

    return movesList;
  }