Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Hi, I made this Minesweeper game because I am bored. Hope you enjoy!
- */
- import java.util.Random;
- import java.util.Scanner;
- public class Minesweeper {
- static final int SIZE = 16; //Must be between 1 and 16
- static final int AREA = SIZE*SIZE;
- static final int NUMBER_OF_MINES = 40;
- static char[] rowName = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
- static int[][] game = new int[SIZE][SIZE];
- static int[][] prefixSum = new int[SIZE][SIZE];
- static String[][] display = new String[SIZE][SIZE];
- static Scanner input = new Scanner(System.in);
- static String answer;
- static void generateGame() {
- int minesLeft = NUMBER_OF_MINES;
- Random random = new Random();
- for (int i = 0; i < SIZE; i++) {
- for (int j = 0; j < SIZE; j++) {
- //Generate Mines
- int cell = i*SIZE + j;
- int next = random.nextInt(AREA);
- if (next < (AREA/6 + 15 * minesLeft/(AREA - cell)) && minesLeft > 0) {
- game[i][j] = 1;
- minesLeft--;
- } else game[i][j] = 0;
- //Prefix Sum
- if (i == 0 && j ==0) {
- prefixSum[i][j] = game[i][j];
- } else if (i == 0) {
- prefixSum[i][j] = game[i][j] + prefixSum[i][j - 1];
- } else if (j == 0) {
- prefixSum[i][j] = game[i][j] + prefixSum[i - 1][j];
- } else {
- prefixSum[i][j] = game[i][j] + prefixSum[i - 1][j] + prefixSum[i][j - 1] - prefixSum[i - 1][j - 1];
- }
- //Display
- display[i][j] = "*";
- }
- }
- }
- static void printArray(int[][] array) { //For internal use
- System.out.println();
- for (int i = 0; i < SIZE; i++) {
- for (int j = 0; j < SIZE; j++) {
- System.out.print(array[i][j] + " ");
- }
- System.out.println();
- }
- }
- static void printArray(String[][] array) {
- System.out.println();
- System.out.print(" ");
- for (int i = 0; i < SIZE; i++) {
- System.out.print(rowName[i] + " ");
- }
- System.out.println();
- for (int i = 0; i < SIZE; i++) {
- System.out.println();
- System.out.print(rowName[i] + " ");
- for (int j = 0; j < SIZE; j++) {
- System.out.print(array[i][j] + " ");
- }
- }
- System.out.println();
- }
- static int linearSearch(char[] array, char target) {
- for (int i = 0; i < array.length; i++) {
- if (array[i] == target) {
- return i;
- }
- }
- return -1;
- }
- static int inputToCell(String input) { //Change a string input into cell number
- input = input.toUpperCase();
- int result = SIZE*linearSearch(rowName, input.charAt(0)) + linearSearch(rowName, input.charAt(1));
- return result >= AREA? -1: result;
- }
- static boolean modifyDisplay(int input, String action) {
- //System.out.println("Modifying " + input + " with action " + action);
- boolean foundMine = false;
- int row = input/SIZE, column = input%SIZE;
- switch (action) {
- case "s" -> {
- if (display[row][column].equals("?")) break; //Flagged cells will not be swept
- if (game[row][column] == 0) { //The cell does not contain a mine
- display[row][column] = "" + (prefixSum[Math.min(row + 1, SIZE - 1)][Math.min(column + 1, SIZE - 1)]
- - (row > 1 ? prefixSum[row - 2][Math.min(column + 1, SIZE - 1)] : 0)
- - (column > 1 ? prefixSum[Math.min(row + 1, SIZE - 1)][column - 2] : 0)
- + (row > 1 && column > 1 ? prefixSum[row - 2][column - 2] : 0));
- if (display[row][column].equals("0"))
- foundMine = modifyDisplay(SIZE * row + column, "a"); //Sweep 3x3 AREA if there aren't any mines around
- } else {
- display[row][column] = "X";
- foundMine = true;
- }
- }
- case "a" -> { //Disclose cells in a 3x3 AREA
- for (int i = Math.max(row - 1, 0); i < Math.min(row + 2, SIZE); i++) {
- for (int j = Math.max(column - 1, 0); j < Math.min(column + 2, SIZE); j++) {
- if (!display[i][j].equals("*")) continue;
- if (foundMine) break;
- foundMine = modifyDisplay(SIZE * i + j, "s");
- }
- if (foundMine) break;
- }
- }
- case "f" -> {
- if (!(display[row][column].equals("*") || display[row][column].equals("?"))) break; //Disclosed cells cannot be flagged
- display[row][column] = display[row][column].equals("?") ? "*" : "?";
- }
- default -> System.out.println("Invalid action! Please use either a, f, or s"); //Do nothing if invalid action
- }
- return foundMine;
- }
- static boolean parseAnswer(String answer) {
- String[] ans = answer.split(" "); //Methods: f - flag, s - sweep, a - all sweep
- int cell = inputToCell(ans[1]);
- if (cell < 0) { //A negative number will be returned if the cell reference is invalid
- System.out.println("Invalid cell!");
- return true;
- }
- return !modifyDisplay(cell, ans[0]);
- }
- static boolean checkGameCompleted() {
- for (int i = 0; i < SIZE; i++) {
- for (int j = 0; j < SIZE; j++) {
- if (game[i][j] == 0 && (display[i][j].equals("*") || display[i][j].equals("?"))) return false;
- }
- }
- return true;
- }
- public static void main(String[] args) {
- boolean safe = true;
- boolean win = false;
- generateGame();
- //printArray(game);
- //printArray(prefixSum);
- printArray(display);
- System.out.println("""
- Welcome to this Minesweeper game! The syntax are as following:
- actions: f - Flag a cell; s - Select a cell; a - Select 3x3 cells centered at selected cell
- action + " " + cell row + cell column (e.g. f 00; a 56; s 99)""");
- while (safe && !win) {
- System.out.println("Please type an action:");
- answer = input.nextLine();
- if (!answer.matches("[asf] [0-9a-fA-F][0-9a-fA-F]")) {
- System.out.println("Invalid input! Please refer to the syntax provided");
- continue;
- }
- safe = parseAnswer(answer);
- win = checkGameCompleted();
- printArray(display);
- }
- System.out.println("You " + (win? " won!": "lost!"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment