Google Answers Logo
View Question
 
Q: minesweeper ( Answered,   1 Comment )
Question  
Subject: minesweeper
Category: Computers > Programming
Asked by: rejct-ga
List Price: $15.00
Posted: 22 Oct 2002 15:58 PDT
Expires: 21 Nov 2002 14:58 PST
Question ID: 88265
1)	The playing board consists of 81 adjacent cells arranged as 9 rows
and 9 columns.
2)	Some of these cells will randomly chosen to contain bombs. There
will be 10 bombs in total.
3)	Initially, all playing cells are covered.
4)	A player must attempt to uncover squares, one by one.
5)	If the square stores a bomb, the player loses. Otherwise, a number
should be revealed that indicates the number of bombs in adjacent
squares.
6)	If that number is 0, then you should automatically recursively
uncover all adjacent squares as well, that are not already cleared.
7)	Play continues until the player has either chosen a bomb, or
uncovered all squares that do not contain a bomb. If the latter
occurs, the player wins.

This is a short explanation of the game, if you have questions about
the details, (ie. what would happen in a particular situation) please
ask either the TAs or the lecturers.

Output specifications
You may alter your output to make it fancy in any way that you like,
but your output must conform to these specifications:

1) In the beginning of the game, you prompt the user with a reasonable
display of the covered board, with each board cell clearly labeled
with its subscripts.

2) After the board is displayed, you must prompt the user for a square
to uncover.

3) Your program should then re-prompt the user with a new picture of
the board showing its status after the player's move has been
executed.

This pattern should persist until either the player uncovers a bomb,
at which point an appropriate message should be printed to the screen
and the game ended, or until the player wins. Another appropriate
message should be printed to the screen in this situation.

A Sample Run:
In the following sample run, the covered cells are displayed as ‘_’,
uncovered cells are either blank (if there are no adjacent bombs) or
contains a number indicating the number of bombs in the 8 adjacent
cells. The bombs are represented with the ‘*’ character.

   0 1 2 3 4 5 6 7 8
--------------------
0| _ _ _ _ _ _ _ _ _
1| _ _ _ _ _ _ _ _ _
2| _ _ _ _ _ _ _ _ _
3| _ _ _ _ _ _ _ _ _
4| _ _ _ _ _ _ _ _ _
5| _ _ _ _ _ _ _ _ _
6| _ _ _ _ _ _ _ _ _
7| _ _ _ _ _ _ _ _ _
8| _ _ _ _ _ _ _ _ _

Enter cell coordinates:0 0

   0 1 2 3 4 5 6 7 8
--------------------
0|           1 _ 1  
1| 2 2 1     1 _ 1  
2| _ _ 2   1 _ 1    
3| _ _ 2   1 _ 1    
4| _ _ 1   1 1 1    
5| _ _ 1            
6| _ _ _ 1          
7| _ _ _ 1          
8| _ _ _ 1          

Enter cell coordinates:3 0		

(NOTE: Here the cells are uncovered recursively. Because there were no
bombs adjacent to the cell [0][0] . The recursion stops when we hit a
cell with at least one adjacent bomb.)

   0 1 2 3 4 5 6 7 8
--------------------
0|           1 _ 1  
1| 2 2 1     1 _ 1  
2| _ _ 2   1 _ 1    
3| 3 _ 2   1 _ 1    
4| _ _ 1   1 1 1    
5| _ _ 1            
6| _ _ _ 1          
7| _ _ _ 1          
8| _ _ _ 1          

Enter cell coordinates:2 1


 

   0 1 2 3 4 5 6 7 8
--------------------
0|           1 * 1  
1| 2 2 1     1 1 1  
2| * * 2   1 1 1    
3| 3 * 2   1 * 1    
4| 1 1 1   1 1 1    
5| 2 2 1            
6| * * 2 1          
7| 3 4 * 1          
8| 1 * 2 1          

Ooops! You hit a bomb...

Game is over !
Answer  
Subject: Re: minesweeper
Answered By: rbnn-ga on 22 Oct 2002 17:34 PDT
 
If this is minesweeper, shouldn't they be called mines, not bombs?

Anyway, I wrote a program, enclosed, to the specification. I lightly
commented it; you should put in more comments perhaps. If you have any
questions, please do not hesitate to ask.

As general rule, I like to recommend that Java programmers try
Topcoder, at http://www.topcoder.com . This sponsors various
programming contests; you can use Java, C++, or C#. It's a lot of fun
and an excellent way to improve programming skills (I'm preparing for
round 3 of their $150,000 invitational Wednesday).

The only things to note about this program are the error checking on
the input. Also, I added a feature whereby if you call the program
with three arguments, like:

java MineSweeper 3 3 2

it will create a board with 3 rows, 3 columns, and 2 bombs in it. This
is useful for checking that the program actually works, since it is
much  easier to win a small board with few bombs.

I am enclosing the program, followed by some sample output

import java.io.BufferedReader;
public class MineSweeper{
    int nrows; //number of rows (9)
    int ncolumns; // number of columns  (9)
    int nbombs; //number of bombs in the grid
    boolean[][]bombs; // true if corresponding grid square has a bomb
    boolean[][]covered; // true if corresponding grid square is
covered
    int[][]bombneighbors; //number of bombs that are neighbors of each
non-bomb grid square
    int nremaining; // number of non-bomb covered grid squares
remaining
    static BufferedReader reader; // reader from where standard input
is to be read

    public MineSweeper (){}
    public String toString(){
	StringBuffer b = new StringBuffer();
	b.append("\n  ");
	for (int i=0;i<ncolumns;++i)b.append(" "+i);
	b.append('\n');
	b.append("--");
	for (int i=0;i<ncolumns;++i)b.append("--");
	b.append("\n");
	for (int r=0;r<nrows;++r){
	    b.append(r+"|");
	    for (int c=0;c<ncolumns;++c){
		b.append(" ");
		char out;
		if (covered[r][c])
		    out='-';
		else if (bombs[r][c])
		    out='*';
		else {
		    int neighbors=bombneighbors[r][c];
		    if (neighbors==0)
			out=' ';
		    else 
			out=(char)(neighbors+'0');
		}
		b.append(out);
	    }
	    b.append('\n');
	}
	return b.toString();
    }

    //Print the board, and ask for input, until done
    public void play(){
	int row;
	int column;
	String line;

	while(true){
	    System.out.println(toString());
	    if (nremaining==0){ //we are done, player won
		for (int r=0;r<nrows;++r)for (int
c=0;c<ncolumns;++c)covered[r][c]=false;
		System.out.print(this);
		System.out.println("\nYou win");
		return;
	    }
	    System.out.print("\nEnter cell coordinates: ");
	    try{ line=reader.readLine();}
	    catch(Exception e){System.err.println("IO error,
exiting");return;}

	    if (line==null||line.equals(""))break; //enter a blank line to
exit
	    String[]nums=line.trim().split(" +"); //parse input
	    if (nums.length!=2){
		System.out.println("Input two numbers please");
		continue;
	    }
	    try {
		row=Integer.parseInt(nums[0]);
		column=Integer.parseInt(nums[1]);
	    }
	    catch(NumberFormatException e){
		System.out.println("Invalid input: please input two digits");
		continue;
	    }
	    if (!valid(row,column)){
		System.out.println("Invalid input: row or column out of range");
		continue;
	    }
	    if (!covered[row][column]){
		System.out.println("Cell at: "+row+" "+column+" is already
uncovered, try again");
		continue;
	    }
	    uncover(row,column);
	    if(bombs[row][column]){
		for (int i=0;i<nrows;++i)for (int
j=0;j<ncolumns;++j)covered[i][j]=false;
		System.out.print(this);
		System.out.println("\nOoops! You hit a bomb...");
		return;
	    }
	}
    }

    //return random integer between 0 and max-1 inclusive.
    int random(int max){
	return (int)(Math.random()*max);
    }

    public static void main(String[] args){
	try{
	    reader=new java.io.BufferedReader(new
java.io.InputStreamReader(System.in));
	}
	catch(Exception e){
	    System.err.println("Could not create reader, exiting");
	    System.exit(1);
	}
	
	MineSweeper m=new MineSweeper();
	if (args.length==3)
	    m.createRandomBoard(Integer.parseInt(args[0]),
				Integer.parseInt(args[1]),
				Integer.parseInt(args[2]));
	else
	    m.createRandomBoard(9,9,10);
	m.play();
	System.out.println("Game is over");
    }

    boolean valid(int row,int column){
	return row>=0&&row<nrows&&column>=0&&column<ncolumns;
    }
    
    void uncover(int row, int column){
	if (valid(row,column)&&covered[row][column]){
	    covered[row][column]=false;
	    --nremaining;
	    if (bombneighbors[row][column]==0) //we have no bomb neighbors so
recursively uncover our neighbors
		for (int newrow=row-1;newrow<=row+1;++newrow)
		    for (int newcolumn=column-1;newcolumn<=column+1;++newcolumn)
			uncover(newrow,newcolumn);
	}
    }



    void createRandomBoard(int nrows,int ncolumns,int nbombs){
	this.nrows=nrows;
	this.ncolumns=ncolumns;
	this.nbombs=nbombs;
	covered=new boolean[nrows][ncolumns];
	for (int r=0;r<nrows;++r)for (int
c=0;c<ncolumns;++c)covered[r][c]=true;
	bombs=new boolean[nrows][ncolumns];
	bombneighbors=new int[nrows][ncolumns];
	nremaining=nrows*ncolumns-nbombs; //initial number of covered squares
without bombs
	int bombsplaced=0;
	while(bombsplaced<nbombs){
	    int row=random(nrows);
	    int column=random(ncolumns);
	    if (!bombs[row][column]){
		bombs[row][column]=true;
		bombsplaced++;
	    }
	}
	for (int r=0;r<nrows;++r)
	    for (int c=0;c<ncolumns;++c)
		if (!bombs[r][c])
		    for (int newr=r-1;newr<=r+1;++newr)
			for (int newc=c-1;newc<=c+1;++newc)
			    if (valid(newr,newc)&&bombs[newr][newc])
				++bombneighbors[r][c];
    }
}

-----------------------------------------------Test
inputs==============================================

/cygdrive/c/rbnn/google: java MineSweeper

   0 1 2 3 4 5 6 7 8
--------------------
0| - - - - - - - - -
1| - - - - - - - - -
2| - - - - - - - - -
3| - - - - - - - - -
4| - - - - - - - - -
5| - - - - - - - - -
6| - - - - - - - - -
7| - - - - - - - - -
8| - - - - - - - - -


Enter cell coordinates: 5 6

   0 1 2 3 4 5 6 7 8
--------------------
0| - - - - - - - - -
1| - - - - - - - - -
2| - - - - - - - - -
3| - - - - - - - - -
4| - - - - - - - - -
5| - - - - - - 1 - -
6| - - - - - - - - -
7| - - - - - - - - -
8| - - - - - - - - -


Enter cell coordinates: 8 7

   0 1 2 3 4 5 6 7 8
--------------------
0| - - - 1   1 - - -
1| - 1 1 1   2 - - -
2| - 1       1 - - -
3| - 1       1 1 - -
4| - 1 1 1 1   1 - -
5| - - - - 1   1 - -
6| - - - 1 1   1 2 -
7| - - - 1       1 1
8| - - - 1          


Enter cell coordinates: 7 0

   0 1 2 3 4 5 6 7 8
--------------------
0| - - - 1   1 - - -
1| - 1 1 1   2 - - -
2| - 1       1 - - -
3| - 1       1 1 - -
4| 1 1 1 1 1   1 - -
5|     1 - 1   1 - -
6|     1 1 1   1 2 -
7|   1 1 1       1 1
8|   1 - 1          


Enter cell coordinates: 8 2

   0 1 2 3 4 5 6 7 8
--------------------
0|   1 * 1   1 * 2 1
1|   1 1 1   2 3 * 1
2| 1 1       1 * 2 1
3| * 1       1 1 2 1
4| 1 1 1 1 1   1 2 *
5|     1 * 1   1 * 3
6|     1 1 1   1 2 *
7|   1 1 1       1 1
8|   1 * 1          

Ooops! You hit a bomb...
Game is over

# we use smaller boards to check that winning works...

java MineSweeper 3 3 1

   0 1 2
--------
0| - - -
1| - - -
2| - - -


Enter cell coordinates: 0 0

   0 1 2
--------
0|   1 -
1|   1 1
2|      


   0 1 2
--------
0|   1 *
1|   1 1
2|      

You win
Game is over
/cygdrive/c/rbnn/google: java MineSweeper

   0 1 2 3 4 5 6 7 8
--------------------
0| - - - - - - - - -
1| - - - - - - - - -
2| - - - - - - - - -
3| - - - - - - - - -
4| - - - - - - - - -
5| - - - - - - - - -
6| - - - - - - - - -
7| - - - - - - - - -
8| - - - - - - - - -


Enter cell coordinates: 0 0

   0 1 2 3 4 5 6 7 8
--------------------
0| 1 - - - - - - - -
1| - - - - - - - - -
2| - - - - - - - - -
3| - - - - - - - - -
4| - - - - - - - - -
5| - - - - - - - - -
6| - - - - - - - - -
7| - - - - - - - - -
8| - - - - - - - - -


Enter cell coordinates: 0 1

   0 1 2 3 4 5 6 7 8
--------------------
0| 1 1 - - - - - - -
1| - - - - - - - - -
2| - - - - - - - - -
3| - - - - - - - - -
4| - - - - - - - - -
5| - - - - - - - - -
6| - - - - - - - - -
7| - - - - - - - - -
8| - - - - - - - - -


Enter cell coordinates: 8 8

   0 1 2 3 4 5 6 7 8
--------------------
0| 1 1 - - 1        
1| - - - - 1 1 1 1  
2| - - - - - - - 1  
3| - - - - - - - 1  
4| - - - - - - - 1  
5| - - - - - - - 1  
6| - - - - - - 1 1  
7| - - - - - - 1    
8| - - - - - - 1    


Enter cell coordinates: 5 2

   0 1 2 3 4 5 6 7 8
--------------------
0| 1 1 - - 1        
1| - - - - 1 1 1 1  
2| - - - - - - - 1  
3| - - - - - - - 1  
4| - - - - - - - 1  
5| - - 1 - - - - 1  
6| - - - - - - 1 1  
7| - - - - - - 1    
8| - - - - - - 1    


Enter cell coordinates: 0 1
Cell at: 0 1 is already uncovered, try again

   0 1 2 3 4 5 6 7 8
--------------------
0| 1 1 - - 1        
1| - - - - 1 1 1 1  
2| - - - - - - - 1  
3| - - - - - - - 1  
4| - - - - - - - 1  
5| - - 1 - - - - 1  
6| - - - - - - 1 1  
7| - - - - - - 1    
8| - - - - - - 1    


Enter cell coordinates: 7 0

   0 1 2 3 4 5 6 7 8
--------------------
0| 1 1 - - 1        
1| - - - - 1 1 1 1  
2| - - - - - - - 1  
3| - - - - - - - 1  
4| - - - - - - - 1  
5| - - 1 - - - - 1  
6| - - - - - - 1 1  
7| 2 - - - - - 1    
8| - - - - - - 1    


Enter cell coordinates: hello
Input two numbers please

   0 1 2 3 4 5 6 7 8
--------------------
0| 1 1 - - 1        
1| - - - - 1 1 1 1  
2| - - - - - - - 1  
3| - - - - - - - 1  
4| - - - - - - - 1  
5| - - 1 - - - - 1  
6| - - - - - - 1 1  
7| 2 - - - - - 1    
8| - - - - - - 1    


Enter cell coordinates: -3 4
Invalid input: row or column out of range [NOTE THE ERROR CHECKING
HERE -rbnn]

   0 1 2 3 4 5 6 7 8
--------------------
0| 1 1 - - 1        
1| - - - - 1 1 1 1  
2| - - - - - - - 1  
3| - - - - - - - 1  
4| - - - - - - - 1  
5| - - 1 - - - - 1  
6| - - - - - - 1 1  
7| 2 - - - - - 1    
8| - - - - - - 1    


Enter cell coordinates: 8 6
Cell at: 8 6 is already uncovered, try again

   0 1 2 3 4 5 6 7 8
--------------------
0| 1 1 - - 1        
1| - - - - 1 1 1 1  
2| - - - - - - - 1  
3| - - - - - - - 1  
4| - - - - - - - 1  
5| - - 1 - - - - 1  
6| - - - - - - 1 1  
7| 2 - - - - - 1    
8| - - - - - - 1    


Enter cell coordinates: 8 5

   0 1 2 3 4 5 6 7 8
--------------------
0| 1 1 1 * 1        
1| * 1 1 1 1 1 1 1  
2| 1 1       1 * 1  
3| 1 1 1 1 1 1 1 1  
4| * 1 1 * 1 1 1 1  
5| 1 1 1 1 1 1 * 1  
6| 1 1 1     1 1 1  
7| 2 * 2 1 2 1 1    
8| * 2 2 * 2 * 1    

Ooops! You hit a bomb...
Game is over

Clarification of Answer by rbnn-ga on 22 Oct 2002 17:38 PDT
I'm sorry to see that my indentation of the program did not translate
very well to the to the google format; I'm not sure there is a good
standard way to upload programs.

Most editors have an automatic indentation format, so you should be
able to import the code into your editor and reformat it. If you have
trouble using this or the indentation is a problem, let me know and I
will try and think of another way to get the program to you, or I will
explore alternative indentation strategies.

Note that I think some of the long comments might have been split onto
two lines.

Clarification of Answer by rbnn-ga on 22 Oct 2002 17:54 PDT
Well, I went through and tried to shorten all the long lines. 

I hope this version looks better in the HTML; it is the same program,
but I hope reformatted to look better on your computer screen when you
view it in HTML:


import java.io.BufferedReader;
public class MineSweeper{
    //number of rows (9)
    int nrows; 
    // number of columns  (9)
    int ncolumns; 
    //number of bombs in the grid
    int nbombs; 
    // true if corresponding grid square has a bomb
    boolean[][]bombs; 
    // true if corresponding grid square is covered
    boolean[][]covered; 
    //number of bombs that are neighbors of each non-bomb grid square
    int[][]bombneighbors; 
    // number of non-bomb covered grid squares remaining
    int nremaining;
    // reader from where standard input is to be read
    static BufferedReader reader; 

    public MineSweeper (){}
    public String toString(){
        StringBuffer b = new StringBuffer();
        b.append("\n  ");
        for (int i=0;i<ncolumns;++i)
            b.append(" "+i);
        b.append('\n');
        b.append("--");
        for (int i=0;i<ncolumns;++i)
            b.append("--");
        b.append("\n");
        for (int r=0;r<nrows;++r){
            b.append(r+"|");
            for (int c=0;c<ncolumns;++c){
                b.append(" ");
                char out;
                if (covered[r][c])
                    out='-';
                else if (bombs[r][c])
                    out='*';
                else {
                    int neighbors=bombneighbors[r][c];
                    if (neighbors==0)
                        out=' ';
                    else 
                        out=(char)(neighbors+'0');
                }
                b.append(out);
            }
            b.append('\n');
        }
        return b.toString();
    }

    //Print the board, and ask for input, until done
    public void play(){
        int row;
        int column;
        String line;

        while(true){
            System.out.println(toString());

            if (nremaining==0){ 
                //we are done, player won
                for (int r=0;r<nrows;++r)
                    for (int c=0;c<ncolumns;++c)
                        covered[r][c]=false;
                System.out.print(this);
                System.out.println("\nYou win");
                return;
            }
            System.out.print("\nEnter cell coordinates: ");
            try{ line=reader.readLine();}
            catch(Exception e)
                {System.err.println("IO error, exiting");return;}

            //enter a blank line to exit
            if (line==null||line.equals(""))break;

            //parse input 
            String[]nums=line.trim().split(" +"); 
            if (nums.length!=2){
                System.out.println("Input two numbers please");
                continue;
            }
            try {
                row=Integer.parseInt(nums[0]);
                column=Integer.parseInt(nums[1]);
            }
            catch(NumberFormatException e){
                System.out.println
                    ("Invalid input: please input two digits");
                continue;
            }
            if (!valid(row,column)){
                System.out.println
                    ("Invalid input: row or column out of range");
                continue;
            }
            if (!covered[row][column]){
                System.out.println
                    ("Cell at: "+row+" "+column+" is already
uncovered, try again");
                continue;
            }
            uncover(row,column);
            if(bombs[row][column]){
                for (int i=0;i<nrows;++i)
                    for (int j=0;j<ncolumns;++j)
                        covered[i][j]=false;
                System.out.print(this);
                System.out.println
                    ("\nOoops! You hit a bomb...");
                return;
            }
        }
    }

    //return random integer between 0 and max-1 inclusive.
    int random(int max){
        return (int)(Math.random()*max);
    }

    public static void main(String[] args){
        try{
            reader=
                new java.io.BufferedReader
                (new java.io.InputStreamReader
                 (System.in));
        }
        catch(Exception e){
            System.err.println
                ("Could not create reader, exiting");
            System.exit(1);
        }
        
        MineSweeper m=new MineSweeper();
        if (args.length==3)
            m.createRandomBoard
                (Integer.parseInt(args[0]),
                 Integer.parseInt(args[1]),
                 Integer.parseInt(args[2]));
        else
            m.createRandomBoard(9,9,10);
        m.play();
        System.out.println("Game is over");
    }

    boolean valid(int row,int column){
        return row>=0&&
            row<nrows&&
            column>=0&&
            column<ncolumns;
    }
    
    void uncover(int row, int column){
        if (valid(row,column)&&
            covered[row][column]){
            covered[row][column]=false;
            --nremaining;
 //we have no bomb neighbors so recursively uncover our neighbors
            if (bombneighbors[row][column]==0) 
                for (int newrow=row-1;
                     newrow<=row+1;
                     ++newrow)
                    for (int newcolumn=column-1;
                         newcolumn<=column+1;
                         ++newcolumn)
                        uncover(newrow,newcolumn);
        }
    }



    void createRandomBoard(int nrows,
                           int ncolumns,
                           int nbombs){
        this.nrows=nrows;
        this.ncolumns=ncolumns;
        this.nbombs=nbombs;
        covered=new boolean[nrows][ncolumns];
        for (int r=0;r<nrows;++r)
            for (int c=0;c<ncolumns;++c)
                covered[r][c]=true;
        bombs=new boolean[nrows][ncolumns];
        bombneighbors=new int[nrows][ncolumns];
        //initial number of covered squares without bombs
        nremaining=nrows*ncolumns-nbombs; 
        int bombsplaced=0;
        while(bombsplaced<nbombs){
            int row=random(nrows);
            int column=random(ncolumns);
            if (!bombs[row][column]){
                bombs[row][column]=true;
                bombsplaced++;
            }
        }
        for (int r=0;r<nrows;++r)
            for (int c=0;c<ncolumns;++c)
                if (!bombs[r][c])
                    for (int newr=r-1;
                         newr<=r+1;
                         ++newr)
                        for (int newc=c-1;
                             newc<=c+1;
                             ++newc)
                            if (valid(newr,newc)
                                &&bombs[newr][newc])
                                ++bombneighbors[r][c];
    }
}

--TEST INPUT--

/cygdrive/c/rbnn/google: java MineSweeper

   0 1 2 3 4 5 6 7 8
--------------------
0| - - - - - - - - -
1| - - - - - - - - -
2| - - - - - - - - -
3| - - - - - - - - -
4| - - - - - - - - -
5| - - - - - - - - -
6| - - - - - - - - -
7| - - - - - - - - -
8| - - - - - - - - -


Enter cell coordinates: 5 6

   0 1 2 3 4 5 6 7 8
--------------------
0| - - - - - - - - -
1| - - - - - - - - -
2| - - - - - - - - -
3| - - - - - - - - -
4| - - - - - - - - -
5| - - - - - - 1 - -
6| - - - - - - - - -
7| - - - - - - - - -
8| - - - - - - - - -


Enter cell coordinates: 8 7

   0 1 2 3 4 5 6 7 8
--------------------
0| - - - 1   1 - - -
1| - 1 1 1   2 - - -
2| - 1       1 - - -
3| - 1       1 1 - -
4| - 1 1 1 1   1 - -
5| - - - - 1   1 - -
6| - - - 1 1   1 2 -
7| - - - 1       1 1
8| - - - 1          


Enter cell coordinates: 7 0

   0 1 2 3 4 5 6 7 8
--------------------
0| - - - 1   1 - - -
1| - 1 1 1   2 - - -
2| - 1       1 - - -
3| - 1       1 1 - -
4| 1 1 1 1 1   1 - -
5|     1 - 1   1 - -
6|     1 1 1   1 2 -
7|   1 1 1       1 1
8|   1 - 1          


Enter cell coordinates: 8 2

   0 1 2 3 4 5 6 7 8
--------------------
0|   1 * 1   1 * 2 1
1|   1 1 1   2 3 * 1
2| 1 1       1 * 2 1
3| * 1       1 1 2 1
4| 1 1 1 1 1   1 2 *
5|     1 * 1   1 * 3
6|     1 1 1   1 2 *
7|   1 1 1       1 1
8|   1 * 1          

Ooops! You hit a bomb...
Game is over

# we use smaller boards to check that winning works...

java MineSweeper 3 3 1

   0 1 2
--------
0| - - -
1| - - -
2| - - -


Enter cell coordinates: 0 0

   0 1 2
--------
0|   1 -
1|   1 1
2|      


   0 1 2
--------
0|   1 *
1|   1 1
2|      

You win
Game is over
/cygdrive/c/rbnn/google: java MineSweeper

   0 1 2 3 4 5 6 7 8
--------------------
0| - - - - - - - - -
1| - - - - - - - - -
2| - - - - - - - - -
3| - - - - - - - - -
4| - - - - - - - - -
5| - - - - - - - - -
6| - - - - - - - - -
7| - - - - - - - - -
8| - - - - - - - - -


Enter cell coordinates: 0 0

   0 1 2 3 4 5 6 7 8
--------------------
0| 1 - - - - - - - -
1| - - - - - - - - -
2| - - - - - - - - -
3| - - - - - - - - -
4| - - - - - - - - -
5| - - - - - - - - -
6| - - - - - - - - -
7| - - - - - - - - -
8| - - - - - - - - -


Enter cell coordinates: 0 1

   0 1 2 3 4 5 6 7 8
--------------------
0| 1 1 - - - - - - -
1| - - - - - - - - -
2| - - - - - - - - -
3| - - - - - - - - -
4| - - - - - - - - -
5| - - - - - - - - -
6| - - - - - - - - -
7| - - - - - - - - -
8| - - - - - - - - -


Enter cell coordinates: 8 8

   0 1 2 3 4 5 6 7 8
--------------------
0| 1 1 - - 1        
1| - - - - 1 1 1 1  
2| - - - - - - - 1  
3| - - - - - - - 1  
4| - - - - - - - 1  
5| - - - - - - - 1  
6| - - - - - - 1 1  
7| - - - - - - 1    
8| - - - - - - 1    


Enter cell coordinates: 5 2

   0 1 2 3 4 5 6 7 8
--------------------
0| 1 1 - - 1        
1| - - - - 1 1 1 1  
2| - - - - - - - 1  
3| - - - - - - - 1  
4| - - - - - - - 1  
5| - - 1 - - - - 1  
6| - - - - - - 1 1  
7| - - - - - - 1    
8| - - - - - - 1    


Enter cell coordinates: 0 1
Cell at: 0 1 is already uncovered, try again

   0 1 2 3 4 5 6 7 8
--------------------
0| 1 1 - - 1        
1| - - - - 1 1 1 1  
2| - - - - - - - 1  
3| - - - - - - - 1  
4| - - - - - - - 1  
5| - - 1 - - - - 1  
6| - - - - - - 1 1  
7| - - - - - - 1    
8| - - - - - - 1    


Enter cell coordinates: 7 0

   0 1 2 3 4 5 6 7 8
--------------------
0| 1 1 - - 1        
1| - - - - 1 1 1 1  
2| - - - - - - - 1  
3| - - - - - - - 1  
4| - - - - - - - 1  
5| - - 1 - - - - 1  
6| - - - - - - 1 1  
7| 2 - - - - - 1    
8| - - - - - - 1    


Enter cell coordinates: hello
Input two numbers please

   0 1 2 3 4 5 6 7 8
--------------------
0| 1 1 - - 1        
1| - - - - 1 1 1 1  
2| - - - - - - - 1  
3| - - - - - - - 1  
4| - - - - - - - 1  
5| - - 1 - - - - 1  
6| - - - - - - 1 1  
7| 2 - - - - - 1    
8| - - - - - - 1    


Enter cell coordinates: -3 4
Invalid input: row or column out of range [NOTE THE ERROR CHECKING
HERE -rbnn]

   0 1 2 3 4 5 6 7 8
--------------------
0| 1 1 - - 1        
1| - - - - 1 1 1 1  
2| - - - - - - - 1  
3| - - - - - - - 1  
4| - - - - - - - 1  
5| - - 1 - - - - 1  
6| - - - - - - 1 1  
7| 2 - - - - - 1    
8| - - - - - - 1    


Enter cell coordinates: 8 6
Cell at: 8 6 is already uncovered, try again

   0 1 2 3 4 5 6 7 8
--------------------
0| 1 1 - - 1        
1| - - - - 1 1 1 1  
2| - - - - - - - 1  
3| - - - - - - - 1  
4| - - - - - - - 1  
5| - - 1 - - - - 1  
6| - - - - - - 1 1  
7| 2 - - - - - 1    
8| - - - - - - 1    


Enter cell coordinates: 8 5

   0 1 2 3 4 5 6 7 8
--------------------
0| 1 1 1 * 1        
1| * 1 1 1 1 1 1 1  
2| 1 1       1 * 1  
3| 1 1 1 1 1 1 1 1  
4| * 1 1 * 1 1 1 1  
5| 1 1 1 1 1 1 * 1  
6| 1 1 1     1 1 1  
7| 2 * 2 1 2 1 1    
8| * 2 2 * 2 * 1    

Ooops! You hit a bomb...
Game is over

Request for Answer Clarification by rejct-ga on 22 Oct 2002 19:14 PDT
on minor problem with this program ... i intended it to be a C
program... and the program sent seems to have three erros

Request for Answer Clarification by rejct-ga on 22 Oct 2002 19:23 PDT
i know in my haste i did not specify what language to write the code
in but is there anyway or any one you know who can implement the code
in see using structures

Clarification of Answer by rbnn-ga on 22 Oct 2002 21:13 PDT
You wrote: "on minor problem with this program ... i intended it to be
a C
program... and the program sent seems to have three erros"

I can rewrite in C, certainly. However, before I do so, what are the
"three errors" that the current version of the program has?

Clarification of Answer by rbnn-ga on 22 Oct 2002 22:22 PDT
At any rate, here is a C program that does the same thing as the Java
program, except the size of the board and the number of bombs is a
compile-time constant.

Again, please feel free to ask for any clarifications or questions. I
put fairly basic functionality in the program since I am not sure
exactly how you intend to use it. To run, just compile and run with no
arguments:
/*simple minesweeper program for google answers by rbnn*/

#include <stdlib.h>
#include <stdio.h>
#define nrows 9
#define ncolumns 9
#define nbombs 10

int bombs[nrows][ncolumns];
int covered[nrows][ncolumns];
int bombneighbors[nrows][ncolumns];
int nremaining;

void uncover(int row, int column){
  int newrow,newcolumn;
  if (MineValid(row,column)&&
      covered[row][column]){
    covered[row][column]=0;
    --nremaining;
    if (bombneighbors[row][column]==0) 
      for (newrow=row-1;newrow<=row+1;++newrow)
        for (newcolumn=column-1; newcolumn<=column+1; ++newcolumn)
          uncover(newrow,newcolumn);
  }
}

void MinePrint(){
  int i,r,c;
  char out;
  int neighbors;
  printf("\n  ");
  for (i=0;i<ncolumns;++i)
    printf(" %1d",i);
  printf("\n");
  printf("--");
  for (i=0;i<ncolumns;++i)
    printf("--");
  printf("\n");
  for (r=0;r<nrows;++r){
    printf("%1d|",r);
    for (c=0;c<ncolumns;++c){
      printf(" ");
      if (covered[r][c])
        out='-';
      else if (bombs[r][c])
        out='*';
      else 
        out=bombneighbors[r][c]==0?' ':(bombneighbors[r][c]+'0');
      printf("%c",out);
    }
    printf("\n");
  }
}

void MinePlay(){
  int row,column,r,c;
  while(1){
    MinePrint();
    if (nremaining==0){
      for (r=0;r<nrows;++r)
        for (c=0;c<ncolumns;++c)
          covered[r][c]=0;
      MinePrint();
      printf("\nYou win\n");
      return;
    }
    printf("\nEnter cell coordinates: ");
    row=column=-1;
    if (scanf("%d%d",&row,&column)<0){
      printf("Could not read int inputs");
      continue;
    }
    if (row==-1&&column==-1)
      return;
    if (!MineValid(row,column)){
      printf("Got invalid row: %d or column %d, try
again\n",row,column);
      continue;
    }
    if (!covered[row][column]){
      printf("Cell at %d %d is already uncovered\n");
      continue;
    }
    uncover(row,column);
    if (bombs[row][column]){
      for (r=0;r<nrows;++r)
        for (c=0;c<ncolumns;++c)
          covered[r][c]=0;
      MinePrint();
      printf("\nOoops! You hit a bomb...");
      return;
    }
  }
}

int MineValid(int row, int column){
        return row>=0&&
            row<nrows&&
            column>=0&&
            column<ncolumns;
}


void createRandomBoard(){
  int r,c,bombsplaced,newr,newc,row,column;
  nremaining=nrows*ncolumns-nbombs;
  bombsplaced=0;
  for (r=0;r<nrows;++r)
    for (c=0;c<ncolumns;++c){
      bombs[r][c]=bombneighbors[r][c]=0;
      covered[r][c]=1;
    }
  while(bombsplaced<nbombs){
    row=rand()%nrows;
    column=rand()%ncolumns;
    if (!bombs[row][column]){
      bombs[row][column]=1;
      bombsplaced++;
    }
  }
  for (r=0;r<nrows;++r)
    for (c=0;c<ncolumns;++c)
      if (!bombs[r][c])
        for (newr=r-1;newr<=r+1; ++newr)
          for (newc=c-1; newc<=c+1; ++newc)
            if (MineValid(newr,newc)&&bombs[newr][newc])
              ++bombneighbors[r][c];
}

int main(int argc, char**argv){
  printf("creating board\n");
  createRandomBoard();
  printf("starting play\n");
  MinePlay();
  printf("Game over\n");
  return 0;
}
Comments  
Subject: Re: minesweeper
From: rbnn-ga on 30 Oct 2002 10:43 PST
 
Thank you for your comment; people often do ask about google's homework policy. 

Google permits homework questions to be asked and answered.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy