Read about arrays in Chapter 9 of the text, up through page 395.
In the lab this week, we will try out multiple things with arrays, to give you an understanding on how they work. Here are the tasks that you will need to accomplish in the lab:
Save this program as Array.java
You roll two fair, six-sided die. For example,
You insert pegs into the holes corresponding to the total of the two dice or any combination that adds up to the total. In this example, you just rolled a 12. You place a yellow peg in locations 2, 4 and 6 on the board.
You could have chosen a different combination. For example, 2 and 10, or 8 and 4, or even 1, 2, 4 and 5. Also, you may select the pegs in any order. You don't have to go in increasing order, for example. You can place the peg in hole 6 first or maybe in hole 4 first. It doesn't matter. But once you insert a peg into a hole, you can't change your mind and take it out again. It's stuck.
You roll again...
... and do the same thing. Note that the previously inserted pegs are now in green, and the new pegs that you just selected for this roll of 4 are in yellow.
Let's roll one more time...
and place the pegs. There's not a lot of choice this time:
The game continues in this way until you have placed a peg in every hole (in which case, you win) or you have no move (in which case, you lose.)
Write an applet to play this game. Here's a demo: Elimination Demo
Requirements
and display an appropriate message, like this:
If you play the game for a while, you'll soon recognize that you very rarely win this game.
In this case, your program just continues to wait for the player to place a peg. He can certainly place a peg in hole 8, 9 or 10, but then he has no way to make up the remainder of this roll. So, your program is stuck in an infinite loop, since all remaining clicks will be ignored (unless, of course, you've implemented the restart button, in which case, the player can start up another game.)
Trying to recognize when the player has lost is a difficult problem since you must determine that none of the remaining combinations sum to the current roll.
For the ambitious:
In the demo, I've incorporated a "Restart button" that, when clicked, begins the game over again. This is just a hollow rectangle with a message draw inside it.
I just recognize if the user happens to click within the bounds of this rectangle, and if so, I clear the board and start over. This is not part of the basic assignment, but you are encouraged to try to incorporate this feature into your program.
Hints
There are several ways to go about writing this program, and I don't want to point you in any one direction. I want you to think about what information you need to "remember" and how you will go about doing that. As always, begin by simplifying the problem. Work on drawing the pegboard and being able to recognize when the user has clicked on a hole and, if so, which one.
int hole = getClickedHole(); // ignores spurious clicks
System.out.println("The player just clicked on the hole numbered "
+ hole + ".");
while (alreadyPegged(hole, pegBoard)) {
System.out.println("Hole number " + hole +
" is already pegged... ignoring...");
hole = getClickedHole();
System.out.println("The player just clicked on the hole numbered "
+ hole + ".");
}
Save this program in a file called Eliminate.java.
For this lab, you should submit only one file: Eliminate.java. Attach these files to an e-mail sent to a201@cs.indiana.edu with lab8 as the subject.