CS105L Introduction to Game Programming Using Alice (Lab)
Fall 2009
MID-TERM NEXT WEEK!
Here's last year's lab mid-term. If you
can produce this in 2 hours and meet spec, you should be able to
ace the mid-term. If you're having trouble with it, make sure to
come by during my office hours or make an appointment with me via
email: dogwynn@cis.uab.edu.
Understand: this is last year's mid-term. This year's
will be similar in scope, but not in content.
Chapter 4: Decision Structures
Boolean Values, If/Else decision structure, Relational comparisons
Part 1 - Basic If/Else with a Boolean variable
-
Do tutorial 4-1 page 156 in book.
-
Add a line to have the penguin say "See ya" before he falls down
the hole.
Part 2 - Relational Comparison - Kick the closest thing
-
Using the monkey kicks the ball program. Place a soccer ball in
the scene and have the monkey kick the closest ball.
-
Open the file from Lab 3 (part 3)
in which the monkey kicks the ball using methods
-
Save this file under the new name Lab6-2.
-
From the web gallery > sports
add a soccer ball to the scene.
-
Place the soccer ball 2 meters to the left of the monkey (some
distance that is father away from the monkey than the toy ball.)
-
Place an If/Else statement
above the first line of code. Use a default value
of true.
-
In lab 4, you learned to
use world functions. Use the
world function a < b as the
If statement’s condition.
That is, drag the a < b
over the true value. Select a
default value of .25 < .25.
-
Use the monkey functions monkey
distance to to return the distance to the toy ball and
the distance to the soccer ball.
Drag monkey distance to
toyBall to the first 0.25 in
the If condition space. Drag
monkey distance to soccerBall
to the second 0.25 in the If
condition space. You should now
have If monkey distance to toyBall
< monkey distance to soccerBall as the first line of
the program.
-
Move the method calls into
the If block (the
first Do Nothing space).
-
Press play.
Part 3 - String comparison, Which ball to kick
-
Create a new string
variable, kickBall, set the value
to default string.
-
As the first line of code, set the value
of kickBall to ask user for a
string. Drag the kickBall
variable to the top of the code, set the value
to default string.
-
Replace the default string
with the world ask user for a
string function, set the value
to other > Which Ball should I
kick?
-
Replace the a < b condition (from
Part 2) with the world function a ==
b.
-
Create two new string variables tBall
= toy ball and sBall = soccer
ball
-
In the condition replace a
with kickBall
and b
with tBall
-
Play the scene. Answer the question
with toy ball.
Part 4 - Nested If/Else
-
Create a new world method for the monkey to approach and kick
the soccer ball and the ball to roll away. Name it
kickSoccerball
-
Drag a new If/Else construct
to the existing Else block
(second Do Nothing.) Default
to true. Add a comment above
this block: "Kick Soccer Ball"
-
Replace the true condition
with kickball == sBall.
Similar to Part 3 above.
-
Drag the kickSoccerball method
into the If block.
-
In the Else block have the
monkey say Which Ball?
-
Play the scene 3 times entering toy
ball, soccer ball, and
ball for the input strings.
Part 5 - Logical Operators, Lazy monkey
The and operator allows you to
check two conditions at once and verify that both are true. The
monkey will only kick the closest ball. Ask the monkey to kick a
ball. If it is the closest ball, kick the ball. If it is not the
closest have the monkey say, it's too far away.
-
In Part 2 the monkey kicked the closest ball. In part 4 the
monkey asked which ball to kick. We will combine these two
operations.
-
As the first line of code, have the monkey
say tell me which ball to
kick.
-
Drag the world function both a and
b to the first If
condition block (kick toy ball); over
the kickball == tBall
statement.
-
You should have both kickball ==
tBall and true as
the If condition. Replace the
true value with the result of
a comparison of which ball is closest from part 2.
Replace true
with a < b.
Replace a
with monkey distance to
toyBall and b with
monkey distance to soccerBall.
-
In the Else block have the
monkey say "it's to far away".
-
Play the scene twice answering toy
ball and soccer ball
(the toy ball should be the closest.)
-
Change the scene such that the soccer ball is the closest.
Replay the scene answering toy
ball and soccer ball.
Part 6 - Fix the last program
Assignment
In the last program, what happens if the user specifies a tennis
ball?
Fix the code so that the program checks for a valid ball name and
then executes the existing code. If the ball entered is neither
the toy ball nor soccer ball, have the monkey say I don't see that
ball.
Hint: Add an outside If/Else that uses Boolean logic.