Title: Java Minesweeper Game – Where Strategy and Coding Collide
1Java Minesweeper Game Where Strategy and
Coding Collide
In this article, we are talking about Java
Minesweeper. Minesweeper is a classic computer
game with the objective of clearing a rectangular
grid containing hidden mines. The grid is
divided into cells, and each cell can either be
empty or contains a mine. The players task is to
uncover all the empty cells without triggering
any mines.
About Java Minesweeper Game
- At the beginning of the game, the player is
presented with a grid and has to select a cell
to start the game. - If the selected cell contains a mine, the game
ends, and the player loses. - If the selected cell is empty or doesnt contain
a mine, then it reveals a number which indicates
the total number of mines adjacent to that cell(
eight neighbouring cells) - Based on the number, the player can deduce the
presence of mines in the neighbouring cells.
- Prerequisites for Minesweeper Game using Java
- IDE Used IntelliJ
- Java 1.8 or above must be installed.
2Download Java Minesweeper Game Project
Please download the source code of Java
Minesweeper Game Project from the following
link Java Minesweeper Game Converter Project Code
- Steps to Create Minesweeper Game Using Java
- These are the steps to build a minesweeper game
in Java - Set up the project.
- Design the grid.
- Place mine randomly
- Calculate adjacent mines.
- Print grid and reveal cells.
- Create the game loop.
- Implement game-over conditions.
- Test the game.
- Set up the project
- In this step, we basically create a Java project
and initialize all necessary variables to build
the game. - Create a new Java project in your preferred IDE.
- Set up the basic project structure by importing
appropriate packages. - Declare and Initialize various variables and data
structures needed for the game, such as ROWS,
COLUMNS, MINES, grid, remainingCells ,
move, revealed, and boolean flags like
gameWon and gameLost. - Also, create a Scanner class object to read input
from the user.
3- import java.util.Scanner public class
Minesweeper - static int ROWS,COLUMNS,MINES, remainingCells
static char grid - static int move new int2 static
boolean revealed static boolean gameWon,
gameLost - static Scanner sc new Scanner(System.in)
- Design the grid
- The grid size is decided based on the difficulty
chosen by the user and also initialized. - The grid_size method uses a switch statement to
assign values to the ROWS, COLUMNS and
MINES variables based on the level of
difficulty chosen. - In case of custom difficulty level, the user is
prompted to enter the number of rows, columns
and mines manually. - private static void grid_size(int difficulty)
switch (difficulty) the difficulty. case
1 ROWS 30 COLUMNS 16 MINES 99 break
case 2 ROWS 16 COLUMNS 16 MINES
40 break case 3 ROWS 9 COLUMNS 9 MINES
10
// A menu based system for adjusting
4- break case 4
- System.out.println("Enter the number of rows,
columns, and mines \n") - ROWS sc.nextInt() COLUMNS sc.nextInt()
MINES sc.nextInt() break - default
- System.out.println("Invalid choice\n")
System.exit(0) -
- grid new charROWSCOLUMNS
-
- The initializeGrid method is used to initialize
the grid. This method also initializes the
revealed array, which is a boolean array used to
track the revealed cells. - private static void initializeGrid() //
Initialize the game grid with empty cells. - for (int i 0 i lt ROWS i)
- for (int j 0 j lt COLUMNS j) gridij
'-' -
-
- revealed new booleanROWSCOLUMNS //
Initialize the revealed array. - remainingCells ROWS COLUMNS - MINES //
Calculate the number of remaining cells to be
revealed.
5- The placeMines method randomly places the mines
by generating random coordinates and checking if
the cell is already occupied by a mine. - private static void placeMines() // Place mines
randomly on the grid. - int minesPlaced 0
- while (minesPlaced lt MINES)
- int row (int) (Math.random() ROWS) int col
(int) (Math.random() COLUMNS) if
(gridrowcol ! 'M') - gridrowcol 'M' minesPlaced
-
-
-
- Calculate adjacent mines
- For every cell that is not a mine, the number of
mines in the neighbouring cells is counted, and
that value is assigned to that cell. - The calculateAdjacentMines method iterates over
the grid, and for each cell that is not a mine,
it counts the adjacent cells that contain mines. - The count is then stored in the corresponding
cell to represent the number of adjacent mines. - private static void calculateAdjacentMines() //
It calculated the adjacent numbers along the
mines for each cell. - for (int i 0 i lt ROWS i)
- for (int j 0 j lt COLUMNS j) if
(gridij ! 'M')
6- if ((x ! 0 y ! 0) isValidCell(i x, j
y) gridi xj y 'M') - count
-
-
-
- if (count gt 0)
- gridij (char) (count '0')
-
-
-
-
-
- The isValidCell method checks if a given row
and column are within the grid boundaries before
performing any operations. - private static boolean isValidCell(int row, int
col) // Used to check for a valid cell. - return row gt 0 row lt ROWS col gt 0 col
lt COLUMNS
7- for (int j 0 j lt COLUMNS j)
System.out.printf(" 3s", j) -
- System.out.println() // Print column numbers on
top for reference - for (int i 0 i lt ROWS i)
System.out.print("\t" i) - for (int j 0 j lt COLUMNS j) if
(gridij 'F') - System.out.printf("\tF")
- else if (revealedij)
- if (gridij '-') System.out.print("\t0")
- else
- System.out.print("\t" gridij)
-
- else
- System.out.printf("\t-")
-
-
- System.out.println() // Print the grid
-
-
8-
-
-
- The revealAllCells method is called to reveal
all the cells at once in the grid presented on
the output screen. - private static void revealAllCells() // It is
to reveal all the cells in output screen. - for (int i 0 i lt ROWS i)
- for (int j 0 j lt COLUMNS j)
revealedij true -
-
-
- The getPlayerMove method prompts the player to
enter the row and column numbers of their next
move. - private static void getPlayerMove() // This
method is defined to take input from the user. - System.out.print("Enter row number ") move0
sc.nextInt() System.out.print("Enter column
number ") move1 sc.nextInt() -
9- public static void play() initializeGrid() //
Game initialization printGrid() - getPlayerMove() int row move0 int col
move1 - placeMines() // Place mines after the first move
calculateAdjacentMines() - while (!gameWon !gameLost)
- if (gridrowcol 'M') // Check if player
stepped on a mine - gameLost true revealAllCells() printGrid()
- System.out.println("Game Over! You stepped on a
- mine.")
- break // Exit the loop
- else
- revealCell(row, col) remainingCells--
printGrid() - if (checkWin()) // Check if player cleared all
non-mine cells - gameWon true
- System.out.println("Congratulations! You cleared
all the mines.") - break // Exit the loop
-
- getPlayerMove() row move0 col move1
-
-
10- After receiving the first move, it places the
mines on the grid - randomly using the placeMines method and
calculates the number of adjacent mines for each
cell using calculateAdjacentMines - method. Reveals the players chosen cell using
revealCell and updates the remaining cells
counter. - It then enters a loop where it repeatedly asks
the player for their next move until the game is
won or lost. - If the player steps on a mine, the game is lost,
the remaining cells are revealed, and the grid
is printed with the Game Over message. - If the player clears all non-mine cells, the game
is won, and a victory message is displayed. - Implement game-over conditions
- The checkwin method checks if the game is won
by verifying if all non-mine cells have been
revealed. - private static boolean checkWin() //This is to
check if the user has completed the game
successfully or not. - for (int i 0 i lt ROWS i)
- for (int j 0 j lt COLUMNS j)
- if (gridij ! 'M' !revealedij)
return false -
-
-
- The Main Method
11public static void main(String args)
System.out.format("40s", "TechVidvan's
Minesweeper Game\n") // Game title System.out.pri
ntln("\n----MENU---- ") //Adding Menu to choose
difficulty level of the game System.out.println("
1. Hard\n 2. Medium\n 3. Easy\n
4. Custom\n") System.out.println("\nPlease
choose the difficulty ") int difficulty
sc.nextInt() grid_size(difficulty)
play() 9. Test the game Verify that the
game functions correctly by playing through
various scenarios, including wins and
losses. Conclusion In conclusion, the Java
version of the Minesweeper game exhibits a useful
and engaging gaming environment. Users of the
game can choose between easy, medium, hard, or
custom settings for the difficulty. As the player
advances, the game logic takes care of mine
installation, neighbouring mine counts
calculation, and cell revealing. The grid size is
updated accordingly. The game keeps track of how
many cells are still hidden and decides whether
the player wins or loses. The procedural
technique used in the code implementation successf
ully produces a competitive version of
Minesweeper.