lecture 12 or president’s day holiday

16
Lecture 12 or President’s Day Holiday • Chapter 7.2 While Loops – Finish up our race of the penguins

Upload: javier

Post on 23-Feb-2016

18 views

Category:

Documents


0 download

DESCRIPTION

Lecture 12 or President’s Day Holiday. Chapter 7.2 While Loops Finish up our race of the penguins. Hint to getting while loops right. Humans naturally think “until” I’ll keep spending until I run out of money (balance

TRANSCRIPT

Page 1: Lecture  12 or  President’s Day Holiday

Lecture 12 or President’s Day Holiday

• Chapter 7.2 While Loops– Finish up our race of the penguins

Page 2: Lecture  12 or  President’s Day Holiday

Hint to getting while loops right

• Humans naturally think “until”– I’ll keep spending until I run out of money

(balance <= 0)– I’ll keep dancing until I fall asleep

• Computers use “while” loops – the opposite– I’ll keep spending while I still have money

(balance > 0)– I’ll keep dancing while I am not asleep– While I have a dirty dish, I’ll keep washing dishes

Page 3: Lecture  12 or  President’s Day Holiday

Let’s have a race…

• A – Wind up penguin (he just goes)• While loop with “walk and spin” inside it

– Jet-pack penguin2 (controlled by <- event)• Moves forward .5 meters

• Race to a stop sign (within 2 meters)• Whenever someone gets within 2 meters– Stop looping (going)

Page 4: Lecture  12 or  President’s Day Holiday

When should we keep going?e.g. while loop expression true

(P1: wind up, P2: jet pack)

Cases A B C DP1, P2 outside T T T TP1 inside, P2 outside T T T FP2 inside, P1 outside T T F FP1, P2 inside T F F F

Page 5: Lecture  12 or  President’s Day Holiday

Which while loop header (tile) would you use to control the “going”?

Page 6: Lecture  12 or  President’s Day Holiday

This would STOP the game (evaluate to false) when

A. Both penguins must be close to the stop signB. Either penguin is close to the stop signC. Neither penguin is close to the stop signD. I don’t know

What does the other one do?

Page 7: Lecture  12 or  President’s Day Holiday

Cases E1 E2 Evaluates To (keep playing while true)

P1, P2 outside T TP1 inside, P2 outside

F T

P2 inside, P1 outside

T F

P1, P2 inside F F

Truth Table for OR logical operator

Page 8: Lecture  12 or  President’s Day Holiday

Cases E1 E2 Evaluates To(keep playing while true)

P1, P2 outside T TP1 inside, P2 outside

F T

P2 inside, P1 outside

T F

P1, P2 inside F F

Truth Table for AND logical operator

Page 9: Lecture  12 or  President’s Day Holiday

Let’s look at the code I wrote:

Page 10: Lecture  12 or  President’s Day Holiday

The jet pack penguin (P2) can move forward on a <- event when

A. Neither penguin is close to the stop signB. The windup penguin is close, but the jet pack penguin

isn’tC. The jet pack penguin is close, but the windup penguin

isn’tD. Any time (any possible situation of TT, TF, FT, FF)

Page 11: Lecture  12 or  President’s Day Holiday

Both penguins stop moving when someone “close”

To fix this we’d need to create how many of the following?

1. Method for <- event handler2. Method to be called by the windUpAndGo

method3. If statement in <- event handler4. If statement in penguin move method5. If statement in windUpAndGo method

Page 12: Lecture  12 or  President’s Day Holiday
Page 13: Lecture  12 or  President’s Day Holiday

Which if statement would you want and why?Allow to move when…

C) Both A and BD) Neither A nor BE) I don’t know

Page 14: Lecture  12 or  President’s Day Holiday
Page 15: Lecture  12 or  President’s Day Holiday

Let’s Build This…

Page 16: Lecture  12 or  President’s Day Holiday

2 meters from the stop sign? That seems far!

(3-D object representation trickiness)

• Stop Sign center: in middle SIGN• Penguin center: in middle of FEET• I want to control stopping by distance of

penguin center from BASE of stop sign!• Use math – again

SQRT( c*c – b*b)SQRT (penguin.distanceTo(stopSign) * penguin.distanceTo(stopSign) – stopSign.height * stopSign.height)