DUE: Sun April 24, 11 pm
(10% penalty for late submission, up to 1 day late)
Any changes to this document, typically due to an error on the part of the author, will be logged here.
Collaboration is strongly ENCOURAGED within a programming team, but the program you submit must still represent YOUR OWN original work. Teams should work on the algorithm together, and help debug/test each others code. However, copying code from ANY outside source (any book, current or past students, past solutions, web sites, etc.) is STRICTLY FORBIDDEN. Copy and Paste from another solution, including the solution of a team mate will incur a stiff penalty.
The mathematician John Horton Conway invented the "Game of Life." Though not a "game" in any traditional sense, it provides interesting behavior that is specified with only a few rules.
LIFE is an organism that lives in a discrete, two-dimensional world. While this world is actually unlimited, we don't have that luxury, so we restrict the area in which LIFE lives to an 80 by 22 grid of cells. Each cell is capable of holding one LIFE cell. Generations mark the passing of time. Each generation brings births and deaths to the LIFE community. The births and deaths follow the following set of rules:
For this program you will simulate the "Game of Life." Your program will read in a series of cell coordinates the contain a living LIFE cell.( How this is done is explained below ) After all the coordinates have been read in, your program will display the current "World" of LIFE. You will use astericks ( * ) to represent a living cell, and a blank space to represent empty ( or dead ) cells.
Once the initial configuration has been read in and displayed, time will begin to pass and new generations will happen. ( ie. every iteration of a while loop will signify a the passing of a new generation. Some cells may die, others may be born; your program must calculate these changes and display the resulting world. Between each iteration ( generation ) you should pause momentarily to allow the current configuration to be seen by the user. This can be done with a call to the usleep function which can be used by sharp including unistd.h. A pause of about 1/5th of a second, or 200000 micro seconds should be about right. The program will end when the user types "ctrl-c" and terminates the program. If you choose to implement the extra credit, and draw the Game of LIFE in the graphic window; don't use usleep to pause between generations. Instead, advance to the next generation every time the user clicks the mouse. The reason for this difference is that usleep tends to wreak havoc with how things are drawn in the graphic window.
Typing in an initial configuration every time you want to run the program can be quite tedious. For this reason we are going to use a handy feature of the unix shell called "file redirection." In linux everything is a file; the monitor is a file, the keyboard, the network card, etc. So when your programs are run they take there input from the "standard input" file. By using file redirection, you can have your program take input from a file other than standard input.
This may be a little bit conceptually weird because you are probably used to thinking in terms of the input to the program being generated while the program is running. However, think about programs that you have run over and over again; you tend to type the input to the program even before you are prompted for it. Expand this idea of pre-emption so that you type all the input to the program before it even gets a chance to start. Now, instead of typing the input into the terminal, type it in emacs and save it to a file. Now we can take that file and give it to your program using file redirection. Suppose you've written the input for your program in a file named "input.txt". To give the files contents to your program for input you would type.
When you read in the initial configuration you do not need to give any prompts to the user, just read in the information. The person writing the file already knows the format ( see below ) so they do not need to be prompted.
The format of the input file will be a list of x and y coordinates that specify which cells contain live cells in the initial configuration. Each set of coordinates will apear on a separate line with one or more spaces separating the x and y coordinate. The following is an example of the format.
We will use the same familiar notion of the upper left hand corner being the point ( 0, 0 ); with the x coordinate growing to the right and the y coordinate growing downward. You may also notice that we start counting at 0 rather than 1.
Some configurations grow from relatively small starting configurations. Others move across the region. Some die out, others reach a stable configuration or repeat a cycle of growth and reduction. As you find interesting configurations, please post them to the mailing list along with a brief comment about what makes the configuration interesting. Extra credit may be awarded to those who contribute interesting configurations.
The source of this exercise provides a hint that may be useful. Although it talks about a "function"; what it talks about could also be a "member function" of a class.
Define a void function named generation that takes the array we call world, an 80-column by 22-row array of char, which contains the initial configuration. The function scans the array and modifies the cells, marking the cells with birth and deaths in accord with the rules listed earlier. This involves examining each cell in turn, either killing the cell, letting it live, or, if the cell is empty, deciding whether a cell should be born. The should be a function display that accepts the array world and displays the array on the screen.
There is no framework for this assignment, you are free to implement it however you choose. However, you must supply a makefile which adheres to the class coding standards along with your source files.
Submit your work electronically to the correct folder on the cs secure server. Don't forget to include the header template at the top of each file that you submit.
For this assignment you will turn in your source files and makefile.
You can download a sample program right-clicking and save-as here. You will probably need to change the permissions on assn4_sample so that it is user executable. You can do this with the command:
You can also download a sample program that incorporates the extra credit right-clicking and save-as. here. Again, you will need to change the permissions on the file as you did above.
If you want something to try these programs with, you can download this sample input file: assn4_sample_input.
Only write a small portion of code before checking that it still compiles. This way when you get a syntax error, you can be fairly certain the error is in the part you just wrote.
The points for this assignment will be distributed as follows:
| Points | Feature Description |
|---|---|
| 2 pts | Program compiles |
| 3 pts |
Adherence to the class coding standard -- ( .5 pts ) Program does not use global variables or objects -- ( .5 pts ) Good variable and function names -- ( .5 pts ) Proper indentation and spacing -- ( .5 pts ) Good comments ( including header and function comments ) -- ( .5 pts ) No line wraps -- ( .5 pts ) Other miscellaneous parts of the standard. |
| .5 pts | Program displays initial configuration. |
| 1 pts | Initial configuration is read in and displayed correctly. |
| .5 pts | Program advances through generations of LIFE. |
| 3 pts | Program advances correctly through generations of LIFE. |
| 2 pts | Extra Credit: Write your program so that it displays the game of life in the graphic window. You can probably use Points to represent live cells. |