CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall...
Transcript of CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall...
![Page 1: CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall 2011 1 . Announcements • No reading or reading quiz for Thursday • Assignment](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6cd7eb33a1e15ab23ad8af/html5/thumbnails/1.jpg)
CompSci 4 Chap 7 Sec 2 Oct 25, 2011
Prof. Susan Rodger
CompSci 4 Fall 2011 1
![Page 2: CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall 2011 1 . Announcements • No reading or reading quiz for Thursday • Assignment](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6cd7eb33a1e15ab23ad8af/html5/thumbnails/2.jpg)
Announcements
• No reading or reading quiz for Thursday • Assignment 6 out, due Nov 4 • Today
– Lecture on Chap 7 Sec 2 and Tips and Tech. • While loop – indefinite loop • Event Loops
CompSci 4 Fall 2011 2
![Page 3: CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall 2011 1 . Announcements • No reading or reading quiz for Thursday • Assignment](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6cd7eb33a1e15ab23ad8af/html5/thumbnails/3.jpg)
Last time -Loop – definite number • What happens when this code runs?
![Page 4: CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall 2011 1 . Announcements • No reading or reading quiz for Thursday • Assignment](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6cd7eb33a1e15ab23ad8af/html5/thumbnails/4.jpg)
Repetition
• Sometimes don’t know exactly how many times a set of instructions are repeated.
• Stopping is based on a condition • Example:
– Game of Chess, how many moves until win – Stop: when markers are in check mate position
CompSci 4 Fall 2011 4
![Page 5: CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall 2011 1 . Announcements • No reading or reading quiz for Thursday • Assignment](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6cd7eb33a1e15ab23ad8af/html5/thumbnails/5.jpg)
Indefinite Repetition
• In programs where number of repetitions not known in advance, can use – While statement
CompSci 4 Fall 2011 5
![Page 6: CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall 2011 1 . Announcements • No reading or reading quiz for Thursday • Assignment](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6cd7eb33a1e15ab23ad8af/html5/thumbnails/6.jpg)
While statement
• While some condition is true – execute instructions
condition true?
execute instructions
no
yes
exit loop
6
![Page 7: CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall 2011 1 . Announcements • No reading or reading quiz for Thursday • Assignment](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6cd7eb33a1e15ab23ad8af/html5/thumbnails/7.jpg)
Example • Common feature in popular “action films”
is a chase scene • Example: hungry shark chasing fleeing
goldfish – Repeat: fish swim away from shark, and shark
swim toward fish – Shark swim distance a little more than fish
swim distance – Eventually, shark will catch up with fish and
eat fish CompSci 4 Fall 2011 7
![Page 8: CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall 2011 1 . Announcements • No reading or reading quiz for Thursday • Assignment](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6cd7eb33a1e15ab23ad8af/html5/thumbnails/8.jpg)
Storyboard World.chase While goldfish more than .5 meters from shark Do in order shark point at goldfish Do together shark swim (toward goldfish) goldfish flee (away from shark) shark eat goldfish
shark.Swim, shark.eat and goldfish.flee in book 8
![Page 9: CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall 2011 1 . Announcements • No reading or reading quiz for Thursday • Assignment](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6cd7eb33a1e15ab23ad8af/html5/thumbnails/9.jpg)
World.chase
CompSci 4 Fall 2011 9
![Page 10: CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall 2011 1 . Announcements • No reading or reading quiz for Thursday • Assignment](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6cd7eb33a1e15ab23ad8af/html5/thumbnails/10.jpg)
Shark will catch goldfish
• How do you know the shark will eventually catch the goldfish? – Shark always moves 0.4 meters towards goldfish – Goldfish moves randomly away from shark at most
.2 meters – Shark will eventually catch up, the loop will end
CompSci 4 Fall 2011 10
![Page 11: CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall 2011 1 . Announcements • No reading or reading quiz for Thursday • Assignment](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6cd7eb33a1e15ab23ad8af/html5/thumbnails/11.jpg)
General “Rule of Thumb”
• As a general rule, a While loop should be written so the loop will eventually end – Requires statements inside the loop change the
conditions of the world such that condition for While eventually becomes false
• If While loop never ends – Infinite while loop
CompSci 4 Fall 2011 11
![Page 12: CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall 2011 1 . Announcements • No reading or reading quiz for Thursday • Assignment](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6cd7eb33a1e15ab23ad8af/html5/thumbnails/12.jpg)
Practice – From Bunny eats Broccoli
• Replace with while loop, bunny hops until close to broccoli (< 1 meter)
CompSci 4 Fall 2011 12
![Page 13: CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall 2011 1 . Announcements • No reading or reading quiz for Thursday • Assignment](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6cd7eb33a1e15ab23ad8af/html5/thumbnails/13.jpg)
Using While with Events • Create new event “while something is true”
• Result
We call this a "BDE"
13
![Page 14: CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall 2011 1 . Announcements • No reading or reading quiz for Thursday • Assignment](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6cd7eb33a1e15ab23ad8af/html5/thumbnails/14.jpg)
BDE – Begin During End
• Event – actions occur at different times • When the event is first true
– Begin action • While the event is still true
– During action repeats • When the event condition is false
– End action occurs
CompSci 4 Fall 2011 14
![Page 15: CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall 2011 1 . Announcements • No reading or reading quiz for Thursday • Assignment](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6cd7eb33a1e15ab23ad8af/html5/thumbnails/15.jpg)
Example – Penguins meeting
• While 2 penguins are > 3 meters apart – Turn to face each other (Begin) – Move towards each other repeatedly (During) – Bow to each other (End)
CompSci 4 Fall 2011 15
![Page 16: CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall 2011 1 . Announcements • No reading or reading quiz for Thursday • Assignment](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6cd7eb33a1e15ab23ad8af/html5/thumbnails/16.jpg)
Penguins Event Code
CompSci 4 Fall 2011 16
![Page 17: CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall 2011 1 . Announcements • No reading or reading quiz for Thursday • Assignment](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6cd7eb33a1e15ab23ad8af/html5/thumbnails/17.jpg)
Penguin Begin and During parts
CompSci 4 Fall 2011 17
![Page 18: CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall 2011 1 . Announcements • No reading or reading quiz for Thursday • Assignment](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6cd7eb33a1e15ab23ad8af/html5/thumbnails/18.jpg)
Penguin End Part
CompSci 4 Fall 2011 18
![Page 19: CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall 2011 1 . Announcements • No reading or reading quiz for Thursday • Assignment](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6cd7eb33a1e15ab23ad8af/html5/thumbnails/19.jpg)
How do we get this event to happen again?
CompSci 4 Fall 2011 20
![Page 20: CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall 2011 1 . Announcements • No reading or reading quiz for Thursday • Assignment](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6cd7eb33a1e15ab23ad8af/html5/thumbnails/20.jpg)
Another Type of Event – When something is true
• When you want ONE thing to happen when something is true
• Not listed as a new event • Must create event “while
something is true” • Then “change to” “when
something becomes true”
![Page 21: CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall 2011 1 . Announcements • No reading or reading quiz for Thursday • Assignment](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6cd7eb33a1e15ab23ad8af/html5/thumbnails/21.jpg)
Add in “mama Penguin” (penguin3 resize by 2)
• Control her with arrow keys event
CompSci 4 Fall 2011 22
![Page 22: CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall 2011 1 . Announcements • No reading or reading quiz for Thursday • Assignment](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6cd7eb33a1e15ab23ad8af/html5/thumbnails/22.jpg)
Penguin3 “take” penquin
• What happens when we move penguin3 over to penguin and then steer penguin3 away?
CompSci 4 Fall 2011 23
![Page 23: CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall 2011 1 . Announcements • No reading or reading quiz for Thursday • Assignment](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6cd7eb33a1e15ab23ad8af/html5/thumbnails/23.jpg)
How do we turn off the move over to penguin2?
• Everytime Penguin3 carries away penguin, penguin wants to move over to penguin2
CompSci 4 Fall 2011 24
![Page 24: CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall 2011 1 . Announcements • No reading or reading quiz for Thursday • Assignment](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6cd7eb33a1e15ab23ad8af/html5/thumbnails/24.jpg)
??
CompSci 4 Fall 2011 25
![Page 25: CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall 2011 1 . Announcements • No reading or reading quiz for Thursday • Assignment](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6cd7eb33a1e15ab23ad8af/html5/thumbnails/25.jpg)
Need another condition
• Can’t turn off the BDE, it kicks in everytime the condition is true
• Must make the BDE condition stronger – AND another condition
CompSci 4 Fall 2011 26
![Page 26: CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall 2011 1 . Announcements • No reading or reading quiz for Thursday • Assignment](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6cd7eb33a1e15ab23ad8af/html5/thumbnails/26.jpg)
What is the difference between?
• While something is true (BDE) • When something is true
• Add in one more event:
• Can the “when” event happen again if it
becomes true again? CompSci 4 Fall 2011 27
![Page 27: CompSci 4 Chap 7 Sec 2 Oct 25, 2011 · Chap 7 Sec 2 Oct 25, 2011 Prof. Susan Rodger CompSci 4 Fall 2011 1 . Announcements • No reading or reading quiz for Thursday • Assignment](https://reader030.fdocuments.in/reader030/viewer/2022040823/5e6cd7eb33a1e15ab23ad8af/html5/thumbnails/27.jpg)
Classwork • World 1 - Modify game from last time with while loop • World 2 - Start a new world with snow background
– Add a penguin and a fish resting on the ice – Create a BDE that moves the penguin over to the fish and
eats it (fish disappears) – More with “when event”
CompSci 4 Fall 2011 28