CompSci 4Chap 6 – Problem Solving Mar 29, 2005Prof. Susan RodgerAnnouncements• Read Chapter 7.1 for next time• Assignment 7 is extended, due Tuesday April 5!What we will do today• Problem Solving – Chapter 6• Practice writing methods and functions• Talk about assignment 7Problem To Solve• Given three animals standing in a row• Resize each of them randomly by 1(no change) to 4 times its size• Whichever is taller, spin around once to the right, once to the left• Sort the animals by their height• Display message “In order”whenever in order by increasing heightObjects in the world• Animals – chicken, rabbit, and penquin• Balls (will be invisible always)– tennisball1 – represent position 1– tennisball2 – represent position 2– tennisball3 – represent position 3• 3D text – “In order! – Invisible until animals in order by heightRandom Integers• Bug in Alice with “integers only” and how they are used…..• Return ?world. my first methodmakeTallerAnimalSpinAround• Need to know which of the three animals is tallermakeTallerAnimalSpinAround• Use isTaller3Another way – whichIsTaller3?How do we use whichIsTaller3?• How do we turn the taller of the three around once to the left?How do we check if items are sorted by increasing height?• Check if object in position 1 is shorter than object in position 2 which is shorter than object in position 3• How do we know which animal is in a position?isSortedByIncreasingHeightwhichObjectIsClosestToCheckIfSortedByHeight• Use isSortedByIncreasingHeight• Use whichObjectIsClosestToSortAnimalsByHeight• How? Lots of answersOne way – moveAnimalsToSortedPositionIfAnimal1IsTallest• 3 parameters – anim1, anim2, anim3• If anim1 is the tallest, then compare other 2 and move each to corresponding tennisball• If anim1 is not the tallest, don’t do anythingmoveAnimalsToSortedPositionSortAnimalsByHeightProblem is Solved!•DemoAssignment 7 - Gatekeeper•Demo• Useful things to do– When S is typed – show answer– Display tennis balls til program is working– Randomly set gatekeeper – game different each time playedAssignment 7 – Gatekeeper Useful functions• World.SwapPlayers (obj player)– Swap player with player furthest away• World.whichObjectNearBall (obj ball)– Return object closest to ball• World.NearWhichBall (obj player)– Returns ball that player is closest to• World.whichObjectIsGateKeeper– Returns object that is gatekeeperAssignment 7 – Binary Code Game•Demo• Useful info– When S is typed show answer– Randomly set answer– How do you tell if lever is up or down?• Use invisible sphere – two colors • Don’t compare sphere’s position with lever – bug in Alice so this doesn’t workAssignment 7 – BCG Useful Functions• changeSphereColor (obj sphere)– changes color of sphere to other color• switchFlip (obj switch)– Change lever position (and sphere color)– Check if valid code and if so, indicate• isValidCode– Return true if code is valid• isSwitchUp (obj switch)– Returns true is lever is up• whichSphereIsClosest (obj switch)– Returns sphere that is closes to
View Full Document