This is the code base for my second project for CS2303.
Author: Ankur Gupta
This program is an implementation of James Conway's Game of Life. It is a biological simulation and fun to just play around with.
To compile, simply run make
. You should then have an executable called life
.
To run the program, the syntax is
./life rows columns generations inputFile [print] [pause]
, where the
parameters are defined as per the following:
rows
- how long you want your playing board to becolumsn
- how wide you want your playing board to begenerations
- how many generations you want to simulate before it stops itselfinputFile
- file name of starting boardprint
- [optional] Whether you want every generation to be printed outpause
- [optional] Whether you want to be asked to continue to the next gen.
To try it out with a test case, you can run
./life 40 40 100 start/spaceship.txt y
There are a couple places where this program implementation could have been sexier. For starters, instead of saving 3 versions of the entire board, it would have been nice to create a hash function for the board. That way, you only need to fully traverse a board once and you would be able to compare any two boards uniquely. Unfortunately, I cut time a bit close on this assignment, and collision management in hash functions are a harsh reality.
Additionally, I often noticed myself constantly iterating through the entire game board multiple times, with the only real change being the logic inside of the loops. It would have been cool to use anonymous functions to keep the code DRYer, but I don't really know how to do that in C. I'm definitely going to look into it... but again, time was a constraint.
Loop in charge of looping over until the user inputs a valid answer.
This is purposefully an infinite loop, and is designed for the program to stop working until the user finally decides to comply with the program.
Loop invariant: Everytime this loop runs, it means that the user has either given an invalid input, or hasn't inputted anything yet. In either case, the only way that this loop will stop is if the user inputs in a valid character.
Loop that handles the generation traversal in the program.
Loop invariant: i is the current generation that is going on. The loop will continue until i reached the max generations (as defined by the user) or will leave early in case of a steady state / 2 state osciallation.
This loop traverses through the first set of arrays in the game map.
Loop invariant: row will be the current row in the 2d array. Row is in the range of [0, ROWS)
This loop traverses through each sub array in the 2d game map.
Loop invariant: col will be the current colum in the 2d array. Col is in the range of [0, COLS)
Iterate through the horizontal translations when checking for neighbors.
Loop invariant: i indicates which number is to be added to row so that the corresponding neighbor can be checked.
Iterate through the vertical translations when checking for neighbors.
Loop invariant: j indicates which number is to be added to col so that the corresponding neighbor can be checked.
Iterate through the rows in the board.
Loop invariant: row will always be the current row that is being worked on in the board
Iterate through the columns in the board.
Loop invariant: col will always be the current column that is being worked on in the board.
Iterate through the rows in the board.
Loop invariant: row will always be the current row that is being worked on in the board
Iterate through the columns in the board.
Loop invariant: col will always be the current column that is being worked on in the board.
Iterate through the rows of the grid.
Loop invariant: the row variable is always the current row that is being worked on in the grid
iterate through the items in each row.
Loop Invariant: the col variable indicates which column of the board is currently being worked on
Loop in charge of iterating through the text file.
Loop invariant: x is always the next character in the file. The loop will only break when x == EOF, or x runs out of characters to read
Add whitespace to teh top of the grid
Loop invariant: the row variable is always the current row that is being worked on in the grid
Add whitepspace in the extra top rows of the grid
Loop invariant: the item indicates the x position of the current item that is being filled with 0s
Fill in the rest of the board with the data from the text file.
Loop invariant: The row variable indicates the current (non-padded) row that is being filled from the text file
Left pad the initial setup from the text file
Loop invariant: i indicates the column in which a 0 has to be added in order for the starting formation to be centered
Input in the values from the text box
Loop Invariant: item indicates the column in which an organism from the starting formation occupies or not.
If there are still valid characters, simply move the file pointer up until a new line or EOF. This allows for the lines in the text file to be longer than board that the user speicifed.
Loop Invariant: The val is a pointer to a character in the same line as the previous loops. This val will continue to iterate through the line in the file until it reaches a special character, indicating that it has reached the end of the line or the file.