Lab 12 -- Arrays.

Plan of the lab:
  1. A brief review of System.out.println() and System.out.print().
  2. Array of integers: printing and reversing an array.
  3. Array of buggles (or, more exactly, of FreezeBuggles).

To get started: Download the lab12_programs folder from cs111d account.

System.out.print() vs. System.out.println() (a reminder)

print() and println() are two methods that allow you to print text to the Java console window.

print() prints your text and leaves the cursor at the end of the line, ready to print the next thing.

println(), on the other hand, prints your text and then waits at the start of the next line to print the next thing.

This is best demonstrated by example. Each snippet of code produces the window below it.


		System.out.print("Hi, there!");
		System.out.print("Good");
		System.out.println(" to see you!");

(Note: no spaces between there! and Good )

		System.out.println("Hi, there!");
		System.out.println("Good");
		System.out.println(" to see you!");


		System.out.println("Hi, there!");
		System.out.print("Good");
		System.out.println(" to see you!");

Use these examples to format the printing of elements of an array. In this method we print all elements on one line.

Part 1: Printing out the contents of an array, reversing an array

Your code for this part goes into the file labArray.java in the folder labArray in lab12_programs. Use the project in the Test folder as a sample.

Printing out an array

 We want to be able to print out the contents of our arrays (so that when we test our array methods, we can print out the arrays to make sure that the methods are working). Let's write a method called printArray(int [] arr) that prints out the contents of the integer array arr on one line. For example, say we have an array B = [2, 4, 6, 8]. Then invoking printArray(B) should produce the following in the Java Console window:

[2, 4, 6, 8]

Use a for loop to traverse the array.

Reversing the contents of an array

Now we want to reverse the contents of our array. So, if array C= [0, 1, 2, 3, 4] then reverse(C) should produce [4, 3, 2, 1, 0]. Now, aren't you glad that we wrote printArray above? We can use that to check that our reverse actually works! Our test code looks like this:


      	int[] a = { 9, -1, 3, 6, -10}; // creates an array named a
      
		System.out.println("Original a "); 
		printArray(a); // print out the original array
		System.out.println("Reversed a ");
		reverseArray(a); // reverse the array
		printArray(a);   // print out the reversed array
		System.out.println("--------");      
      
  
Note that the method reverseArray() must change the order of elements of the given array. It does not do any printing itself (you may, of course, insert print statements for debugging, and remove or comment out them later).

This method also uses a for loop to traverse the array.

Part 2: buggle Freeze Tag using arrays.

For this problem write your code in the file FreezeTag.java in the FreezeTag folder. As before, use the the program in the Test folder as a sample.

In this problem you will be creating and traversing arrays of FreezeBuggles. A FreezeBuggle is an extension of a Buggle which has extra methods for setting a random position and a random color, and for making a random turn. FreezeBuggles play a Freeze Tag game.

What is a buggle Freeze Tag game?

In this game a buggle runs around randomly until she gets over a bagel. Then the buggle is "tagged": she changes the color to blue and does not move. We will simulate the game and see how many buggles stay in the game (i.e. are not "tagged") after a specified number of rounds.

More specifically,

To run the program, the user enters the size of the grid, the number of bagels and buggles, and the number of rounds in the small window. The program then generates the grid with a random configuration of bagels and creates an array of buggles with random positions and colors (except for blue). An example of the initial configuration is shown on the left picture below.

At every round of the game:

After several rounds of the game the grid might look like the picture above on the right.

After the game is played for the given number of rounds, the number of buggles still not tagged is displayed in the little window:

The positions of all buggles in the end of the game are printed on the Java console.

Run the project in the Test folder to see examples.

What you need to do

Your task is to fill in the code for several methods of the class FreezeTag (not FreezeBuggle!) in the file FreezeTag.java. You also need to write the test code for some of these methods (see below).

Your methods should use for loops to traverse the array of FreezeBuggles.

Here are the methods that you need to write. Write them in the given order.

  1. public FreezeBuggle [] createPlayers(int howMany)
    

    In this method you need to create a new array of FreezeBuggles. The parameter howMany specifies the number of FreezeBuggles in the array.

    After the array has been created, you need to go through it using a for loop, create the FreezeBuggles to populate the array (hint: you need to use the constructor to create a FreezeBuggle), and set their random position and color.

    The position and the color of buggles are set at random using these FreezeBuggles methods:

    • randomPosition(int width,int height) Here width and height are the width and the height of the grid. Instance variables width and height are inherited from BuggleWorld, you can use them directly. In other words, invoking the method like this
      fanny.randomPosition(width,height);
      
      will work (assuming that fanny is a FreezeBuggle).
    • randomColor() randomly sets the color of a FreezeBuggle to one of the following: red, yellow, green, cyan, or magenta.
    The method returns the newly created array.

    The method is already invoked in run(), you don't need to write the testing code for it.

  2. public void printPositions(FreezeBuggle [] players)
    

    Prints out the positions of the FreezeBuggles in the array players. Invoke the method in PlayTag() on the array players to test.

  3. public int howManyNotBlue(FreezeBuggle [] players)
    

    Returns the number of FreezeBuggles in the array players whose color is not blue. Reminder: use equals() method of class Color to compare two colors.

    If you return the result of this method from PlayTag, it will appear in the small dialog window. Initially (before you write the PlayTag method) the number returned should be the same as the number of buggles in the game.

  4. And the last, but not the least,
    public int playTag(FreezeBuggle [] players, int rounds)
    

    Here players is the array of FreezeBuggles playing the game, round is the number of rounds of the game. The rules of the game are given above. Use the FreezeBuggle method randomTurn() to turn the buggle randomly.

    You need nested loops to write this method, since the iteration goes through the number of rounds and, within each round, through the array of FreezeBuggles.

    In the end print out the positions of all FreezeBuggles (using the method printPositions()) and return the number of non-blue FreezeBuggles (using the method howManyNotBlue()).

Now that you have written all the methods, sit back and enjoy the game!