# MCS-170 The Nature of Computer Science

## 1. List Processing -- Creating Lists of Objects

• Overview
• Creating Lists of Objects
• Iterating Through a List
• List Searching
• Frogger Game
• Creating a List of Objects
• Suppose you wished to create a scene where many objects of the same type were involved -- for example, a bumper car simulation.  Suppose that we created 8 cars and then wrote a general car method that randomly drove the car around. To use this method we would have to drag it into the method editor many times (at least once for each car). This is tedious. It would be better to have a list of cars and then tell the list to implement the method for each car it contains.
• A List is a data mechanism that is used to collect or organize a group of items. Is is often called a data strucure in computer science classes.  In Alice a list will contain items of similar type (items such as Objects, or Numbers, or Strings, etc).
• To create a List, we first have to create a group of Objects on the screen.
• Example: Open the ducks world (ducks.a2w). Here we have created a square and positioned three ducks in front of the square. To create a list of these three ducks, a list variable must be created, and since we are dealing with multiple objects, we will create this variable in the World Class.
• Note the picture that is on the square. This is accomplished by importing a texture map into the Properties of the square.  The image is located here. (Demo how to do this)
• Select the Properties tab for the World Class and click the create new variable button.  In the pop-up panel check the box marked make a list to create the list. Then, click the button labeled new item to add an object. Do this three times, each time choosing one of the ducks to add to the list.  Hit Okay when done.
• Iterating Through a List
• Sequential Iteration Suppose we want all the items in a list to do a method.  By using the tab For all in order we can have each object sequentially do the method.
• Example: In our ducks world, suppose we want the ducks to move back and forth in front of the square. Here is a textual description as to how to make that happen.
• Loop Forever
• For all ducks, one at a time do
• duck move forward square width -1
• duck turn around
• Implement this and test (create a world method called duckMove that moves duck across and turns -- then use this in the iteration) Here is an example world (ducks2.a2w)
• Simultaneous Iteration:  Suppose we want the ducks to randomly move back and forth across the screen.
• Example: In our ducks world suppose we want the following:
• For all ducks, every one together do
• Loop Forever
• duck move forward square width -1 for a random duration (3-5 seconds)
• duck turn around
• Implement this and test it out. Here is an example world (ducks3.a2w)
• List Searching
• It is often necessary to search through a list of items to find an item that matches some condition.  List searches can be implmented in ALice by sequentially iterating through a list and checking a match condition.
• Example: In our ducks world, suppose we want to be able to click on a duck and make it say "hello."  (Assume the ducks world has all the features we implemented above)  So, to do the search, we must do the following:
• Create an event that handles a mouse click on an object
• Create a World method (checkForHit) with a duck parameter that is called from the new event
• In checkForHit ->
• For all ducks, one at a time do
• if list->duck == clicked duck
• list->duck say hi
• Implement this. Here is an example world (ducksSearch.a2w)
• Frogger Game
• Here is an example of the classic Frogger game (simplified) implemented in Alice, using lists to handle the lily pads moving back and forth. (AliceFrogger.a2w)