CS
110 Spring 2012
Homework05:
Mindbending with Recursion
plus Loops
Part I
Due: Monday,
February 27th at class time
Part II Due: Wednesday, February 29th at
class time
Part 1 - Drawing with Recursion
Recursion 1 - Snowflake
Now we’ll use
recursion to draw some neat patterns. We will
again use the TurtleWorld module, just as we did
for polygon.py and for the class exercise where we
did our first Turtle-based recursion drawing.
Create a new
program called Snowflake.py. In the
main function, create a TurtleWorld object and a Turtle object and move
the turtle's position to (x,y) coordinate (-190,
-50). The function below should help
performing an absolute move. Look carefully
at the code below and try and understand exactly
how it does its job.
#-------------------------------------------------------------------------------
# moveto() moves the given turtle, t, to coordinates x, y without drawing a line
# and restoring the heading to its original orientation
#-------------------------------------------------------------------------------
def moveto(t, x, y):
# Start by retrieving current coordinates and heading for turtle t
currx = t.get_x()
curry = t.get_y()
currheading = t.get_heading()
# Figure out how far to get to desired coordinates
diffx = x - currx
diffy = y - curry
pu(t)
lt(t,360-currheading)
fd(t,diffx)
lt(t,90)
fd(t,diffy)
lt(t,-90)
lt(t,currheading)
pd(t)
Now create a
function called snow which returns nothing but has
three parameters: a turtle object and two ints
called iteration and length.
The body of
this function should do one of two things.
First, if
iteration is 0, all you should do is use the
forward function on the turtle that was passed in,
drawing forward the distance given by length.
The more
complicated case is if iteration is greater than
0. In
that case, you will have 7 python steps. Steps 1,
3, 5 and 7 all make (recursive) calls to the
function snow.
Each call will be identical, and will be
passed the turtle, iteration-1, and length/3. That is,
if we aren’t down to iteration 0 yet, our function
will call the snow function four times with a
lower iteration value and shorter lengths.
Between each
of these recursive calls, we’ll change the
direction of our turtle. To do
this, you’ll use the lt() or rt() with an
approriate angle.
The
turns are 60 degrees to the left, 120 degrees to
the right, and 60 degrees to the left,
respectively.
Once you’ve
got this, we can test it out. Try
calling the function snow in your main function
with parameters (turtle, 0, 729). (I
picked 729 just because it is a power of 3, so
when we do integer division by 3 repeatedly, we’ll
still have the right integer). Display
the picture.
You should see a blue line (assuming the
default turtle pen color) drawn across most of the
image. Try
this again, but this time change the iteration
count to 1. This
should add a kink to the middle of the line.
Once you have this
working, change your main function to make 6 calls
to snow, where you start with snow(turtle, 0, 729)
and go up to snow(turtle, 5, 729), incrementing
iteration by 1 each time. Between
each call, reset the turtle position to its
starting position, and change the pen color so you
can distinguish the lines.
For example, a turtle
named bob can change its color to red and reduce
drawing time via the following commands:
bob.delay = 0.01
bob.set_pen_color('red')
Recursion
2 - Sierpinski’s carpet
Sierpinski’s
carpet is defined as follows. Start
with an uncolored square. Split it
into 9 equal-sized squares. Color in
the middle square.
Draw Sierpinski’s carpet in the remaining 8
squares. Here
is what you should get:
Write a program
called Carpet.py that creates this pattern. Your
function will probably need to take in a number of
parameters: a reference to the window object, the
current iteration (at zero, stop the recursion),
the current width, and the current x and y
coordinates of the top left of the area to be
drawn. You will probably use the
graphics library (from graphics import * or from
myro import *) to implement this program.
Recursion
3 - Xander's Bubbles
Xander’s bubbles
are defined as follows. Start
with an uncolored square. Draw a
circle in the middle of the square with radius one
quarter the length of the square. Then
split the square into 4 equal pieces, and draw
Xander’s bubbles in each quadrant. You
should get something like this:
Write a program
called Bubbles.py that creates this pattern. Again,
your function will probably need to take in a
number of parameters: the graphics window object,
the current iteration, the current width, and the
current x and y coordinates.
Part 2 - Practicing with Loops and Drawing
Loops
1 - Rock, Paper, Sissors
Write the game Rock, Paper, Scissors. In a file called RPS.py, write a program that does the following:
- Ask the user if they want the simple or the image based version of the game.
- For the simple version of the game, the playing screen consists of two control "buttons" (really just Rectangles with appropriate labels drawn in a GraphWin) with labels of 'Exit' and 'Again'. There are also three buttons for each of 'Rock', 'Paper', and 'Sissors'. The program should repeatedly:
- Have the user select a choice of "Rock", "Paper", or "Sissors" by getting a mouse click and then to determine whether or not the click was in the "Rock" rectangle, the "Paper" rectangle, or the "Sissors" rectangle.
- Once the user has made a selection, the program makes a random selection by generating a random integer from 1 to 3, inclusive, and use that random selection to determine the computer's rock, paper, or sissors.
- Compare the computers selection with the user's selection and determine a winner of the round.
- Display to the user (in some creative fashion) the Computer's selection, along with a message indicating who won.
- Once the round is complete, the program should wait until one of the control buttons is clicked by the user, continuing on to another round if 'Again' is clicked, and exiting the program if 'Exit' is clicked.
- When waiting for a control button click, clicks outside either of those two rectangles should have no effect.
- When waiting for a rock/paper/sissors click, clicks outside either of those two rectangles should have no effect.
- BE CREATIVE
- For the image based version of the game, you should find jpeg images on the web of a rock, a sheet of paper, and sissors, and use these images as the "buttons" for the user. The compter's selection should also show as an image.
Write and debug the code for the simple version of the game _first_, before spending the time on the image based version of the game. The majority of the grade for this will be based on correct and robust operation of the simple version of the game. ... I will try and make it "break" by clicking all over the place ... just like real users would do!
Loops
2 - Turingscraft
I have added the section from Chapter 7 in Turingscraft on numeric loops, also due for Wednesday, as the second assignment of Part II.
Uploading
your lab
For HW05 Part
I, folder hw05 should contain:
Snowflake.py
Carpet.py
Bubbles.py
For HW05 Part
II, folder hw05 should contain:
RPS.py