CS 40 - Lecture 4

Cay S. Horstmann
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 eCampus
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 Tuesday?
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 ess.a2w
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 meter didn't work out. Use trial/error to find a better
value.
- Save as trex.a2w
Reminders

- Please submit two worlds (ess.a2w and trex.a2w) in eCampus
- Put your buddy's name in the comment field
- Homework #1 is in the eCampus schedule