COMP 141 Fall 2019

Program 7: The Strange Garden

Assigned: Wednesday, October 30
Due: Sunday, November 10, 2019 by 11:55pm

Computer science, while fun, is not something you can do 24/7. Keeping up with hobbies is important, so you decide to take up gardening as a new hobby. You know nothing about growing plants, however, so you decide to visit a new flower shop in your neighborhood.

The shop owner seems a bit strange, however, and sells you some plants you've never seen before, known only as "Plant A" and "Plant B". The shop owner tells you that these plants grow in specific ways according to a strange set of rules. In this project, you will develop a computer program that shows how the plants grow.

Rules for the plants

The shopkeeper tells you some basic rules for the two kinds of Plants, Plant A and Plant B.

Plant A

Every plant in a row of Plant A will always be either "alive" or "dormant". This plant has a very simple growing pattern that is based on its neighboring plants:

Let's examine a few generations of Plant A. Suppose we have a row of eight plants. We represent an alive plant with an asterisk (*), and a dormant plant with a period (.). To make it easier to talk about the plants, let's number them from left to right, starting with 0. Suppose the plants begin like this:

01234567  <-- Plant numbers
.***..*.  <-- Plant status

To figure out what happens at the next generation, let's examine the plants from left to right.

So the first two generation of plants in the row looks like this, with the initial generation on top and the second generation below.

01234567
.***..*.
.*.***..

If we apply the same rules again, we end up with the third generation of plants, which when added below the first two generations, looks like this:

01234567
.***..*.
.*.***..
...*.**.

Plant B

The shopkeeper has warned you that Plant B is trickier than Plant A.

Let's examine a few generations of Plant B, with a neighborhood of 1. Suppose we have a row of eight plants. We represent a big plant with a caret (^), a small plant with an asterisk (*), and a dormant plant with a period (.). Suppose the plants begin like this:

01234567
.^*.^.*.

Here's how we compute the next generation:

01234567
.^*.^.*.
.*..^.^.

Adding in a third generation, we get this:

01234567
.^*.^.*.
.*..^.^.
.^**^.^.

How your program will work

You will write a program that asks the user whether they are growing Plant A or B, along with the initial plant configuration, and how many generations the user wants to see. Then you will simulate the growth of the row of plants for the specified number of generations, showing each generation, along with various statistics.

How to get started

  1. Begin by downloading the starter code here. Read it over and familiarize yourself with it. The program will come together by writing a series of functions.

    We will first write the functions to get Plant A to work, then write the code for Plant B.

  2. Write the functions countSmall, countBig, and countDormant. They should be self-descriptive from their names and the comments in the code, and they all work in a similar way.

    Test these functions from the Python shell by calling them manually with different garden configurations. All three of these functions should work fine with either plant A or plant B. Here are some examples:

>>> countDormant("..*^.^^...***.")
7
>>> countSmall("..*^.^^...***.")
4
>>> countBig("..*^.^^...***.")
3
  1. Write the function nextGenerationA. This function takes one argument, which is the current generation of a row of plants (which we call a garden). See the comments in the function code to help you.

    When you’re finished, test the code from the Python shell. Here are some examples (the same three generations used above for Plant A). You should also make up your own test cases (make up some garden strings):

>>> nextGenerationA(".***..*.") # generation 1 -> 2
'.*.***..'
>>> nextGenerationA(".*.***..") # generation 2 -> 3
'...*.**.'
>>> nextGenerationA(".*.*****.*.") # another example
'...*...*...'
  1. Now jump down to the main function and write some code to get Plant A to run. Look at the sample interactions below, and read the comments in the code to guide you.
  2. Once Plant A is running, test the output thoroughly. Make sure your output matches mine (again, see the sample interactions below).
  3. Now go back and write nextGenerationB. Follow the directions in the code.

    Test this function thoroughly with various gardens and neighborhood sizes.

>>> nextGenerationB(".^*.^.*.", 1)  
'.*..^.^.'
>>> nextGenerationB(".*..^.^.", 1)
'.^**^.^.'
>>> nextGenerationB("......*......", 1)
'.....*^*.....'
>>> nextGenerationB(".....*^*.....", 1)
'....*.^.*....'
>>> nextGenerationB("......*......", 2)
'....**^**....'
>>> nextGenerationB("....**^**....", 2)
'..*.^.^.^.*..'
>>> nextGenerationB("......*......", 3)
'...***^***...'
>>> nextGenerationB("...***^***...", 3)
'....^.^.^....'
  1. Write the remaining part of main to get Plant B to work.

Sample interactions

Plant A, Test 1

Are you growing plant A or B? A
What is the starting garden? .***..*.
How many dormant plants are on either side? 0
How many generations do you want to see? (not including the first) 5
.***..*. 4 4
.*.***.. 4 4
...*.**. 5 3
..*..**. 5 3
.*.****. 3 5
...*..*. 6 2

Plant A, Test 2

Are you growing plant A or B? A
What is the starting garden? *
How many dormant plants are on either side? 32
How many generations do you want to see? (not including the first) 32
................................*................................ 64 1
...............................*.*............................... 63 2
..............................*...*.............................. 63 2
.............................*.*.*.*............................. 61 4
............................*.......*............................ 63 2
...........................*.*.....*.*........................... 61 4
..........................*...*...*...*.......................... 61 4
.........................*.*.*.*.*.*.*.*......................... 57 8
........................*...............*........................ 63 2
.......................*.*.............*.*....................... 61 4
......................*...*...........*...*...................... 61 4
.....................*.*.*.*.........*.*.*.*..................... 57 8
....................*.......*.......*.......*.................... 61 4
...................*.*.....*.*.....*.*.....*.*................... 57 8
..................*...*...*...*...*...*...*...*.................. 57 8
.................*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*................. 49 16
................*...............................*................ 63 2
...............*.*.............................*.*............... 61 4
..............*...*...........................*...*.............. 61 4
.............*.*.*.*.........................*.*.*.*............. 57 8
............*.......*.......................*.......*............ 61 4
...........*.*.....*.*.....................*.*.....*.*........... 57 8
..........*...*...*...*...................*...*...*...*.......... 57 8
.........*.*.*.*.*.*.*.*.................*.*.*.*.*.*.*.*......... 49 16
........*...............*...............*...............*........ 61 4
.......*.*.............*.*.............*.*.............*.*....... 57 8
......*...*...........*...*...........*...*...........*...*...... 57 8
.....*.*.*.*.........*.*.*.*.........*.*.*.*.........*.*.*.*..... 49 16
....*.......*.......*.......*.......*.......*.......*.......*.... 57 8
...*.*.....*.*.....*.*.....*.*.....*.*.....*.*.....*.*.....*.*... 49 16
..*...*...*...*...*...*...*...*...*...*...*...*...*...*...*...*.. 49 16
.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*. 33 32
................................................................. 65 0

Plant B, Test 1

Are you growing plant A or B? B
What is the starting garden? .^*.^.*.
How many dormant plants are on either side? 0
How many generations do you want to see? (not including the first) 5
What is the neighborhood size? 1
.^*.^.*. 4 2 2
.*..^.^. 5 1 2
.^**^.^. 3 2 3
.*^^*.^. 3 2 3
..^^..^. 5 0 3
.*****^. 2 5 1

Plant B, Test 2

Are you growing plant A or B? B
What is the starting garden? *
How many dormant plants are on either side? 20
How many generations do you want to see? (not including the first) 20
What is the neighborhood size? 1
....................*.................... 40 1 0
...................*^*................... 38 2 1
..................*.^.*.................. 38 2 1
.................*^.^.^*................. 36 2 3
................*.*.^.*.*................ 36 4 1
...............*^.^.^.^.^*............... 34 2 5
..............*.*.^.^.^.*.*.............. 34 4 3
.............*^.^.^.^.^.^.^*............. 32 2 7
............*.*.^.^.^.^.^.*.*............ 32 4 5
...........*^.^.^.^.^.^.^.^.^*........... 30 2 9
..........*.*.^.^.^.^.^.^.^.*.*.......... 30 4 7
.........*^.^.^.^.^.^.^.^.^.^.^*......... 28 2 11
........*.*.^.^.^.^.^.^.^.^.^.*.*........ 28 4 9
.......*^.^.^.^.^.^.^.^.^.^.^.^.^*....... 26 2 13
......*.*.^.^.^.^.^.^.^.^.^.^.^.*.*...... 26 4 11
.....*^.^.^.^.^.^.^.^.^.^.^.^.^.^.^*..... 24 2 15
....*.*.^.^.^.^.^.^.^.^.^.^.^.^.^.*.*.... 24 4 13
...*^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^*... 22 2 17
..*.*.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.*.*.. 22 4 15
.*^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^*. 20 2 19
..*.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.*.. 22 2 17

Plant B, Test 3

Are you growing plant A or B? B
What is the starting garden? *
How many dormant plants are on either side? 20
How many generations do you want to see? (not including the first) 20
What is the neighborhood size? 2
....................*.................... 40 1 0
..................**^**.................. 36 4 1
................*.^.^.^.*................ 36 2 3
..............**..^.^.^..**.............. 34 4 3
............*...*.*.^.*.*...*............ 34 6 1
..........**^*.*..^.^.^..*.*^**.......... 28 8 5
........*.^.*.*...*.^.*...*.*.^.*........ 30 8 3
......**..^.^..*.*..^..*.*..^.^..**...... 28 8 5
....*...*.*.*.......^.......*.*.*...*.... 32 8 1
..**^*.*..^..**...**^**...**..^..*.*^**.. 20 16 5
..^.*.*...^.*.....^.^.^.....*.^...*.*.^.. 28 6 7
..*.^..*.**..**.***.^.***.**..**.*..^.*.. 20 18 3
....*...*^.**.^..^.*^*.^..^.**.^*...*.... 24 10 7
..**^*...^.^^**..**.^.**..**^^.^...*^**.. 18 14 9
..^.*^..*****.^**.^*^*^.**^.*****..^*.^.. 14 18 9
..**^***^.^...*^..*.^.*..^*...^.^***^**.. 18 14 9
..^.^^^.****...**...^...**...****.^^^.^.. 20 12 9
..***^*....^.......*^*.......^....*^***.. 26 10 5
..^.^*^.***^**...*.^^^.*...**^***.^*^.^.. 16 14 11
..***^*...^^.^..*.**^**.*..^.^^...*^***.. 18 14 9
..^.^*^...*^**...*..^..*...**^*...^*^.^.. 22 10 9

Plant B, Test 4

Are you growing plant A or B? B
What is the starting garden? *
How many dormant plants are on either side? 20
How many generations do you want to see? (not including the first) 20
What is the neighborhood size? 3
....................*.................... 40 1 0
.................***^***................. 34 6 1
..............*.*.^.^.^.*.*.............. 34 4 3
...........**..*^.^.^.^.^*..**........... 30 6 5
........*...^...^.*.^.*.^...^...*........ 32 4 5
.....***^...^..***^.^.^***..^...^***..... 22 12 7
....*...*.*.**.^.^^*^*^^.^.**.*.*...*.... 22 12 7
...*^..*.*.....^*^*.^.*^*^.....*.*..^*... 24 10 7
....^*.^...*..**.^^*^*^^.**..*...^.*^.... 22 10 9
....^^*^*...**..***.^.***..**...*^*^^.... 20 14 7
...**^^^...*.^.*...*^*...*.^.*...^^^**... 22 10 9
....^^^*.**.*^*.**.^^^.**.*^*.**.*^^^.... 16 14 11
...***^.*^^*^^^*^^*^^^*^^*^^^*^^*.^***... 8 14 19
.....^*..**^^^^^^^^^^^^^^^^^^^**..*^..... 14 6 21
.....*^.*^^*^^^^^^^^^^^^^^^^^*^^*.^*..... 12 6 23
.....^**.**^^^^^^^^^^^^^^^^^^^**.**^..... 12 8 21
....*^.^...*^^^^^^^^^^^^^^^^^*...^.^*.... 16 4 21
....^^*^**..^*^^^^^^^^^^^^^*^..**^*^^.... 12 8 21
...**^.*^^**^^*^^^^^^^^^^^*^^**^^*.^**... 8 12 21
...^.^..**^^^^^^^^^^^^^^^^^^^^^**..^.^... 12 4 25
...*.^..^^*^^^^^^^^^^^^^^^^^^^*^^..^.*... 12 4 25

How do I know when my program works?

When your program output matches mine on all 6 tests, you can be reasonably sure everything works. Note that I will be testing your program with other inputs as well, so your code should work for any starting garden configuration and any neighborhood size for Plant B.

What to turn in

Through Moodle, turn in your code as a file called
yourLastName_yourFirstName_prg7.py.

Challenge Problems

Notes

This project is based on something called a cellular automaton. In particular, Plant A is a particularly well-studied cellular automaton commonly known as Rule 90.

Grading

Your program will be graded on correctness, as well as on coding style, which refers to choices you make when writing your code, such as good use of variable names, appropriate indentation, and comments (this is not an exhaustive list). See the syllabus for further grading details.

You will receive one bonus point for every complete day your program is turned in early, up to a maximum of five points. For instance, if your program is due on September 20 at 11:59pm, if you turn in your code on Moodle any time on September 19 from 12:00am through 11:59pm, you will receive one extra point on your project. Programs submitted on September 18 from 12:00am through 11:59pm will receive two points. This pattern continues for up to five points.