CS 151: Project #4

Ticker Tape Turtles

The purpose of this project is to give you more practice in designing and writing an algorithm. The focus of the assignment is to integrate user input with the string interpreter from last week.

Tasks

The first part of this project is to write a program that lets the user repeatedly send strings to the turtle to execute in real time. The second part is to write a program that generates a string and then passes it on to the processString() function for interpretation.

  1. Create a new directory for this week's project. Copy your turtleUtils.py, linear.py, and username_shapes.py file to the new directory. Rename linear.py to follow.py. Open the file follow.py.
  2. Modify your processString function so that it does not move the turtle at all anywhere in the function. The only thing this function should do now is iterate over the characters in the string and call drawShape with each character in turn.
  3. Modify the drawShape function so that the characters listed below have the specified meaning. You should also leave three characters to represent your shapes. You may need to change the characters for your shapes so they don't conflict with the list below. Once you are done with that, try passing a string like 'fffllfffrrff' into the processingString function in your main function and see what happens.

    'u' is up()
    'd' is down()
    'f' is forward(10)
    'b' is backward(10)
    'r' is right(30)
    'l' is left(30)

  4. Modify your main function so that after calling turtleSetup() it goes into a while loop forever (while True:). Within the while loop print out a quick reminder of the characters and their meanings and ask for a string. Grab the string using raw_input() and store the resulting string in a variable. If the length of the string is zero, break out of the loop. Otherwise, send the string to process string. Finally, remove the turtleWait() call at the end of the main function. You don't need it for this program.
  5. Give the user the ability to save their work to a file. The basic algorithm here is:
    • Open a file in which to save the user's commands prior to the interactive loop.
    • Each time through the loop, save their command string to the file.
    • When the user decides to quit, close the file.
    You can either ask the user for a filename in which to save their work when the program starts, or you can use a default filename, in which case the file will be overwritten each time the user runs the program.
  6. Create an image by typing strings into the terminal interface using your follow.py program. Hand this in as sceneA. Include in your writeup the string that created sceneA.
  7. Copy follow.py to linearC.py. Modify the main function in linearC.py so it does the following:
    1. Asks the user for a filename
    2. Opens the file
    3. Reads in the first line of the file and stores the string
    4. Sends the string to the processString function
    5. Calls turtleWait()
  8. Put a string into a file and run it with linearC.py. Call this image sceneB. Include in your writeup the string that created sceneB.
  9. The remaining steps are optional and qualify as an extension for the project (but it's really cool).

  10. Create a new python file in your editor called koch.py. Import the string module.

    from string import *

  11. In koch.py, you will need only a main() function. The function should do the following.
    1. Assign a variable with the name base the value 'frrrrfrrrrfrrrr'
    2. Assign a variable with the name symbol the value 'f'
    3. Assign a variable with the name rule the value 'fllfrrrrfllf'
    4. Assign a variable with the name complexity the value 3
    5. Iterate over range(complexity)
    6. Within each iteration, call the replace() function defined by the string package with the arguments base, symbol, and rule, in that order. Assign the output of the function back to the base variable. The replace function finds every occurrance of the string symbol in the string base and replaces it with the string in rule.
    7. Write the resulting string, which ends up in the variable base to a file.
  12. Use linearC.py to run the string generated by koch.py. Call it sceneC. What happens if you change the number of iterations and make it smaller? If you make it bigger, first change your forward command in drawShapes() to be forward(5) intead of forward(10).

Extensions

Below are some suggested extensions. Focus your extensions on the use of conditionals and iteration to make the results more interesting.

  1. Give the user of the interactive program the ability to read in a file with a command string so they can start where they left off last time.
  2. Use string manipulation to create other patterns.
  3. Create an interesting scene by building a long string in a file.
  4. Let your symbols like 'f', 'r', and 'l' take arguments (numbers) after them in the string. So a string like 'f10r90f10r90f10r90f10r90' would draw a square 10 pixels on a side.
  5. Generate random strings that use appropriate characters.

Writeup

Create a web page for your writeup. Your writeup should include the following.

Handing in

When your writeup is ready for viewing and linked to your index page, send me an email.

To hand in your python code, place it on the Academics fileserver in the private folder. Do not post it on the web. You may include snippets from your code in your writeup to demonstrate how certain aspects of the function work, but please do not include the whole thing.

To place the code on the fileserver, go to the server fileserver1 and select the Academics volume. Within that there ought to be a CS151 folder, and within that there should be a folder with your username, and within that ought to be a folder labeled private. You can read and write files to the private directory and I can read them, but no one else can access them. Put your python files in that directory to hand them in.