Minesweeper in Java

Minesweeper is a video game where you are trying to clear a "board" while avoiding the hidden traps. There are individual squares which serve as these traps; all of the other squares are defined by the number of traps adjacent to that square. This means that a non-trap square can have a maximum value of eight (although this is highly unlikely). (Source). Here is an example of minesweeper wit a 2D GUI (Graphical User Interface): http://minesweeperonline.com/

This implementation utilizes 2D arrays but does not include a graphical user interface. For this java code, the "9" represents a trap or bomb while -1 represents a spot on the board which isn't visible. Therefore the original board looks like this:


This version also does not include any method to "flow" and reveal all zeroes adjacent to one another. Therefore, you need to reveal each square one at a time.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
public class MinesweeperGame {
    private int rows;
    private int columns;
    private int mines;
    private int board[][];
    private int visibleBoard[][];

    public MinesweeperGame(int inputRows, int inputColumns, int inputMines) { // Service Constructor
        rows = inputRows;
        columns = inputColumns;
        mines = inputMines;
        visibleBoard = new int[rows][columns];
        board = this.createBoard();
        board = populateBoard(board);
        visibleBoard();
    }

    public int playerInteract(int inputRow, int inputColumn) { // Determines the result of an interaction with the board and changes all components appropriately
        if (board[inputRow][inputColumn] == 9) {
            return 2;
        }
        else {
            visibleBoard[inputRow][inputColumn] = board[inputRow][inputColumn];
        }
        for (int i = 0; i< rows; i++) { // Checks for a win, if they haven't won yet, then they need to keep playing
            for (int j = 0; j < columns; j++) {
                if (board[i][j] != 9) {
                    if (visibleBoard[i][j] == board[i][j]) {
                        visibleBoard[i][j] = visibleBoard[i][j];
                    }
                    else {
                        return 0;
                    }
                }
            }
        }
        return 1;
    }

    public void getVisibleBoard() {
        printBoard(visibleBoard);
    } // Calls the "visible" board, i.e. what the player can see
    public void getBoard() {
        printBoard(board);
    } // Calls the master board with all of the answers

    private int[][] createBoard(){ // Helper Method to instantiate the board
        int temp[][] = new int[rows][columns];
        return temp;
    }

    private int[][] populateBoard(int inputBoard[][]) { // Helper Method to randomly place mines
        while (mines != 0) {
            int a = (int)(Math.random()*rows);
            int b = (int)(Math.random()*columns);
            if (inputBoard[a][b] == 0) {
                inputBoard[a][b] = 9;
                mines -= 1;
            }
        }

        for(int i = 0; i < rows; i++) { // This section then assigns each space of number based on the surrounding mines.
            for (int j = 0; j < columns; j++) {
                if (inputBoard[i][j] != 9) {
                    int tempMines = 0;
                    if ((j - 1) != -1) {
                        if (inputBoard[i][j - 1] == 9) {
                            tempMines += 1;
                        }
                    }
                    if ((i - 1) != -1) {
                        if (inputBoard[i - 1][j] == 9) {
                            tempMines += 1;
                        }
                    }
                    if ((i - 1) != -1 && (j - 1) != -1) {
                        if (inputBoard[i - 1][j - 1] == 9) {
                            tempMines += 1;
                        }
                    }
                    if ((i - 1) != -1 && (j + 1) != columns) {
                        if (inputBoard[i - 1][j + 1] == 9) {
                            tempMines += 1;
                        }
                    }
                    if ((i + 1) != rows) {
                        if (inputBoard[i + 1][j] == 9) {
                            tempMines += 1;
                        }
                    }
                    if ((j + 1) != columns) {
                        if (inputBoard[i][j + 1] == 9) {
                            tempMines += 1;
                        }
                    }
                    if ((i + 1) != rows && (j + 1) != columns) {
                        if (inputBoard[i + 1][j + 1] == 9) {
                            tempMines += 1;
                        }
                    }
                    if ((i + 1) != rows && (j - 1) != -1) {
                        if (inputBoard[i + 1][j - 1] == 9) {
                            tempMines += 1;
                        }
                    }
                    inputBoard[i][j] = tempMines;
                }
            }
        }
        return (inputBoard);
    }

    private void printBoard(int inputBoard[][]) { // Prints the board in a neat matrix fashion
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                System.out.print(inputBoard[i][j] + "   ");
            }
            System.out.println();
        }
    }

    private void visibleBoard() { // Generates a blank "visible board"
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                visibleBoard[i][j] = -1;
            }
        }
        printBoard(visibleBoard);
    }

}

Here is the driver class for the above game. This version is very customizable; the user selects the # of traps and the dimensions of the board. Furthermore, this edition does not have an option to replay the game; it automatically shuts down after a win or loss.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.Scanner;

public class MinesweeperPlayer {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter three positive constants (the # of rows | the # of columns | the # of mines): ");
        int rows = sc.nextInt();
        int columns = sc.nextInt();
        int mines = sc.nextInt();
        System.out.println("Please note that mines are represented by the # 9 while -1 refers to 'hidden' spaces");
        System.out.println("");
        MinesweeperGame game = new MinesweeperGame(rows, columns, mines); // Creates the game
        System.out.println("");
        System.out.println("Let's get started!");
        int x = 0;
        while (x != -1) { // Actually plays the game
            System.out.println("");
            System.out.println("Please select a row | column to examine (zero-based indexing please): ");
            int inputRow = sc.nextInt();
            int inputColumn = sc.nextInt();
            int y = game.playerInteract(inputRow, inputColumn); // User input to select square
            if (y == 2) { // Response System: Win, Hit or Miss
                System.out.println("");
                System.out.println("You have hit a mine!");
                System.out.println("");
                game.getBoard();
                System.exit(0); // Exits after the game is complete
            }
            if (y == 1) {
                System.out.println("");
                System.out.println("You won! Congratulations");
                System.out.println("");
                game.getBoard();
                System.exit(0); // Exits after the game is complete
            }
            if (y == 0) {
                System.out.println("");
                System.out.println("Here is the board for reference. Play you next move wisely.");
                System.out.println("");
                game.getVisibleBoard(); // Continues if the game has not been completed. 
            }
        }
    }
}

Thank you to http://hilite.me/ for the source code formatting.

Github Code: https://github.com/MakeTheBrainHappy/Minesweeper

What is Wilkins Bill?

Wilkins Bill, also known as the Force Bill of 1833, was passed in response to the 1832-1833 nullification crisis between the federal government and the state of South Carolina. This was a severe crisis with vice-president John C. Calhoun having resigned June 1832 in protest. The different sides were preparing for full-scale battle as this letter from Joel Roberts Poinsett on Feb. 22nd, 1833, a former U.S. Rep and former U.S. Ambassador to Mexico, states:

"I do not think therefore I over estimate the force of the Union party in the city at One thousand men. I should be much disappointed not to find this number, at least, at their rendevous in case of an alarm. The Nullifiers estimate their force in the City at 1600 men. We consider our strength equal to theirs here. In the State they report 16,000 Volunteers." - Source

He conviction later won him appointment to the position of U.S. Secretary of War under president Martin van Buren.

"It is rumoured, that if they do secede, which I verily believe they will do if the enforcem't law passes and the tariff is not modified ... I have addressed a circular to the Union members of the state convention urging them not to join that body in any act violating the constitution of the united states and not to go to the convention at all. This storm may yet pass off and not burst upon us, but we will be prepared to encounter and to resist it like men." - Source

South Carolina had earlier on passed the Ordinance of Nullification enumerating the reasons for secession (read here).

President Andrew Jackson wrote in inaugural address on March 1st, 1833:

"For myself, when I approach the sacred volume and take a solemn Oath to support and defend this constitution, I feel in the depths of my soul, that it is the highest, most sacred and most irreversible part of my obligation, to preserve the union of these states, although it may cost me my life." - Source

The day afterwards on March 2nd, 1833, Wilkins Bill was passed, named after William Wilkins, a Jacksonian ally representing Pennsylvania who proposed the bill in the senate.

William Wilkins, legislator, lawyer & judge. Source 

Later becoming Secretary of War as Poinsett, he generally supported Jacksonian principles during his varied career as a federal judge, U.S. Senator, U.S. Minister to Russia, U.S. Representative and Cabinet member. For example, during his tenure in Tyler's cabinet, he supported western expansionism.


His bill empowered the president to enforce the laws of the United States with regard to commerce and especially tariffs in the ports of South Carolina, whose leading politicians opposed the measures because they were designed to protect New England manufacturing interests (Source). There inspiration for this form of resistance came from the Virginia and Kentucky resolutions authored by certain founding fathers.

William Hendricks, a senator of Indiana, wrote at the time:

Commentary on the Tariff & Wilkins Bill. Source

"A crisis had indeed arisen, and it would have been, inconsistent with duty, to stand still, and see the revenue officers of the General Government delicacy, fall victims, to the military power of South Carolina, already organized for that purpose. That state must abandon her position, or there must be consequences the most unpleasant." - Source 


The passage of this bill essentially provided Jackson with the power to enforce the collection of the taxes regarding imports/exports, but Congress lowered the tariffs in a successful plot to avoid the implementation of the contents of this "Force" Bill.