Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

i m working on the famous program\" Game of life\". i tried to do it with allegr

ID: 3660576 • Letter: I

Question

i m working on the famous program" Game of life". i tried to do it with allegro, and i got something work , but when i try to get the next generation, the program will suddenly stop running and jump out. here is my codes: #include #include #include #include #include using namespace std; const int row=120,col=160,max_size=10,random=500; void Initialization(int array[][col]);//initialize everything. void Random(int Universe[][col], int rows, int cols);//get some random number and fill the emptey place. void ShowResult(int array[][col]); // show the result,display it on screen int CheckNeighbor( int Universe[][col], int i, int j, int number_of_neighbor); void NextGeneration(int Universe[][col],int New_Universe[][col]); //check how many neighbor each place has//check the cell life or dead;//check the empty place born void CopyEdge(int Universe[][col]); int swapUniverse(int Universe[][col], int New_Universe[][col]); int WriteArrayToFile(int New_Universe[][col],char fileName[]);//write the array into a file int ReadArrayFromFile(int New_Universe[][col],char fileName[]);//read the file and display it on screen int main() { int x=0,y=0; int Universe[row][col], New_Universe[row][col]; char filename[max_size]; char answer; int find,random_times=90; ifstream ifile; ofstream ofile; allegro_init(); install_keyboard(); set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0); acquire_screen(); Initialization(Universe); Initialization(New_Universe); Universe[54][53]=1;Universe[54][54]=1;Universe[53][52]=1;Universe[53][53]=1;Universe[52][53]=1; ShowResult(Universe); while ( !key[KEY_Q] ) { acquire_screen(); clear_keybuf(); textout_ex( screen, font, " ", x, y, makecol( 0, 0, 0), makecol( 0, 0, 0) ); if( key[KEY_Y] ) { clear(screen); CopyEdge(Universe); NextGeneration(Universe,New_Universe); ShowResult(Universe); swapUniverse(Universe,New_Universe); } if( key[KEY_R]) { clear_keybuf(); clear(screen); Initialization(Universe); Initialization(New_Universe); for(int i=0;i<=col-2;i++) //copy top and bottom { Universe[0][i]=Universe[row-2][i]; Universe[row-1][i]=Universe[1][i]; } for(int j=1;j<=row-2;j++) //copy left and right { Universe[j][0]=Universe[j][col-2]; Universe[j][col-1]=Universe[j][1]; } Universe[0][0]=Universe[row-2][col-2]; Universe[0][col-1]=Universe[row-2][1]; //copy corners Universe[row-1][0]=Universe[1][col-2]; Universe[row-1][col-1]=Universe[1][1]; } void NextGeneration(int Universe[][col],int New_Universe[][col])//check the cell live or dead; { //check the empty place born int i,j; for(int i=1;i<=row-1;i++) { for(int j=1;j<=col-1;j++) { int number_of_neighbor=CheckNeighbor(Universe,i, j,number_of_neighbor); //check the cell live or dead; if(Universe[i][j]==1) { if(number_of_neighbor==3||number_of_neighbor==2) New_Universe[i][j]=1; else New_Universe[i][j]=0; } //check the empty place if(Universe[i][j]==0&&number_of_neighbor==3) { New_Universe[i][j]=1; } number_of_neighbor=0; } } } int CheckNeighbor( int Universe[][col], int i, int j, int number_of_neighbor) { int cellHasNeighbor = 0; //check the 8neighbors; if(Universe[i-1][j]==1) cellHasNeighbor+=1; if(Universe[i-1][j-1]==1) cellHasNeighbor+=1; if(Universe[i-1][j+1]==1) cellHasNeighbor+=1; if(Universe[i+1][j]==1) cellHasNeighbor+=1; if(Universe[i+1][j+1]==1) cellHasNeighbor+=1; if(Universe[i+1][j-1]==1) cellHasNeighbor+=1; if(Universe[i][j-1]==1) cellHasNeighbor+=1; if(Universe[i][j+1]==1) cellHasNeighbor+=1; return cellHasNeighbor; } int swapUniverse(int Universe[][col], int New_Universe[][col]) { for(int i=0;i<=row;i++) { for(int j=0;j<=col;j++) { Universe[i][j]=New_Universe[i][j]; } } return 0; } i think the problem should be between if( key[KEY_Y] ) part or NextGeneration part, but i have no idea how to figure it out.

Explanation / Answer

#include #include #define H 30 //Define height #define W 30 //Define width using namespace std; void clear(bool mat[][W]) //Sets matrix to all dead { for (int m = 0; m