Hey guys, =) First year programing student here
I'm having some trouble running a program I have written for the C Program "Conway's Life"
Some help fixing my errors would be HUGELY appreciated. I have spent over 6 hours just trying to fix the errors alone! If smart a C Programming person could fix the errors in the code, I'd love you forever! :P
Enough of my ranting - The problem is as follows:
--------------------------------------------------------------------------------------------------------------
"Conway's life is a special kind of game. It isn't really a game - just a spectator sport. It is played on a chess board, where each cell is either alive or dead. Each turn consists of deciding which squares on the board stay the same or change (becoming either alive or dead) and then admiring the new pattern they produce.
A live square stays alive if it has exactly two or exactly three live neighbours, otherwise it dies.
A dead square becomes alive if it has exactly three live neighbours, otherwise it stays dead.
All births and deaths occur simultaneously. The next round depends only on the positions at the end of the previous round. A neighbouring square is one step away in any direction (including diagonals).
Write a program that uses a 20x20 board, initialised to a given pattern, to play the game. After each turn print out the board pattern using a . for a dead square and a * for a live square. Squares off the board are considered permanently dead.
Hint: you need two 2-D arrays (both global variables). Each one being an array of strings. Form the second array from the first array, using the above rules, then copy it back to the first array, print it and repeat. After each repetition ask the user whether they want to continue or stop.
If your program is working correctly this pattern should show a glider hitting a brick
**..................
**..................
....................
....................
....................
....................
....................
........***.........
........*...........
.........*..........
....................
all the remaining rows are filled with dead squares "
----------------------------------------------------------------------------------------------------
And here is my code:
Code:
/*Conways Life, 0.45, Sarah Stacey */
#include <stdio.h>
#include <conio.h>
#include <string.h>
void display_fctn (char b1, char b2);
void new_board (char b1, char b2);
void copy_back (char b1, char b2);
int neighbours (int p, int q);
char b1[20][21] = {"**..................",
"**..................",
"....................",
"....................",
"....................",
"....................",
"....................",
"........***.........",
"........*...........",
".........*..........",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"...................."};
int main() {
char c;
display_fctn(b1, b2);
printf("Would you like to play? (y/n)");
c = getch();
while (c = 'y') {
new_board(b1, b2);
copy_back(b1, b2);
display_fctn(b1, b2);
c = getch();
}
}
void display_fctn(char b1, char b2) {
int i;
for (i=0; i<20; i++) {
printf("%s \n", b1[i]);
}
}
void new_board(char b1, char b2) {
int i, j, p, q;
for (i=0; i<20; i++) {
for (j=0; j<20; j++) {
neighbours(p, q);
if (b1[i][j] = '*') {
if (neighbours == 2 || neighbours == 3) {
b2[i][j] = '*';
} else {
b2[i][j] = '.';
} else {
if (neighbours == 3) {
b2[i][j] = '*'; /* b2 corresponding position gets a '*' */
} else {
b2[i][j] = '.'; /*b2 gets a '.' */
}
}
}
}
/* b2[i][j] = '\0'; /* ----assign ????? \0 */
}
void copy_back(char b1, char b2); {
int i;
for (i=0; i<20; i++) {
strcpy(b1[i], b2[i]);
}
}
int neighbours(int p, int q); {
int count;
/* ((8 ifs?????))*/
if (p!=0 && q!=19 && b1[p-1][q+1] == '*') {
count = p + q;
}
return count; /* return count */
}
}
/* initialize variables eg int i = 0 ?? */
If someone could fix my code up for me, I'd be absolutely in your debt :P
Thanks again,
~Sarah87