A few c questions regarding game of life

  • Thread starter Thread starter inha
  • Start date Start date
  • Tags Tags
    Game Life
Click For Summary
SUMMARY

The discussion focuses on implementing Conway's Game of Life in C, specifically on structuring the code into multiple files: printer.c, calculator.c, and main.c. The user outlines the functionality of each file, detailing how calculator.c computes the next state of the game using a spherical geometry approach for border effects. The conversation also addresses the necessity of creating header files (calculator.h, printer.h) for function declarations and the correct structure of a Makefile for compiling the project. Key insights include the importance of proper dependencies in the Makefile and ensuring that calculator.c remains independent of other files.

PREREQUISITES
  • Understanding of C programming language
  • Familiarity with Conway's Game of Life rules
  • Knowledge of header files in C
  • Basic experience with Makefiles for project compilation
NEXT STEPS
  • Learn about C header files and their role in code organization
  • Study the principles of modular programming in C
  • Explore advanced Makefile techniques for project management
  • Investigate optimization strategies for Conway's Game of Life implementation
USEFUL FOR

C programmers, computer science students, and developers interested in game development and modular code organization.

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.
 

Similar threads

  • · Replies 26 ·
Replies
26
Views
5K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 13 ·
Replies
13
Views
21K