Dank der Hilfe von dead_orc hab ich es nun hin bekommen. Hier mal mein Code: PHP-Code: <?php session_start(); class field { var $row = 9; # Hier können die Zeilen bestimmt werden; var $col = 9; # Hier können die Spalten bestimmt werden; var $alive = '<img src="alive.png" alt="alive" width="30" height="30" border="1" />'; var $dead = '<img src="dead.png" alt="dead" width="30" height="30" border="1" />'; var $cells = array(); var $_SESSION = array(); var $gameover = 0; function init_gen() # Hier wird das Spielfeld Initialisiert; { $_SESSION['count_gen'] = 0; for($y = 0; $y <= $this->col-1; $y++) { for($x = 0; $x <= $this->row-1; $x++) { $this->cells[$x][$y] = 0; } } $randx = rand(0,$this->row-1); $randy = rand(0,$this->col-1); $this->cells[$randx][$randy] = 1; $this->cells[$randx][$randy+1] = 1; $this->cells[$randx][$randy-1] = 1; $this->cells[$randx+1][$randy] = 1; $this->cells[$randx-1][$randy] = 1; for($y = 0; $y <= $this->col-1; $y++) { for($x = 0; $x <= $this->row-1; $x++) { $_SESSION['cells'][$x][$y] = $this->cells[$x][$y]; } } } function next_gen() # Berechnung der nächsten Generation; { $_SESSION['count_gen']++; for($y = 0; $y <= $this->col-1; $y++) { for($x = 0; $x <= $this->row-1; $x++) { $this->cells[$x][$y] = $_SESSION['cells'][$x][$y]; $cells_new[$x][$y] = $this->cells[$x][$y]; } } $cells_new = $this->cells; for($y = 0; $y <= $this->col-1; $y++) { for($x = 0; $x <= $this->row-1; $x++) { if(0 == $this->cells[$x][$y]) { $dead_neighbours = 0; # if weiß Nachbarn = 3 = 1*grün Geboren if(1 == $this->cells[$x-1][$y]) { $dead_neighbours++; } if(1 == $this->cells[$x+1][$y]) { $dead_neighbours++; } if(1 == $this->cells[$x][$y-1]) { $dead_neighbours++; } if(1 == $this->cells[$x][$y+1]) { $dead_neighbours++; } if(1 == $this->cells[$x-1][$y-1]) { $dead_neighbours++; } if(1 == $this->cells[$x+1][$y+1]) { $dead_neighbours++; } if(1 == $this->cells[$x+1][$y-1]) { $dead_neighbours++; } if(1 == $this->cells[$x-1][$y+1]) { $dead_neighbours++; } if(3 == $dead_neighbours) { $cells_new[$x][$y] = 1; } } elseif(1 == $this->cells[$x][$y]) { $alive_neighbours = 0; if(1 == $this->cells[$x-1][$y]) { $alive_neighbours++; } if(1 == $this->cells[$x+1][$y]) { $alive_neighbours++; } if(1 == $this->cells[$x][$y-1]) { $alive_neighbours++; } if(1 == $this->cells[$x][$y+1]) { $alive_neighbours++; } if(1 == $this->cells[$x-1][$y-1]) { $alive_neighbours++; } if(1 == $this->cells[$x+1][$y+1]) { $alive_neighbours++; } if(1 == $this->cells[$x+1][$y-1]) { $alive_neighbours++; } if(1 == $this->cells[$x-1][$y+1]) { $alive_neighbours++; } # if grün Nachbarn < 2 Stirbt OR grün Nachbarn >= 5 Stirb if(1 <= $alive_neighbours OR 4 <= $alive_neighbours) { $cells_new[$x][$y] = 0; } # if grün Nachbarn = 2 OR 3 Überlebt if(2 == $alive_neighbours OR 3 == $alive_neighbours) { $cells_new[$x][$y] = 1; } } } } for($y = 0; $y <= $this->col-1; $y++) { for($x = 0; $x <= $this->row-1; $x++) { $_SESSION['cells'][$x][$y] = $cells_new[$x][$y]; } } } function show() # Anzeige des Spielfeldes + Lebenden und Toten Zellen; { for($y = 0; $y <= $this->col-1; $y++) { for($x = 0; $x <= $this->row-1; $x++) { $this->cells[$x][$y] = $_SESSION['cells'][$x][$y]; } } $block_row = $this->row*30 + $this->row*2; $block_col = $this->col*30 + $this->col*2; echo "\n".'<div>'."\n".'<p>'."\n".'<a href="gol.php">Re Start</a> - <a href="?next-gen=go">Next-Gen</a> - Generation: '.$_SESSION['count_gen']."\n".'</p>'."\n"; echo '<p style="border: 1px solid black; width: '.$block_row.'px; height: '.$block_col.'px; margin: 0; padding: 0;">'."\n"; for($y = 0; $y <= $this->col-1; $y++) { for($x = 0; $x <= $this->row-1; $x++) { if(1 == $this->cells[$x][$y]) { echo $this->alive; } elseif(0 == $this->cells[$x][$y]) { echo $this->dead; } } } echo "\n".'</p>'."\n".'</div>'; } } $gol = new field(); if('go' != $_GET['next-gen']) { $gol->init_gen(); } else { $gol->next_gen(); } $gol->show(); ?>
<?php session_start(); class field { var $row = 9; # Hier können die Zeilen bestimmt werden; var $col = 9; # Hier können die Spalten bestimmt werden; var $alive = '<img src="alive.png" alt="alive" width="30" height="30" border="1" />'; var $dead = '<img src="dead.png" alt="dead" width="30" height="30" border="1" />'; var $cells = array(); var $_SESSION = array(); var $gameover = 0; function init_gen() # Hier wird das Spielfeld Initialisiert; { $_SESSION['count_gen'] = 0; for($y = 0; $y <= $this->col-1; $y++) { for($x = 0; $x <= $this->row-1; $x++) { $this->cells[$x][$y] = 0; } } $randx = rand(0,$this->row-1); $randy = rand(0,$this->col-1); $this->cells[$randx][$randy] = 1; $this->cells[$randx][$randy+1] = 1; $this->cells[$randx][$randy-1] = 1; $this->cells[$randx+1][$randy] = 1; $this->cells[$randx-1][$randy] = 1; for($y = 0; $y <= $this->col-1; $y++) { for($x = 0; $x <= $this->row-1; $x++) { $_SESSION['cells'][$x][$y] = $this->cells[$x][$y]; } } } function next_gen() # Berechnung der nächsten Generation; { $_SESSION['count_gen']++; for($y = 0; $y <= $this->col-1; $y++) { for($x = 0; $x <= $this->row-1; $x++) { $this->cells[$x][$y] = $_SESSION['cells'][$x][$y]; $cells_new[$x][$y] = $this->cells[$x][$y]; } } $cells_new = $this->cells; for($y = 0; $y <= $this->col-1; $y++) { for($x = 0; $x <= $this->row-1; $x++) { if(0 == $this->cells[$x][$y]) { $dead_neighbours = 0; # if weiß Nachbarn = 3 = 1*grün Geboren if(1 == $this->cells[$x-1][$y]) { $dead_neighbours++; } if(1 == $this->cells[$x+1][$y]) { $dead_neighbours++; } if(1 == $this->cells[$x][$y-1]) { $dead_neighbours++; } if(1 == $this->cells[$x][$y+1]) { $dead_neighbours++; } if(1 == $this->cells[$x-1][$y-1]) { $dead_neighbours++; } if(1 == $this->cells[$x+1][$y+1]) { $dead_neighbours++; } if(1 == $this->cells[$x+1][$y-1]) { $dead_neighbours++; } if(1 == $this->cells[$x-1][$y+1]) { $dead_neighbours++; } if(3 == $dead_neighbours) { $cells_new[$x][$y] = 1; } } elseif(1 == $this->cells[$x][$y]) { $alive_neighbours = 0; if(1 == $this->cells[$x-1][$y]) { $alive_neighbours++; } if(1 == $this->cells[$x+1][$y]) { $alive_neighbours++; } if(1 == $this->cells[$x][$y-1]) { $alive_neighbours++; } if(1 == $this->cells[$x][$y+1]) { $alive_neighbours++; } if(1 == $this->cells[$x-1][$y-1]) { $alive_neighbours++; } if(1 == $this->cells[$x+1][$y+1]) { $alive_neighbours++; } if(1 == $this->cells[$x+1][$y-1]) { $alive_neighbours++; } if(1 == $this->cells[$x-1][$y+1]) { $alive_neighbours++; } # if grün Nachbarn < 2 Stirbt OR grün Nachbarn >= 5 Stirb if(1 <= $alive_neighbours OR 4 <= $alive_neighbours) { $cells_new[$x][$y] = 0; } # if grün Nachbarn = 2 OR 3 Überlebt if(2 == $alive_neighbours OR 3 == $alive_neighbours) { $cells_new[$x][$y] = 1; } } } } for($y = 0; $y <= $this->col-1; $y++) { for($x = 0; $x <= $this->row-1; $x++) { $_SESSION['cells'][$x][$y] = $cells_new[$x][$y]; } } } function show() # Anzeige des Spielfeldes + Lebenden und Toten Zellen; { for($y = 0; $y <= $this->col-1; $y++) { for($x = 0; $x <= $this->row-1; $x++) { $this->cells[$x][$y] = $_SESSION['cells'][$x][$y]; } } $block_row = $this->row*30 + $this->row*2; $block_col = $this->col*30 + $this->col*2; echo "\n".'<div>'."\n".'<p>'."\n".'<a href="gol.php">Re Start</a> - <a href="?next-gen=go">Next-Gen</a> - Generation: '.$_SESSION['count_gen']."\n".'</p>'."\n"; echo '<p style="border: 1px solid black; width: '.$block_row.'px; height: '.$block_col.'px; margin: 0; padding: 0;">'."\n"; for($y = 0; $y <= $this->col-1; $y++) { for($x = 0; $x <= $this->row-1; $x++) { if(1 == $this->cells[$x][$y]) { echo $this->alive; } elseif(0 == $this->cells[$x][$y]) { echo $this->dead; } } } echo "\n".'</p>'."\n".'</div>'; } } $gol = new field(); if('go' != $_GET['next-gen']) { $gol->init_gen(); } else { $gol->next_gen(); } $gol->show(); ?>
Foren-Regeln