A few c questions regarding game of life

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

Discussion Overview

The discussion revolves around implementing Conway's Game of Life as a C programming exercise. Participants focus on structuring the program into multiple files, including considerations for function declarations, dependencies, and makefile creation.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant describes their approach to splitting the Game of Life program into three C files: printer.c, calculator.c, and main.c, detailing the functionality of each.
  • Another participant suggests that calculator.c should only include calculator.h, emphasizing that it should not depend on other files, as it performs an independent calculation.
  • There is a correction regarding the makefile, where one participant points out that calculator.o should not depend on itself, and clarifies that printer.c should depend on printer.h.
  • A participant acknowledges a mistake in their makefile regarding file dependencies, expressing relief at not being far off in their understanding.
  • Another participant comments on the organizational aspect of the project, noting that some students might combine everything into a single file instead of structuring it properly.
  • A participant shares a personal experience of debugging, revealing a conceptual error related to border calculations that led to unexpected results in their program.

Areas of Agreement / Disagreement

Participants generally agree on the structure of the program and the importance of proper file dependencies, but there are some corrections and clarifications made regarding the makefile and file inclusions. The discussion remains unresolved on the optimal efficiency of the program's structure.

Contextual Notes

There are unresolved assumptions about the efficiency and elegance of the program's implementation, as well as the handling of edge cases in the Game of Life logic.

Who May Find This Useful

Students and programmers interested in C programming, software organization, and implementing algorithms in a modular fashion may find this discussion beneficial.

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
7K
  • · 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
3K
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 13 ·
Replies
13
Views
21K