Chapter 1 Introduction So far, our worlds have dealt with just a few objects. But it's very likely that you'll want to put many objects in your worlds to make them more interesting. For example, have you ever seen the Web site that has the Hamster Dance? At one time, this was the most viewed page on the Web. It has 2D images of hamsters that dance and sing. There are hundreds of images on this page, and they all move together in unison. Although this page doesn't use the programming structures that we're talking about in this class, it does present an interesting issue about programming. For example, suppose you decide to create a world with cheerleaders performing a routine during a halftime show. One part of the routine might involve all of the cheerleaders performing the same move. You could add multiple cheerleader objects into the world and then write the code to move these people one at a time. But that would be tedious, and with the multiple lines of code, it could introduce errors. In this lesson, I'll show you another, easier way to create an animation like this. Instead of treating each of our objects separately, we'll group them together in what Alice calls a list. Then we'll have Alice go through each object in the list and perform some task. This will make our job as programmers much easier and give us more time to focus on other things that will make our worlds more interesting. Chapter 2 Alice Lists A common theme throughout this course has been organization. I've stressed how important it is to organize your variables, methods, and functions with good names. I've also stressed organizing the location of your methods and properties. That's because as your worlds grow, there will be more to keep up with. The better organized your code, the easier your job as a programmer. Programmers usually talk about using different structures to organize their objects and variables. These structures are called data structures. There are many different types of data structures, but one of the most common is the list. We're all familiar with lists because we use them nearly Page 1 every day. There are grocery lists, chore lists, lists of items on a menu. A list is just a collection of related things. Alice gives us the ability to create a variable as a list. The simple variables we've used so far could only store a single item. However, list variables can store multiple items. We can then use that list to go through each item, one by one, and have that item perform some task. Let's start with a short example world with a few ice skaters doing some simple turns. Start Alice and create a world with the Snow template. Go to the Environments gallery and add a Lake object. The Lake object is a whole set of objects that includes a frozen lake, hills, trees, a house, and a sky. Since there are so many parts to this object, it'll take a few seconds to add it to your world. However, when you're done you'll see quite a scene. Next, go to the People gallery and find the IceSkater object. Add three of these to your world. When you do this, all three IceSkaters will end up on top of each other. Go ahead and move them so they're side by side. I moved my first skater to her right 2 meters, and I moved the third skater to her left 2 meters. Now we have our objects in the world, but we don't have a list yet. To make a list, we'll need to create a new list variable. Since we'll want the list to be available to everything in the world, we'll make this a variable in the world object. Select this object by clicking world in the Object Tree. Next, go to the Details area and select properties. We're going to create a new list variable, so click the create new variable button. Give this variable the name skater list. You'll need to be sure to specify that this is going to be an object variable by clicking the radio button next to Object. One important thing to remember with list variables is that you can store values of any data type, but all the items in the list need to have the same data type. That is, you can't have one item in the list be a number, one a string, and another an object. This is against the rules. However, when you select Object, you can have different kinds of objects. That is, your list can have a person object, a bunny object, and a ball object. This is possible because even though they're different types of objects, they're all still objects. Finally, to make this a list variable, you'll need to select the check box next to the words make a. Be sure that the drop-down box after make a says List. If it says Array, click the drop-down arrow and select List. Arrays are a different data structure. You'll learn about arrays in the next lesson. For now, just be sure you're creating a list. When you select the check box to make a list, the dialog box will expand down. Now there's a blank area with a button to create a new item. Our list will have all three skaters, so click new item three times. By default, Alice creates a list item with nothing in it. Click the drop-down arrow next to item0's None. Use the shortcut menu to select iceSkater > the entire iceSkater. Do the same thing for item1 and item2, but this time be sure to select iceSkater2 > the entire iceSkater2 and iceSkater3 > the entire iceSkater3, respectively. Your dialog box should now look like this: Page 2 Create new variable dialog box If your dialog box doesn't look like mine, be sure to make the necessary changes before proceeding. When you're finished, click OK. Alice now places our new list variable at the top of the properties in the Details area. Notice that the items in the list appear inside a button after an equals sign. Use this button to view the list or add or remove items. Click the button, and we'll see how this is done. Adding or Removing an Item From a List When you click the button, you'll return to a dialog box that's similar to what you just used to create this variable. It should be pretty obvious that you add items by clicking the new item button, just like in the previous dialog box. You might think that you can delete items just by right-clicking them and using a shortcut menu. But Alice doesn't let you do it this way. Instead, to delete items, you'll need to drag each item from the dialog box and drop it on the trash can icon next to the Redo button below the menu bar. Your list should be correct with the three skaters, so click OK and let's start working with our group in the next chapter. Chapter 3 Working With List Variables Let's start by making each of our skaters do a simple turn. As I said earlier, we could just place a Do in order structure into our world.my first method method and get our skaters to turn one at a time. But since we have a list, all we need is a single instruction. Page 3 This one instruction involves a new programming structure, For all in order. This structure does exactly what its name implies. It will go through a list of items, one by one, and have them perform some task in order. Drag the For all in order icon into the Editor area. When you drop it, a shortcut menu will appear. You'll want to select the skater list by clicking expressions > world.skater list. Alice will now place code in the Editor area that looks like this: For all in order code This code shows that Alice is going to take each of the items from the world.skater list variable, one at a time, and have them perform the code inside the structure. At this point, we'll need to do something a little different than we've been doing. So far in this course, we've been selecting an object in the Object Tree and then dragging a method and dropping it in the Editor area. Then when we played the world, this method would run. However, we're now working with a list variable. This variable is part of the world object, so it doesn't show up in the Object Tree. But if you look at the code in the For all in order structure, you'll see something that looks like a variable at the end of the first line. This is a yellow-orange colored box that says, item_from_skater list. Alice is going to take each item from the list, one at a time, and store it in this variable. We can then use that variable inside our For all in order structure. Let me show you. Drag the item_from_skater list and drop it on top of the Do Nothing in the line below it. Alice now displays a shortcut list of all the methods that are available for this object. Use the list and choose item_from_skater list turn > left > 1 revolution (all the way around). Play the world and watch each of your skaters turn around, one by one. Did your skaters turn one by one from left to right, or did they go out of order? If they went out of order, there are two possible reasons why. First, you might have placed your skaters in the world out of order. Remember, when we first placed them in the world, they were all on top of each other, and we needed to move them. It's possible that you moved a different skater than you thought. You can check this by clicking each skater in the Object Tree and making sure that they're highlighted from left to right. If not, then you should arrange them in the correct order. Another possible reason for the skaters turning out of order is how they're arranged inside the list. Click the world object and look at the skater list variable. Click the button for the list and see how they're arranged. Remember, Alice will just go through the list one by one, starting with the first object going to the next one in order. If your list uses iceSkater, then iceSkater3, then iceSkater2, then the skaters will turn in that order. You can fix this by rearranging the list. Of course, for our exercise here, it isn't terribly important that the skaters turn in order, but it might be important in something else you create. Just remember that you can make this adjustment to fix it. Next, we'll look at another new structure and see how we can use it to get our objects working in unison. Page 4 For all together Similar to the For all in order structure that Alice uses to work with lists, there's also a For all together structure. Again, as the name implies, this structure will make every item in a list perform the same action at the same time. Let's practice by dragging one of these structures from the bottom of the Editor area and dropping it just below our For all in order code. Again, when you drop this structure, a shortcut menu will appear that lets you choose the list. Be sure to select expressions > world.skater list. Notice how the code that Alice places is nearly identical to the For all in order code above it. There's only a small difference. Instead of the words one and at a time in the first line, it has the words every and together. So the line reads, For all world.skater list, every item_from_skater list together. Again, drag the item_from_skater list variable from the For all together line, and drop it on top of Do Nothing. Go ahead and make the skaters turn left one revolution. Play your world. Now each skater makes a complete turn individually, and then they all turn at the same time. Are you experiencing Deja vu? Remember how we made a world with Socrates, Euripides, and Plato randomly moving forward? We used variables there, too, and we randomly picked one to move so we could practice working with loops and decisions in our worlds. But that was also a lot of code. Now that we know about lists, these actions are much easier to program. I'll show you how in the next chapter by teaching you a little more about lists and methods. Chapter 4 Turning a Specific Skater Before we finish exploring some of the other nice features of lists, I want to review how we can use parameters with our user-defined methods or functions. In a previous lesson, we wrote some functions that took variables as parameters. You'll recall that when we created the world to move our dump truck, we created a function to calculate the distance that our truck tire was going to cover. To make our function more modular there, we used a parameter named distance. That way, we could just call the function and tell it how far we wanted the tire to move, and it returned the number of revolutions that the tire needed to make. Well, we're going to use that concept again here with our skaters. We'll create a method that will turn the skater. But instead of writing a different method for each individual skater, we'll just make this method accept a specific skater to turn as a parameter and then turn that skater. Again, this will cut down on the amount of code we need to write and make it more modular. When we finish this example, we'll have a world that has our three skaters turning and skating off in unison. To do this, first we'll create a new world method. Do you remember how to do this? Just select the world object from the Object Tree and click the create new method button in the methods tab. Call this method, quarter turn. Page 5 Remember, the world.my first method method will call on quarter turn and tell it which skater to turn. In order to do this, quarter turn needs a place to store that object. We'll create that place by clicking the create new parameter button in the upper-right corner of the Editor area. The dialog box that opens looks just like the one that we use to create a new variable. Again, this is because we're creating a new variable. The only difference is that this is a special variable that we'll use to communicate with the code that calls this method. Use the dialog box to create an object parameter named which skater. Here's what your dialog box should look like: Create new parameter dialog box When you're finished, click OK. You'll now see this parameter at the top of the Editor area. Now that we have our parameter, we're able to use it just like a variable anywhere in this method. In other words, to make this object turn, just drag it from the top of the Editor and drop it on top of the Do Nothing. When you drop it, use the shortcut menu to choose world.quarter turn.which skater turn > left > 1/4 revolution. That's it. Our method is complete. Next, we'll write code to call this method. Click the world.my first method tab to view this method's code. If you still have the code that uses the For all in order and For all together to turn the skaters, you can either delete it or disable it. I'm disabling mine. Be sure you still have the world object selected in the Object Tree, and look through its methods. You should now see an item for quarter turn. Drag this into the Editor and a shortcut menu appears. Alice needs you to specify the object to be used as the which skater parameter. Select iceSkater3 > the entire iceSkater3 object just to test out our method. Play the world and you'll see the third skater turn to her left, one-quarter of a revolution. This is a complex concept, so let me explain what happened. When the world began, the world.my first method code ran. The only code in here is a call to world.quarter turn. This method will turn any object one-quarter revolution. But our code was written so that it would turn our third ice skater. So in order to do this, we needed to pass that object to the quarter turn method as a parameter. When we did this, the quarter turn code ran and turned the object that we passed. This is a very useful way to write code, but you might be wondering what it has to do with our lists. Well, the great thing about lists is that not only can we use all of them with the For all in order and For all together structures, but we can also select a single item from the list and have it perform some action. Here's how. Page 6 Selecting One Item From a List Be sure you have the world object still selected, and click the properties tab in the Details area. Drag the skater list variable from the Details area, and drop it on top of the which skater = iceSkater3 item in the Editor area of the world.my first method tab. When you drop it, you'll see four options, as shown below: Shortcut menu when dropping a list variable This shortcut menu gives you the ability to choose the first item, last item, random item, or even one of the other items from the list. Let's make this similar to our example from the earlier lesson and choose random item from list. Just to be sure this is really giving us a random item, go ahead and put this line of code inside a loop, and we'll call this method 10 times. Play your world and you should see random skaters turn 10 times. Now, if you'll look back to our world that randomly chose a character to move forward, I think you'll agree that using a list and a method that takes a parameter is far easier. Now that we have all the tools, let's put them together to make our skaters turn and skate off into the world. Putting It All Together: An Ensemble Performance Before we start writing the code, let's plan out what we want to happen. When the world first begins, we'll want each of the skaters to turn, one at a time. Then, after they've all turned, the skaters will move forward together and then move off the screen. To do this, the first thing to do is remove all the code from world.my first method. This will give you a clean slate to work with. Next, write the code to turn the skaters, one at a time. How will you do this? Probably the easiest way is to use a For all in order structure. Drag one into world.my first method. Inside this structure, you'll want to call the quarter turn method for each skater. Drag the quarter turn method and drop it on top of the Do Nothing in the Editor area. Use the shortcut menu and select expressions > item_from_skater list. Play the world and make sure that each skater turns one-quarter revolution. Underneath the For all in order code, place a For all together structure for the skater list. This time, drag the item_from_skater list variable down on top of the Do Nothing, and have the skaters move forward 5 meters. Use the more drop-down arrow to slow this action down so that it takes 5 seconds to run. Just for good measure, also change the style to abruptly so that our skaters' movements are smooth. When you're done, play your world and see how our winter wonderland dancers do! Page 7 Chapter 5 Summary Sometimes after teaching Alice lists to my classes, I get comments like, "What's the point?" I can appreciate this comment because it's something that I thought the first time I learned about lists. But it's a big deal to be able to use lists in our worlds. Remember, if you're working with a small world that only contains a few objects, your code is easy to write and manage. But as your worlds get larger and you need to write code for more and more objects, organization will become key. In this lesson, you saw that lists are just variables. But unlike regular variables that can only store one value, our lists can hold many values. Today, the values that our lists stored were objects. We used this list variable along with two new programming structures: For all in order and For all together. These two structures were similar to the Do in order and Do together structures we've been working with, except that they work with lists. We used these new structures to manipulate every item in our list and make each one turn around. We then turned our attention back to methods and learned about parameters. You saw how parameters help to pass an item from one method to another. This gives you the ability to write generic methods to take any object and perform some task with that object. Earlier in this lesson, I mentioned arrays and told you that we'd discuss them in another lesson. In fact, since these two topics are similar, I'll be showing you about arrays next, in our final lesson. You'll see that while they're similar to lists, there are some differences. Most regular programming languages give you a way to create an array variable, so in an effort to better prepare you for your future programming experiences, I'll show you all about arrays next. Also, because lists and arrays are different things in Alice, I want to show you how they compare to one another and give you a little practice. And to make things even better, I'll show you how to do a visual implementation of the famous Bubble Sort, which you can use to sort the items in an array. So when you're ready to sort it all out, I'll see you in our final lesson! Page 8