Homework 9 - 2D Arrays and Objects


[Homepage] | [General Lab Info] | [TA's & Tutors] | [FAQ's] | [Homework] | [Excellent Programs] | [Exams]

Due: At midnight, November 30, 2001, submit is turned off automatically. Work turned in after that time will not be accepted for grading.

IMPORTANT - READ THIS CAREFULLY

Many students are working together very well using "pair programming". They balance their time driving and reviewing and they do most of their work together, maybe with some time alone before the pair meets.

However, many other students are not following the pair programming guidelines I discussed in class, and reinforced on the header to each log you submit. For this reason I am providing the following clarification, and specific requirements for this, the final programming assignment.

Program Description

For this assignment you will implement the game "Gold Digger" (known to many of you as battleship). This is a two player game. Each player has a 10 x 10 grid. A player cannot see the opponents grid.

To start the game, each player places several "gold bars" somewhere on their board. Each bar is represented by a contiguous sequence of horizontal or vertical grid locations of varying lengths. Specifically each player places one bar that is 5 cells long, one bar that is 4 cells long, TWO bars that are 3 cells long and one bar that is 2 cells long.

The object is to find all of your opponents gold before they find all of yours. You attempt to find their gold by asking them if there is any gold in a specific cell. The players take turns guessing the possible locations of their opponent's gold.

Each player, when it is there turn, names FIVE possible locations for a gold bar. After naming all five locations, the opponent then indicates which, if any of the five locations contained part of a gold bar. If only part of a gold bar is found the opponent does not indicate how big the bar is that has been partially found.

The players take turns until one player has found all of the other player's gold.

For this program, the computer will be one of the players, and the human will always get to move first.

Program Details

You are free to design your own user interface. You are also free to design your own algorithm for having the computer determine where it will guess. Because of the difficulty of testing, you are REQUIRED to submit a text file, showing a complete execution of your program, from start to finish. The easiest way to do this on the unix machines is using the command "script". To capture a session of running your program, at the unix prompt type
    script sampleOutput
then run your program. When your program has finished, type exit at the unix prompt. At that point, the file sampleOutput in your current directory will contain everything that appeared on your computer screen from the time you started script. You can view this file with any text editor.

As with the Video Poker assignment, I am providing you with some starter code. In this case, instead of a package of classes that you simply use, I am instead giving you some source code that you can use (just click the link to view the code - then use "Save As" to get a copy).

I will go over this code in class on Tuesday, November 21st.

A Sample Run

You can run my version of the program on the lab computers by typing

    java SampleGoldDigger

The following is an excerpt from an execution of SampleGoldDigger. Notice that the program refuses to accept an illegal placement (the second 3 cell long gold bar. A partially discovered bar is shown with a $. A guess that didn't find any thing is shown with a +. A dot represents an unknown cell. The player's own board also shows the placement of the gold bars, with digits corresponding to the size of the bar.
os-prompt>java SampleGoldDigger
The current board is:
 0123456789
0..........
1..........
2..........
3..........
4..........
5..........
6..........
7..........
8..........
9..........

Enter row and col of upper left end for the 5 long gold bar.
1 1
Enter the direction, 0 for horizontal, 1 for vertical.
1
The current board is:
 0123456789
0..........
1.5........
2.5........
3.5........
4.5........
5.5........
6..........
7..........
8..........
9..........

Enter row and col of upper left end for the 4 long gold bar.
3 3
Enter the direction, 0 for horizontal, 1 for vertical.
0
The current board is:
 0123456789
0..........
1.5........
2.5........
3.5.4444...
4.5........
5.5........
6..........
7..........
8..........
9..........

Enter row and col of upper left end for the 3 long gold bar.
5 5 
Enter the direction, 0 for horizontal, 1 for vertical.
1
The current board is:
 0123456789
0..........
1.5........
2.5........
3.5.4444...
4.5........
5.5...3....
6.....3....
7.....3....
8..........
9..........

Enter row and col of upper left end for the 3 long gold bar.
8 2
Enter the direction, 0 for horizontal, 1 for vertical.
1
Can't put it there.
The current board is:
 0123456789
0..........
1.5........
2.5........
3.5.4444...
4.5........
5.5...3....
6.....3....
7.....3....
8..........
9..........

Enter row and col of upper left end for the 3 long gold bar.
8 2 
Enter the direction, 0 for horizontal, 1 for vertical.
0
The current board is:
 0123456789
0..........
1.5........
2.5........
3.5.4444...
4.5........
5.5...3....
6.....3....
7.....3....
8..333.....
9..........

Enter row and col of upper left end for the 2 long gold bar.
1 5
Enter the direction, 0 for horizontal, 1 for vertical.
1
Your final board is:
 0123456789
0..........
1.5...2....
2.5...2....
3.5.4444...
4.5........
5.5...3....
6.....3....
7.....3....
8..333.....
9..........

numberExposed = 0
numberExposed = 0
Player board is now:
 0123456789
0..........
1.5...2....
2.5...2....
3.5.4444...
4.5........
5.5...3....
6.....3....
7.....3....
8..333.....
9..........


Computer board is now:
 0123456789
0..........
1..........
2..........
3..........
4..........
5..........
6..........
7..........
8..........
9..........

Enter your guesses.
1 1 2 2 3 3 4 4 5 5
numberExposed = 1
numberExposed = 1
Player board is now:
 0123456789
0.+........
1+5...2....
2.5...2....
3.5.4444...
4.5.....+..
5.5...3....
6.....3....
7....+3....
8..3$3.....
9..........


Computer board is now:
 0123456789
0..........
1.+........
2..+.......
3...$......
4....+.....
5.....+....
6..........
7..........
8..........
9..........

Enter your guesses.
2 3 3 2 3 4 4 3
8 5
numberExposed = 2
numberExposed = 2
Player board is now:
 0123456789
0.+........
1+5...2....
2.$...2....
3+5.4444...
4.5.....+..
5.5...3..+.
6.....3.+..
7....+3....
8..3$3.....
9....+.....


Computer board is now:
 0123456789
0..........
1.+........
2..++......
3..+$+.....
4...$+.....
5.....+....
6..........
7..........
8.....+....
9..........

Enter your guesses.

[Homepage] | [General Lab Info] | [TA's & Tutors] | [FAQ's] | [Homework] | [Excellent Programs] | [Exams]

This page maintained by Charlie McDowell. Email regarding this site.