Простенькая наброска для игры "Жизнь" (клеточный автомат), на Java Script
Игра Жизнь life
28.12.2016
Подробнее про правила игры можно прочесть в статье Conway's Game of Life
game.php (Download)
<? function neighboursCount(&$aField, $x,$y) { global $n, $m; $iRet = 0; if ($x > 1) { if ($aField[$x-1][$y] == 1) { $iRet++; } if ($y > 1) { if ($aField[$x-1][$y-1] == 1) { $iRet++; } } if ($y < $m-1) { if ($aField[$x-1][$y+1] == 1) { $iRet++; } } } if ($x < $n-1) { if ($aField[$x+1][$y] == 1) { $iRet++; } if ($y > 1) { if ($aField[$x+1][$y-1] == 1) { $iRet++; } } if ($y < $m-1) { if ($aField[$x+1][$y+1] == 1) { $iRet++; } } } if ($y > 1) { if ($aField[$x][$y-1] == 1) { $iRet++; } } if ($y < $m-1) { if ($aField[$x][$y+1] == 1) { $iRet++; } } /* if ($x == 10 AND $y == 10) { echo $aField[$x-1][$y].'--'.$iRet.'<br/>'; $aField[$x][$y] = 2; } */ return $iRet; } function Draw(&$aField, &$aRet) { $sRet = '<table border="1" cellpadding="0" cellspacing="0" width="500px" height="500px" class="">'; $aRet = array(); for ($i=0; $i<count($aField); $i++) { $sRet .= '<tr>'; $aRet[$i] = array(); for ($j=0; $j<count($aField[$i]); $j++) { $iNBRs = neighboursCount($aField, $i, $j); switch ($aField[$i][$j]) { case '1': $s = ' style="background-color : red;"'; break; case '2': $s = ' style="background-color : blue;"'; break; default: $s = ''; break; } $sRet .= '<td '.$s.'>'; $sRet .= ' '; #.$iNBRs; $sRet .= '</td>'; if ( ($iNBRs > 1) AND ($iNBRs < 4) ) { $aRet[$i][$j] = 1; } else { $aRet[$i][$j] = 0; } } $sRet .= '</tr>'; } $sRet .= '</table><hr/>'; return $sRet; } $n = 20; $m = 20; $aField = array(); for ($i=0; $i<$n; $i++) { $aField[$i] = array(); for ($j=0; $j<$m; $j++) { $aField[$i][$j] = 0; } } $aField[3][3] = 1; $aField[3][4] = 1; $aField[3][5] = 1; $aField[4][3] = 1; // echo neighboursCount($aField, 10, 10); $Year = isset($_REQUEST['Year'])?$_REQUEST['Year']:0; if (!$Year) { $Year = 1; } echo '<form name="frmMain" action="index.php" method="get"><div width="100%" align="center"><br>'; $aRet = array(); for ($i=0; $i<$Year; $i++) { $sRet = Draw($aField, $aRet); $aField = $aRet; } echo $sRet; echo '</div><input name="Year" type="hidden" value="'.(++$Year).'"><input type="submit" value="Go On"></form><br><a href="index.php">Reset</a>'; ?>