Programming Alice

In a program, you tell the computer what to do when by giving it instructions. In Alice, you will enter instructions in the code editor. Instructions are entered by simply dragging an object's method or property to the code editor.

Many times when you do this, a pop-up menu prompts you to make choices that help to describe the instruction more fully. The choices that you make are known as arguments.

By default, instructions are executed in sequence, i.e. one at a time. You can use the "Do in order" control statement to make this sequential order explicit. Sometimes, however, we need to do several things at the same time. This can be achieved with the "Do together" control statement. Notice that control statements can be nested.

Step-Wise Refinement

As you can imagine, movies can get quite complex. So can computer programs. If you listed every step in the movie sequentially, and then had to change one little thing, making that change might prove to be difficult. We can manage the complexity of a program with a technique know as stepwise refinement.

With stepwise refinement (also known as top-down design), the major parts of a program or movie are described at a very high level of abstraction. Each part can then be refined by breaking it down further into smaller steps. Ultimately, the steps will be broken down into specific instructions.

In Alice, we can control complexity by implementing these parts and steps as custom methods.

Custom Methods

Near the top of each object's list of methods, there is a button to "create new method". One advantage of a method is that it allows us to think about an overall task, instead of all the detailed actions required to complete the task. This is known as abstraction.

Some methods describe the actions of several objects in the world. These are known as world-level methods, and are created as methods of the World. World-level methods are useful for describing major tasks (or scenes) in your Alice world. You call these methods by dragging them into the editing space for the method that uses them. For example, the method that gets called whenever you play the movie (world.my first method) can simply list all the major scenes in your story.

Other methods describe what a specific object can do. These are known as class-level methods, and are created as methods of a specific object in the movie. Class-level methods are useful for describing something that a specific object can do.

In Alice, you can save an object with it's newly-defined methods as a new class. Then, you can create instances of that new class, which can use all of the old methods along with the new ones you defined. This is known as inheritance. This idea of creating new classes that others can use is a common one in computer science. The result is reusable code. Having reusable code means that programmers don't have to keep doing the same thing over and over again.

Parameters and Variables

Sometimes when we define a new method, we want to be able to change how the object does whatever the method defines. For example, if we define a method called "hop" for the bunny, we may want to be able to specify how fast the bunny hops. This can be achieved with a parameter.

A parameter is a place-holder for some value. It represents a place in the computer's memory that holds that value. Then, when we want to use that value, we can simply refer to the parameter name instead. For example, we can define a parameter called speed that holds a number. This number represents the time it takes (in seconds) for the bunny to hop forward once. When the hop method is called (or dragged into the editor), the programmer will be asked to select a number (argument) representing the time it takes to make a hop.

Of course, sometimes we need to use other place-holders in our methods. For these, we can define variables. Like a parameter, a variable represents a numeric or other value that is stored in the computer's memory. When you want to use that value in the program (e.g. the duration of the hop), you use the parameter or variable instead (selected from the menu as an expression). A variable is only available to the method where it is defined.

An expression, like a parameter or variable, represents some value. In fact, variables, parameters, and functions are all types of expressions. Yet an expression also allows you to compute a value using operators. For example, you can use the math operators (+, -, * and /) to add, subtract, multiply, and divide. Operators always work with values known as operands. So, for the expression "2 + 5", 2 and 5 are the operands. Operands can be parameters (such as speed), variables (such as half_speed) or literals (such as 2). You can also use the results of functions (such as bunny's width) and other expressions as operands.

Expressions can also use relational operators that compare one thing to another. For example, you can test if two things are the same (==) or not the same (!=). You can also see if the first operand is less than (<), less than or equal (<=), greater than (>) or greater than or equal to the second operand. You can find these relational operators listed under the world's functions. The result of an expression that uses relational operators is a Boolean value: either true or false. Boolean expressions can also be combined using the Boolean operators: not, and, and or.

Conditions

Sometimes a program needs to make choices about what to do next, based on certain conditions. A condition is simply an expression that is either true or false. Conditional execution determines what the computer will do, based on the value of the condition. In Alice, the "if/else" control structure supports conditional execution.

Repetition

Sometimes a program needs to do something over and over again. Instead of repeating the instructions over and over again, a programmer can tell the program to do something a certain number of times with a control structure known as a loop. The count value, which represents the number of repetitions, must be a whole number, or an expression that represents a whole number.

If the programmer doesn't know how many repetitions are needed, a while control structure can be used to repeat a set of instructions as long as a condition is true. For example, I could tell the bunny to hop toward the carrot as long as he is more than 1 meter away from the carrot.

Events

Most programs that you use are event-driven. Consider, for example, a word processor program. When you type, the text appears in a text window. When you click on text, it becomes highlighted. When you click on the Bold (B) button, the highlighted text becomes bold. Everything that the program does depends on user actions. These actions are also known as events.

Of course, there are other types of events in a program. For example, when you click on the Play button in Alice, this triggers a "the world starts" event. If you look in the upper-right region of the Alice window, you will see that when the world starts, the program will do world.my first method. Whatever instructions are in that method are what will be done when you play the movie.

Alice allows you to define other events, too. If you click on the button that says "create new event", you get to choose what event will occur, what object(s) that event is related to, and what to do when the even occurs.