Sie sind auf Seite 1von 4

class NumberOfBombsOutOfRangeException extends ClassNotFoundException

{}
class M
{
static void setField( int[][] a,Boolean[][] b,int bombs)
{
for(int i=0;i<bombs;)
{
int x= (int)(Math.random()*(a.length));
int y= (int)(Math.random()*(a[0].length));
if(a[x][y]==9)
continue;
else;
{
a[x][y]=9;
i++;
}
}
for(int i=0;i<(b.length);i++)
{
for(int j=0;j<(b[0].length);j++)
{
b[i][j]= false;
}
}
for(int i=0;i<(a.length);i++)
{
for(int j=0;j<(a[0].length);j++)
{
if(a[i][j]!= 9)
{
int n=0;
for(int k=(i-1);(k<=(i+1))&&(k<(a.length));k++)
{
for(int l=(j-1);(l<=(j+1))&&(l<(a[0].length));l++)
{
if((k<0)||(l<0))
continue;
else
{
if(a[k][l]== 9)
n++;
}
}
}
a[i][j]=n;
}
}
}
}
static void makeDisplayable (int[][] a, Boolean[][] b, int i,int j)
{
a[i][j]=-1;
b[i][j]=true;
for(int k=(i-1);(k<=(i+1))&&(k<(a.length));k++)
{
for(int l=(j-1);(l<=(j+1))&&(l<(a[0].length));l++)

{
if((k<0)||(l<0))
continue;
else
{
b[k][l]= true;
if(a[k][l]==0)
makeDisplayable(a,b,k,l);
}
}
}
}
static void move( int[][] a,Boolean[][] b,int i,int j)
{
if(a[i][j]== 9)
{
for(int p=0;p<(b.length);p++)
{
for(int q=0;q<(b[0].length);q++)
{
if(a[p][q]==9)
b[p][q]=true;
}
}
displayField(a,b);
System.out.println("ohh....u hit a mine!!!!");
System.out.println("Better luck next time.....!!!!!");
System.out.println("\n");
System.out.println("$---------------------------------------!!
GAME OVER !!-------------------------------------$");
System.exit(0);
}
else if (a[i][j]== 0)
makeDisplayable(a,b,i,j);
else if ((a[i][j]<=8)&&(a[i][j]>=1))
b[i][j]=true;
}
static void displayField(int[][] a,Boolean[][] b)
{
System.out.print("\n");
System.out.print(" ");
for(int r=0;r<(2*a[0].length);r++)
System.out.print("---");
System.out.println("");
for(int i=0;i<(a.length);i++)
{
if((a.length-i)>=10)
System.out.print(a.length-i+" |");
else
System.out.print(a.length-i+" |");
for(int j=0;j<(a[0].length);j++)
{
if(b[i][j]== false)
System.out.print(" ?"+" |");
else if (a[i][j]==-1)
System.out.print(" "+" |");

else if(a[i][j]==9)
System.out.print(" **"+" |");
else
System.out.print(" "+a[i][j]+" |");
}
System.out.print("\n");
System.out.print(" ");
for(int r=0;r<(2*a[0].length);r++)
System.out.print("---");
System.out.print(" ");
System.out.println("");
}
System.out.println("");
System.out.print(" ");
for(int i=1;i<=(a[0].length);i++)
{
if(i<10)
System.out.print("
"+i);
else
System.out.print("
"+i);
}
System.out.println("");
}
static Boolean gameContinues(int[][] a,Boolean[][] b)
{
int x=0;
for(int i=0;i<(a.length);i++)
{
for(int j=0;j<(a[0].length);j++)
{
if(b[i][j]==false)
if(a[i][j]!=9)
x++;
}
}
if(x==0)
return false;
else
return true;
}
}
class Main
{
public static void main(String[] args)
{
System.out.println("$--------------------------------!! MINESWE
EPER !!----------------------------$");
System.out.println("\n");
System.out.println("ENTER DIMENSIONS OF THE PLAY-FIELD ..... ");
try{
int m= Integer.parseInt(System.console().readLine("Enter horizon
tal length : "));
int n= Integer.parseInt(System.console().readLine("Enter vertica
l length : "));
System.out.println("\n");
int[][] a= new int[m][n];

Boolean[][] b= new Boolean[m][n];


NumberOfBombsOutOfRangeException excp= new NumberOfBombsOutOfRan
geException();
System.out.println("SETUP THE DIFFICULTY LEVEL.....(Number of bo
mbs placed should not be greater than half the number of places in the field)...
. ");
int bombs= Integer.parseInt(System.console().readLine("Enter nu
mber of bombs to be spread across the field: "));
if(bombs>((m*n)/2))
throw excp;
System.out.println("Difficulty level of game is: "+(int)(((doubl
e)bombs/(double)(m*n))*200)+"%");
M.setField(a,b,bombs);
System.out.println("\n");
M.displayField(a,b);
System.out.println("$---------------------------------!! THE GA
ME STARTS !!-------------------------------------$");
System.out.println("\n");
while(M.gameContinues(a,b))
{
System.out.println("Enter your move......");
int y= Integer.parseInt(System.console().readLine("Enter
horizontal number : "));
int x= Integer.parseInt(System.console().readLine("Enter
vertical number: "));
M.move(a,b,a.length-x,y-1);
M.displayField(a,b);
}
System.out.println("\n");
System.out.println("
!!!!BRAVO, YOU WON!!!!");
System.out.println("\n");
System.out.println("$---------------------------------!! THE GA
ME ENDS !!----------------------------------------$");
}
catch(NumberFormatException e)
{
System.out.println("You should enter only an integer!!! RESTART
the game...");
}
catch(NumberOfBombsOutOfRangeException excp)
{
System.out.println("Number of bombs grater than the allowed maxi
mum!!! RESTART the game....");
}
catch(Exception s)
{
System.out.println("Unexpected Error!!! The game needs to close.
...");
}
}
}

Das könnte Ihnen auch gefallen