Minesweeper game in java

Minesweeper game is very funny game In this article we will learn how to code minesweeper in java . We can play minesweeper game using java IDE like eclipse using this source code. In this tutorial we used 2d array for minesweeper game in java. Let’s see simple minesweeper java code :

Minesweeper Program in java

This minesweeper java code have two java classes Minesweeper.java and MainMinesweeper.java. Minesweeper.java class have 2 d array _board and _flagMatrix for maintaining mines . This class have different method for handling logic for minesweeper generation algorithm and minesweeper solving algorithm .  MainMinesweeper.java class take input from users and call the method of  Minesweper.java

Minesweeper.java

import java.util.Random;
public class Minesweeper {
int _row=5,_col=5,_countGameOver=0;
int [][] _board=new int[_row][_col];
boolean [][] _flagMatrix=new boolean[_row][_col];
public void plotBoard(){
int noOfBomb=_col;
while(noOfBomb>0) {
int i=getRandomNumber();
int j=getRandomNumber();
if (_board[i][j]!=-1) {
_board[i][j]=-1;
noOfBomb--;
}
}
}
public int getRandomNumber(){
Random t = new Random();
// random integers in [0, 100]
int num;
{
num=t.nextInt(_col);
}while(num>_col);
return num;
}
boolean isValid(int i,int j){
return((i>=0 && i<_row)&&(j>=0 && j<_col));
}
int getAdjBomb(int i,int j){
int num=0;
//System.out.println("i="+i+"j="+j);
for (int k = i-1; k <=(i+1); k++) {
for (int l= j-1; l<=(j+1); l++) {
if (isValid(k,l)&&(_board[k][l]==-1)) {
num++;
}
}
}
return num;
}
public void calculateCounts(){
for (int i = 0; i <_row; i++) {
for (int j = 0; j <_col; j++) {
if(_board[i][j]!=-1)
_board[i][j]=getAdjBomb(i,j);
}
}
}
public void print(){
for (int i = 0; i <_row; i++) {
for (int j = 0; j <_col; j++) {
System.out.print(" "+_board[i][j]);
}
System.out.println("");
}
}
public void displayFlagMatrix(){
for (int i = 0; i <_row; i++) {
for (int j = 0; j <_col; j++) {
if (_flagMatrix[i][j]==true) {
System.out.print(" "+"*");
}
else{
System.out.print(" "+"+");
}
}
System.out.println(" ");
}
}
public boolean play(int r,int c){
if (!(isValid(r,c))) {
System.out.println("Please Enter row and col in correct range");
return false;
}
if (_flagMatrix[r][c]==true) {
System.out.println("This is a open position please enter other col and row");
return false;
}
if (_board[r][c]==-1)
return true;
if (_board[r][c]!=0) {
_countGameOver++;
_flagMatrix[r][c]=true;
}
else{
_flagMatrix[r][c]=true;
_countGameOver++;
openSpace(r, c);
}
return false;
}
void openSpace(int r,int c){
for (int k = r-1; k <=(r+1); k++) {
for (int l= c-1; l<=(c+1); l++) {
if (isValid(k,l)&&(_board[k][l]!=-1)&&(_flagMatrix[k][l]==false)) {
_flagMatrix[k][l]=true;
_countGameOver++;
if (_board[k][l]==0) {
openSpace(k,l);
}
}
}
}
}
public boolean checkGameOver(){
return(_countGameOver==(_row*_row)-(_row));
}
}

MainMinessweeper.java

import java.util.Scanner;
import com.jan21.*;
public class MainMinesweeper extends Minesweeper {
public static void main(String[] args) {
MainMinesweeper obj=new MainMinesweeper();
obj.plotBoard();
obj.calculateCounts();
System.out.println("afate count");
obj.print();
int r,c;
System.out.println("+++++++++++++++");
obj.displayFlagMatrix();
while(true){
System.out.println("===============");
Scanner input=new Scanner(System.in);
System.out.println("enter row");
r=input.nextInt();
System.out.println("enter col");
c=input.nextInt();
if (obj.play(r,c)) {
System.out.println("You Lose");
break;
}
System.out.println("ok");
if (obj.checkGameOver()) {
obj.displayFlagMatrix();
System.out.println("You Win Game");
break;
}
obj.displayFlagMatrix();
System.out.println("+++++++++++");
obj.print();

}
}

}
This is simple minesweeper java code for minesweeper game . You can also see java code for Tic Tac Toe game . Once import these classes in eclipse you can enjoy for playing minesweeper .

Leave a Reply

Your email address will not be published. Required fields are marked *

68 − = 60