Lab on Using Linked Lists: Asteroids
Due Midnight Monday, April 14
Create a subdirectory of 158 called asteroids. You can find the Java source files we discussed in class in /home/jcaristi/158/asteroids in the files Asteroid.java, Station.java, and Rocket.java, along with the JUnit test files AllTests.java, TestAsteroid.java, TestStation.java, and TestRocket.java. Copy them to your asteroids directory, as well as the file Screen.class.
The Screen class has a constructor that takes no arguments and creates a 400x500 screen. (By the way, those constants are available if needed as Screen.XSIZE and Screen.YSIZE.)
The Screen class contains two public methods that you can use. They are as follows:
public void draw(LinkedList asteroids, Station station, LinkedList rockets)
and
public void erase(LinkedList asteroids, Station station, LinkedList rockets)
The first method places all the asteroids, the station, and the rockets onto the screen in the appropriate places in the appropriate colors. The second method repaints those same objects using the background color, so they are "erased" from the screen, although, of course, nothing happens to the objects in the application.
Create a new class called Game that contains a main method that instantiates a Screen object. Compile and run it, and a window should pop up labeled "Asteroids" with nothing in it, and which you can close by clicking the close function.
In main, create empty LinkedList objects that will contain the asteroids and rockets. Instantiate a Station object that is located at the bottom center of the screen (so the x value will be halfway across, and the y value will be all the way down). Send the draw message to the screen object. Compile and run, and you should see the station at the bottom in the center.
Write a static method in Game called newAsteroid that returns an Asteroid object located 20 units down from the top of the screen at a random x value that is within the bounds of the screen. The velocity should use a random real number between –2 and 2 for dx, and a random real number between 0 and 5 for dy. Add a new Asteroid to the LinkedList of asteroids using the newAsteroid method (before drawing the screen, of course). Compile and run, and you should see an asteroid in white at the top of the screen somewhere, as well as the station in black at the bottom.
Write a JUnit test class called TestNewAsteroid and update AllTests to include it in the test suite. Add a method to TestNewAsteroid called testVelocity that asserts that the x value of the velocity is between –2 and 2 and the y value of the velocity is between 0 and 5. Instantiate at least 10 Asteroid objects using newAsteroid to test this sufficiently. Compile and run AllTests until you get a green bar.
Write a static method in Game called delay that takes an integer argument and goes into a loop that does nothing for the number of times given as the argument.
Add a static method to Game called move that takes as arguments the LinkedList of asteroids, the Station object, and the LinkedList of rockets and sends everything in both lists a move message. Whenever a move message is sent to a rocket, you should also send it the explode message to cause any asteroids to change that require changing. Whenever a move message is sent to an asteroid, you should also send the checkHit message to the station.
Add a loop to main that iterates 100 times and calls delay with a value of ten million, erases the screen, calls move to move the asteroids and rockets, and draws the screen. Compile and run the main in Game several times. You should see the asteroid move near the top of the screen. Add a line to the loop that creates a new Asteroid if the loop variable is divisible by 5. Compile and run, and you should see many asteroids appear and move down the screen. Add a line to the loop that sends the fire message to the station whenever the loop variable is divisible by 5. Compile and run, and you should see the station fire as well as the asteroids moving down the screen. Finally, change the loop so that instead of stopping after 100 iterations, it stops when the station has taken 20 points of hits. Add some code that moves the gun left or right randomly whenever the loop variable is divisible by 5. This will cause the gun to "sweep" somewhat randomly.
One problem you may have noticed is that if the game continues for a while, it starts to slow down. This is due to the fact that asteroids and rockets are never deleted from the linked lists. Add a static method to the Game class called cleanup that takes the two linked lists as arguments. It should remove any asteroid that is off the screen (left, right, or below) or has size equal to zero. It should remove any rocket that is off the screen (left, right, or above). You must call cleanup from main, but it would be very inefficient to call it every iteration of the loop. Instead, call it whenever the loop variable is divisible by 50. Compile and run this until you’re sure that it works properly.