function reachable_squares() { #Array of square where the piece can move to $squares = array(); $csq = $this->get_current_square(); $hdist = abs(Board::horz_distance("a1", $csq)); $vdist = abs(Board::vert_distance("a1", $csq)); $dist = $hdist > $vdist ? $vdist : $hdist; $sq = Board::add_horz_distance($csq, -$dist); $sq = Board::add_vert_distance($sq, -$dist); $squares = array_merge($squares, Board::squares_in_line($csq, $sq)); $hdist = abs(Board::horz_distance("h1", $csq)); $vdist = abs(Board::vert_distance("h1", $csq)); $dist = $hdist > $vdist ? $vdist : $hdist; $sq = Board::add_horz_distance($csq, $dist); $sq = Board::add_vert_distance($sq, -$dist); $squares = array_merge($squares, Board::squares_in_line($csq, $sq)); $hdist = abs(Board::horz_distance("a8", $csq)); $vdist = abs(Board::vert_distance("a8", $csq)); $dist = $hdist > $vdist ? $vdist : $hdist; $sq = Board::add_horz_distance($csq, -$dist); $sq = Board::add_vert_distance($sq, $dist); $squares = array_merge($squares, Board::squares_in_line($csq, $sq)); $hdist = abs(Board::horz_distance("h8", $csq)); $vdist = abs(Board::vert_distance("h8", $csq)); $dist = $hdist > $vdist ? $vdist : $hdist; $sq = Board::add_horz_distance($csq, $dist); $sq = Board::add_vert_distance($sq, $dist); $squares = array_merge($squares, Board::squares_in_line($csq, $sq)); #Removes current position square $squares = preg_grep("/^{$csq}\$/", $squares, PREG_GREP_INVERT); return $squares; }
function reachable_squares() { #Array of square where the piece can move to $squares = array(); $csq = $this->get_current_square(); $x = Board::horz_distance("a4", $csq); $y = Board::vert_distance("d1", $csq); $row_start = 'a' . ($y + 1); $row_end = 'h' . ($y + 1); $col_start = chr(ord('a') + $x) . '1'; $col_end = chr(ord('a') + $x) . '8'; $row = Board::squares_in_line($row_start, $row_end); $col = Board::squares_in_line($col_start, $col_end); $squares = array_merge($squares, $row, $col); #Removes current position square $squares = preg_grep("/^{$csq}\$/", $squares, PREG_GREP_INVERT); return $squares; }
/** * Takes two optional parameters containing optional names for the players. * If none are provided, the player names 'white' and 'black' are used. * Creates a new Board and places 16 Pieces per player and * initializes an empty MoveList. * @param string $p1 Player1's name * @param string $p2 Player2's name * @return Game */ function __construct($p1 = '', $p2 = '') { #player names $player1 = $p1 ? $p1 : 'white'; $player2 = $p2 ? $p2 : 'black'; #create the board content $this->board = new Board(); $this->pieces = array($player1 => array(), $player2 => array()); $this->captures = array($player1 => array(), $player2 => array()); $this->player_has_moves = array($player1 => array(1 => 1), $player2 => array(1 => 1)); $this->movelist = new MoveList($player1, $player2); $this->players = array($player1, $player2); #Add Rooks, Knights, Bishops and Queens to the board $this->add_pieces('Rook', array('a1', 'h1'), $player1); $this->add_pieces('Rook', array('a8', 'h8'), $player2); $this->add_pieces('Knight', array('b1', 'g1'), $player1); $this->add_pieces('Knight', array('b8', 'g8'), $player2); $this->add_pieces('Bishop', array('c1', 'f1'), $player1); $this->add_pieces('Bishop', array('c8', 'f8'), $player2); $this->add_pieces('Queen', array('d1'), $player1); $this->add_pieces('Queen', array('d8'), $player2); #add kings to the board $hash = $this->add_pieces('King', array('e1'), $player1); $this->kings[] = $this->pieces[$player1][$hash]; $hash = $this->add_pieces('King', array('e8'), $player2); $this->kings[] = $this->pieces[$player2][$hash]; #add pawns to the board $pawn_row = Board::squares_in_line("a2", "h2"); $this->add_pieces('Pawn', $pawn_row, $player1); $pawn_row = Board::squares_in_line("a7", "h7"); $this->add_pieces('Pawn', $pawn_row, $player2); return $this; }