Created
December 7, 2011 16:07
-
-
Save ShiroKappa/1443359 to your computer and use it in GitHub Desktop.
LifeGame Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
/** | |
* LifeGame with MySQL Spatial Information | |
*/ | |
class LifeGame { | |
private $x; | |
private $y; | |
private $playCount; | |
private $mysql; | |
public function __construct($x,$y) { | |
$this->x = $x; | |
$this->y = $y; | |
$this->playCount = 0; | |
$this->mysql = mysqli_init(); | |
$this->mysql->real_connect('********','****','****','****'); | |
$this->createNewWorld(); | |
print $this->worldToString(); | |
} | |
public function start($count) { | |
$this->playCount = $count; | |
for ($i = 0; $i < $this->playCount; $i++) { | |
$this->copyWorld(); | |
$this->changeWorld(); | |
print "----------\n"; | |
system('clear'); | |
print $this->worldToString(); | |
} | |
} | |
private function createNewWorld() { | |
$this->clearTmpWorld(); | |
$this->clearWorld(); | |
for ($y = 0; $y < $this->y; $y++) { | |
for ($x = 0; $x < $this->x; $x++) { | |
$status = mt_rand(0,1); | |
$this->mysql->query("INSERT INTO lifeGame VALUES (0,{$status},GeomFromText('POINT({$x} {$y})'))"); | |
} | |
} | |
} | |
private function clearWorld() { | |
$this->mysql->query('TRUNCATE TABLE lifeGame'); | |
} | |
private function clearTmpWorld() { | |
$this->mysql->query('TRUNCATE TABLE tmpLifeGame'); | |
} | |
private function copyWorld() { | |
$this->clearTmpWorld(); | |
$this->mysql->query('INSERT INTO tmpLifeGame SELECT * FROM lifeGame ORDER BY id'); | |
} | |
private function changeWorld() { | |
$sql = <<<SQL | |
UPDATE lifeGame SET status=(status + 1) % 2 WHERE id IN | |
( | |
SELECT | |
lg1.id | |
FROM | |
tmpLifeGame AS lg1, tmpLifeGame AS lg2 | |
WHERE | |
GLength(LineStringFromWKB(LineString(AsBinary(lg1.p),AsBinary(lg2.p)))) < 1.5 AND lg2.status=1 | |
GROUP BY lg1.id HAVING (max(lg1.status)=0 AND count(lg2.id)=3) OR (max(lg1.status)=1 and count(lg2.id) NOT IN (3,4)) | |
) | |
SQL; | |
$this->mysql->query($sql); | |
} | |
private function worldToString() { | |
$str = (string) ''; | |
$i = (int) 0; | |
$result = $this->mysql->query('SELECT status FROM lifeGame ORDER BY id'); | |
while ($res = $result->fetch_assoc()) { | |
$str .= $res['status'] ? "●" : "○"; | |
$i++; | |
if ($i === $this->x) { | |
$str .= "\n"; | |
$i = 0; | |
} | |
} | |
return $str; | |
} | |
} | |
if ($argc === 4) { | |
$x = (int) $argv[1]; | |
$y = (int) $argv[2]; | |
$c = (int) $argv[3]; | |
} else { | |
$x = 3; | |
$y = 3; | |
$c = 3; | |
} | |
print "Start Game {$x}x{$y} Map\n"; | |
$LifeGame = new LifeGame($x,$y); | |
$LifeGame->start($c); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
必要なテーブルは
CREATE TABLE lifeGame (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
status TINYINT UNSIGNED NOT NULL DEFAULT 0,
p GEOMETRY NOT NULL,
PRIMARY KEY (id),
SPATIAL KEY (p)
) ENGINE=MyISAM;
と、全く同じ構造の tmpLifeGame