Lab 7: Cellular Automata

This assignment assumes that you have read up through Chapter 6. Please bring your book to lab, as you will need it as a reference. Each lab assignment is more work than the previous one, so get started early.

The objectives for this assignment are for you to:

  • Get even more experience using arrays.
  • Get even more practice in decomposing programs into small functions.
  • Learn about the basics of cellular automata and discrete-event simulation.
  • Getting Started

    As in the previous lab, start by creating a directory just for this assignment, and changing to that directory:
    mkdir cse20211/lab7
    cd cse20211/lab7
    

    Conway's Game of Life

    Conway's Game of Life is an early example of a cellular automaton which can be easily simulated on a computer. A cellular automaton consists of a large number of cells, each operating according to some simple local rules. Given a large enough number of cells and an interesting initial configuration, non-trivial emergent behavior can result.

    (Philosophically speaking, emergent behavior from simple initial conditions is another way of explaining the rich complexity of the natural world. This is a starting point for the study of chaos theory, and is complementary to the fractal structures we studied earlier.)

    See the Wikipedia article on Life, and carefully read the "Rules" section and look over some of the examples of cellular automata. We will use the simple rules where a dead cell with 3 neighbors becomes live, and only a live cell with 2 or 3 neighbors survives, all others die.

    Part One: Play Life

    Write a program (life.c) that plays Life interactively with the user on a 40-by-40 board, displayed as Xs (live cells) and blank spaces (dead cells.) Just use scanf and printf for input and output; the graphics library is not required here.

    Initially, the program should set up a completely blank board, and then ask the user for input:

  • If the user enters a, then ask for coordinates where to add a new live cell. (No action is needed if it is already alive.)
  • If the user enters r, then ask for coordinates where a cell should be removed. (No action is needed if it is already dead.)
  • If the user enters n, then advance the simulation to the next "tick" by applying the rules of the game.
  • If the user enters q, then quit the program.
  • If the user enters p, then play the game forever without asking for more input. (You can press control-C to stop the program.)
  • Hints:
  • To apply the rules of life correctly, you will need TWO boards. Use one board to store the current state of the world, and use the other board to fill in the future state of the world. When done, switch them.
  • If the continuous-play runs at full speed, you won't be able to see anything. To improve the display, try sleeping for one second between each tick, and clearing the screen before drawing each iteration of the board.
  • To clear the screen, try this: printf("\033[2J\033[H"); If you are curious as to why this works, read about VT100 Escape Codes, noting that \033 is the octal ASCII code for ESC (escape).
  • Part Two: Invent Life!

    Using your program, create number of scenes with a variety of complex automata.

    Of course, entering the scene by hand every time would be ridiculous, so instead put your keyboard commands in a file called scene1.life and then test them by using the less-than symbol to read commands from the file instead, like this:

    ./life < scene1.life
    
    For example, putting the following in an input file would create a glider that moves across the screen diagonally.
    a 2 0
    a 3 1
    a 1 2
    a 2 2
    a 3 2
    p
    
    Create several scenes called scene1.life, scene2.life and so forth that demonstrate a wide variety of behavior within Life.
  • scene1.life should show four different "still life" objects that do not change as time passes.
  • scene2.life should show four different "oscillators" that progress through two or more stages, but always return to the same shape.
  • scene3.life should show four different objects that move across the screen in different directions.
  • scene4.life should show an "explosion" that creates a large amount of initial activity, but then settles down into still life, oscillators, or nothing.
  • scene5.life is your choice: show me something unusual or unexpected.
  • Turning In

    Please review the general instructions for lab reports.

    Turn in life.c, and scene1.life through scene5.life and report.txt. Your lab report should explain the final part of the assignment, explaning how it works from the user perspective, how the program works internally, and how you verified that the output of the program is correct.

    Turning In

    This assignment is due on Monday, Nov 4th at noon. Late assignments are not accepted.