import java.util.Scanner;
import java.io.File;

/**
 * Class the models a 2D-gel, including a method for identifying blobs and their
 * sizes (using recursive backtracking).
 *   @author Dave Reed
 *   @version 3/10/17
 */
public class Gel {
    private char[][] grid;
    
    /**
     * Constructs aGel object from a data file.
     *   @param filename the file containing the grid in ASCII format.
     */
    public Gel(String filename) throws java.io.FileNotFoundException {
        Scanner infile = new Scanner(new File(filename));
        int rows = infile.nextInt();
        int cols = infile.nextInt();
        this.grid = new char[rows][cols];

        for (int r = 0; r < rows; r++) {
            String line = infile.next();
            this.grid[r] = line.toCharArray();
        }
        infile.close();
    }
    
    /**
     * Identifies and displays the locations and sizes of blobs in the Gel.
     */
    public void findBlobs() {
      for (int row = 0; row < this.grid.length; row++) {
          for (int col = 0; col < this.grid[0].length; col++) {
              if (this.grid[row][col] == '*') {
                  System.out.println("Blob at [" + row + "][" +
                                     col + "] : size " + 
                                     this.blobSize(row, col));
              }
          }
      }

      for (int row = 0; row < this.grid.length; row++) {
          for (int col = 0; col < this.grid[0].length; col++) {
              if (this.grid[row][col] == 'O') {
                  this.grid[row][col] = '*';
              }
          }
      }
    }

    /**
     * Private helper method that searches for a blob at the specified location.
     *   @param row the row of the spot to be tested
     *   @param col the column of the spot to be tested
     *   @return the size of the blob that contains this row/column spot
     */
    private int blobSize(int row, int col) {
        if (row < 0 || row >= this.grid.length ||
            col < 0 || col >= this.grid[0].length || 
            this.grid[row][col] != '*') {
            return 0;
        }
        else {
            this.grid[row][col] = 'O';    
            return 1 + this.blobSize(row-1, col-1)
                     + this.blobSize(row-1,   col)
                     + this.blobSize(row-1, col+1)
                     + this.blobSize(  row, col-1)
                     + this.blobSize(  row, col+1)
                     + this.blobSize(row+1, col-1)
                     + this.blobSize(row+1,   col)
                     + this.blobSize(row+1, col+1);
        }
    }

    /**
     * Converts the Gel into a String made up of * and -. 
     *   @return the String representation of the Gel
     */
    public String toString() {
        String strVal = "";
        for (int r = 0; r < this.grid.length; r++) {
            for (int c = 0; c < this.grid[0].length; c++) {
                strVal += this.grid[r][c];
            }
            strVal += "\n";
        }
        return strVal;
    }
    
    /////////////////////////////////////////////////////////////
    
    public static void main(String[] args) throws java.io.FileNotFoundException {
        Gel g = new Gel("gel1.txt");
        
        System.out.println(g);
        
        g.findBlobs();
    }
}
