public function move(Game $game, $level) { $analyser = new Analyser($game->getBoard()); $moveTree = $analyser->getPlayerPossibleMoves($game->getTurnPlayer()); // choose random piece do { $from = array_rand($moveTree); } while (empty($moveTree[$from])); // choose random move $to = $moveTree[$from][array_rand($moveTree[$from])]; return $from . ' ' . $to; }
/** * @depends testGameCreation */ public function testFilterNotClass(Entities\Game $game) { $piece1 = $game->getBoard()->getPieceByPos(1, 1); $piece2 = $game->getBoard()->getPieceByPos(2, 1); $this->assertSame(array($piece2), PieceFilter::filterNotClass(array($piece1, $piece2), 'Rook')); }
public function diffToMove(Game $game, $forsythe) { $moves = array('from' => array(), 'to' => array()); $x = 1; $y = 8; $board = $game->getBoard(); $forsythe = str_replace('/', '', preg_replace('#\\s*([\\w\\d/]+)\\s.+#i', '$1', $forsythe)); for ($itForsythe = 0, $forsytheLen = strlen($forsythe); $itForsythe < $forsytheLen; $itForsythe++) { $letter = $forsythe[$itForsythe]; $key = Board::posToKey($x, $y); if (is_numeric($letter)) { for ($x = $x, $max = $x + intval($letter); $x < $max; $x++) { $_key = Board::posToKey($x, $y); if (!$board->getSquareByKey($_key)->isEmpty()) { $moves['from'][] = $_key; } } } else { $color = ctype_lower($letter) ? 'black' : 'white'; switch (strtolower($letter)) { case 'p': $class = 'Pawn'; break; case 'r': $class = 'Rook'; break; case 'n': $class = 'Knight'; break; case 'b': $class = 'Bishop'; break; case 'q': $class = 'Queen'; break; case 'k': $class = 'King'; break; } if ($piece = $board->getPieceByKey($key)) { if ($class != $piece->getClass() || $color !== $piece->getColor()) { $moves['to'][] = $key; } } else { $moves['to'][] = $key; } ++$x; } if ($x > 8) { $x = 1; --$y; } } if (empty($moves['from'])) { return null; } // two pieces moved: it's a castle if (2 === count($moves['from'])) { if ($board->getPieceByKey($moves['from'][0])->isClass('King')) { $from = $moves['from'][0]; } else { $from = $moves['from'][1]; } if (in_array($board->getSquareByKey($moves['to'][0])->getX(), array(3, 7))) { $to = $moves['to'][0]; } else { $to = $moves['to'][1]; } } else { $from = $moves['from'][0]; $to = $moves['to'][0]; } return $from . ' ' . $to; }