/*******************
 * MineSweeper Board Generator
 * CSC 204 - Spring 2008
 * Mark Goadrich
 *******************/
public class MineSweeper {
    public static void main(String[] args) {
	
	// Gather data from user
	int M = Integer.parseInt(args[0]);
	int N = Integer.parseInt(args[1]);
	double p = Double.parseDouble(args[2]);
	
	// Fill in initial bomb matrix
	// This is larger than M and N because we can get rid of special
	// cases later with more space here
	int[][] bombs = new int[M+2][N+2];
	for (int i = 1; i < M + 1; i++) {
	    for (int j = 1; j < N + 1; j++) {
		if (Math.random() < p) {
		    bombs[i][j] = 1;
		}
	    }
	}

	// Print out bombs matrix, 1 is bomb, 0 is nothing
	System.out.println("Bomb Matrix:");
	for (int i = 1; i < M + 1; i++) {
	    for (int j = 1; j < N + 1; j++) {
		System.out.print(bombs[i][j]);
	    }
	    System.out.println();
	}
	
	// Print out finished Minesweeper Puzzle with numbers
	System.out.println("Finished Matrix:");
	for (int i = 1; i < M + 1; i++) {
	    for (int j = 1; j < N + 1; j++) {

		// if we found a bomb, print a *
		if (bombs[i][j] == 1) {
		    System.out.print("*");

		} else { // otherwise, we calculate the number of bombs
		         // in the surrounding 8 spaces
		    int count = 0;
		    for (int k = -1; k < 2; k++){
			for (int m = -1; m < 2; m++) {
			    count += bombs[i + k][j + m];
			}
		    }
		    
		    // if we found anything, print out that number
		    if (count != 0) {
			System.out.print(count);

		    } else { // otherwise, print nothing
			System.out.print(" ");
		    }
		}
	    }
	    System.out.println();
	}
    }
}

