So I'm trying to do Conway's Game of Life, but I'm running into errors while running it- it comes up at different points but always when dealing with one of the 2D arrays.
So it made me suspect I'm doing something wrong with the passing of the arrays to the various functions, but I've searched online and it appears I'm doing it right, so I'm not really sure what the issue is.
These are the two errors:
Unhandled exception at 0x001a1aa5 in GameOfLife.exe: 0xC0000005: Access violation writing location 0x00380029.
Unhandled exception at 0x00171eb9 in GameOfLife.exe: 0xC0000005: Access violation writing location 0x00000001.
And one of the watches I put up sometimes shows: "initialState CXX0017: Error: symbol "initialState" not found ", but sometimes not when i step through it.
The errors are happening inside the void functions gameRandom, gameOn, setWorld1 & setWorld2, so I'll focus on the code for those and just summarize the stuff preceding their implementation that does work and the stuff following that I haven't tested yet.
I have my inclusions ("stdafx.h", <iostream>, <ctime>, <windows.h>) & namespace.
Then two const ints: MAXROW=22 & MAXCOL=78, as well as all my function prototypes.
Then main which basically just calls up a menu void function. That menu function uses a switch to select how the game will be played, and selecting option 1 brings us to a function that is supposed to make the initial state of the game determined randomly without "life" limits.
The random state is determined by the decide function, which fills in the initialState 2D array with either ' ' or '*' characters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
void gameRandom()
{
int col, row;
char initialState[MAXROW][MAXCOL];
cout<<"Random!"<<endl;
for (row=0; row<=MAXROW; ++row)
{
for (col=0; col<=MAXCOL; ++col)
{
srand(time(0));
initialState[row][col]=decide();
}
}
gameOn(initialState);
}
void gameOn(char world1[][MAXCOL])
{
int worldState=1, row, col;
char world2[MAXROW][MAXCOL];
while (1)
{
system("cls");
if (worldState==1)
{
for (row=0; row<=MAXROW; ++row)
{
for (col=0; col<=MAXCOL; ++col)
{
cout<<world1[row][col];
}
cout<<endl;
}
setWorld2(world1, world2);
worldState=2;
}
else if (worldState==2)
{
for (row=0; row<=MAXROW; ++row)
{
for (col=0; col<=MAXCOL; ++col)
{
cout<<world2[row][col];
}
cout<<endl;
}
setWorld1(world1, world2);
worldState=1;
}
Sleep(250);
}
}
void setWorld1(char world1[][MAXCOL], char world2[][MAXCOL])
{
int row, col;
for (row=0; row<=MAXROW; ++row)
{
for (col=0; col<=MAXCOL; ++col)
{
world1[row][col]=world2[row][col];
}
}
updater(world1);
}
void setWorld2(char world1[][MAXCOL], char world2[][MAXCOL])
{
int row, col;
for (row=0; row<=MAXROW; ++row)
{
for (col=0; col<=MAXCOL; ++col)
{
world2[row][col]=world1[row][col];
}
}
updater(world2);
}
|
void updater(char world[][MAXCOL])
{
(this function uses a bunch of if statements to revise the world being sent to it according to the rules of the game)
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
char decide()
{
int roll;
char decision;
roll=rand()%2;
if (roll==0)
{
decision=' ';
}
else
{
decision='*';
}
return decision;
}
|
EDIT- added code tag