Lab Assignment #8: Arrays

Due by 11:59PM on Monday, April 12.


Reading Assignment

Read about arrays in Chapter 9 of the text, up through page 395.


In-Lab Work

Playing with arrays

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:

  1. Create an array that can contain a fixed number of integers, say, 20.
  2. Insert random numbers into the array using a for loop
  3. Using another for loop, calculate the maximum number in the array, and the index in which it occurs.
  4. Modify the same loop above to also include the minimum number.
  5. Now write a function called average, that takes in an array of any length, and returns the average value of the numbers in the array.

Save this program as Array.java


Lab Assignment

  1. The game of Elimination is played on a peg board with 10 holes, labeled 1, 2, 3, ..., 10.

    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

    1. Ignore spurious clicks! That is, clicks not inside a hole, and clicks on a hole already containing a peg.

    2. Ignore a click inside a hole if it's value is "too big". For example, if the roll is a 7, then a click on 8, 9 or 10 should just be ignored, whether or not these holes are pegged. Furthermore, if the roll is a 7 and the player has already pegged holes 1 and 2 (for a total of 3) on this turn, then a click on 5 or above should be ignored.

    3. You must detect a winning situation, like this:

      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.

    4. You need not try to detect a losing situation, like this:

      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.

    5. You'll need to draw pegs in two different colors. I used yellow and green in the demo, but you may use whatever colors you desire. Use one color for the pegs that you are placing on the current roll and green for all the other previously placed pegs.

    6. Clear the screen and redraw the board before every roll.

    7. As you place the pegs for the current roll, just draw the filled yellow circles, without clearing the screen in between pegs.

    8. Make good use of functions to solve this program. You'll find this program very difficult to write if you don't think in terms of functional units from the very beginning.

    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.

    1. Obviously, it is important to choose a reasonable coordinate system for this problem.

    2. From your experience with throwing darts, you already know how to detect if a click point lies within a certain circle. In this case, we have ten different circles. With the right coordinate system, it should be easy to figure out which circle the user click is "nearest" by just looking at the click's x-coordinate and, possibly, doing a floor operation.

    3. The board has 10 holes. As the game is being played, some of the holes will be empty and some will be pegged. You've got to keep track of this information somehow. An array will probably be useful. What kind of array? An array of Circles? An array of ints? An array of boolean?

    4. When you make the filled circles for the pegs, use a radius slightly smaller than the radius that you used to draw the holes. This way, you'll still be able to see the black circle around the holes after you insert the peg.

    5. Use println statements to help you trace through your program. (In a Cafe applet, all output sent to System.out is directed to the Output window.) Sprinkle these output statements everywhere, such as:

      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.


    Submitting Your Lab Assignment

    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.