Functions
WebDev/*nix assorted functions & hacks
Search
find all
Array
-
Bash
-
Classes
-
Config
-
DateTime
-
Debug
-
dotjs
-
File
-
foooo
-
Git
-
HTML
-
Images
-
Javascript
-
Linux
-
Math
-
Misc
-
MySQL
-
OS X
-
Reading
-
Redis
-
Shell scripts
-
snippets
-
SQL
-
Strings
-
TextMate
-
Web
-
\(^o^)/ Fun!
-
_Misc hacks
-
_Pages
\(^o^)/ Fun!
* Readme
Alphanumeric images to ascii
Conway's Game of Life as TextMate command
Langton's Ant
Quine
Sierpiński Carpet
Sierpiński Triangle
Towers of Hanoi
8
found
auth
Name:
Category: \(^o^)/ Fun!
Body:
<?php /* An implementation of Langton's Ant (a simple cellular automata) in PHP. Copyright (C) 2009 Maarten Spreij <mspreij@gmail.com> This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/> ------------------------------------------------------------------------------------ I was bored one hot, humid afternoon, browsing through old bookmarks, happened upon a page on Langton's Ant. Figured I'd write a quick'n'dirty version of it. +-------<http://en.wikipedia.org/wiki/Langton%27s_ant>------- | Langton's ant is a two-dimensional Turing machine with a very simple set of rules but complicated emergent | behavior. It was invented by Chris Langton in 1986. | | Squares on a plane are colored variously either black or white. We arbitrarily identify one square as the "ant". | The ant can travel in any of the four cardinal directions at each step it takes. The ant moves according to the | rules below: | | * At a black square, turn 90° right, flip the color of the square, move forward one unit | * At a white square, turn 90° left, flip the color of the square, move forward one unit | +---<end http://en.wikipedia.org/wiki/Langton%27s_ant>------- (for kicks, also take a look at <http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life> if you haven't heard of it.) Then there are quick&dirty *simple* implementations (if one can call 'm that) of run-length encoding, see <http://en.wikipedia.org/wiki/Run-length_encoding> so that the state of the grid can be passed via the page url a little easier. Any type of feedback welcome. Have fun! :-) */ // settings $len = 60; // width/height of the grid (once the ant walks off, strange things happen...) $black = 'x'; $white = 'o'; $dirs = array(1=>'u', 'r', 'd', 'l'); $ant_dirmoves = array(1=>-$len, 1, $len, -1); $starting_string = ($len*$len).$white; $me = basename($_SERVER['SCRIPT_FILENAME']); // Starting position $string = isset($_GET['string']) ? $_GET['string'] : $starting_string; $ant = isset($_GET['ant']) ? $_GET['ant'] : round(($len)*($len/2)+($len/2)).',1'; // defaults to somewhere near the middle of the board, direction up. list($ant, $dir) = explode(',', $ant); // No animals were harmed. $gen = (int) @$_GET['gen']; // decode, calculate ant's next move $field = rldec($string); if ($field[$ant] == $black) { $field[$ant] = $white; $dir = (array_search($dir, array_keys($dirs)) == 3) ? 1 : $dir+1; }else{ $field[$ant] = $black; $dir = (array_search($dir, array_keys($dirs)) == 0) ? 4 : $dir-1; } $ant += $ant_dirmoves[$dir]; $url = "$me?string=". urlencode(rlenc($field)) ."&ant=$ant,$dir&gen=".++$gen; ?> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <meta http-equiv="refresh" content="1;url=<?php echo $url; ?>" /> <title>Langton's Ant</title> <style type="text/css" media="screen"> .x { background: black; } .o { background: white; } table { border: 1px; border-collapse: collapse; } td { width: 12px; height: 12px; color: red; padding: 0px; margin: 0px; border: 0px; font-size: 10px; } </style> </head> <body> <a href="<?php echo $url; ?>">Next →</a><br> <?php echo $gen; $out = '<table><tr>'; for ($i=0; $i < strlen($field); $i++) { $c = $field[$i]; if ($i%$len==0) $out .= "</tr>\n<tr>"; $out .= ($i!=$ant) ? "<td class='$c'></td>" : "<td class='$c'>&{$dirs[$dir]}arr;</td>"; } $out .= '</tr></table>'; echo $out; echo '</body> </html>'; //______________ // rlenc($str) / function rlenc($str) { $counter = 1; $str = trim($str); $len = strlen($str); $out = ''; $last_char = $char = $str[0]; for ($i=1; $i < $len; $i++) { $char = substr($str, $i, 1); if ($char == $last_char) { $counter++; }else{ $out .= ($counter>1 ? $counter : '') . $last_char; $last_char = $char; $counter = 1; } } return $out . ($counter > 1 ? $counter : '') . $last_char; } //______________ // rldec($str) / function rldec($str) { $count = $out = ''; for ($i=0; $i < strlen($str); $i++) { $char = $str[$i]; if (is_numeric($char)) { $count .= $char; }else{ $out .= $count ? str_repeat($char, (int) $count) : $char; $count = ''; } } return $out; } ?>
Footer