CompSci 4 Chap 6 Sec 2 Part 1 Mar 10, 2005AnnouncementsWhat we will do todayThinking about More Advanced WorldsExamples of DecisionsLogical ExpressionsIf/ElseExample: Boolean FunctionsStoryboardStoryboard (cont)isTooCloseByDistanceUsing a Relational OperatorImplementing the FunctionVertical Distance FunctionistooCloseByVerticalSlide 16Implementation and Calling FunctioncheckForHeightCollisionAvoid CollisionDemo and TestingClasswork todayCompSci 4Chap 6 Sec 2 Part 1 Mar 10, 2005Prof. Susan RodgerNote: thanks to Wanda Dann and Steve Cooper for slide ideasAnnouncements•Finish Chapter 6, Sec 2 for next time•Next assignment handed out after spring break – enjoy your break!•Pixar Talk – Monday, March 21, 7:30pm–Computers Don’t Make Movies–LSRC B101What we will do today•Lecture on Chap 6, Sec 2 through page 170–Execution control with if/else statements and Boolean functions•ClassworkThinking about More Advanced Worlds•How do you build animations like simulations and video games?•Need to write code that involves dec is ionsExamples of Decisions•A car-race simulation – the driver steers the car around curves and past mile markers–If the car stays on the road the score increases–If the car goes off the road into the stands, the car crashes–If the driver gets the car over the finish line, the time is posted and the driver wins!Logical Expressions•Decision is made based on current conditions.•Condition is checked in a logical expression that evaluates to true or false (Boolean) value.–car on road true–car over finish line falseIf/Else•In Alice, a logical expression is used as the condition in an If/Else control structure•Decisions (using If/Else) are used in–Functions–MethodsExample: Boolean Functions•Suppose we build a simulation system used to train flight controllers•One of the tasks of a flight controller is to be alert for possible collisions in flight spaceStoryboard• Two aircraft – biplane and helicopter•As the biplane moves towards the helicopter we want to make sure they do not collide•If they are too close, they need to adjust their altitude (height)Storyboard (cont)•Two factors in determining whether two aircraft are in danger of collision–Total distance between them–Vertical distance between them•Both functions return true if aircraft are too close, otherwise falseisTooCloseByDistanceisTooCloseByDistance:Parameters: aircraft1, aircraft2, minDistanceIf distance between aircraft1 and aircraft2 is less than minDistance return trueElse return falseUsing a Relational Operator•Use the < relational operator from the World’s built-in functions to check the distance against the minimumis equal tois not equal tois greater thanis greater than or equal tois less thanis less than or equal toImplementing the FunctionVertical Distance Function•To find the difference in altitude, use the built-in distance above function–Don’t know which aircraft is above the other–To avoid a possible negative value, use absolute value of the distanceistooCloseByVerticalStoryboardforwardAndCheckCollisionParameters: aircraft1, aircraft2, distanceaircraft1 move forward distanceIf aircraft1 and aircraft2 are closer than twice distance avoid collision if they are too close heightwise move aircraft1 forward twice the distanceImplementation and Calling FunctioncheckForHeightCollisionAvoid CollisionDemo and Testing•Create several events–Move plane backword–Move plane forward slowly and check for collisions–Try with planes at different heightsClasswork
View Full Document