A few c questions regarding game of life

  • Thread starter inha
  • Start date
  • Tags
    Game Life
In summary, the programmer had to split the program into smaller pieces in order to meet the requirements of the class, and they had questions regarding how to do it. They mentioned that they needed .h files for each of the .c files, and that there would be a makefile. Finally, they said that everything was working just fine but that they had run into a problem with the border calculations.
  • #1
inha
576
1
c questions: game of life, splitting a program

I'm doing the game of life as an exercise project for a c course. I've got a few questions regarding splitting the program into smaller pieces which is required for the project.

I've split it into these pieces:

printer.c: Used to print the matrix representing the current generation. Pretty useless considering it's not a too big piece of code.

calculator.c: Recieves the matrix representing the current generation and the indices of the element to be dealt with as an argument. Then calculates the sum of the surrounding elements and returns the state of the element in the next generation (1 for alive, 0 for dead). I'm using a "spherical geometry" to deal with the border effects and that's taken into consideration here by going through all the special cases too.

main.c: Generates the initial state (50x50 matrix) with the random number generator. I'm going to have a loop that goes through the previous generation with the function calculator. The next generation is stored into a temporary matrix element by element as the loop progeresses. When a state is returned by calculator it is compared to the previous state and if they're different a "change counter" is incremented and the loop is terminated if the change counter is too small (static state reached, I'm going to determine a proper number via a few trial runs once I get the whole thing done). When a new generation is ready it's printed and made the new initial state for the next run of the loop.

So I have 3 .c files. From what I've gathered I have to write .h files for each of those and they're supposed to include the declarations of the functions in their .c files. So would calculator.h be just like this since there's only the function calcultor in there:

Code:
int calculator(table[50][50], int i, int j)

?

I know I have to also somehow link the files to each other by using "#include"s in the .c files. So main.c would have #include "calculator.h" and #include "printer.h", correct? And calculator.c would have #include "calculator.h" and printer.c #include "printer.h"? Also I'm supposed to write the makefile for the whole thing. would that be just

Code:
 CC=gcc -ansi -pedantic -Wall
gameoflife: main.o calculator.o printer.o 
$(CC) -o gameoflife main.o calculator.o printer.o
calculator.o: calculator.c calculator.o
$(CC) -c calculator.c
printer.o: printer.c printer.o
$(CC) -c printer.c
main.o: main.c calculator.h printer.h
$(CC) -c main.c

?

I suppose the whole thing could be done a lot more efficiently and elegantly but the main purpose here is to produce a working program so I kept this at a level I'm comfortable at.
 
Last edited:
Computer science news on Phys.org
  • #2
Everything you've said sounds good to me, except that there's no reason for calculator.c to include anything other than calculator.h. Calculator should not be calling any functions in any other files; it provides an "atomic" calculation operation that depends on nothing else. The main.c file, of course, will have to include both calculator.h and printer.h

Also, in your makefile, calculator.o should not depend on itself -- it makes no sense. Same with printer.o.

Other than that, you seem to be doing a great job.

- Warren
 
  • #3
printer.c should depend on printer.h (and similarly for calculator). Maybe he meant .h instead of .o?
 
  • #4
Yes I meant .h. Or tried to mean .h but got confused and wrote .o. Thanks for the confirmation. Knowing that I'm not waaaay off makes things much easier.
 
  • #5
at least your learning to organize...some students would just dump everything into 1 file.
 
  • #6
Somewhat amusing turn this project took:

The code worked just fine but in my test runs everything except a few cells in the borders of the matrix died. I kept going through the calculator for some conceptual error for about 30 minutes until it hit me. The border calculations (special cases) were there but I forgot to put in the calculation for an arbitary cell that's not in the corners or borders.

I got served by me. Hard.
 

Related to A few c questions regarding game of life

1. What is the Game of Life?

The Game of Life is a cellular automaton created by mathematician John Conway in 1970. It is a zero-player game, meaning its evolution is determined by its initial state and requires no further input.

2. How does the Game of Life work?

The Game of Life takes place on a grid of cells, with each cell being either alive or dead. The game follows four rules for the evolution of the cells: 1) Any live cell with fewer than two live neighbors dies, as if by underpopulation. 2) Any live cell with two or three live neighbors lives on to the next generation. 3) Any live cell with more than three live neighbors dies, as if by overpopulation. 4) Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

3. What is the significance of the Game of Life?

The Game of Life is significant because it is a simple model that exhibits complex and unpredictable behavior. It has been used to study and explore concepts in mathematics, computer science, and biology. It has also been used as a benchmark for testing computer algorithms and as a teaching tool for basic programming concepts.

4. Is the Game of Life Turing complete?

Yes, the Game of Life is Turing complete, meaning that it is capable of simulating a universal Turing machine. This means that any computation that can be performed by a computer can also be performed within the Game of Life.

5. Can the Game of Life be solved?

No, the Game of Life cannot be solved in the traditional sense because its evolution is determined by its initial state and does not involve any decision-making. However, specific patterns and configurations within the game can be studied and analyzed for interesting or notable characteristics.

Similar threads

  • Programming and Computer Science
Replies
1
Views
661
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
6K
  • Programming and Computer Science
Replies
13
Views
19K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
5K
Back
Top