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.

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: describe a simple logo or shape to the artist using 20 words or less. Write down your commands and then give it to the artist without any further explanation.

Artist: draw the shape as you interpret the directions

2. Director (swap roles from task 1): describe a different simple logo or shape to the artist 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 words 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.

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 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. The set of commands should leave the pen in the same position and orientation where it began.

For example, if we set the angular unit to 90 degrees, the following would be a set of commands that makes a 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.

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.

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: Now 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 in the function triangle.

Your shapeA and shapeB functions should just 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()
```

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. 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 assign a number to any single capital letter and then use that letter in place of a number later in the command list. For example, the following two lines would tell the artist to move the pen forward 5 units.

X = 5
Pen forward X

With this extension, we could easily change the size of the rectangle defined by the shape Z2 command list by just changing two numbers. 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 = 2
B = 5
Pen down
Pen forward A
Pen left
Pen forward B
Pen left
Pen forward A
Pen left
Pen forward B
Pen left

What if we assigned the values of A and B before executing Shape Z3? For example:

 Label: Shape Z4 Pen down Pen forward A Pen left Pen forward B Pen left Pen forward A Pen left Pen forward B Pen left Label: Shape Q Pen down A = 1 B = 2 Execute list Shape Z4 Pen up Pen forward 2 Pen down A = 2 B = 4 Execute list Shape Z4

When the artist draws Shape Q, then the first time Shape Z4 will have sides of length 1 and 2, while the second time, Shape Z4 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 Z4 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.

In python, you can assign a value to a variable using an assignment statement. A variable is simply a string of characters that starts with a letter. An assignment statement is a variable on the left side of an equals sign and an expression on the right side. The variable on the left always receives the value of the expression on the right. For example, to create a variable A and assign the value 100 to it, you would use the python statement:

A = 100

Note that you will never hear us say 'A equals 100' to describe the statement above. Instead, we will always say either 'A gets 100' or 'A is assigned the value 100'. An assignment always involves moving information, it is not a test of equality.

You can also 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 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. Instead of calling the shapeC function at the end of the shapes.py file, have it call 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. One extension, done well, is typical.

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.

### 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 Academics server in your handin directory. In your writeup, indicate which directory you used.

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