MINESWEEPER
Minesweeper is a single-player puzzle game that combines strategy,
logic, and a bit of luck. Its goal is to clear a grid of hidden mines without
detonating any of them, using numerical clues to deduce their locations.
Minesweeper engages logical reasoning and strategic thinking .Games are
short, making it a convenient pastime .Its simple rules and increasing
difficulty cater to players of all skill levels.
Gameplay Mechanics
1. Grid Layout:
o The game board is a rectangular grid divided into cells.
o Each cell can either hide a mine or be empty.
2. Objective:
o Reveal all cells without triggering a mine.
o Mark (flag) the locations of all mines correctly.
3. Numerical Clues:
o Clicking an empty cell reveals a number that indicates how many
mines are adjacent to that cell.
o Use these clues to deduce safe cells and mine locations.
4. Winning:
o The game is won when all non-mine cells are revealed, and all
mines are flagged.
5. Losing:
o Clicking on a mine results in an immediate loss, and the game
ends.
Difficulty Levels
Minesweeper typically offers three difficulty levels based on the grid size and
number of mines:
• Beginner: 9x9 grid with 10 mines.
• Intermediate: 16x16 grid with 40 mines.
• Expert: 30x16 grid with 99 mines.
CODE FOR MINESWEEPER GAME
import java.io.*;
import java.util.*;
class MyClass
{public static void main(String ab[])throws Exception
{DataInputStream ob =new DataInputStream(System.in);
Scanner sc=new Scanner(System.in);
int
div=0,n1,i2=0,n2,i=0,k,k1,t=0,b,c1,r1,w,x,y,w1,c2,j,l,i1,t1,t2,t3,m,iT,ac,score=0,
er=0;
double d1,d2,c12,r12;
char [][]b1=null;
int [][]a=null;
String c9;
char j9,pl;
System.out.println();
System.out.println();
//for display screen
Thread.sleep(800);
System.out.println(" ");
System.out.println();
System.out.println();
Thread.sleep(500);
System.out.println("************************************************
****************************************************************
****************************************************************
*********************");
System.out.println("************************************************
****************************************************************
****************************************************************
*********************");
System.out.println(" ||||| ||||| ||| |||||| ||| ||||||||| ||||||||| |||
||| ||||||||| ||||||||| ||||||||| ||||||||| ||||||||| |||");
Thread.sleep(50);
System.out.println(" ||| || || ||| ||| ||| ||| ||| ||| ||| |||
||| ||| ||| ||| ||| ||| ||| ||| |||");
Thread.sleep(50);
System.out.println(" ||| || ||| ||| ||| ||| ||| ||| ||| |||
||| ||| ||| ||| ||| ||| ||| ||| |||");
Thread.sleep(50);
System.out.println(" ||| ||| ||| ||| ||| ||| |||||| ||||||||| ||| |||
||| |||||| |||||| ||||||||| |||||| ||||||||| |||");
Thread.sleep(50);
System.out.println(" ||| ||| ||| ||| ||| ||| ||| ||| ||| ||||||
||| ||| ||| ||| ||| ||| ||| |||");
Thread.sleep(50);
System.out.println(" ||| ||| ||| ||| ||| ||| ||| ||| ||| ||| |||
||| ||| ||| ||| ||| ||| ||| ");
Thread.sleep(50);
System.out.println(" ||| ||| ||| ||| |||||| ||||||||| ||||||||| ||| |||
||||||||| ||||||||| ||| ||||||||| ||| ||| |||");
Thread.sleep(50);
System.out.println();
Thread.sleep(50);
System.out.println();
Thread.sleep(50);
System.out.println();
System.out.println("************************************************
****************************************************************
****************************************************************
*********************");
System.out.println("************************************************
****************************************************************
****************************************************************
*********************");
System.out.println();
System.out.println();
System.out.println();
System.out.println("Developed by-Shubhi Singh");
Thread.sleep(500);
System.out.println("The game is to gain as many points as you can by flagging
the mine or playing the field. ");
Thread.sleep(500);
System.out.println("When the player plays the field a number will be displayed
indicating the number of mines around that block.");
Thread.sleep(500);
System.out.println("If anywhere between the game the player plays a mine the
board will explode and the game will come to an end.");
Thread.sleep(500);
System.out.println("Use your brains, flag the mines, take a risk play the field
and when in doubt question mark the block. Try to gain as many points as
possible.");
System.out.println();
System.out.println();
System.out.println();
System.out.println("* To start a new game enter 'N' ");
System.out.println("* To resume an old game enter 'R' ");
System.out.println("* To exit the game enter 'E'");
pl=((ob.readLine()).toUpperCase()).charAt(0);
c:if(pl=='N')
{System.out.println("How to play the game->");
Thread.sleep(500);
System.out.println("1.Play the land and flag the mine");
Thread.sleep(500);
System.out.println("2.Place a doubt where unsure");
Thread.sleep(500);
System.out.println("3.If you play a mine the game ends and the player loses");
Thread.sleep(500);
System.out.println("4.The size of the board and number of mines will be entered
by the player");
Thread.sleep(500);
System.out.println("5.The player can place as many flags as he wants");
Thread.sleep(500);
System.out.println("6.When the player plays a land a number will be displayed
which will indiacte number of mines in its surrounding");
Thread.sleep(500);
System.out.println("7.The flag is to be entered by the 'm' symbol, play is to be
entered by the 'p' symbol and doubt is to be placed by the 'd' symbol");
Thread.sleep(500);
System.out.println("8.If the player places the flag without playing any mine and
fills the entire board by either flagin or playing the game will come to an end");
Thread.sleep(500);
System.out.println("9.500 points for flaging a mine");
Thread.sleep(500);
System.out.println("10.-100 points for flaging a field");
Thread.sleep(500);
System.out.println("11.0 points for a doubt");
Thread.sleep(500);
System.out.println("12. -200 points for exceeding the limits of placing the bomb
");
System.out.println();
System.out.println();
System.out.println("enter the grid size");
n1=sc.nextInt();
n2=sc.nextInt();
a = new int[n1][n2];
b1 = new char[n1][n2];
System.out.println("enter no. mines");
m=sc.nextInt();
for(i=0;i<n1;i++)
{
for(j=0;j<n2;j++)
{
System.out.print("_");
System.out.print("|");
}
System.out.println();
}
for(i=0;i<n1;i++)
{
for(j=0;j<n2;j++)
{
a[i][j]=0;
}
}
ab:for(i1=1;i1<=m;i1++) //placing bombs
{
d1=Math.random();
d2=Math.random();
c12=d1*n1;
r12=d2*n2;
c1=(int)c12;
r1=(int)r12;
if(a[c1][r1]>=10)
{--i1;
continue ab;
}
a[c1][r1]=10;
if(c1-1<0)
i++;
else
a[c1-1][r1]=1+a[c1-1][r1];
if(c1+1==n1)
i++;
else
a[c1+1][r1]=1+a[c1+1][r1];
if(r1-1<0)
i++;
else
a[c1][r1-1]=1+a[c1][r1-1];
if(r1+1==n2)
i++;
else
a[c1][r1+1]=1+a[c1][r1+1];
if(c1-1<0||r1-1<0)
i++;
else
a[c1-1][r1-1]=1+a[c1-1][r1-1];
if(c1+1==n1||r1+1==n2)
i++;
else
a[c1+1][r1+1]=1+a[c1+1][r1+1];
if(r1-1<0||c1+1==n1)
i++;
else
a[c1+1][r1-1]=1+a[c1+1][r1-1];
if(r1+1==n2||c1-1<0)
i++;
else
a[c1-1][r1+1]=1+a[c1-1][r1+1];
c1=0;
d1=0;
d2=0;
r1=0;
}
System.out.println("lets start the game");
abc:for(iT=div;iT<(n1*n2);iT++)
{
System.out.println("choose the row and comlumn");
t1=sc.nextInt()-1;
t2=sc.nextInt()-1;
t3=a[t1][t2];
System.out.println("enter m for mine, p for play ,d for doubt and e to save and
exit the game");
c9=sc.next();
if(c9.equalsIgnoreCase("m")) //for playing on
mine
{
b1[t1][t2]='*';
if(a[t1][t2]>=9)
score=score+500;
else
score=score-100;
i2++;
if(i2>=m)
{
System.out.println("****YOU HAVE REACHED YOUR LIMITS OF
PLACING THE BOMB****");
score=score-200;
}
}
else if(c9.equalsIgnoreCase("e"))
{FileWriter FW=new FileWriter("game_state.txt");
PrintWriter PW=new PrintWriter(FW);
PW.println (n1);
PW.println (n2);
PW.println (m);
PW.println (score);
PW.println (iT);
for(x=0;x<n1;x++)
{for(y=0;y<n2;y++)
PW.print (a[x][y]+" ");
PW.println();}
for(x=0;x<n1;x++)
{for(y=0;y<n2;y++)
PW.print(b1[x][y]+" ");
PW.println();}
PW.close();
FW.close();
break c;
}
else if(c9.equalsIgnoreCase("p"))
{
j9=(char)(a[t1][t2]+'0');
b1[t1][t2]=j9;
if(a[t1][t2]>=9)
{
System.out.println(" ***oops .wrong move***");
System.out.println(" MINE FOUND");
break abc;
}
else
score=score+100;
}
else if(c9.equalsIgnoreCase("d"))
b1[t1][t2]='d';
for(i=0;i<n1;i++)
{
for(j=0;j<n2;j++)
{if(b1[i][j]==' ')
System.out.print("_");
else
System.out.print(b1[i][j]);
System.out.print("|");
}
System.out.println();
}
}
System.out.println();
System.out.println();
System.out.println("*****location of bombs*****");
for(i=0;i<n1;i++)
{
for(j=0;j<n2;j++)
{if(a[i][j]<5)
System.out.print("_");
else if(a[i][j]>=8)
System.out.print("b" );
System.out.print("|");
}
System.out.println();
}
System.out.println("GAME OVER");
System.out.println("FINAL SCORE"+score);
}
else if (pl == 'R')
{
try{
FileReader fw=new FileReader("game_state.txt");
BufferedReader br = new BufferedReader(fw);
{
n1 = Integer.parseInt(br.readLine());
n2 = Integer.parseInt(br.readLine());
m = Integer.parseInt(br.readLine());
score = Integer.parseInt(br.readLine());
div=Integer.parseInt(br.readLine());
a = new int[n1][n2];
b1 = new char[n1][n2];
for (i = 0; i < n1; i++) {
for (j = 0; j < n2; j++) {
a[i][j] = Integer.parseInt(br.readLine());
b1[i][j] = (char) br.read();
br.read();
}
}
System.out.println("Game loaded successfully!");
} }catch (IOException e) {
System.out.println("Error loading saved game. Starting a new
game...");
pl = 'N'; // Fallback to new game
}
}
else
{System.out.println();
System.out.println();
System.out.println();
System.out.print("GAME ENDS");
}
}
Output for the code
NAME-Shubhi Singh
UID-8156785