Skip to content

Instantly share code, notes, and snippets.

Created May 12, 2013 05:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/0cb3456a0d6ebeb94dad to your computer and use it in GitHub Desktop.
Save anonymous/0cb3456a0d6ebeb94dad to your computer and use it in GitHub Desktop.
#include "stdafx.h"
#include <iostream>
using namespace std;
#define HEIGHT 3
#define WIDTH 3
bool liveOrDie(int sum, int p)
{
bool decision;
if(p==1)
{
if(sum < 2)
decision = false;
else if(sum > 3)
decision = false;
else
decision = true;
}
else
{
if(sum==3)
decision = true;
else
decision = false;
}
return decision;
}
bool determine(int *rows[HEIGHT], int i, int j)
{
int p = rows[i][j];
bool decision;
int sum;
if(i==0 && j==0)
{
sum = rows[1][1] + rows[1][0] + rows[0][1];
decision = liveOrDie(sum, p);
}
else if(i==0)
{
sum = rows[i][j-1] + rows[i+1][j-1] + rows[i+1][j] + rows[i+1][j+1] + rows[i][j+1];
decision = liveOrDie(sum, p);
}
else if(j==0)
{
sum = rows[i-1][j] + rows[i-1][j+1] + rows[i][j+1] + rows[i+1][j] + rows[i+1][j+1];
decision = liveOrDie(sum, p);
}
else if(i==(HEIGHT-1))
{
sum = rows[i][j-1] + rows[i-1][j-1] + rows[i-1][j] + rows[i-1][j+1] + rows[i][j+1];
decision = liveOrDie(sum, p);
}
else if(j==(WIDTH-1))
{
sum = rows[i-1][j] + rows[i-1][j-1] + rows[i][j+1] + rows[i+1][j-1] + rows[i+1][j];
decision = liveOrDie(sum, p);
}
else if(i==0 && j==(WIDTH-1))
{
sum = rows[i][j-1] + rows[i+1][j] + rows[i+1][j-1];
decision = liveOrDie(sum, p);
}
else if(i==(HEIGHT-1) && j==0)
{
sum = rows[i-1][j] + rows[i][j+1] + rows[i-1][j+1];
decision = liveOrDie(sum, p);
}
else if(i==(HEIGHT-1) && j==(WIDTH-1))
{
sum = rows[i-1][j] + rows[i-1][j-1] + rows [i][j-1];
decision = liveOrDie(sum, p);
}
else
{
sum = rows[i-1][j] + rows[i-1][j-1] + rows[i][j+1] + rows[i+1][j-1] + rows[i+1][j] + rows[i-1][j+1] + rows[i][j+j] + rows[i+1][j+1];
decision = liveOrDie(sum, p);
}
return decision;
}
void display(int *rows[HEIGHT])
{
for(int i=0; i<HEIGHT; i++)
{
cout << endl;
for(int j=0; j<WIDTH; j++)
cout << rows[i][j];
}
cout << endl;
}
void handlePoints(int *rows[HEIGHT])
{
int p;
bool decision;
for(int i=0; i<HEIGHT; i++)
{
for(int j=0; j<WIDTH; j++)
{
decision = determine(rows, i, j);
if(decision == true)
rows[i][j] = 1;
else
rows[i][j] = 0;
}
}
display(rows);
}
int main()
{
int *rows[HEIGHT];
int p;
bool decision;
for(int i=0; i<HEIGHT; i++)
rows[i] = new int[WIDTH];
for(int i=0; i<WIDTH; i++)
{
for(int j=0; j<HEIGHT; j++)
cin >> rows[i][j];
}
display(rows);
while(true)
{
handlePoints(rows);
system("CLS");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment