Hi there,
I am a first year Programming student, and this is my first time asking for assistance. Due to Programming not being what I expected, this will probably be the last time I ever do any form of Programming. I have been put off for life.
Basically we have been given a task where we have to write a C version of John Conway's Game of Life, using a 20x20 board and the pattern shown in my code (below).
Code:
#include <stdio.h>
#include <string.h>
void display (char b1[][21], char b2[][21]);
void new_board (char b1[][21], char b2[][21]);
void copy_back (char b1[][21], char b2[][21]);
int next_cells (int p, int q);
char b1[20][21] = {"**..................",
"**..................",
"....................",
"....................",
"....................",
"....................",
"....................",
"........***.........",
"........*...........",
".........*..........",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"...................."};
char b2[20][21];
int main() {
char c;
display(b1, b2);
printf("Hi! Would you like to play the Game of Life? (y/n)");
c = getchar();
while (c == 'y') {
new_board(b1, b2);
copy_back(b1, b2);
display(b1, b2);
c = getchar();
}
}
void display(char b1[][21], char b2[][21]) {
int i;
for (i=0; i<20; i++) {
printf("%s \n", b1[i]);
}
}
void new_board(char b1[][21], char b2[][21])
{
int i, j, p;
for (i=0; i<20; i++)
{
for (j=0; j<20; j++)
{
p = next_cells(i,j);
if(b1[i][j]=='*')
{
if (p ==2 || p == 3)
{
b2[i][j] = '*';
}
else
{
b2[i][j] = '.';
}
}
else
{
if (p == 3)
{
b2[i][j] = '*';
}
else
{
b2[i][j] = '.';
}
}
}
}
b2[i][j] = '\0';
}
void copy_back(char b1[][21], char b2[][21]) {
int i;
for (i=0; i<20; i++) {
strcpy(b1[i], b2[i]);
}
}
int next_cells(int p, int q) {
int count;
if (p!=0 && q!=19 && b1[p-1][q+1] == '*') {
count = p + q;
}
return count;
}
Unable to get the program working myself, I spoke to the lecturer who gave the following tips, however I have been unable to implement them successfully. Some extra guidance would be really amazing.
* I should use getch rather than getchar (since it doesn't clear the buffer).
* Basically my next_calls function is awful, and I am only using 1 IF when I should be using another 7, because I am only counting one of the neighboring cells.
* I need to increment my count variable.
* In every one of the 8 IFs, I should be protecting against the possibility that [p][q] is an edge entry on b1.