A few c questions regarding game of life

  • Thread starter Thread starter inha
  • Start date Start date
  • Tags Tags
    Game Life
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
5 replies · 7K views
inha
Messages
576
Reaction score
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:
Physics news on Phys.org
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
 
printer.c should depend on printer.h (and similarly for calculator). Maybe he meant .h instead of .o?
 
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.
 
at least your learning to organize...some students would just dump everything into 1 file.
 
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.