# Author : Ogo #
int returnNextState(Mat int[5][5] game, int i, int j){  # function to calculate if cell game[i][j] should live to next life based off state of neighbors #
	int j_up;
	int j_down;
	int i_up;
	int i_down;
	int neighbors_alive;
	int val;

	neighbors_alive = 0;
	j_up = j+1;
	j_down = j-1;
	i_up = i+1;
	i_down = i-1;

	#counting numbers of neighbors alive, a neighbor is alive if their cell contents are 1#
	if( i_up < 5) {
		val = game[i_up][j];
		if(val == 1) {
			neighbors_alive = neighbors_alive +1;
		}
	}

	if( i_down >= 0) {
		val = game[i_down][j];
		if(val == 1) {
			neighbors_alive = neighbors_alive +1;
		}
	}

	if(j_up < 5) {
		val = game[i][j_up];
		if(val == 1) {
			neighbors_alive = neighbors_alive +1 ;
		}
		if(i_up < 5) {
			val = game[i_up][j_up];
			if(val == 1){
				neighbors_alive = neighbors_alive +1;
			}
		}

		if(i_down >= 0) {
			val = game[i_down][j_up];
			if(val == 1){
				neighbors_alive = neighbors_alive +1;
			}
		}
	}

	if(j_down >=0) {
		val = game[i][j_down];
		if(val == 1) {
			neighbors_alive = neighbors_alive +1 ;
		}
		if(i_up < 5) {
			val = game[i_up][j_down];
			if(val == 1){
				neighbors_alive = neighbors_alive +1;
			}
		}

		if(i_down >= 0) {
			val = game[i_down][j_down];
			if(val == 1){
				neighbors_alive = neighbors_alive +1;
			}
		}
	}

	val = game[i][j];

	if(val == 1){
		if(neighbors_alive < 2) {
			return 0;
		}else if(neighbors_alive > 3) {
			return 0;
		} else {
			return 1;
		}
	} else {
		if(neighbors_alive == 3) {
			return 1;
		}
	}
	return 0;
}

int printBoard(Mat int[5][5] game) { #function to print the board, simple looping through game board#
	int i;
	int j;
	int val;

	for(i=0; i<5; i= i+1){
		for(j=0; j<5; j= j+1) {
			val = game[i][j];
			if(val == 1) {
				printsil(" x|");
			}else{
				printsil("  |");
			}
		}
		prints("");
	}
	prints("");
	return 0;
}


int main() {  #main method#
	Mat int [5][5] game;
	Mat int [5][5] nextState;
	int i;
	int j;
	int ret;
	int ind;

	ind = 0;
	game = [[0,0,0,0,0],[0,1,1,1,0],[0,1,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]; #setting game board to be repeating pattern#
	nextState = [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]];

	while(true) {
		printBoard(game);
		for(i= 0; i<5; i= i+1) {
			for(j=0; j<5; j=j+1) {
				nextState[i][j] = returnNextState(game, i, j); #saving next state of game board#
			}
		}

		for(i= 0; i<5; i= i+1) {
			for(j=0; j<5; j=j+1) {
				game[i][j] = nextState[i][j]; #apply next state of game board, to current board#
			}
		}
		sleep(1);
	}

}
