Hi there, I'm a first time poster and first year C programming student.
I am a little stuck on the following question:
-----------------------------------------------------------------------------
"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
**..................
**..................
....................
....................
....................
....................
....................
........***.........
...................
...................
....................
I have given it a very serious attempt, believe me, I've been trying all week and was up all night last night trying to fix the errors in it :( I can see there are many errors showing in the compiler, but whenever I try to fix the remaining errors, more appear!
Here is the code I have come up with so far, I got the basic layout from a tutorial at my college, so that should be ok. My code is as follows:
#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() {
display_fctn();
printf("Would you like to play? (y/n)");
c = getch();
while (c = 'y') {
new_board();
copy_back();
display_fctn(b1);
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;
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 */
}
}
Sorry if I waffled on a bit, I would really like to get this running and the errors out of the way, so that I can move on with my programing. Thank you very much to anyone who takes the time to help, I would really appreciate it! :icon_confused: Hopefully one day I could return the favor, you look like a great bunch of people, and a great community to be a part of!
Thanks.
edit: Incase anyone wanted to know, these are the errors showing up in my compiler (A lot, I know): - Every time I go through to try and fix them, more come up.
:31: warning: initializer-string for array of chars is too long
: In function `int main()':
:7: error: too few arguments to function `void display_fctn(char, char)'
:35: error: at this point in file
:37: error: `c' undeclared (first use this function)
:37: error: (Each undeclared identifier is reported only once for each function it appears in.)
:8: error: too few arguments to function `void new_board(char, char)'
:39: error: at this point in file
:9: error: too few arguments to function `void copy_back(char, char)'
:40: error: at this point in file
:41: warning: invalid conversion from `char (*)[21]' to `char'
:7: error: too few arguments to function `void display_fctn(char, char)'
:41: error: at this point in file
: In function `void display_fctn(char, char)':
:50: error: invalid types `char[int]' for array subscript
: In function `void new_board(char, char)':
:58: error: `p' undeclared (first use this function)
:58: error: `q' undeclared (first use this function)
:59: error: invalid types `char[int]' for array subscript
:60: error: ISO C++ forbids comparison between pointer and integer
:60: error: ISO C++ forbids comparison between pointer and integer
:61: error: invalid types `char[int]' for array subscript
:63: error: invalid types `char[int]' for array subscript
:64: error: expected primary-expression before "else"
:64: error: expected `;' before "else"
:73: error: invalid types `char[int]' for array subscript
:79: error: invalid types `char[int]' for array subscript
:79: error: invalid types `char[int]' for array subscript
:89: warning: return-statement with a value, in function returning 'void'
Failure