Michael228p

Untitled

Mar 8th, 2023
952
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.94 KB | None | 0 0
  1. /*
  2. Hi, I made this Minesweeper game because I am bored. Hope you enjoy!
  3. */
  4. import java.util.Random;
  5. import java.util.Scanner;
  6.  
  7. public class Minesweeper {
  8.     static final int SIZE = 16; //Must be between 1 and 16
  9.     static final int AREA = SIZE*SIZE;
  10.     static final int NUMBER_OF_MINES = 40;
  11.     static char[] rowName = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  12.     static int[][] game = new int[SIZE][SIZE];
  13.     static int[][] prefixSum = new int[SIZE][SIZE];
  14.     static String[][] display = new String[SIZE][SIZE];
  15.     static Scanner input = new Scanner(System.in);
  16.     static String answer;
  17.  
  18.     static void generateGame() {
  19.         int minesLeft = NUMBER_OF_MINES;
  20.         Random random = new Random();
  21.  
  22.         for (int i = 0; i < SIZE; i++) {
  23.             for (int j = 0; j < SIZE; j++) {
  24.                 //Generate Mines
  25.                 int cell = i*SIZE + j;
  26.                 int next = random.nextInt(AREA);
  27.                 if (next < (AREA/6 + 15 * minesLeft/(AREA - cell)) && minesLeft > 0) {
  28.                     game[i][j] = 1;
  29.                     minesLeft--;
  30.                 } else game[i][j] = 0;
  31.  
  32.                 //Prefix Sum
  33.                 if (i == 0 && j ==0) {
  34.                     prefixSum[i][j] = game[i][j];
  35.                 } else if (i == 0) {
  36.                     prefixSum[i][j] = game[i][j] + prefixSum[i][j - 1];
  37.                 } else if (j == 0) {
  38.                     prefixSum[i][j] = game[i][j] + prefixSum[i - 1][j];
  39.                 } else {
  40.                     prefixSum[i][j] = game[i][j] + prefixSum[i - 1][j] + prefixSum[i][j - 1] - prefixSum[i - 1][j - 1];
  41.                 }
  42.  
  43.                 //Display
  44.                 display[i][j] = "*";
  45.             }
  46.         }
  47.     }
  48.  
  49.     static void printArray(int[][] array) { //For internal use
  50.         System.out.println();
  51.         for (int i = 0; i < SIZE; i++) {
  52.             for (int j = 0; j < SIZE; j++) {
  53.                 System.out.print(array[i][j] + " ");
  54.             }
  55.             System.out.println();
  56.         }
  57.     }
  58.  
  59.     static void printArray(String[][] array) {
  60.         System.out.println();
  61.         System.out.print("    ");
  62.         for (int i = 0; i < SIZE; i++) {
  63.             System.out.print(rowName[i] + " ");
  64.         }
  65.         System.out.println();
  66.         for (int i = 0; i < SIZE; i++) {
  67.             System.out.println();
  68.             System.out.print(rowName[i] + "   ");
  69.             for (int j = 0; j < SIZE; j++) {
  70.                 System.out.print(array[i][j] + " ");
  71.             }
  72.         }
  73.         System.out.println();
  74.     }
  75.  
  76.     static int linearSearch(char[] array, char target) {
  77.         for (int i = 0; i < array.length; i++) {
  78.             if (array[i] == target) {
  79.                 return i;
  80.             }
  81.         }
  82.         return -1;
  83.     }
  84.  
  85.     static int inputToCell(String input) { //Change a string input into cell number
  86.         input = input.toUpperCase();
  87.         int result = SIZE*linearSearch(rowName, input.charAt(0)) + linearSearch(rowName, input.charAt(1));
  88.         return result >= AREA? -1: result;
  89.     }
  90.  
  91.     static boolean modifyDisplay(int input, String action) {
  92.         //System.out.println("Modifying " + input + " with action " + action);
  93.         boolean foundMine = false;
  94.  
  95.         int row = input/SIZE, column = input%SIZE;
  96.         switch (action) {
  97.             case "s" -> {
  98.                 if (display[row][column].equals("?")) break; //Flagged cells will not be swept
  99.                 if (game[row][column] == 0) { //The cell does not contain a mine
  100.                     display[row][column] = "" + (prefixSum[Math.min(row + 1, SIZE - 1)][Math.min(column + 1, SIZE - 1)]
  101.                             - (row > 1 ? prefixSum[row - 2][Math.min(column + 1, SIZE - 1)] : 0)
  102.                             - (column > 1 ? prefixSum[Math.min(row + 1, SIZE - 1)][column - 2] : 0)
  103.                             + (row > 1 && column > 1 ? prefixSum[row - 2][column - 2] : 0));
  104.                     if (display[row][column].equals("0"))
  105.                         foundMine = modifyDisplay(SIZE * row + column, "a"); //Sweep 3x3 AREA if there aren't any mines around
  106.                 } else {
  107.                     display[row][column] = "X";
  108.                     foundMine = true;
  109.                 }
  110.             }
  111.             case "a" -> { //Disclose cells in a 3x3 AREA
  112.                 for (int i = Math.max(row - 1, 0); i < Math.min(row + 2, SIZE); i++) {
  113.                     for (int j = Math.max(column - 1, 0); j < Math.min(column + 2, SIZE); j++) {
  114.                         if (!display[i][j].equals("*")) continue;
  115.                         if (foundMine) break;
  116.                         foundMine = modifyDisplay(SIZE * i + j, "s");
  117.                     }
  118.                     if (foundMine) break;
  119.                 }
  120.             }
  121.             case "f" -> {
  122.                 if (!(display[row][column].equals("*") || display[row][column].equals("?"))) break; //Disclosed cells cannot be flagged
  123.                 display[row][column] = display[row][column].equals("?") ? "*" : "?";
  124.             }
  125.             default -> System.out.println("Invalid action! Please use either a, f, or s"); //Do nothing if invalid action
  126.         }
  127.         return foundMine;
  128.     }
  129.  
  130.     static boolean parseAnswer(String answer) {
  131.         String[] ans = answer.split(" "); //Methods: f - flag, s - sweep, a - all sweep
  132.         int cell = inputToCell(ans[1]);
  133.         if (cell < 0) { //A negative number will be returned if the cell reference is invalid
  134.             System.out.println("Invalid cell!");
  135.             return true;
  136.         }
  137.         return !modifyDisplay(cell, ans[0]);
  138.     }
  139.  
  140.     static boolean checkGameCompleted() {
  141.         for (int i = 0; i < SIZE; i++) {
  142.             for (int j = 0; j < SIZE; j++) {
  143.                 if (game[i][j] == 0 && (display[i][j].equals("*") || display[i][j].equals("?"))) return false;
  144.             }
  145.         }
  146.         return true;
  147.     }
  148.  
  149.     public static void main(String[] args) {
  150.         boolean safe = true;
  151.         boolean win = false;
  152.         generateGame();
  153.         //printArray(game);
  154.         //printArray(prefixSum);
  155.         printArray(display);
  156.         System.out.println("""
  157.                Welcome to this Minesweeper game! The syntax are as following:
  158.                actions: f - Flag a cell; s - Select a cell; a - Select 3x3 cells centered at selected cell
  159.                action + " " + cell row + cell column (e.g. f 00; a 56; s 99)""");
  160.         while (safe && !win) {
  161.             System.out.println("Please type an action:");
  162.             answer = input.nextLine();
  163.             if (!answer.matches("[asf] [0-9a-fA-F][0-9a-fA-F]")) {
  164.                 System.out.println("Invalid input! Please refer to the syntax provided");
  165.                 continue;
  166.             }
  167.  
  168.             safe = parseAnswer(answer);
  169.             win = checkGameCompleted();
  170.             printArray(display);
  171.         }
  172.         System.out.println("You " + (win? " won!": "lost!"));
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment