public function createPieces(Game $game)
 {
     $pieces = array();
     $player = $game->getPlayer('white');
     // Bishop on black square
     $file = 2 * mt_rand(1, 4) - 1;
     $pieces[$file] = $this->createPiece('Bishop', $file, 1);
     // Bishop on white square
     $file = 2 * mt_rand(1, 4);
     $pieces[$file] = $this->createPiece('Bishop', $file, 1);
     // Queen and Knights
     foreach (array(6 => 'Queen', 5 => 'Knight', 4 => 'Knight') as $rand => $class) {
         $file = $this->getEmptyFile($pieces, mt_rand(1, $rand));
         $pieces[$file] = $this->createPiece($class, $file, 1);
     }
     // Rooks and King
     foreach (array('Rook', 'King', 'Rook') as $class) {
         $file = $this->getEmptyFile($pieces, 1);
         $pieces[$file] = $this->createPiece($class, $file, 1);
     }
     // Pawns
     for ($it = 1; $it <= 8; $it++) {
         $pieces[] = $this->createPiece('Pawn', $it, 2);
     }
     $pieces = array_values($pieces);
     $player->setPieces($pieces);
     $player->getOpponent()->setPieces($this->mirrorPieces($pieces));
     $game->ensureDependencies();
     $game->setInitialFen(Forsyth::export($game));
 }
示例#2
0
 /**
  * Create and position pieces of the game for the forsyth string
  *
  * @param Game $game
  * @param string $forsyth
  * @return Game $game
  */
 public static function import(Game $game, $forsyth)
 {
     static $classes = array('p' => 'Pawn', 'r' => 'Rook', 'n' => 'Knight', 'b' => 'Bishop', 'q' => 'Queen', 'k' => 'King');
     $x = 1;
     $y = 8;
     $board = $game->getBoard();
     $forsyth = str_replace('/', '', preg_replace('#\\s*([\\w\\d/]+)\\s.+#i', '$1', $forsyth));
     $pieces = array('white' => array(), 'black' => array());
     for ($itForsyth = 0, $forsythLen = strlen($forsyth); $itForsyth < $forsythLen; $itForsyth++) {
         $letter = $forsyth[$itForsyth];
         if (is_numeric($letter)) {
             $x += intval($letter);
         } else {
             $color = ctype_lower($letter) ? 'black' : 'white';
             $pieces[$color][] = new Piece($x, $y, $classes[strtolower($letter)]);
             ++$x;
         }
         if ($x > 8) {
             $x = 1;
             --$y;
         }
     }
     foreach ($game->getPlayers() as $player) {
         $player->setPieces($pieces[$player->getColor()]);
     }
     $game->ensureDependencies();
 }