CS 151: Project #1

### Project 1: Computational Thinking

The purpose of this assignment is to explore how we go about describing an algorithm, which is simply a series of steps in a process. Part of the assignment will be done with a paper and pencil, and part of it will involve Python programming.

This is an exercise you need to do with a partner. For part of each task, one of you will be the artist and the other person the director. Switch roles for each new task (there are six, so each of you should get three chances at each role). When doing programming as a pair, take turns at being in control of the keyboard.

You and your partner need to create only one writeup and one set of code.

While the description is a bit long, the exercise shouldn't take more than 2-3 hours, and you don't need to do it all at once. Each of you should read through each task completely before starting it.

Get some graph paper...

1. Director: Write down a description of a simple logo or shape with at least 3-4 different parts using 20 words or less. Write down your description and then give it to the artist without any further explanation. Make it interesting.

Artist: draw the shape as you interpret the written directions.

The first required part of the writeup is your 20 word description and an evaluation of whether the artist succeeded in creating the shape.

2. Director (swap roles from task 1): Write down a description of an interesting logo or shape using 20 words or less. This time, however, the director may provide an additional piece of paper that gives up to a 10 word description for each unique term in the 20 word description. For example, if the description has the word thingy you could provide the additional description thingy: a 2cm circle with a filled in circle inside it. The director may provide no other information other than what is written.

Artist: draw the shape as you interpret the directions and definitions.

The second required part of the writeup is your 20 word description, the additional definitions, and an evaluation of whether the artist succeeded in creating the shape.

3. Director: your task is to describe a simple logo or shape to the artist using only commands from the set defined below. Label the paper with the commands as Shape A.

Artist: your task is to read and execute the commands written by the director. Do so as strictly as possible.

For this task the allowed command terms are:

1. Pen up
2. Pen down
3. Pen Forward 1 unit
4. Pen Turn left 1 angular unit
5. Pen Turn right 1 angular unit
6. Pen Goto point (x, y)

Before you begin, you need to discuss the meaning of each term. Write these meanings down. The artist may want to use a second pencil to point in the current direction, which is the direction in which the pen should move on a forward command.

Once you have agreed upon the meanings of each term, the director should describe a shape to the artist by writing a sequence of no more than 15 commands, where each command is one of the six terms above.

For example, if we set the angular unit to 90 degrees, the following would be a set of commands that makes a 1:2 rectangle and leaves the pen exactly where it started.

Label: Shape Z1
Pen down
Pen forward
Pen left
Pen forward
Pen forward
Pen left
Pen forward
Pen left
Pen forward
Pen forward
Pen left

Python task: Once the artist has finished rendering the shape, work together to write a Python program that draws the same shape using turtle graphics. Put your code into a file named shapeA.py. To run the commands, type

python shapeA.py

on the command line. Save a picture of your shape for your writeup.

4. Switch roles and repeat the prior task, but use a different shape. Write the second list of commands on a different piece of paper and label it Shape B.

Python task: Once the artist has finished rendering the shape, work together to write a Python program that draws the same shape using turtle graphics. Put your code in a file called shapeB.py. Save a picture of your shape for your writeup.

5. For the next task, the director needs to tell the artist to make two copies of Shape A in different locations on the page. If you could use only the commands from the first task, how many commands would it take?

Instead of just copying the list of commands twice, however, we're going to make use of the Shape A and Shape B labels by adding a new term to our vocabulary: Execute list. If the director writes the command

Execute list Shape A

then the artist should pick up the paper labeled Shape A, follow all of the commands on it, then return to the command following the Execute list command. For example, the following series of commands would draw two copies of Shape Z1 one unit apart (try it out). We'll call this one Shape Q.

Label: Shape Q
Pen down
Execute list Shape Z1
Pen up
Pen forward
Pen forward
Pen down
Execute list Shape Z1

Director: write down a list of commands that makes at least one drawing of Shape A and one drawing of Shape B. Label this page as Shape C.

Artist: on a new piece of paper, follow the commands and draw the shapes.

Python task: Make a new Python file called shapes.py. In this file, create two functions shapeA and shapeB. A function is simply a collection of Python commands with a label, just like our lists above. For example, I could make a function that draws a triangle by use the following code.

```def triangle():
forward( 100 )
left( 120 )
forward( 100 )
left( 120 )
forward( 100 )
left( 120 )
```

Note how the commands that make up the function are all tabbed in the same amount relative to the function definition. That is how you tell Python those commands are part of the function triangle.

Your shapeA and shapeB functions should draw the shapeA and shapeB shapes you defined in the prior step. You can copy and paste that code into the functions, tabbing appropriately.

Duplicate your shape C from above in Python, calling the shapeA and shapeB functions. Calling a function is the same as the execute list command from above. You can call a function in Python by using the function name followed by parentheses. For example:

```def shapeC():
shapeA()
forward( 100 )
shapeB()

shapeC()
raw_input('Enter to continue')
```

At the end of your shapes.py file, put a call to the shapeC function, as in the example above, so that it will call the function when you run the shapes.py program. The last line means the program will not terminate until you hit return/enter. Note that the call to shapeC is not tabbed in and will therefore be executed by Python when you run your file as below.

python shapes.py

6. The final task is to make your commands more flexible by enabling the director to specify how many units forward to traverse. For example, the command:

Pen forward 3

would tell the artist to draw forward 3 units. With this modification, the commands from shape Z1 could be written as follows.

Label: Shape Z2
Pen down
Pen forward 1
Pen left
Pen forward 2
Pen left
Pen forward 1
Pen left
Pen forward 2
Pen left

Adding one more feature to the commands gives us even more flexibility. Let's allow the director to use a capital letter, like X, to represent a number within a labeled shape. For example, the code below would draw a single line of whatever distance is assigned to X. If we said Execute List Shape L(X = 10), then X would get the value 10, and the artist would draw a line 10 units long.

Label: Shape L( X )
Pen forward X

With this extension, we can easily change the size of the rectangle defined by the shape Z2 command list by using two variables, or parameters, in the shape definition. In the example command list below, the size of the rectangle is determined by the values given to the symbols A and B.

Label: Shape Z3( A, B )
Pen down
Pen forward A
Pen left
Pen forward B
Pen left
Pen forward A
Pen left
Pen forward B
Pen left

Now we can modify the size of Shape Z3 each time we call it. For example:

Label: Shape Q
Pen down
Execute list Shape Z3( A=1, B=2 )
Pen up
Pen forward 2
Pen down
Execute list Shape Z3( A=2, B=4 )

When the artist draws Shape Q, then the first time Shape Z3 will have sides of length 1 and 2, while the second time, Shape Z3 will have sides of size 2 and 4.

Director: create a Shape D that is like Shape A or Shape B but uses one or more variables in the forward commands, just like Shape Z3 above. Then write a Shape E command list that makes two versions of Shape D of different size.

Artist: draw the shape, but be careful interpreting the commands so that each forward command goes the proper distance.

Python task: Now duplicate your work in Python, creating functions and shapes using variables instead of raw numbers so that you can use the same set of commands to draw different size shapes.

You can create parameters for functions that allow you to pass information into a function. In the function definition, you put a list of variable names inside the parentheses. You can then use the parameter variables within the function.

When you call a function with parameters, you put the value of the parameters in the parentheses. For example, the following code defines a triangle and then calls it with two different values.

```def triangle( distance ):
forward( distance )
left( 120 )
forward( distance )
left( 120 )
forward( distance )
left( 120 )

triangle( 50 )
triangle( 100 )
```

Working together, in your shapes.py file, create a Python function shapeD that implements your Shape D with one or more function parameters. Then write a shapeE function that calls the shapeD function multiple times with different parameters. At the end of your file, make sure to put a call to shapeE.

### Extensions

Each assignment will have a set of suggested extensions. The required tasks constitute about 85% of the assignment, and if you do only the required tasks and do them well you will earn a B+. To earn a higher grade, you need to undertake at least one extension. The difficulty and quality of the extension or extensions will determine your final grade for the assignment.

The following are a few suggestions on things you can do as extensions to this assignment. You are free to choose other extensions.

• Create some additional functions that draw different shapes. Create a function that calls all of them to make an interesting graphic.
• Figure out how to have a set of instructions repeat a certain number of times.
• Write a function that will draw an N-gon. It should take in the distance of a side and the number of sides.
• Build a hierarchy of functions. For example, you already have a function A that draws one shape. Make a new function B that calls function A multiple times, but in different places, so that you get multiple shapes. Then make another function C that calls B multiple times. Go up one more level of complexity.

### Writeup

Each pair should create one writeup on the wiki. Make sure you put both of your names at the top of the writeup. In your writeup, put a copy of the command lists you each created as a director. If you're feeling ambitious, take a photo of your work as an artist and put up the pictures (cell phones work just fine).

You can also take a picture of your Python images on a mac using cmd-shift-4 to grab part of the screen. It will save the picture as Picture 1.png on your Desktop.

One of you should put the Python files you wrote (shapeA.py, shapeB.py, and shapes.py) on the Courses/CS151 server in your handin directory. In your writeup, indicate which directory you used.

Please put the label cs151f12project1 on your writeup page (down below the editing window on the edit tab).