minesweeper

code
problem
shortest-path

#1

import java.io.;
import java.util.
;
class minesweeper
{
int board[][],n;
minesweeper()
{
board=new int[0][0];
n=0;
}
void assign()
{
int i=0,j=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
int c=0;
if(board*[j]!=9)
{
int k=1;
if(i+k<n&&i+k>=0)
if(board[i+k][j]==9)
c++;
if(j+k<n&&j+k>=0)
if(board*[j+k]==9)
c++;
if(i-k<n&&i-k>=0)
if(board[i-k][j]==9)
c++;
if(j-k<n&&j-k>=0)
if(board*[j-k]==9)
c++;
if(i+k<n&&j+k<n&&i+k>=0&&j+k>=0)
if(board[i+k][j+k]==9)
c++;
if(i-k<n&&j-k<n&&i-k>=0&&j-k>=0)
if(board[i-k][j-k]==9)
c++;
if(i-k<n&&j+k<n&&i-k>=0&&j+k>=0)
if(board[i-k][j+k]==9)
c++;
if(i+k<n&&j-k<n&&i+k>=0&&j-k>=0)
if(board[i+k][j-k]==9)
c++;
}
if(c>0)
board*[j]=c;
}
}
}
void setfill()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
do
{
System.out.print(“Enter dimension length “);
n=Integer.parseInt(br.readLine());
if(n<=1)
System.out.println(“Sorry, minimum side length is 2”);
}while(n<=1);
board=new int[n][n];
int count=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
board*[j]=0;
}
for(int i=0;i<n;i++)
{
int c=(int)(Math.random()100%n);
board
[c]=9;
}
assign();
}
private void fill()
{
board=new int[n][n];
int count=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
board*[j]=0;
}
for(int i=0;i<n;i++)
{
int c=(int)(Math.random()100%n);
board
[c]=9;
}
assign();
}
void chance()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i=0, j=0;
do
{
System.out.print(”\f”);
nulldisplay();
if(wincheck())
{
smiley();
break;
}
do
{
System.out.println(“Enter your choice”);
String ch=br.readLine();
StringTokenizer st=new StringTokenizer(ch,",");
i=Integer.parseInt(st.nextToken())-1;
j=Integer.parseInt(st.nextToken())-1;
if(i>=n||j>=n||board*[j]>10&&board*[j]<=18)
System.out.println(“Sorry, this position is out of reach”);
}while(i>=n||j>=n||board*[j]>10&&board*[j]<=18);
int k=1;
if(board*[j]==0)
{
board*[j]+=10;
if(i+k<n&&i+k>=0)
if(board[i+k][j]<9)
board[i+k][j]+=10;
if(j+k<n&&j+k>=0)
if(board*[j+k]<9)
board*[j+k]+=10;
if(i-k<n&&i-k>=0)
if(board[i-k][j]<9)
board[i-k][j]+=10;
if(j-k<n&&j-k>=0)
if(board*[j-k]<9)
board*[j-k]+=10;
if(i+k<n&&j+k<n&&i+k>=0&&j+k>=0)
if(board[i+k][j+k]<9)
board[i+k][j+k]+=10;
if(i-k<n&&j-k<n&&i-k>=0&&j-k>=0)
if(board[i-k][j-k]<9)
board[i-k][j-k]+=10;
if(i-k<n&&j+k<n&&i-k>=0&&j+k>=0)
if(board[i-k][j+k]<9)
board[i-k][j+k]+=10;
if(i+k<n&&j-k<n&&i+k>=0&&j-k>=0)
if(board[i+k][j-k]<9)
board[i+k][j-k]+=10;
}
else if(board*[j]>0&&board*[j]<9)
board*[j]+=10;
else
{
board*[j]=90;
System.out.print("\f");
sad();
System.out.println(“Sorry. GAME OVER…”);
System.out.println(“You have hit a mine”);
display();
}
}while(board*[j]!=90);
}
void display()
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(board*[j]==9)
System.out.print(""+" ");
else if(board
[j]==90)
System.out.print("()"+" ");
else if(board
[j]==10)
System.out.print((char)(981)+" “);
else if(board*[j]>10)
System.out.print((board*[j]-10)+” “);
else System.out.print(“X”+” ");
}
System.out.println();
}
}
void nulldisplay()
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{

                if(board*[j]==10)
                    System.out.print((char)(981)+"	");
                else if(board*[j]>10&&board*[j]!=90)
                    System.out.print((board*[j]-10)+"	");
                else System.out.print("X"+"	");
            }
            System.out.println();
        }
    }
    boolean wincheck()
    {
        boolean f=true;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(board*[j]<9)
                    {
                        f=false; break;
                    }
            }
        }
        return f;
    }
    void smiley()
    {
        System.out.println("|(o)     (o)|");
        System.out.println("|     O     |");
        System.out.println("|  l_____l  |");
    }
    void sad()
    {
        System.out.println("|(.)     (.)|");
        System.out.println("|     O     |");
        System.out.println("|    ____   |");
        System.out.println("|   /    \\ |");
    }
    static void main()throws IOException
    {
        minesweeper m=new minesweeper();
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int c=0,ch=0;
        do
        {
            System.out.println("1. New Game...
  1. Change Settings
  2. Exit");
    ch=Integer.parseInt(br.readLine());
    switch(ch)
    {
    case 2:m.setfill();
    case 1:if(c==0)
    {
    c=1;
    m.setfill();
    m.chance();
    }
    else
    {
    m.fill();
    m.chance();
    }
    break;
    case 3:break;
    default:System.out.print(“Wrong Choice, Enter again”);
    }
    }while(ch!=3);
    }
    }

how to improve my program? And also shorten my code???


#2

Better do your homework yourself.