<?php session_start() ?>

    <!--
    Brian Platz
    CS85 Section 1643
    Professor Geddes
    March 27, 2012
    Assignment: Unit 5, Conway's Life
    --> 
    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  <head>
      <title>Conway's Life</title>
      <style type="text/css">
         body {background-color:#B0E0E6;}
         table {margin-left: auto;
                margin-right: auto;}
         h1 {color:black; font-family:Ariel; font-size:14pt; 
                 font-weight:bold; text-align: center}
         h2 {color:red; font-family:Ariel; font-size:14pt; font-weight:bold}
         h3 {color:black; font-family:Ariel; font-size:24pt; font-weight:normal}
<!-- WHY IS MY TABLE NOT CENTERING?! -->
         table.center {
            margin-left:auto; 
            margin-right:auto;
              }
      </style>    
  </head>
  <body>       
   <?php 
          extract
($_REQUEST); //kg: create form variables
          
error_reporting(E_ALL & ~E_NOTICE); //kg: get rid of run-time notices
          
          
global $world;    //two dimensional boolean array showing living organisms
          
global $alive;    //boolean, true if there are any living organisms in world
          
          
            
if ( filter_input(INPUT_GET"killAll" ) ) {    // this check for 'kill' command to reset static world
              
unset( $_SESSION['started'] );
            }
            
          if (isset(
$_SESSION["started"]) ) {            // session already in progress
              
nextGen();
              
printResult();
              
showWorld();
              } else {                                    
// first time through
              
echo "<h1>Welcome to Conway's Life<br/>Please select living cells to start your colony...</h1>\n<br/>";
              
initializeGame();
              
showWorld();
              
$_SESSION["started"] = true;
              }

          function 
initializeGame() {                    // sets up board
            
global $world$alive$btnText$generation;
           
              
$alive true;
              
$btnText "Next Generation...";
              
$generation 0;
              
$world = array(
                  array(
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse),
                  array(
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse),
                  array(
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse),
                  array(
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse),
                  array(
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse),
                  array(
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse),
                  array(
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse),
                  array(
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse),
                  array(
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse),
                  array(
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse),
                  array(
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse),
                  array(
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse),
                  array(
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse),
                  array(
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse),
                  array(
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse),
                  array(
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse),
                  array(
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse),
                  array(
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse),
                  array(
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse),
                  array(
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse),
                  array(
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse),
                  array(
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse),
                  array(
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse),
                  array(
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse),
                  array(
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse),
                  );
              
              
$_SESSION["world"] = $world;
              
$_SESSION["alive"] = $alive;
              
$_SESSION["btnText"] = $btnText;
              
$_SESSION["generation"] = $generation;
               }
         
          function 
nextGen() {                    // calculates the next generation
          // If the cell is alive, it will die of loneliness if it has fewer than two neighbors. 
          // It will die from overcrowding if it has more than three neighbors.
          // If there is no life in the cell and there are exactly three neighbors, birth occurs.
              
global $world;
              
$world $_SESSION["world"];            // get current world
              
                                                    // get checked checkboxes and toggle $world to true for them
              
for ($x 0$x<25$x++) {
                  for (
$y 0$y<30$y++) {
                      if ( 
filter_input(INPUT_GET$x."_".$y ) ) {
                          
$world[$x][$y] = true;
                          }
                      }
                  }
              
$_SESSION["world"] = $world;            // save world

              
$newWorld $world;                        // allows us to manipulate new world, while maintaining the old for reference
              
              
for ($x 0$x<25$x++) {                // cycle through all checkbox 'cells'
                  
for ($y 0$y<30$y++) {
                      
$cw conway($x$y);              // find the 'conway number' for that cell
                      
if ( ($world[$x][$y] == true && $cw 2) ||            // if alive but less than 2 neighbors...
                                  
($world[$x][$y] == true && $cw 3) ) {    // or alive with more than 3... 
                          
$newWorld[$x][$y] = false;                        // cell dies
                          
}
                      else if (
$world[$x][$y] == false && $cw == 3) {     // if dead with 3 neighbors...
                          
$newWorld[$x][$y] = true;                        // cell is born
                          
}
                      }
                  }
              
$_SESSION["world"] = $newWorld;        // replace world with the next generation
              
$_SESSION["generation"]++;            // increment generation
              
}
          
          function 
conway($xx$yy) {            // calculates the number of adjacent cells ('conway number')
              
global $world;                        // will reference the state of the current world
              
$count 0;
              
              if ( 
$xx-> -) {
                  if ( 
$yy-> -&& $world[$xx-1][$yy-1] ) {$count++;}
                  if ( 
$world[$xx-1][$yy] )  {$count++;}
                  if ( 
$yy+30 && $world[$xx-1][$yy+1] )  {$count++;}
                  }
              if ( 
$yy-> -&& $world[$xx][$yy-1] )  {$count++;}
              if ( 
$yy+> -&& $world[$xx][$yy+1] )  {$count++;}
              if ( 
$xx+25 ) {
                  if ( 
$yy-> -&& $world[$xx+1][$yy-1] ) {$count++;}
                  if ( 
$world[$xx+1][$yy] )  {$count++;}
                  if ( 
$yy+30 && $world[$xx+1][$yy+1] )  {$count++;}
                  }
              return 
$count;
              }
              
          function 
printResult() {                // outputs the status of the world (alive/dead)
              
global $started;
              global 
$btnText;
              
$btnText $_SESSION["btnText"];
              
$started $_SESSION["started"];
              
              if ( 
areAnyAlive() ) {                // looks for living cells
                
echo "<br/><h1>Your world is still alive!</h1>\n<br/>";
                } else {
                
$btnText "Play Again...";
                  
$_SESSION["btnText"] = $btnText;
                  unset( 
$_SESSION['started'] );
                echo 
"<br/><h1>Your world has died :(</h1>\n<br/>";
                }
            }
            
          function 
areAnyAlive() {                // runs through world looking for life
            
global $world;
              
$world $_SESSION["world"];
              global 
$generation;
              
$generation $_SESSION["generation"];
              
              for (
$x 0$x<25$x++) {
                  for (
$y 0$y<30$y++) {
                      if ( 
$world[$x][$y] ) {
                          return 
true;            // found life
                          
}
                      }
                  }
              return 
false;                        // no life found
            
}
          
          function 
showWorld() {                // outputs current generation in the world
              
global $world;
              global 
$btnText;
              global 
$generation;
              
$world $_SESSION["world"];
              
$btnText $_SESSION["btnText"];
              
$generation $_SESSION["generation"];
              
              echo 
"<form  id='myForm' method='get' ><table class='center'>";
              for (
$x 0$x<25$x++) {
                  echo 
"<tr>";
                  for (
$y 0$y<30$y++) {
                      if (
$world[$x][$y] == false) {
                          echo 
"<input type='checkbox' name='".$x."_".$y."' />";
                          } else {
                          echo 
"<input type='checkbox' name='".$x."_".$y."' checked='checked' />";
                          }
                      }
                  echo 
"</tr><br/>";
                  }
                  
// note: 'killAll' checkbox below acts as a 'reset' if the world is in a perpetual state of living, or if
                  // the user gets tired of the current world and wants to start a new one.
              
echo "</table><input type='checkbox' name='killAll'  /> Check to kill the world...<br />
                      <input type='submit' value='
$btnText' /><br/><br/>Generation: ".$generation."<br/>";
        echo 
"<br/><a href='../index.html#unit5' >Return to Index</a>";
              
              
$_SESSION["world"] = $world;
              
$_SESSION["btnText"] = $btnText;
              }      

    
?>  
 </body>
</html>