You are not logged in. Please login at www.codechef.com to post your questions!

×

minesweeper

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[i][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[i][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[i][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[i][j]=c;
            }
        }
    }
    void setfill()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        do
        {
            System.out.print("Enter dimension length\t");
            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[i][j]=0;
        }
        for(int i=0;i<n;i++)
        {
            int c=(int)(Math.random()*100%n);
            board[i][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[i][j]=0;
        }
        for(int i=0;i<n;i++)
        {
            int c=(int)(Math.random()*100%n);
            board[i][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[i][j]>10&&board[i][j]<=18)
                    System.out.println("Sorry, this position is out of reach");
            }while(i>=n||j>=n||board[i][j]>10&&board[i][j]<=18);
            int k=1;
            if(board[i][j]==0)
            {
                board[i][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[i][j+k]<9)
                            board[i][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[i][j-k]<9)
                            board[i][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[i][j]>0&&board[i][j]<9)
                board[i][j]+=10;
            else
            {
                board[i][j]=90;
                System.out.print("\f");
                sad();
                System.out.println("Sorry. GAME OVER...");
                System.out.println("You have hit a mine");
                display();
            }
        }while(board[i][j]!=90);
    }
    void display()
    {
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(board[i][j]==9)
                    System.out.print("*"+"\t");
                else if(board[i][j]==90)
                    System.out.print("(*)"+"\t");
                else if(board[i][j]==10)
                    System.out.print((char)(981)+"\t");
                else if(board[i][j]>10)
                    System.out.print((board[i][j]-10)+"\t");
                else System.out.print("X"+"\t");
            }
            System.out.println();
        }
    }
    void nulldisplay()
    {
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {

                if(board[i][j]==10)
                    System.out.print((char)(981)+"\t");
                else if(board[i][j]>10&&board[i][j]!=90)
                    System.out.print((board[i][j]-10)+"\t");
                else System.out.print("X"+"\t");
            }
            System.out.println();
        }
    }
    boolean wincheck()
    {
        boolean f=true;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(board[i][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...\n2. Change Settings\n3. 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???

asked 24 Oct '12, 14:42

archieami's gravatar image

0★archieami
-1111
accept rate: 0%

edited 24 Oct '12, 15:01

betlista's gravatar image

3★betlista ♦♦
16.9k49115225

Better do your homework yourself.

(24 Oct '12, 15:59) rajneesh2k103★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×592
×242
×116

question asked: 24 Oct '12, 14:42

question was seen: 2,436 times

last updated: 24 Oct '12, 15:59