25
Repetition: Definite Loops Alice

Repetition: Definite Loops · Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Repetition: Definite Loops · Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear

Repetition: Definite Loops

Alice

Page 2: Repetition: Definite Loops · Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear

RepetitionIn many kinds of animations, especiallysimulations and games, some actionshappen again and again.

Example: Gallery games where targets appearrandomly on screen and then disappear only toappear elsewhere in the scene.

Of course, actions are made to happenagain and again by running an animationinstruction (or a method) more than once

Page 3: Repetition: Definite Loops · Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear

Example

A bunny sneaks into a garden and wants toeat the broccoli. The bunny will need to hopseveral times to get to the broccoli.

Page 4: Repetition: Definite Loops · Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear

bunny.hop

Page 5: Repetition: Definite Loops · Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear

One solution

Creating the same instruction again andagain is somewhat tedious and the codegets longer and longer.

Page 6: Repetition: Definite Loops · Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear

Counted LoopA counted loop is an alternate way to writerepetitive code

Repeats instructions a counted number of times

Page 7: Repetition: Definite Loops · Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear

DemoCh07Lec1BunnyHopConcepts illustrated in this example

The loop instruction executes a definite number oftimes, specified by a countUsing a loop instruction

saves time is convenient

Page 8: Repetition: Definite Loops · Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear

Demo

Ch07Lec1CarouselinfinityConcept illustrated in this example

If “Infinity times” is selected for a loop, this means the loopwill run until the program is shut down

Page 9: Repetition: Definite Loops · Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear

More complicated loops

It is also possible to place a loopstatement within another loop statementThis is called nested loops

Page 10: Repetition: Definite Loops · Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear

An example of nested loops

The whole Ferris wheel will rotate clockwise, whilethe two inner wheels will rotate counterclockwise.The inner wheels should perform 2 revolutions foreach outer loop revolution.

Page 11: Repetition: Definite Loops · Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear

Demo

Ch07Lec1FerrisWheelConcept illustrated in this example

The inner loop runs completely each time theouter loop runs once. An outer loop that executes 2 times and aninner loop that executes 5 times will actuallyexecute the inner loop 10 times.

Page 12: Repetition: Definite Loops · Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear

Using a function

A loop count can be computed by calling afunction that returns a number value.The loop instruction automatically rounds thereturned value to the nearest whole number.Demo: Ch07Lec1LoopWithFunctionCall

Page 13: Repetition: Definite Loops · Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear

While: Indefinite Loops

Alice

Page 14: Repetition: Definite Loops · Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear

Repetition

In some situations, we don’t know exactlyhow many times a block of instructionsshould be repeated.All we know is that repetition is needed

For example, in a board game like chess orcheckers, we don’t know exactly how manymoves it will take for a player to win or losethe game – all we know is that severalmoves will be needed.

Page 15: Repetition: Definite Loops · Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear

Indefinite RepetitionIn programs where a count of repetitions is notknown (indefinite), we can use one of tworepetition control mechanisms:

While statement Recursion

This session focuses on the While statement.

Page 16: Repetition: Definite Loops · Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear

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 thecondition that determines whether the loop will berepeated.

Page 17: Repetition: Definite Loops · Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear

ExampleA common feature in popular "action films" is anexciting chase scene.As an illustration of an animated chase scene, considerthe hungry shark in this world. The shark is going tochase after and catch a fleeing fish.

Page 18: Repetition: Definite Loops · Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear

Problem

The problem is how do we get the sharkto chase the goldfish in a chase-likeaction?

The shark should not immediately catch thegoldfish (otherwise, there would be no chase). The goldfish (assuming self-preservationinstincts) should appear to be fleeing.

Page 19: Repetition: Definite Loops · Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear

Solution To create a chase scene,

At the same time, the shark will swim ashort distance toward the fish and the fishwill swim a short distance away from theshark. The fish will flee to a random (but nearby)location. As long as the goldfish is still 0.5 metersaway from the shark, repeat the actions.

Page 20: Repetition: Definite Loops · Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear

Storyboardchase

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 arecomplex. Use stepwise refinement to break them down intosimple steps.

Page 21: Repetition: Definite Loops · Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear

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 fleeshark eat (goldfish)

swim

tDo in order urn 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 22: Repetition: Definite Loops · Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear

Demo

Ch07Lec2ChaseConcepts illustrated in this example

A While statement uses a Boolean conditionto determine when the repetition ends. Code written in a previous program can bereused in a new program.

In this example, the flee method calls thepreviously written randomMotion method.

Page 23: Repetition: Definite Loops · Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear

Shark will catch goldfish

How do we know the shark will eventuallycatch the goldfish?

The shark always moves 0.4 meters towardthe goldfish The goldfish's random motion is restricted bythe min and max values used in the randomnumber function.

Page 24: Repetition: Definite Loops · Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear

The loop will end

Geometrically, the fish can never move morethan 0.35 meters away The shark has a distance advantage andwill eventually catch up. The loop will end.

0.2 0.2

0.20.35

Page 25: Repetition: Definite Loops · Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example: Gallery games where targets appear

General “Rule of Thumb”

As a general rule, a While loop should bewritten so the loop will eventually end.

Requires that statements within the loopchange the conditions of the world such thatthe condition for the While statement willeventually become false.

If the While loop never ends, it is aninfinite while loop.