This repository has been archived on 2026-03-18. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
CSE-122/MineSweeper.java
2026-03-18 00:39:22 -07:00

100 lines
3.6 KiB
Java

// 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();
}
}