Saturday, March 20, 2010

Alice and the world of OOP

In this post we'll introduce Alice, described on the web site as:
Alice is an innovative 3D programming environment that makes it easy to create an animation for telling a story, playing an interactive game, or a video to share on the web. Alice is a teaching tool for introductory computing. It uses 3D graphics and a drag-and-drop interface to facilitate a more engaging, less frustrating first programming experience.

The splash screen for the software describes as a free gift from Carnegie Mellon. We'll be using version 2.2 for Windows in these examples.


In this entry we'll build a simple Alice world with one object and a few instructions. Download and install Alice 2.2 from the alice.org website.

Playing in the Sand
When you start Alice you will presented with an initial dialog box that is positioned on the tutorial tab. I encourage you to do the tutorials, but for now click on the Template tab. Select the Sand template and click Open. I want you to think about what you are doing and play with the software. As such I will not be including any screen captures with this exercise. Whatever you get is "correct".

The main Alice window is divided into several parts. In the top center is a small preview window of the world. In the lower right is a small green button labeled Add Objects. Click that button to open the object gallery. You will see that Alice provides a wealth of objects to play with. Scroll the object groups list until you find the Sports category. Click on the Sports category and then select Beach Ball from the list of available objects. Click the Add Instance to World button. Then click the Done button in the gallery. You should now be back at the main window with a beach ball sitting on your sand.

Let's make our beach ball do something and then we'll talk terminology and what the different areas of the main window indicate. We'll be working in the section of the screen labeled world.my first method. We won't worry about the parameter and variable sections right now. We're going to add some actions where it currently says Do Nothing. At the bottom of this section you will see several process control instructions. Click on the Do In Order instruction and drag it in place of the Do Nothing instruction.

In the upper left of the screen you will see a list of all the objects in your world. Click on the beachBall object. This will change the contents of the lower left pane in the window. It now displays things related the beach ball. Make sure that the Methods tab is selected. (This should be the default.)  The first instruction listed in the methods area is beachBall move. Click on the method and drag it into the window where the Do Nothing indicator is below the Do In Order instruction. You will then be prompted to select a direction and an amount. Select up and 5 meters to have your instruction added to the method. Save your world using the option from the File menu. Then click the Play button in the upper left. The Alice World window will open and you will see your beach ball rise into the air and out of view. Congratulations you've built your first Alice world! Let's add some additional instructions to make it a little more satisfying.

What goes up...
Let's add an instruction that brings the ball back down to the sand. Drag another copy of the beachBall move method into our world method pain. This time reverse the instructions and have it move down 5 meters. Save your world and then Play it. Better the ball goes up and then comes back down.

Let's enhance the process with a little sound. In the method list find the beachBall play sound method. Drag it below the second beachBall move. Select thud1 as the sound to play. Save and play your world. Not great but at least there some audio. We'll add one more sound that will require us to modify the program a little bit. Grab the Do Together instruction from below the main method code. Drag it up above the first beachBall move instructions and drop. Drag and move the first beachBall move instruction into the Do Together instruction. Then drag a new beachBall play sound instruction into the Do Together grouping. Select the whoosh1 sound to play. Save your world then click Play.

Much more satisfying as the ball makes a sound as it flies up and another as it lands. Experiment with this world and also the tutorial worlds. Next time we'll explore the terminology of Object Oriented Programming and how that applies to Alice.

Wednesday, March 3, 2010

Moving Beepers

As promised in the previous post here is a world and some code for moving beepers.

First the world definition:
robot 2 2 N 0
wall 2 2 W 8
wall 2 9 N 8
wall 9 2 E 8
wall 2 2 S 8
wall 6 2 W 3
wall 6 7 W 3
beepers 5 9 1

If you saved the "two rooms" world file this is the same thing with a couple of changes. First off the last number on the robot line is now 0. This indicates that Guido starts off with no beepers in his beeper bag. The other change is the addition of the beepers line at the end.

Now for some code. Since we don't have an indicator of when to stop, like the bag full of beepers in the previous post, we need to do a counted loop. I made some changes in approach to allow counting the corners that the robot hits. The code looks like this:

define turnright:
   
do 3:
        turnleft

do 
8:
    while front_is_clear:
        if left_is_clear:
            while left_is_clear:
                turnleft
                move
        else:
            move
   
turnright
    if
any_beepers_in_beeper_bag:
        if not_next_to_a_beeper:
            putbeeper
    else:
        if next_to_a_beeper:
            pickbeeper   
turnoff

The main thing here is that we are looking for corners so the while loop is controlled by the front_is_clear condition.

Changes to try: Put the do 8 loop inside of another loop, for example make a do 4 loop. Then indent all of the rest of the code in by 4 spaces at the beginning of the line. Change the number beepers at the point indicated to 4. Run the program and GvR should do 4 laps and place one beeper in each corner of the "second" room.

That's it for now. Next time we'll switch gears to Alice for a little while.