COSC 123 - Computer Creativity
Lab 3: Decisions and Loops


In this lab, we will practice with decisions and loops. The tutorials in the lab will be presented by the TA step-by-step. However, you are free to work at your own pace on the lab. The lab assignment questions will not be presented by the TA.

Tutorial #1

This part of the lab is a tutorial to cover decisions, while loops, and detecting distances to objects.

Step #1: Initial Setup

Open Alice and create an empty world with a grass template. Put a chicken (or some other object) in the air considerably above the ground.

Step #2: Make Object Fall

Our goal is to make the object fall until it hits the ground. In the my first method create a while statment. Initially, the condition is true, but we will change it to be while object is above ground. Then we will move the object down at 1 meter per second until it hits the ground. When it hits the ground have the object say "Ouch!".

First, we drag the while structure into our method and select true for a default condition. Our condition will involve a ">" comparison. All relational operators are under functions of the world object. Drag the condition a > b to replace true. Pick the value 1 for both a and b to begin.

Click on your chicken object and select functions. Under proximity is the function distance above. Drag that in front of the ">". Then replace the second 1 with a 0. This condition says that keep repeating this loop as long as the chicken is above the ground. What happens when we run the animation now?

An infinite loop is not what we want. We need to move the chicken down every loop iteration. Add a move method to make the chicken more down 1 meter every loop iteration.

Step #3: Advanced Object Fall

We can make the fall look more realistic by increasing the object's speed as it falls. To do this, we need to create a variable called speed. We will increase this speed by 1 every loop iteration.

However, this change sometimes makes the object go right through the ground. We should add an decision using if and only move the full distance down in the time if it will not bring us below the ground. Otherwise, we only move the distance remaining. Answer file: Falling Chicken Answer.

Tutorial #2

This part of the lab is a tutorial to cover decisions, loops, and prompting for input.

Step #1: Initial Setup

Open Alice and create an empty world with a dirt template. Put a car in the world so that you can see if from the side with the car facing right. Move the camera backwards a fair distance.

Step #2: Getting Data

In the my first method create three variables called maxspeed, acceleration, and time. We will set maxspeed = 100 and acceleration = 5. We will ask the user for the value of time. To do that, there are world functions called ask user for either a number, text, or yes/no. We will ask for a number. Drag the time variable into the code section which will make it create a set method. Just have it set to 1 to begin. Then, drag the function ask user for a number to replace the 1. You may change the text that is shown to the user. Note: Alice 2.3 has a bug that prompting for a number >= 10 does not work.

Step #3: Moving the Car

Our task will be to move the car forward. The car will continue to move for the number of seconds given by time. It will continue to accelerate until it gets to its top speed. Each second the car will accelerate. If the time is an even number, it will accelerate twice as fast, otherwise it will accelerate half as fast. Try to create the code that will do this. One thing you need to know is how to determine if a number is even and odd. We can use the world function IEEEremainder of and divide by 2. If the remainder is 0, the number is even, otherwise it is odd. You will need to track the car's current speed. Answer file: Speeding Car Answer.

Lab Question #1

Download the sample world with two frogs. The frog should race right toward a finish line. Every second each frog should hop forward a random distance between 1 and 2 meters. The world ends when both frogs cross the finish line (use in front of function). Make sure that the frog who crossed the finish line first stops jumping once he won. At the end, have the winning frog say "I won!". It should look something like this video.

Lab Question #2

Create a world with a penguin and a hole in the ice. The hole should be put at a random location each time. This can be done by moving a circle forward and left a random amount. Make the penguin move toward the hole in the ice at a speed of 1 m/s until it gets to the hole then jump in. It should look something like this video.

Lab Question #3

Download the sample world with two chickens. We will have the chickens "play chicken". Make each chicken go up a different random distance between 1 and 5 meters. Then make each chicken fly over towards the other side. If the two chickens hit have each say "Ouch!" and then put them back into their original spots. If they do not hit, put the chickens back on to the other chicken's landing spot and turn around. Repeat 10 times and count how many times the chickens collide. It should look something like this video.


Submission Instructions and Marking

For this lab assignment, each group should submit using Connect the three Alice world files for the questions.

Grading (20 Marks Total)

Question #1 (5 marks)

  1. +1 - move frogs together a random distance
  2. +1 - hop until both cross finish line
  3. +1 - frog stop hopping if already crossed line
  4. +2 - say who is winner

Question #2 (5 marks)

  1. +2 - random circle position
  2. +2 - walk to circle (using While)
  3. +1 - jump in circle

Question #3 (10 marks)

  1. +2 - fly chicken's up a random distance
  2. +2 - fly chicken's forward and detect collision
  3. +2 - handle collision and replace chicken
  4. +2 - replace and turn around chickens if no collision
  5. +1 - track number of hits in 3D Text
  6. +1 - repeat 10 times

*Home