#!/usr/bin/perl
$file = 'stats.ca';
open(CA, ">$file");

print "Cellular Automata generation program\n\n";

# first of all, we need to set up our array to hold the simulation
for ( $row = 0 ; $row < 10 ; $row += 1 ) {
	for ( $col = 0 ; $col < 10 ; $col += 1 ) {
		if ((rand(10) % 3) == 1 ) {
			$currentGen[$row][$col] = 'X';
		} else {
			$currentGen[$row][$col] = 'O';
		}
	}
} # initialize the ten by ten array

# glider coords
#$currentGen[6][6] = "X";
#$currentGen[6][7] = "X";
#$currentGen[7][6] = "X";
#$currentGen[7][8] = "X";
#$currentGen[8][6] = "X";


$gen = 0;
print CA "gen0 = [";
print CA "0 = 0"; # formatting issues, just puts in a rest in the beginning
$placeholder = 0;
$keyKitArr = 1;

while ( isDead( @currentGen ) != 1 && $gen < 10) {
	# while there are cells alive
	@flatfile = printGen( @currentGen );

	foreach $element ( @flatfile ) {
		if ( $element eq "X" ) {
			print CA ", " . $keyKitArr . " = " .$placeholder;
			$keyKitArr += 1;
		}
		$placeholder += 1;
	} 
	$placeholder = 0;
	print "\nCurrent Generation : " . $gen . " \n";
	print "\n";
	@currentGen = nextGen( @currentGen );
	$gen += 1;
}
	print CA "]";
	close(CA);






sub nextGen {
	# creates the next generation from the current generation
	my @original = @_;
	my @new_gen;
	my $row, $col, $neighbor = 0;

	for ( $row = 0 ; $row < 10 ; $row += 1 ) {
		for ( $col = 0 ; $col < 10 ; $col += 1 ) {
			# for each cell, follow these rules
			if ( $row > 0 ) { $neighbor = $neighbor + isAlive( $original[$row-1][$col] ); }
			if ( $row < 9 ) { $neighbor = $neighbor + isAlive( $original[$row+1][$col] ); }
			if ( $col > 0 ) { $neighbor = $neighbor + isAlive( $original[$row][$col-1] ); }
			if ( $col < 9 ) { $neighbor = $neighbor + isAlive( $original[$row][$col+1] ); }
			for ( $i = 0; $i < 4 ; $i += 1) {
				if ( $i == 0 ) {
					# row-1 and col+1
					$diagRow = $row - 1;
					$diagCol = $col + 1;
				} else { 
					if ( $i == 1 ) {
						# row+1 and col+1
						$diagRow = $row + 1;
						$diagCol = $col + 1;
					} else {
						if ( $i == 2 ) {
						# row-1 and col-1
						$diagRow = $row - 1;
						$diagCol = $col - 1;
						} else {
							if ( $i == 3 ) {
								# row+1 and col-1
								$diagRow = $row + 1;
								$diagCol = $col - 1;
							}
						}
					}
				}

				if ( $diagRow == -1 || $diagRow == 10 || $diagCol == -1 || $diagCol == 10 ) {
				# out of bounds testing
				} else {
					$neighbor = $neighbor + isAlive( $original[$diagRow][$diagCol])
				}

			}

			#	print "\nneighbor is " . $neighbor . ". ";
			if ( isAlive($original[$row][$col]) == 0 ) {
				# apply dead rules
				if ( $neighbor != 3  ) { # only way for it to come alive(changed it from just 3)
					# stays dead
					$new_gen[$row][$col] = "O";
				} else {
					#	print "Is alive at " . $row . " " . $col;
					$new_gen[$row][$col] = "X";
				}
			} else {
				# does this cell stay alive?
				if ( $neighbor == 2 || $neighbor == 3 ) { # only way for it to come alive
					$new_gen[$row][$col] = "X";
				} else {
					# stays dead
					$new_gen[$row][$col] = "O";
				}
			}

			$neighbor = 0;
		}
	} 
	return(@new_gen);
}


sub isAlive {
	my $cell = @_[0];
	if ( $cell eq "X" ) {
		return(1);
	} else {
		return(0);
	}
}

sub isDead {
	my @gen = @_;
	my $row, $col;
	for ( $row = 0 ; $row < 10 ; $row += 1 ) {
		for ( $col = 0 ; $col < 10 ; $col += 1 ) {
			if ( $gen[$row][$col] eq "X" ) {
				return(0);
			}
		}
	}
	return(1);
}


sub printGen {
	my @Generation = @_;
	my @flat_array;
	my $counter = 0;

	my $row, $col;
	for ( $row = 0 ; $row < 10 ; $row += 1 ) {
		for ( $col = 0 ; $col < 10 ; $col += 1 ) {
			print $Generation[$row][$col] . " ";
			$flat_array[$counter] = $Generation[$row][$col];
			$counter += 1;
		}
		print "\n";
	}
return(@flat_array);
}