Level Checklist

Table of Contents

1. Level

The Level class should maintain a searchable collection of Script objects. I expect you will use an ArrayList to hold this collection of objects.

1.1. Maintaining a list of Scripts

The first order of business is to make yourself an ArrayList in Level to hold your scripts, and get them adding and removing properly.
The constructor for Level should prepare an empty ArrayList instance variable.
The addScript() method should add the given script to that list.
The removeScript() method should remove the given script from the list.

1.2. Checkpoint

To test what you've done so far, in the BlueJ code pad, make yourself a Level and a Script and store them in variables:

new Level()
new Script(3, 4, "Creature", "Bob")

Leaving off the semicolon at the end of these lines makes the object reference they create appear with a red square that you can drag to the object bench to store it in a variable. I would suggest putting these two in variables named level and script.

Next, you want to add the script to the level. Again, you can do this in the code pad:


Now, with the inspector, check to make sure that the ArrayList in your level now has one object in it. (Remember that you can inspect an object by right-clicking a variable and selecting "Inspect" from the menu. Then, you can inspect an instance variable by clicking on it and hitting the "Inspect" button.

Now, you can test out removeScript() in the same way: tell teh level to remove the script, and verify that it disappeared from the list.

1.3. Searching for Scripts

Now, we need to write all the methods that search for scripts.
The getScript(Script template) method should return the first match.
The getScript(int n, Script template) method should return the nth match, with n = 0 being the first one.
The getScripts(Script template) method should return an ArrayList containing all the matches.

1.4. Test Case

At this point, you shoudl be able to run the "Test Searching" test case and have it work properly.

2. Script

Because a Script needs to be able to move around and interact with the other Scripts in its level, it needs to know what Level it belongs to. You will write methods to get the level and move the script, and you will change the addScript() and removeScript() in Level to keep the Script informed about what Level it's in.

2.1. A script should know what level it's in

Your script class should store information on what level it's in. It starts out in no level (null); when it is added or removed from a level, its level is updated by Level.
The constructor for Script should put it in level null, meaning it is not yet placed in a level.
The getLevel() method in Script should return the script's level.
The addScript() method of Level should change the level of the script being added to be equal to the level it is added to.
The removeScript() method of Level should change the level of the script being removed to null.

2.2. Checkpoint

At this point, you can try creating a level and a script as described in section 1.2 above. First, verify that the script's getLevel() returns null when it is first created. Then, add the script to the level, and verify that this is true:

script.getLevel() == level

2.3. Scripts can be moved or removed

The final thing you need to do in this project is to make it possible to ask a script to remove() itself from a level, or move to a new location, possibly in another level.
Calling the remove() method should tell the level to remove this script.
Calling the move(x, y, level) method should move the script to a new location.
Calling the move(x, y, level) method should remove the script from its current level and add it to the new one, but only if it is not currently in the given level.
Moving to a null level should simply remove the script from its level.
Calling the move(script) method should do the same as the other move you wrote, but using the x, y, and level of the given script.
Be careful that you check whether Level variables are null before telling them to do something, to avoid a NullPointerException.

2.4. Test Case

Once you have finished the items above, you can run the "Test AddAndRemove" test case. In fact, it would be a good idea to rerun all the test cases and make sure that everything is still working, since sometimes a change you make in one place can mess up something else.