In an attempt to node my homework and do a favor to anyone who needs it, here's the source code in C++ for the game of life.
If you see a way I can optimize it (boy, my code can usually be optimized quite a bit), /msg me, and I'll credit you at the bottom.
Skill level: Moderately skilled beginner
#include <iostream.h>
#include "apstring.h"
#include "apmatrix.h"
// you can get the ap*.h files from the college board website
void dispgrid(apmatrix <int> grid);
void figsurr(apmatrix <int> &grid, apmatrix <int> &grids);
void change(apmatrix <int> &grid, apmatrix <int> &grids);
void main()
{
apmatrix <int> grid(10,10,0);
// grid holds the alive/dead value
apmatrix <int> grids(10,10,0);
// grids holds the surrounding alive/dead cells
char cont='Y';
// this is where we make the base setup.
// you can change it however you want
// everything is declared as a 0 already,
// so just make it a '1' to start.
// the suggested values for beginning:
grid[2][4]=1;
grid[3][5]=1;
grid[4][3]=1;
grid[4][4]=1;
grid[4][5]=1;
grid[5][3]=1;
grid[5][4]=1;
grid[5][5]=1;
while(cont=='Y')
{
dispgrid(grid);
// display the grid
figsurr(grid, grids);
// figure out surrounding alive/dead cells
change(grid, grids);
// do the next generation
cout<<"\nDo you want to continue? [Y/N] ";
cin>> cont;
cont = toupper(cont);
cin.ignore(100,'\n');
// allows the program to repeat
}
}
void figsurr(apmatrix <int> &grid, apmatrix <int> &grids)
{
int surr;
// the two for loops run through the whole grid
// regardless of size
for(int i=0; i<grid.numrows(); i++)
{
for(int j=0; j<grid.numcols(); j++)
{
surr=0;
// the i!=__ clauses are basic bounds checking
// the grid____==1 clauses are to check if a surrounding
// cell is alive or dead
if(i!=0&&j!=0&&grid[i-1][j-1]==1)
surr++;
if(i!=0&&grid[i-1][j]==1)
surr++;
if(i!=0&&j!=9&&grid[i-1][j+1]==1)
surr++;
if(j!=0&&grid[i][j-1]==1)
surr++;
if(j!=9&&grid[i][j+1]==1)
surr++;
if(i!=9&&j!=0&&grid[i+1][j-1]==1)
surr++;
if(i!=9&&grid[i+1][j]==1)
surr++;
if(i!=9&&j!=9&&grid[i+1][j+1]==1)
surr++;
grids[i][j]=surr;
// send the results to the corresponding cell on "grids"
}
}
}
void change(apmatrix <int> &grid, apmatrix <int> &grids)
{
// here's where the rules come into play
// you can change them depending on how you
// want to run the game.
// Currently: if the current cell is alive
// and there are less than two surrounding living
// cells, the cell will die of starvation.
// More than three and it will die of overcrowding
// If it's dead, and there are exactly 3 surrouding
// living cells, the cell will gain life.
for(int i=0; i<grid.numrows(); i++)
{
for(int j=0; j<grid.numcols(); j++)
{
if(grid[i][j]==1&&(grids[i][j]<2||grids[i][j]>3))
{
grid[i][j]=0;
}
else if(grid[i][j]==0&&grids[i][j]==3)
grid[i][j]=1;
}
}
}
void dispgrid(apmatrix <int> grid)
{
// just neato formatting.
// Change it to please you.
cout << "\n 0123456789\n";
cout << " __________\n";
for(int i=0; i<grid.numrows(); i++)
{
cout << i << " |";
for(int j=0; j<grid.numcols(); j++)
{
if(grid[i][j]==0)
cout << ".";
if(grid[i][j]==1)
cout << "x";
//InfoDisplay:cout << grid[i][j] << grids[i][j];
}
cout << "|\n";
}
cout << " ----------\n";
}