summaryrefslogtreecommitdiff
path: root/MineSweeper.java
blob: 22b8966745a908198c77f3b5f71c461785891b0d (plain)
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
// Miya Natsuhara
// CSE 122
// 1-5-2024

import java.util.*;

// This program randomly-generates and displays a MineSweeper board according to the difficulty 
// level specified by the user. 

public class MineSweeper {
    public static final int BOARD_SIZE = 15; // assume a board size board, square
    public static final int MINE_INDICATOR = -1; 

    public static void main(String[] args) {
        int[][] board = new int[BOARD_SIZE][BOARD_SIZE];

        int difficultyLevel = intro(); // introduce game and get level of difficulty (more mines -> harder)
        int numMines = getNumMines(difficultyLevel);
        generateMines(numMines, board); // generate coordinates for all the mines
        updateCounts(board);  // updates the board with all the counts of mines
        displayBoard(board);
    }

    // Prints a welcome message and prompts the user for difficulty level (between 1 and 10). 
    // Restricts the user's response to this range and returns the result. 
    public static int intro() {
        Scanner console = new Scanner(System.in);
        System.out.println("Welcome to MineSweeper!");
        System.out.print("Please choose game difficulty (NOTE: Difficulty: 1 to 10): ");
        int userNum = console.nextInt();
        if (userNum > 10) {
            userNum = 10;
        } else if (userNum < 1) {
            userNum = 1;
        }
        return userNum;
    }

    // Given a difficulty level, returns the number of mines that should be placed on the board. 
    public static int getNumMines(int difficultyLevel) {
        return difficultyLevel * (BOARD_SIZE / 5); // arbitrary
    }

    // Given the desired number of mines and game board, randomly places the specified number of
    // mines throughout the board. 
    public static void generateMines(int numMines, int[][] board) {
        Random r = new Random();
        for (int i = 0; i < numMines; i++) {
            int xCoord = r.nextInt(BOARD_SIZE);
            int yCoord = r.nextInt(BOARD_SIZE);
            board[xCoord][yCoord] = MINE_INDICATOR;
        }
    }

    // Given the board and a location (x, y) to check, returns the number of mines adjacent to 
    // location (x, y). 
    public static int placeCount(int[][] board, int x, int y) {
        int count = 0;
        for (int i = -1; i < 2; i++) {
            for (int j = -1; j < 2; j++) {
                int xCoord = x + i;
                int yCoord = y + j;
                boolean validSquare = xCoord >= 0 && xCoord < BOARD_SIZE && 
                                      yCoord >= 0 && yCoord < BOARD_SIZE;
                if (validSquare && board[xCoord][yCoord] == MINE_INDICATOR) {
                    count++;
                }
            }
        }
        return count;
    }

    // Given the game board, places the appropriate numbers in non-mine cells throughout the
    // board. 
    public static void updateCounts(int[][] board) {
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                if (board[i][j] != MINE_INDICATOR) {
                    board[i][j] = placeCount(board, i, j);
                }
            }
        }
    }

    // Prints the board to the console, with each square's contents evenly-spaced. 
    public static void displayBoard(int[][] board) {
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                if (board[i][j] == MINE_INDICATOR) {
                    System.out.print("X ");
                } else {
                    System.out.print(board[i][j] + " ");
                }
            }
            System.out.println();
        }
        System.out.println();
    }
}