/** * This program counts how many "blobs" are in a 2-D array. * The program tests the recursive function blob_check, which examines a square to * see how many squares are filled around it. A filename is passed in, then blobCheck * is performed on that file. the result is printed on the screen. Laura Toma csci107 */ public class BlobCheck { //constants to set the values for which the grid is filled or empty private static final int ZERO = 0; private static final int FILLED = 1; //array to hold the grid of numbers static int grid[][] = { {ZERO, FILLED, ZERO, FILLED,FILLED}, {FILLED, ZERO, ZERO, ZERO, FILLED}, {ZERO, ZERO, FILLED, ZERO, FILLED}, {ZERO, FILLED, FILLED, ZERO, ZERO}, {FILLED, FILLED, ZERO, ZERO, ZERO} }; static int first = 5, second = 5; //dimensions of the grid public static void main(String args[]) { //first print the grid printGrid(grid); //create a new grid int gridCopy[][] = new int[first][second]; //loop to call checkThisBlob for each possible position in the grid for (int x = 0; x < first; x++) { for (int y = 0; y < second; y++) { //copies the grid so that when the grid is emptied in //checkThisBlob, the original is not lost for (int b = 0; b < first; b++) for (int c = 0; c < second; c++) gridCopy[b][c] = grid[b][c]; //gets the value of checkThisBlob int blobSize = checkThisBlob(gridCopy, x, y); //prints out the result of each trial of blob_check System.out.println("The point " + x + ", " + y + " = " + blobSize); } //end of for y } //end of for x } //end of main /* prints the 2D array given as argument */ static void printGrid(int [][] arr) { System.out.println("The grid: "); for (int x = 0; x < first; x++) { for (int y = 0; y < second; y++) { System.out.print(arr[x][y] + " "); } System.out.println(); } System.out.println(); } //end of printGrid /** recursive function that checks to see if an element of a grid * is filled, and if it is, it sets the value of the function * to 1 plus the value of each of the squares around it. The * value for an unfilled square is 0. The function is called * recursively because each square that is filled must look at * each square around it to see if it is filled. The base * cases are the unfilled square, a square with something other * than the filled value, or array out of bounds. returns the * value of the square checked (the number of squares around it * filled) * * @parameter: arr[][] A copy of the original array, where we * count blobs. x, y The coordinates that we're looking for * blobs in * @return: the size of the blob */ static int checkThisBlob(int arr[][], int x, int y){ return 0; //just a dummy return, to make it compile } //end of checkThisBlob } //end of class