package bin;public class Game { int[][] board; // game board int width; // board width int height; // board height boolean[][] shown; // keeps track of the squares that have been flipped boolean[][] flagged; // keeps track of the squares that have been flagged int flags; // keeps track of the number of squares that have been flagged int num_mines; // number of mines in the game final int MINE = 9; // number denoting a mine /** * Creates a new game board. * * I was lazy so I haven't created a game loop yet, * although, all the logic is done so it shouldn't be hard at all. * (Plus, it should be in another class calling this class). * * I could create a GUI but I'll save that for another time. * * Right now it just demos what the reveal algorithm can do. * Run it a few times to see. MAKE ME PROUD. * * @param width * @param height * @param num_mines */ public Game(int width, int height, int num_mines) { // set the height and width this.width = width; this.height = height; // set flags this.flags = 0; // set mines this.num_mines = num_mines; // instatiate the board board = new int[height][width]; // instantiate the shown board shown = new boolean[height][width]; // instantiate the flagged board flagged = new boolean[height][width]; // add mines and numbers to the board randomizeBoard(); // display the board displayBoard(); // space System.out.println(); // display the board if (!reveal(4, 4)) System.out.println("Game Over!"); else displayBoard(); // space System.out.println(); // display the actual board to make sure numbers are right for (int i = 0; i < height; i++) { for (int k = 0; k < width; k++) { System.out.print(board[i][k] + " "); } System.out.println(); } } /** * Displays the ASCII version of the board in the console. * * - = square that hasn't been shown yet * # = square that's been shown and holds a number * 1-8 = has # mine(s) around it * 9 = mine * o = square that's been shown and is empty */ public void displayBoard() { for (int i = 0; i < height; i++) { for (int k = 0; k < width; k++) { if (shown[i][k] && !flagged[i][k]) { if (board[i][k] != 0) System.out.print(board[i][k] + " "); else System.out.print("o "); } else System.out.print("- "); } System.out.println(); } } /** * Places or removes a flag on a square. * * @param row * @param col */ public void flag(int row, int col) { if (flagged[row][col]) { flagged[row][col] = false; flags--; } else { flagged[row][col] = true; flags++; } } /** * Searches the perimeter of a specified square and counts * how many mines surround it. * * @param row * @param col * @return number of mines surrounding the specified square */ public int numbers(int row, int col) { // keep track of mines int mine_count = 0; // search the perimeter of the square for (int i = -1; i < 2; i++) for (int k = -1; k < 2; k++) // check if out of bounds if ((row + i != -1) && (col + k != -1) && (row + i != height) && (col + k != width)) // if not, check if it's a mine if (board[row+i][col+k] == MINE) mine_count++; // return number of mines around bRow, bCol return mine_count; } /** * Places mines on the board in random locations. * The amount of mines equals the the size of the NUM_MINES variable. * * Places numbers on the board denoting how many mines * are around a certain square on the board. */ public void randomizeBoard() { // coordinate variables and mine count int x = 0, y = 0, mine_count = 0; // add the mines to the board while (mine_count < num_mines) { // choose a random place on the map x = (int)(Math.random() * height); y = (int)(Math.random() * width); // if it's taken, continue loop (not necessary, clarity purposes) if (board[x][y] == MINE) continue; // if not, place a mine and increment mine count else { mine_count++; board[x][y] = MINE; } } // add the numbers to the board for (int i = 0; i < height; i++) for (int k = 0; k < width; k++) if (board[i][k] != MINE) board[i][k] = numbers(i, k); } /** * Reveals spots around the specified square. * * If the specified square is already shown or is flagged, exit. * * If the specified square is a number, only reveal the numbered square. * * If the specified square is empty, reveal the squares * arond it that are not bomb until it reaches a square that * has a number on it. * * If the specified square is a bomb, game over. * * @param row * @param col * * @return whether the revealed square is a mine or not */ public boolean reveal(int row, int col) { // if specified square has already been shown, leave if (!shown[row][col] && !flagged[row][col]) { // if the specified square is a bomb, game over if (board[row][col] == MINE) return false; else { // set current square to true shown[row][col] = true; // if the specified square is a number, reveal it if (board[row][col] > 0 && board[row][col] < 9) shown[row][col] = true; // if the specified square is empty, reveal around it else { // search the perimeter of the square for (int i = -1; i < 2; i++) for (int k = -1; k < 2; k++) // check if out of bounds, otherwise reveal around it if ((row + i != -1) && (col + k != -1) && (row + i != height) && (col + k != width)) reveal(row+i, col+k); } } } return true; } /** * Starts the game. * * @param args */ public static void main(String args[]) { new Game(16, 8, 16); }}/************ DEMO OUTPUT ***************/Output of the user revealing a square that is empty.- - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - 2 1 1 1 - - - - - - - - - -- 2 1 o o 1 - - - - - - - - - -- 1 o o o 1 - - - - - - - - - -1 1 o o o 1 1 2 - - - - - - - -o o 1 1 1 o o 2 - - - - - - - -o o 1 - 1 o o 2 - - - - - - - -0 1 1 1 1 1 1 0 1 9 1 0 0 0 0 01 2 9 1 1 9 1 0 1 1 1 0 0 0 0 01 9 2 1 1 1 1 0 0 0 1 1 1 0 0 02 2 1 0 0 1 1 1 0 0 2 9 2 0 1 19 1 0 0 0 1 9 1 0 0 2 9 2 0 1 91 1 0 0 0 1 1 2 1 1 1 2 3 2 2 10 0 1 1 1 0 0 2 9 2 0 2 9 9 2 10 0 1 9 1 0 0 2 9 2 0 2 9 4 9 1Output of the user revealing a square that is a mine.- - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - -Game Over!0 0 0 0 0 1 2 2 1 1 9 1 0 0 1 10 0 0 0 0 2 9 9 1 1 1 1 0 0 1 90 0 0 0 0 2 9 4 3 2 1 0 0 0 1 10 1 1 2 1 2 1 2 9 9 2 1 0 1 1 10 1 9 2 9 1 0 1 2 3 9 1 0 1 9 10 1 1 2 1 1 0 0 0 1 1 1 1 2 3 20 0 0 0 0 1 2 2 1 0 1 1 2 9 2 90 0 0 0 0 1 9 9 1 0 1 9 2 1 2 1