import java.util.Random; import java.util.Queue; import java.util.LinkedList; public class MapGrid extends Grid { protected int seed; protected int count; // variable to store number of continents char[][] continent; // 2d char array to store continent identifier for each cell (i,j) int[] centrality; // int array to store centrality for the capital of each continent int[][] capital; // 2d int array to store coordinates of the capital for each continent boolean[][] seen; // 2d boolean array which is used in method findCapitals() Queue q; // queue of moves used to enqueue neighbors of a cell (i,j) public MapGrid(int rows, int cols, int percentage, int seed) // Preconditions: rows and cols > 0 // 0 <= percentage <= 100 // // Instantiates a grid of size rows by cols, where locations are set to // indicate blob characters based on the percentage probability. { super(rows,cols,percentage); this.rows = rows; this.cols = cols; this.seed = seed; // seed to generate random numbers grid = new boolean [rows][cols]; // to generate random numbers int randInt; Random rand = new Random(seed); for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) { randInt = rand.nextInt(100); // random number 0 .. 99 if (randInt < percentage) grid[i][j] = true; else grid[i][j] = false; } } private void markBlob(int row, int col, int counter) // Mark position row, col as having been visited & also update each cell's continent identifier // Check and if appropriate mark locations above, below, left, // and right of that position. { visited[row][col] = true; continent[row][col]='a'; // updating each cell's continent identifier continent[row][col]+=counter; // (counter is the current continent integer ID) // check above if ((row - 1) >= 0) // if its on the grid if (grid[row - 1][col]) // and has a blob character if (!visited[row - 1][col]) // and has not been visited markBlob(row - 1, col,counter); // then mark it // check below if ((row + 1) < rows) // if its on the grid if (grid[row + 1][col]) // and has a blob character if (!visited[row + 1][col]) // and has not been visited markBlob(row + 1, col,counter); // then mark it // check left if ((col - 1) >= 0) // if its on the grid if (grid[row][col - 1]) // and has a blob character if (!visited[row][col - 1]) // and has not been visited markBlob(row, col - 1,counter); // then mark it // check right if ((col + 1) < cols) // if its on the grid if (grid[row][col + 1]) // and has a blob character if (!visited[row][col + 1]) // and has not been visited markBlob(row, col + 1, counter); // then mark it } public String continentMap() { // output the continent_map String map = ""; count = 0; visited = new boolean [rows][cols]; // true if location visited continent = new char[rows][cols]; // stores continent identifier for each blob // initialize visited & continent for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++){ visited[i][j] = false; continent[i][j] = '-'; } // mark continents and update continent_map for (int i = 0; i < rows; i++){ for (int j = 0; j < cols; j++){ if (grid[i][j]) if (!visited[i][j]){ markBlob(i, j,count); count++; } map+=continent[i][j]; } map+="\n"; } return map; } private void enqueueNeighbors(int r,int c,int d){ } public void findCapitals(){ } public String capitalMap(){ return "capital map not implemented here"; } public void continentTable(){ } }