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

2 comments:

  1. Hey, where do you fill out the info?

    ReplyDelete
  2. pg slot รับฟรีเครดิต ผู้ให้บริการเกมสล็อตออนไลน์ ที่มาแรงที่สุด pg slot รับรองความคุ้มราคาจากถ้าเกิดคูณเริ่มจะมีความรู้สึกว่าของฟรีไม่มีในโลก บอกเลยว่าคุณคิดผิดแล้ว ด้วยเหตุว่าเว็บไซต์ของเราแจกจริงไม่ต้องแชร์

    ReplyDelete