我用PHP为Langton的Ant编写代码。但我无法得到那个结果。
规则:
如果机器位于白色方块中,请顺时针旋转90°并向前移动1个单位;
如果机器位于黑色方块中,请逆时针旋转90°并向前移动1个单位;
每走一步,翻转基方块的颜色。
这是我的代码:
Matris.php
<?php
declare(strict_types = 1);
class Matris
{
const white = 0;
const black = 1;
private $cells = array();
public function Matris($size)
{
for ($x = 0; $x < $size; $x++) {
for ($y = 0; $y < $size; $y++) {
$this->cells[$x][$y] = Matris::white;
}
}
}
public function getCells()
{
echo "<table>";
for ($x = 0; $x < count($this->cells); $x++) {
echo "<tr>";
for ($y = 0; $y < count($this->cells); $y++) {
$r = $this->cells[$x][$y];
echo "<td>$r</td>";
}
echo "</tr>";
}
echo "</table>";
}
public function getSize()
{
return count($this->cells);
}
public function getCellColor($x, $y)
{
return $this->cells[$x][$y];
}
public function setCellColor($x, $y, $color)
{
$this->cells[$x][$y] = $color;
}
}
Ant.php
<?php
class Ant
{
const face_right = 0;
const face_left = 1;
const face_top = 2;
const face_down = 3;
private $x, $y, $steps = 0;
private $direction;
private $facing;
function __construct($x, $y, $facing)
{
$this->x = $x;
$this->y = $y;
$this->facing = $facing;
}
public function getSteps()
{
return $this->steps;
}
public function getDirection()
{
return $this->direction;
}
public function isInMatris($matris)
{
return 0 <= $this->getX() && $this->getX() < $matris->getSize() &&
0 <= $this->getY() && $this->getY() < $matris->getSize();
}
public function getX()
{
return $this->x;
}
public function getY()
{
return $this->y;
}
public function step($matris)
{
$c = $matris->getCellColor($this->x, $this->y);
$oldX = $this->x;
$oldY = $this->y;
$newColor = 0;
if (0 === $c) {
$this->direction = $this->turnClockwise();
$newColor = 1;
} else {
$this->direction = $this->turnCounterClockwise();
$newColor = 0;
}
$matris->setCellColor($oldX, $oldY, $newColor);
$this->steps++;
}
private function turnClockwise()
{
if ($this->facing === Machine::face_right) {
$this->x += 1;
$this->facing = Machine::face_down;
} else if ($this->facing === Machine::face_down) {
$this->y -= 1;
$this->facing = Machine::face_left;
} else if ($this->facing === Machine::face_left) {
$this->x -= 1;
$this->facing = Machine::face_top;
} else if ($this->facing === Machine::face_top) {
$this->y += 1;
$this->facing = Machine::face_right;
}
return "right";
}
private function turnCounterClockwise()
{
if ($this->facing === Machine::face_right) {
$this->x -= 1;
$this->facing = Machine::face_top;
} else if ($this->facing === Machine::face_down) {
$this->y -= 1;
$this->facing = Machine::face_left;
} else if ($this->facing === Machine::face_left) {
$this->x += 1;
$this->facing = Machine::face_down;
} else if ($this->facing === Machine::face_top) {
$this->y += 1;
$this->facing = Machine::face_right;
}
return "left";
}
public function getStringResult($matris)
{
$fa = "";
switch ($this->facing) {
case $this->facing === 0:
$fa = "right";
break;
case $this->facing === 1:
$fa = "left";
break;
case $this->facing === 2:
$fa = "top";
break;
case $this->facing === 3:
$fa = "down";
break;
}
return "(" . $this->x . "," . $this->y . "," . $this->direction . "," . $matris->getCellColor($this->x, $this->y) . "," . $fa . ")";
}
}
Index.php
<?php
require 'Machine.php';
require 'Matris.php';
$matris = new Matris(150);
$ant = new Ant(50, 50, 0);
for ($i = 1; $i <= 100; $i++) {
if ($ant->getSteps() < 100 && $ant->isInMatris($matris)) {
//echo $ant->getStringResult($matris) . "<br/>";
$ant->step($matris);
} else {
// echo "Finish";
}
}
$matris->getCells();
但是在运行代码之后,我没有看到任何错误,但是结果如下图所示。
我需要使用OOP进行操作。
感谢您的任何建议。