/////////////////////////////////////////////////////////////////////////
//
// Name:	GameOfLife.C
//
// Purpose:	Class for John Horton Conway's "Game of Life"
//			Cellular Automata
//
/////////////////////////////////////////////////////////////////////////

#include <unistd.h>
#include "GameOfLife.h"



//////////////////////////////////////////////////////////////////
//
//	GameOfLife::ApplyRules
//		Applies the 2D cellular automata rules defined by
//		John Horton Conway, commonly referred to as the
//		"game of life," to each cell in the 2D lattice.
//
//	Input:
//		None
//
//	Output:
//		None
//
//	Returns:
//		Status_t typed value:
//			1) SUCCESS if all cells in lattice[curr] were
//				successfully updated according to the "game
//				of life" rules;
//			2) FAILURE otherwise.
//
//	Notes:
//		Conway's Game of Life rules are defined as follows:
//			1) If a cell is dead and has 3 living neighbors,
//				the cell becomes alive
//			2) If a cell is alive and has 2 or 3 living neighbors,
//				the cell remains alive
//			3) Otherwise, the cell becomes dead
//
//////////////////////////////////////////////////////////////////
Status_t
GameOfLife::ApplyRules()
{
	Dimension_t i, j, k, m;	// lattice index counters
	Dimension_t	iEast, iWest, jNorth, jSouth;	// directional offsets

	//
	// First swap the previous and current lattice indexes
	//
	SwapLattices();


	//
	// Apply the "game of life" rules to each cell in the
	// current lattice. (See "Notes" in the function header.)
	//
	for (i=0; i<Rows(); i++) {

		iWest = i==0 ? Rows()-1 : i-1;
		iEast = i==Rows()-1 ? 0 : i+1;

		for (j=0; j<Columns(); j++) {

			jSouth = j==0 ? Columns()-1 : j-1;
			jNorth = j==Columns()-1 ? 0 : j+1;

			int livingNeighbors = 0;
			//
			// Count the number of "living" neighbors from the
			// 8 cells surrounding the (i, j)'th cell. In CA
			// terminology, these 8 cells are known as the
			// "Moore" neighborhood.
			//
			if (latticeArr[prev](iWest, jSouth).isAlive()) livingNeighbors++;
			if (latticeArr[prev](iWest, j).isAlive()) livingNeighbors++;
			if (latticeArr[prev](iWest, jNorth).isAlive()) livingNeighbors++;
			if (latticeArr[prev](i, jSouth).isAlive()) livingNeighbors++;
			if (latticeArr[prev](i, jNorth).isAlive()) livingNeighbors++;
			if (latticeArr[prev](iEast, jSouth).isAlive()) livingNeighbors++;
			if (latticeArr[prev](iEast, j).isAlive()) livingNeighbors++;
			if (latticeArr[prev](iEast, jNorth).isAlive()) livingNeighbors++;


			//
			// Now we can apply the rules (see Notes above)
			//
			switch(latticeArr[prev](i, j).State()) {

			case BinaryCell::DEAD:
				if (livingNeighbors == 3) {
					latticeArr[curr](i, j).SetState(BinaryCell::ALIVE);
				} else {
					latticeArr[curr](i, j).SetState(BinaryCell::DEAD);
				}
				break;

			case BinaryCell::ALIVE:
				if (livingNeighbors == 2 || livingNeighbors == 3) {
					latticeArr[curr](i, j).SetState(BinaryCell::ALIVE);
				} else {
					latticeArr[curr](i, j).SetState(BinaryCell::DEAD);
				}
				break;
			}
		}
	}
	return SUCCESS;
}



//////////////////////////////////////////////////////////////////
//
//	GameOfLife::Randomize
//		Randomly gives life to each cell in the 2D lattice
//		based on the probability conferred by invP.
//
//	Input:
//		invP:	defines the probability that each cell
//				will be initialized to the ALIVE state.
//
//	Output:
//		None
//
//	Returns:
//		void
//
//////////////////////////////////////////////////////////////////
void
GameOfLife::Randomize(
const int&	invP)
{
	Dimension_t	i, j;	// row, col index counters

	// seed the pseudo-random number generator
	srand(getpid());

	// randomly assign life to each of the cells
	// based upon the probability defined
	// by invP.
	for (i=0; i<Rows(); i++) {
		for (j=0; j<Columns(); j++) {
			if (rand()%invP == 0) {
				latticeArr[curr](i, j).SetState(BinaryCell::ALIVE);
			}
		}
	}
}
