Esempio n. 1
0
 /**
  * Verify if position at (x,y) contains a square that is enclosed and has not been visited yet.
  *
  * @param board
  * @param current_location
  * @param pieces_in_enclosed_region
  * @param queue
  */
 public static void addToQueueIfEnclosed(
     Board board,
     int x,
     int y,
     ArrayList<Square> pieces_in_enclosed_region,
     ArrayList<Square> queue) {
   if (board.isInsideBoard(x, y)) {
     Square square_at_location = board.squares[x][y];
     Piece piece_at_location = board.squares[x][y].getPieceOnTop();
     if (piece_at_location.isEnclosed()
         && !(queue.contains(square_at_location)
             || pieces_in_enclosed_region.contains(square_at_location))) {
       queue.add(square_at_location);
     }
   }
 }
Esempio n. 2
0
  /**
   * Insert all enclosed regions in a list.
   *
   * @param board
   * @return ArrayList of ArrayLists, each one containing square objects representing the position
   *     of the enclosed pieces of the same region
   */
  public static ArrayList<ArrayList<Square>> createArrayOfEnclosedRegions(Board board) {

    ArrayList<ArrayList<Square>> list_of_enclosed_regions = new ArrayList<ArrayList<Square>>();
    ArrayList<Square> unchecked_enclosed_pieces = new ArrayList<Square>();

    // Insert all enclosed squares in an ArrayList.
    for (int i = 0; i < board.dimension; i++) {
      for (int j = 0; j < board.dimension; j++) {
        Piece piece = board.squares[i][j].getPieceOnTop();
        if (piece.isEnclosed()) {
          unchecked_enclosed_pieces.add(board.squares[i][j]);
        }
      }
    }

    while (!unchecked_enclosed_pieces.isEmpty()) {
      ArrayList<Square> pieces_in_enclosed_region = new ArrayList<Square>();
      ArrayList<Square> queue = new ArrayList<Square>();

      Square location = unchecked_enclosed_pieces.get(0);
      queue.add(location);

      while (!queue.isEmpty()) {
        Square current_location = queue.get(0);
        queue.remove(0);
        pieces_in_enclosed_region.add(current_location);
        unchecked_enclosed_pieces.remove(current_location);
        addToQueueIfEnclosed(
            board,
            current_location.getX(),
            current_location.getY() + 1,
            pieces_in_enclosed_region,
            queue);
        addToQueueIfEnclosed(
            board,
            current_location.getX(),
            current_location.getY() - 1,
            pieces_in_enclosed_region,
            queue);
        addToQueueIfEnclosed(
            board,
            current_location.getX() - 1,
            current_location.getY(),
            pieces_in_enclosed_region,
            queue);
        addToQueueIfEnclosed(
            board,
            current_location.getX() + 1,
            current_location.getY(),
            pieces_in_enclosed_region,
            queue);
        addToQueueIfEnclosed(
            board,
            current_location.getX() - 1,
            current_location.getY() - 1,
            pieces_in_enclosed_region,
            queue);
        addToQueueIfEnclosed(
            board,
            current_location.getX() - 1,
            current_location.getY() + 1,
            pieces_in_enclosed_region,
            queue);
        addToQueueIfEnclosed(
            board,
            current_location.getX() + 1,
            current_location.getY() + 1,
            pieces_in_enclosed_region,
            queue);
        addToQueueIfEnclosed(
            board,
            current_location.getX() + 1,
            current_location.getY() - 1,
            pieces_in_enclosed_region,
            queue);
      }

      list_of_enclosed_regions.add(pieces_in_enclosed_region);
    }
    return list_of_enclosed_regions;
  }