Java代写:CSC110-Cellular-Automaton

Program Specification

In this program you will simulate a simple, one-dimensional cellular automaton. You will implement it using a list, where each element of the list represents 1 cell. Each cell has a state (or value) of dead or alive. You can represent these two states however you’d like (0/1, True/False or any other pairs of data).

What will this automaton do? It changes. I will refer to a change in the automaton as one step. In one step, all the cells have an opportunity to change (some will, some won’t). Going from left to right in the automaton, the new state of a cell is based on the old state of that current cell and based on the old state of the cells to the left and right.
Here are the rules:

  • The new state of cell[j] will be alive If the old states of cell[j-1], cell[j], and cell[j+1] were all the same (either all alive or all dead),
  • Otherwise, the new state of cell[j] will be dead.

Consequently, your program has to keep track of old states and new states while the automaton is going through one step. Remember, even though you’ve determined the new state of cell[j], the program still needs to know its old state when it becomes the neighbor of the next cell over.

What about the edge cells: cell[0] and cell[len(cell) - 1]? If we need to check the cell to the left and right that means using indices that are beyond the boundary of the list! Our solution is for your algorithm to assume that there are neighbors there, and that they are always dead. You should not store these values in the list (in otherwords, don’t make your list length 2 larger than it needs to be). Instead, your algorithm should handle these end cells separately.

Here is an example with a 6 cell automaton and running 4 steps. I’m going to use D for dead and A for alive so it’s easier to see. This automaton initially has all ALIVE cells:

1
2
3
4
5
AAAAAA # initial state
DAAAAD # after step 1. Notice that the edge cells changed to dead.
DDAADD # after step 2
ADDDDA # after step 3. Make sure you understand why the ends became alive.
DDAADD # after step 4

REQUIREMENT: The only list method you may use for this request is .append() Do not use any other list library methods to solve this problem. Do not turn the list into a string or other data type either. I want you to work with a list object and come up with all the algorithms yourself.

User Input

There are 2 pieces of information that your program needs from the user:

  1. The name of the file with the starting values of the automaton. You can read more about the file format below.
  2. The number of steps to run. This must be a positive value.
    Your program must handle invalid input for both of these: filename for a file that doesn’t exist; a non-positive number as well as an invalid type. If either input is invalid, display a helpful error message and prompt again.

Display

For increased readability, if a cell’s state is alive, print the ‘+’ character. If a cell’s state is dead, print the space character, ‘ ‘. Print only these characters; don’t just call the print() function on the list. The program should not display commas or the surrounding list brackets [ ]. Also, do not print any blank lines between the displays. Print nothing else on the lines besides the state of the automaton. Print one complete step per line.

I recommend you change the font used by IDLE so these 2 characters have the same width:

  1. Under the Options Menu, choose Configure IDLE (on the MAC,choose Preferences under the IDLE menu)
  2. Choose a font such as Courier New, Lucida Console, or Lucida Sans Typewriter (fixed-width fonts). This will change the font in both the shell window and the editor window.
  3. You may also want to select bold so that the display patterns are easy to see.

File Format

The text file will contain 4 lines. The first 3 lines will have either the word alive or dead, specifying a pattern. The 4th line is an integer, indicating how many times to repeat this pattern. For example:

1
2
3
4
ALIVE
ALIVE
ALIVE
20

Your program needs to read this data in and use it to intialize the automaton. For example, based on this file, the automaton will have a length of 60 cells.

You can use this example to create a text file, or create your own pattern.

Documentation and Other Style Issues

You should follow all of the commenting guidelines covered in the commenting guidelines.

Written Report

Please type up the answers to the following questions and include as a comment at the bottom of your .py file:

  1. How did you go about starting this request?
  2. During development, where did you get stuck, if at all, and how did you get unstuck?
  3. How did you test your program? Does your program meet the request specification? If not, in what ways does it fall short?
  4. What did you learn from this request? What would you do differently next time?