A few c questions regarding game of life

  • Thread starter Thread starter inha
  • Start date Start date
  • Tags Tags
    Game Life
AI Thread Summary
The discussion revolves around structuring a C program for the Game of Life project, focusing on splitting the code into three main components: printer.c for displaying the matrix, calculator.c for computing the next generation of cells, and main.c for managing the overall program flow. The program employs a spherical geometry approach to handle border effects, and the main logic involves generating an initial random state, iterating through generations, and tracking changes to determine when to stop the simulation.For proper organization, header files (calculator.h and printer.h) are needed to declare functions, with main.c including both headers. The makefile structure is also discussed, emphasizing that object files should not depend on themselves and that dependencies should be correctly set up to ensure proper compilation. A notable issue encountered was a conceptual error in the border calculations, highlighting the importance of thorough testing and debugging in programming. Overall, the focus is on creating a functional program while maintaining a manageable code structure.
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:
Computer science 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.
 
In my discussions elsewhere, I've noticed a lot of disagreement regarding AI. A question that comes up is, "Is AI hype?" Unfortunately, when this question is asked, the one asking, as far as I can tell, may mean one of three things which can lead to lots of confusion. I'll list them out now for clarity. 1. Can AI do everything a human can do and how close are we to that? 2. Are corporations and governments using the promise of AI to gain more power for themselves? 3. Are AI and transhumans...
Sorry if 'Profile Badge' is not the correct term. I have an MS 365 subscription and I've noticed on my Word documents the small circle with my initials in it is sometimes different in colour document to document (it's the circle at the top right of the doc, that, when you hover over it it tells you you're signed in; if you click on it you get a bit more info). Last night I had four docs with a red circle, one with blue. When I closed the blue and opened it again it was red. Today I have 3...
Back
Top