Program 2

Objective

Work with 2D arrays.

Program Description

For this assignment you will implement the game "Scat Scout" (known to many of you as Mine Sweeper).

The game is played on a two-dimensional grid of squares. For this game the grid should be 10x10. Each square in the grid will either be empty, or contain some animal scat (not something you want to step in). The objective is to locate all of the scat without stepping in any of it.

You locate scat by "stepping" into a square. If the square contains scat then the game is over and you lose. If the square does not contain scat, then you are told how many neighboring squares contain scat (although you aren't told which ones the scat is in - that is for you to figure out).

If you are lucky enough to step into a square that does not contain scat, and none of that's squares immediate neighbors contain scat, then the entire region of scatless squares will identified. See the examples below to see how this works.

If you manage to discover all of the scat without stepping in any of it then you win and the game is over.

Your implementation

For this implementation, you will display the grid as an array of dots, asterisks, and digits. A dot will indicate a square that you don't know anything about. A digit will represent a square for which you know how many neighboring squares contain scat - the digit being that number of neighbors. An asterisk will be used to show the squares that contain scat - these only appear in the final display.

Here is an example of the initial display.

  0123456789
0 .......... 0
1 .......... 1
2 .......... 2
3 .......... 3
4 .......... 4
5 .......... 5
6 .......... 6
7 .......... 7
8 .......... 8
9 .......... 9
  0123456789

Here is the display after the user guessed 1 3, indicating that four neighors of square (1, 3) contain scat and the rest do not.

  0123456789
0 .......... 0
1 ...4...... 1
2 .......... 2
3 .......... 3
4 .......... 4
5 .......... 5
6 .......... 6
7 .......... 7
8 .......... 8
9 .......... 9
  0123456789

On the next move the player guesses 3 5. This turns out to be a very lucky guess. There are many cells with no neighbors containing scat so a large region is filled in.

  0123456789
0 ........10 0
1 ...4....10 1
2 ....211110 2
3 ....100000 3
4 ..11100000 4
5 ..10000000 5
6 ..10000000 6
7 ..11110000 7
8 .....10000 8
9 .....10000 9
  0123456789

Notice that the area of zeros, is bounded by non-zero digits. Once you find a square that is not surrounded by any scat, the entire region up to a boundary where scat is detected nearby, will be filled in. (Actually, this method is provided for you.)

For this assignment, your program must conform precisely to the input and output formats specified. An optional integer used to seed the random number generator may be passed on the command line to aid in testing. The parameter is optional. Implementing the feature of having the optional parameter is not optional. The user simply enters pairs of integers, separated by a space. Here is the complete run from which the above examples were taken. In this run, the user managed to win. If the user loses, you should immediately print out the entire yard, showing all of the scat and the counts.

Here is a losing execution followed by a winning execution.

os-prompt>java ScatScout 123
The object is to locate all of the animal droppings (scat)
in the yard without stepping in any of it.
You are able to step anywhere in the yard, and when you do, you
can smell how much scat is around you but you can't be sure where
it is.

You indicate where you want to step by entering a row and column number.
If you didn't step in any, that cell will be filled with a
number indicating how many neighboring cells have scat in them.
Also, if no neighboring cells have any scat, then all adjoining
cells that are empty, and their immediate neighbors will display
the count of neighboring cells that have scat.

Try it!
0123456789
0 .......... 0
1 .......... 1
2 .......... 2
3 .......... 3
4 .......... 4
5 .......... 5
6 .......... 6
7 .......... 7
8 .......... 8
9 .......... 9
0123456789
Enter two integers (row and column):3 5
0123456789
0 .......... 0
1 11........ 1
2 01..111... 2
3 01..201... 3
4 02..101... 4
5 01..2012.. 5
6 012.1001.. 6
7 001110013. 7
8 0000000011 8
9 0000000000 9
0123456789
Enter two integers (row and column):1 7
0123456789
0 .......... 0
1 11.....1.. 1
2 01..111... 2
3 01..201... 3
4 02..101... 4
5 01..2012.. 5
6 012.1001.. 6
7 001110013. 7
8 0000000011 8
9 0000000000 9
0123456789
Enter two integers (row and column):1 6
Eeewww! You stepped in it!
0123456789
0 1*10011100 0
1 112111*100 1
2 012*111100 2
3 01*3201121 3
4 023*101*2* 4
5 01*3201243 5
6 012*1001** 6
7 001110013* 7
8 0000000011 8
9 0000000000 9
0123456789
os-prompt>
os-prompt> java ScatScout 4444
The object is to locate all of the animal droppings (scat)
in the yard without stepping in any of it.
You are able to step anywhere in the yard, and when you do, you
can smell how much scat is around you but you can't be sure where
it is.

You indicate where you want to step by entering a row and column number.
If you didn't step in any, that cell will be filled with a
number indicating how many neighboring cells have scat in them.
Also, if no neighboring cells have any scat, then all adjoining
cells that are empty, and their immediate neighbors will display
the count of neighboring cells that have scat.

Try it!
0123456789
0 .......... 0
1 .......... 1
2 .......... 2
3 .......... 3
4 .......... 4
5 .......... 5
6 .......... 6
7 .......... 7
8 .......... 8
9 .......... 9
0123456789
Enter two integers (row and column):3 4
0123456789
0 .......... 0
1 .......... 1
2 .......... 2
3 ....1..... 3
4 .......... 4
5 .......... 5
6 .......... 6
7 .......... 7
8 .......... 8
9 .......... 9
0123456789
Enter two integers (row and column):6 6
0123456789
0 ........10 0
1 ........10 1
2 ....211110 2
3 ....100000 3
4 ..11100000 4
5 ..10000000 5
6 ..10000000 6
7 ..11110000 7
8 .....10000 8
9 .....10000 9
0123456789
Enter two integers (row and column):9 0
0123456789
0 ........10 0
1 ........10 1
2 ....211110 2
3 ....100000 3
4 ..11100000 4
5 ..10000000 5
6 ..10000000 6
7 ..11110000 7
8 .....10000 8
9 1....10000 9
0123456789
Enter two integers (row and column):9 2
0123456789
0 ........10 0
1 ........10 1
2 ....211110 2
3 ....100000 3
4 ..11100000 4
5 ..10000000 5
6 ..10000000 6
7 .211110000 7
8 .101.10000 8
9 1101.10000 9
0123456789
Enter two integers (row and column):9 4
0123456789
0 ........10 0
1 ........10 1
2 ....211110 2
3 ....100000 3
4 ..11100000 4
5 ..10000000 5
6 ..10000000 6
7 .211110000 7
8 .101.10000 8
9 1101110000 9
0123456789
Enter two integers (row and column):0 0
0123456789
0 001.....10 0
1 123.....10 1
2 ....211110 2
3 ....100000 3
4 ..11100000 4
5 ..10000000 5
6 ..10000000 6
7 .211110000 7
8 .101.10000 8
9 1101110000 9
0123456789
Enter two integers (row and column):2 0
0123456789
0 001.....10 0
1 123.....10 1
2 1...211110 2
3 ....100000 3
4 ..11100000 4
5 ..10000000 5
6 ..10000000 6
7 .211110000 7
8 .101.10000 8
9 1101110000 9
0123456789
Enter two integers (row and column):4 0
0123456789
0 001.....10 0
1 123.....10 1
2 1...211110 2
3 123.100000 3
4 0011100000 4
5 1110000000 5
6 ..10000000 6
7 .211110000 7
8 .101.10000 8
9 1101110000 9
0123456789
Enter two integers (row and column):6 0
0123456789
0 001.....10 0
1 123.....10 1
2 1...211110 2
3 123.100000 3
4 0011100000 4
5 1110000000 5
6 1.10000000 6
7 .211110000 7
8 .101.10000 8
9 1101110000 9
0123456789
Enter two integers (row and column):7 0
0123456789
0 001.....10 0
1 123.....10 1
2 1...211110 2
3 123.100000 3
4 0011100000 4
5 1110000000 5
6 1.10000000 6
7 2211110000 7
8 .101.10000 8
9 1101110000 9
0123456789
Enter two integers (row and column):0 7
0123456789
0 001....210 0
1 123.....10 1
2 1...211110 2
3 123.100000 3
4 0011100000 4
5 1110000000 5
6 1.10000000 6
7 2211110000 7
8 .101.10000 8
9 1101110000 9
0123456789
Enter two integers (row and column):0 5
0123456789
0 001..3.210 0
1 123.....10 1
2 1...211110 2
3 123.100000 3
4 0011100000 4
5 1110000000 5
6 1.10000000 6
7 2211110000 7
8 .101.10000 8
9 1101110000 9
0123456789
Enter two integers (row and column):1 3
0123456789
0 001..3.210 0
1 1234....10 1
2 1...211110 2
3 123.100000 3
4 0011100000 4
5 1110000000 5
6 1.10000000 6
7 2211110000 7
8 .101.10000 8
9 1101110000 9
0123456789
Enter two integers (row and column):1 5
0123456789
0 001..3.210 0
1 1234.3..10 1
2 1...211110 2
3 123.100000 3
4 0011100000 4
5 1110000000 5
6 1.10000000 6
7 2211110000 7
8 .101.10000 8
9 1101110000 9
0123456789
Enter two integers (row and column):1 6
0123456789
0 001..3.210 0
1 1234.32.10 1
2 1...211110 2
3 123.100000 3
4 0011100000 4
5 1110000000 5
6 1.10000000 6
7 2211110000 7
8 .101.10000 8
9 1101110000 9
0123456789
Enter two integers (row and column):2 3
Well done!
0123456789
0 001**3*210 0
1 1234*32*10 1
2 1**3211110 2
3 123*100000 3
4 0011100000 4
5 1110000000 5
6 1*10000000 6
7 2211110000 7
8 *101*10000 8
9 1101110000 9
0123456789
os-prompt>

Getting Started

A starting main with the needed arrays already declared and the random number generator setup can be found in ScatScout.java in the Files folder. This starter program also contains the all important expose() method which does the hard work of exposing an entire region of zeros. Although you were not asked to write the method, you should study it and understand it. It is a nice example of the power of recursive function calls.

Your program must follow the outline provided, using the arrays as established. Note that although the "board" array is filled with booleans, symbolic constants have been defined so that the values SCAT and CLEAR can be used instead of true and false, making the code more readable or "self documenting."

I suggest you follow these steps as you develop your solution. Be sure and test what you have written at each step.

  1. Write a method to print the board.
  2. Write a method to initialize the board with some scat.
  3. Write a method to compute the neighbor counts.
  4. It is now time to write the main game loop. Just let them move anywhere and expose where ever they step.
  5. Figure out how to know when they have won the game.
  6. Check if they step in "it" and if so end the game.

Very early on you will want to run the game with some seed and not much scat (low probability of placing scat - like 1 in 50) and make a note of where the scat is. You will then use this knowledge in testing.

What to turn in

You should have just the one file, ScatScout.java, to turn in on CrowdGrader for this assignment. Be sure an enter your programming log here in Canvas.