alicewhileloop1 while loop in alice stephen cooper wanda dann randy pausch barb ericson oct 2009

21
AliceWhileLoop 1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009

Upload: larry-badman

Post on 01-Apr-2015

221 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009

AliceWhileLoop 1

While Loop in Alice

Stephen CooperWanda Dann

Randy PauschBarb Ericson

Oct 2009

Page 2: AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009

AliceWhileLoop 2

Learning Goals

• Understand at a conceptual and practical level– Random Numbers– Random 3D motion– While Loops– Infinite Loops

Page 3: AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009

AliceWhileLoop 3

Random Numbers

• Random numbers are used in certain kinds of computer programs

• Examples:– security for web applications– encryption for satellite transmissions– gaming programs– scientific simulations

• In this session, we will look at examples of how to use random numbers in animations

Page 4: AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009

AliceWhileLoop 4

Built-in functions

• Alice provides built-in functions for generating random numbers as a function in the world

Page 5: AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009

AliceWhileLoop 5

Random 3D Motion• In some animations, we want an object to move

to a random location in 3D. We call this random 3D motion.

• For example, the goldfish in this world is to swim in a random motion, where the fish can move in any direction .

Page 6: AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009

AliceWhileLoop 6

Six possible directions• Of course, six move directions are possible

– forward, backward, left, right, up, down

• In this example, we can eliminate backward because goldfish do not swim backward.

• To simplify the code, we can take advantage of negative numbers.– For example, this instruction actually moves the

goldfish right:

Page 7: AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009

AliceWhileLoop 7

Storyboard• Only three move instructions are needed:

– up (will move down if the random number is negative)– left (will move right if random number is negative)– forward (no backward motion)

• Two parameters (min, max) will be used to restrict the motion of the fish to a nearby location-- to look like swimming.

randomMotion

Parameters: min, max

Do together fish moves up a random number distance fish moves left a random number distance fish moves forward a random number distance

Page 8: AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009

AliceWhileLoop 8

Demo 07Goldfish• Concepts illustrated in this example

– A random movement in 3D space is accomplished by three simultaneous move instructions.

– In this example, the minimum distance of the move forward instruction is 0 (the goldfish always moves forward).

– To call the randomMotion method, min and max values are sent as arguments to the parameters

Page 9: AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009

AliceWhileLoop 9

Indefinite repetition• In some situations, we don’t know exactly

how many times a block of instructions should be repeated.

• All we know is that repetition is needed– For example, in a board game like chess or

checkers, we don’t know exactly how many moves it will take for a player to win or lose the game – all we know is that several moves will be needed.

• We can use a while loop in this case

Page 10: AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009

AliceWhileLoop 10

How the While statement works

• The general idea is: While some condition is true

execute instruction(s)• To write a While statement, we need to know the

condition that determines whether the loop will be repeated.

Page 11: AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009

AliceWhileLoop 11

Example• A common feature in popular "action films" is an

exciting chase scene.

• As an illustration of an animated chase scene, consider the hungry shark in this world. The shark is going to chase after and catch a fleeing fish.

Page 12: AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009

AliceWhileLoop 12

Problem

• The problem is how do we get the shark to chase the goldfish in a chase-like action?– The shark should not immediately catch the

goldfish (otherwise, there would be no chase).– The goldfish (assuming self-preservation

instincts) should appear to be fleeing.

Page 13: AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009

AliceWhileLoop 13

Solution• To create a chase scene,

– At the same time, the shark will swim a short distance toward the fish and the fish will swim a short distance away from the shark.

– The fish will flee to a random (but nearby) location.

– As long as the goldfish is still 0.5 meters away from the shark, repeat the actions.

Page 14: AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009

AliceWhileLoop 14

Storyboard

chase

While the goldfish is more than 0.5 meters away from the shark Do in order shark point at the goldfish Do together shark swim (toward the goldfish) goldfish flee (away from the shark)shark eat (the goldfish)

The shark swim, goldfish flee, and shark eat actions are complex. Use stepwise refinement to break them down into simple steps.

Page 15: AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009

AliceWhileLoop 15

chase

While the goldfish is more than 0.5 meters from the shark Do in order Point the shark at the goldfish Do together shark swim goldfish flee shark eat (goldfish)

swim

Do in order turn torso left and move forward turn torso right and move forward turn torso left and move forward

flee

Do together

wiggle tail move to random location

eat

Parameter: whatDo in order shark points at what shark opens jaw and what disappears shark closes jaw

Page 16: AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009

AliceWhileLoop 16

Demo 07Chase

• Concepts illustrated in this example– A While statement uses a Boolean condition

to determine when the repetition ends.– Code written in a previous program can be

reused in a new program.

Page 17: AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009

AliceWhileLoop 17

The shark will catch the goldfish

• How do we know the shark will eventually catch the goldfish?– The shark always moves 0.4 meters toward

the goldfish– The goldfish's random motion is restricted by

the min and max values used in the random number function.

Page 18: AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009

AliceWhileLoop 18

The loop will end

– Geometrically, the fish can never move more than 0.35 meters away

– The shark has a distance advantage and will eventually catch up. The loop will end.

0.20.2

0.20.35

Page 19: AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009

AliceWhileLoop 19

General “Rule of Thumb”

• As a general rule, a While loop should be written so the loop will eventually end. – Requires that statements within the loop

change the conditions of the world such that the condition for the While statement will eventually become false.

• If the loop never ends, it is an infinite loop.

Page 20: AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009

AliceWhileLoop 20

Challenge

• Modify 07frogAndLadybugNoCode so that the while the distance from the frog to the ladybug is greater than 2 – The frog turns toward the ladybug– The frog hops

• Once the frog is within the distance to the ladybug – Have the frog turn to face the camera– And say “ribbit”

Page 21: AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009

AliceWhileLoop 21

Summary

• There is a function in the world that will create a random number

• You can use random numbers to generate random 3D movement

• You can use a while loop to repeat a block of statements while a condition is true or false

• An inifinite loop never ends