а почему бы и не перечислить варианты? Вот Конь:
class Chess
{
public function isEmptyLocation($x, $y)
{
return true;
}
}
class Horse
{
public $currentX, $currentY;
@var
public $game;
public function __construct($x, $y, Chess $game)
{
$this->currentX = $x;
$this->currentY = $y;
$this->game = $game;
}
private function getSteps()
{
return array(
array(-1, -2),
array(1, -2),
array(2, -1),
array(2, 1),
array(1, 2),
array(-1, 2),
array(-2, 1),
array(-2, -1)
);
}
public function isAllow($x, $y = null)
{
if (is_array($x)) {
$y = $x[1];
$x = $x[0];
}
return $this->game->isEmptyLocation($this->currentX + $x, $this->currentY + $y);
}
public function getAllowedSteps()
{
$steps = array();
foreach ($this->getSteps() as $step) {
if ($this->isAllow($step)) {
$steps[] = $step;
}
}
return $steps;
}
public function move($x, $y = null)
{
if (is_array($x)) {
$y = $x[1];
$x = $x[0];
}
if (!$this->isAllow($x, $y)) {
return false;
}
$this->currentX += $x;
$this->currentY += $y;
return true;
}
public function randomMove()
{
$steps = $this->getAllowedSteps();
if ($steps) {
$i = rand(0, count($steps) - 1);
return $this->move($steps[$i]);
}
return false;
}
}
Использование, осторожно, искусственный интеллект
$horse = new Horse(2,0,$game);
$horse->randomMove();