Jemand hat mir erzählt, dass er diesen Hamster mal für kurze Zeit beobachten konnte und der soll ungefähr so gelaufen sein:
(am besten in irfanview gezoomt anschauen)
achja, läuft nur fast 11minuten, aber kann sagen, dass zum schluss eine überraschung kommt :)
Das Script erzeugt eine Textdatei aktueller Position+Farbe in jeder Zeile und gibt Felddimension wieder, also alles was ihr zum Erzeugen der Gif benötigt(war anstrengend... sind auhc nur 13k frames.. habe das mit gifsicle nur so umständlich lösen können, dass der aufwand zum mergen zum schluss gefühlt exponentiell stieg... -.-)
Hier der Code:
[php]<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);
define("TOP",0);
define("RIGHT",1);
define("BOTTOM",2);
define("LEFT",3);
define("X",0);
define("Y",1);
function mod($x,$y) {
return ($x>=0) ? $x%$y : $x + ceil(abs($x)/$y)*$y;
}
function po($pos) {
print "(".$pos[X].",".$pos[Y].")";
}
class Field {
var $black=array();
var $blackCount=0;
function setBlack($pos) {
if(!$this->isBlack($pos)) $this->blackCount++;
$this->black[$pos[X]][$pos[Y]]=true;
//print "Set ".po($pos)." black\n";
}
function setWhite($pos) {
if($this->isBlack($pos)) $this->blackCount--;
$this->black[$pos[X]][$pos[Y]]=false;
//print "Set ".po($pos)." white\n";
}
function isBlack($pos) {
return $this->black[$pos[X]][$pos[Y]];
}
function blacks() {
return $this->blackCount;
}
function fieldDim() {
$possibleX = array_keys($this->black);
$dim['minX'] = min($possibleX);
$dim['maxX'] = max($possibleX);
$dim['minY']=0;
$dim['maxY']=0;
foreach($possibleX as $x) {
$possibleY = array_keys($this->black[$x]);
$tmpMinY=min($possibleY);
$tmpMaxY=max($possibleY);
if($tmpMinY<$dim['minY']) {
$dim['minY']=$tmpMinY;
}
if($tmpMaxY>$dim['maxY']) {
$dim['maxY']=$tmpMaxY;
}
}
return $dim;
}
}
class Hamster {
var $dir;
var $pos;
var $log;
function Hamster() {
$this->dir = RIGHT;
$this->pos = array(0,0);
$this->log = fopen("positions.txt","a+");
}
function turnCW() {
$this->dir = mod(($this->dir + 1) , 4);
}
function turnCCW() {
$this->dir = mod(($this->dir - 1) , 4);
}
function getPos() {
return $this->pos;
}
function move() {
switch($this->dir) {
case TOP:
$this->pos[Y]--;
break;
case RIGHT:
$this->pos[X]++;
break;
case BOTTOM:
$this->pos[Y]++;
break;
case LEFT:
$this->pos[X]--;
break;
}
}
function debug() {
switch($this->dir) {
case TOP:
$direction="top";
break;
case RIGHT:
$direction="right";
break;
case BOTTOM:
$direction="bottom";
break;
case LEFT:
$direction="left";
break;
}
$color = $GLOBALS['f']->isBlack($this->pos) ? "black" : "white";
$col = $GLOBALS['f']->isBlack($this->pos) ? "b" : "w";
fwrite($this->log,$this->pos[X].",".$this->pos[Y].",".$col."\n");
print "Pos: (".$this->pos[X].",".$this->pos[Y].")\nDir: ".$direction."\nField: ".$color."\n\n";
}
}
function f($b) {
if($b) print "true\n";
else print "false\n";
}
$f=new Field();
$h=new Hamster();
for($i=0;$i<15000;$i++) {
$h->debug();
if($f->isBlack($h->getPos())) {
//Turn field white, turn ccw
$f->setWhite($h->getPos());
$h->turnCCW();
} else {
//Turn field black, turn cw
$f->setBlack($h->getPos());
$h->turnCW();
}
$h->move();
}
print "Blacks: " . $f->blacks()."\n";
print_r ($f->fieldDim());
?>[/php]