CS 40 - Lecture 4

David S. Taylor
Lecture 3 Recap

- Top-down design: Break story into scenes, scenes into tasks,
etc.
- Storyboard: Image on top, description, list of actors
and actions below
- Code reading: Important skill. Lesser artists borrow, great
artists steal...
How Can You Get Alice Outside Class?
Please log in to ActiveLecture.org. Click
Discuss, type 1 2 3, click Send.
- I already installed it
- I brought my laptop today so I can get installation help
- I will bring my laptop on Thursday so I can get help
- I will install it myself over the weekend and use the google group
discussion feature if I have problems
- I need to use a computer in a lab that has Alice preinstalled
How Was the Lab Work on Monday?
Please respond in ActiveLecture.org
- Too easy/boring
- Just right
- Too hard/confusing
Thanks for your feedback!
Decisions, Decisions...

- One-time: If a condition is fulfilled, do x
- Repeated: While a condition is fulfilled, do x
- Condition is either true or false. E.g. "the red shoes are on sale"
- Condition may involve relational operators. E.g. "price of red shoes
< price of black shoes"
- Note: Some operators look weird: ==, !=,
<=, >=
If/Else

- Three parts
- condition: an expression that is true or false
- statement (or statement sequence) that executes when condition is
true
- "else" part: (optional), statement (or sequence) that executes when
condition is false
- One of the two branches executes
- Afterwards, the statement following the if/else executes
Example: Eating the Closest Cookie

- Everyone knows that evil step sisters love eating cookies...
- ...but they are vain and don't want to get fat, so they only eat one
per day...
- ...and they are lazy, so they eat the closest one
Lab #1
- Make a world with one evil step sister (People) and two cookies
(Kitchen/Food)
- One cookie should be closer to the ESS
- In world.my first method, use an if/else to have the
ESS move to the closer cookie and eat it
- To “eat” the cookie, turn its opacity to 0.
- Don't fuss with the details. We care about if/else, not
realistic eating.
- Test that your code works: rearrange the cookies, hit Play again
- Feel free to use a different story. Everyone knows that a T.Rex loves
eating toasters...
- Save as essScribe(First Last)Driver(First Last).a2w
example: essScribe(David Taylor)Driver(Joe Smith).a2w
- You MUST have two people lab
- Do not upload to Google Groups until you read the last slide.
Alice Tedium
Recipe for dragging the condition "if the distance from the ESS to cookie1
is less than the distance from the ESS to cookie2".
- Drag an if/else tile. When forced to make a choice, select
true from the menu.
- Click on world, then functions in world's details
- Drag the a < b tile into the if, replacing the
useless true.
- When forced to make choices for a and b, pick any
values.
- Now click on evilStepSister, then functions in
evilStepSister's details
- Now drag the distance to tile over the useless value to the
left of the <
- Repeat for the right side
While

- Two parts
- condition: an expression that is true or false
- statement (or statement sequence) that executes as long as
condition is true
- Execution flow
- Check condition
- If true, execute statement(s)
- Check condition again
- If true, execute statement(s)
- etc.
- If condition is false, execute statement after
while
Example: Waiting for the Toaster

- Everyone knows that T.Rex love eating toasters...
- ...they wait as toasters move across the desert...
- ...and when one comes close enough, they eat it...
Lab #2
- Make a lab with a Trex (Animals) and a toaster
- Drag a while tile. Leave the condition as true for
now.
- In the loop, make the toaster move by a short distance.
- Play the world. The toaster should move across the desert, passing the
Trex. If needed, adjust positions/directions.
- Change the while condition. Use one of the trex
proximity functions. Eat the toaster if it is closer than 2 meter.
- Add code for eating. Again, realism isn't important.
- Where do you put that code? In the loop? After the loop?
- Probably the 2 meters didn't work out. Use trial/error to find a better
value.
- Save as trexScribe(First Last)Driver(First Last).a2w
example: trexScribe(David Taylor)Driver(Joe Smith).a2w
- You MUST have two people lab
- Do not upload to Google Groups until you read the last slide.
Reminders

- Get rid of your previous Google Group pages. (I can do it, but in
case you want to have a copy yourself, get one now. Google Groups
doesn't have enough room for too many worlds, and being that I have no
idea who worked on most of these...)
- Please submit two worlds (from two labs in these slides) to the Google Group.
- Name the files as specified. Also put both names in the comment
field.
- Homework 1 will be posted this evening, perhaps late.