function reachable_squares() { #Array of square where the piece can move to $squares = array(); $csq = $this->get_current_square(); $tsq = Board::add_vert_distance($csq, 2); if ($tsq) { $sq = Board::square_right_of($tsq); if ($sq) { $squares[] = $sq; } $sq = Board::square_left_of($tsq); if ($sq) { $squares[] = $sq; } } $tsq = Board::add_vert_distance($csq, -2); if ($tsq) { $sq = Board::square_right_of($tsq); if ($sq) { $squares[] = $sq; } $sq = Board::square_left_of($tsq); if ($sq) { $squares[] = $sq; } } $tsq = Board::add_horz_distance($csq, 2); if ($tsq) { $sq = Board::square_up_from($tsq); if ($sq) { $squares[] = $sq; } $sq = Board::square_down_from($tsq); if ($sq) { $squares[] = $sq; } } $tsq = Board::add_horz_distance($csq, -2); if ($tsq) { $sq = Board::square_up_from($tsq); if ($sq) { $squares[] = $sq; } $sq = Board::square_down_from($tsq); if ($sq) { $squares[] = $sq; } } return $squares; }
function reachable_squares() { #Array of square where the piece can move to $squares = array(); $csq = $this->get_current_square(); $sq1 = Board::square_left_of($csq); if ($sq1) { $squares[] = $sq1; $sq2 = Board::square_up_from($sq1); if (defined($sq2)) { $squares[] = $sq2; } $sq2 = Board::square_down_from($sq1); if (defined($sq2)) { $squares[] = $sq2; } } $sq1 = Board::square_right_of($csq); if ($sq1) { $squares[] = $sq1; $sq2 = Board::square_up_from($sq1); if ($sq2) { $squares[] = $sq2; } $sq2 = Board::square_down_from($sq1); if ($sq2) { $squares[] = $sq2; } } $sq1 = Board::square_up_from($csq); if ($sq1) { $squares[] = $sq1; } $sq1 = Board::square_down_from($csq); if ($sq1) { $squares[] = $sq1; } $sq1 = Board::add_horz_distance($csq, 2); if ($sq1 && !$this->moved()) { $squares[] = $sq1; } $sq1 = Board::add_horz_distance($csq, -2); if ($sq1 && !$this->moved()) { $squares[] = $sq1; } return $squares; }
function reachable_squares() { #Array of square where the piece can move to $squares = array(); $color = $this->get_player(); $csq = $this->get_current_square(); if ($color == 'white') { if ($csq) { $tsq1 = Board::square_up_from($csq); } } else { if ($csq) { $tsq1 = Board::square_down_from($csq); } } if (isset($tsq1)) { $squares[] = $tsq1; } if ($color == 'white') { if ($tsq1) { $tsq2 = Board::square_up_from($tsq1); } } else { if ($tsq1) { $tsq2 = Board::square_down_from($tsq1); } } if (!$this->moved() and $tsq2) { $squares[] = $tsq2; } if ($tsq1) { $tsq2 = Board::square_left_of($tsq1); } if ($tsq2) { $squares[] = $tsq2; } if ($tsq1) { $tsq2 = Board::square_right_of($tsq1); } if ($tsq2) { $squares[] = $tsq2; } return $squares; }
/** * Returns MoveListEntry representing the * last move made, and sets the state of the game to what it was prior to the * returned move being made. * * @return MoveListEntry the last MoveListEntry object of the move that has been taken back */ public function take_back_move() { $movelist = $this->movelist; $board = $this->board; $curr_player = $movelist->get_last_moved(); $player1 = $this->players[0]; $move = $movelist->delete_move(); #There is a existing last move if ($move) { $movenum = $move->get_move_num(); $piece = $move->get_piece(); $player = $piece->get_player(); $ssq = $move->get_start_square(); $dsq = $move->get_dest_square(); #If last move was a promotion if ($move->is_promotion()) { //delete the prometed piece $hash = $move->get_promoted_hash(); unset($this->pieces[$piece->get_player()][$hash]); } #If last move was a capture if ($move->is_capture()) { $capture = $this->captures[$player][$movenum]; if ($move->is_en_passant()) { if ($player == $player1) { $dsq = Board::square_down_from($dsq); } else { $dsq = Board::square_up_from($dsq); } } $board->set_piece_at($dsq, $capture); $capture->set_current_square($dsq); $capture->set_captured(0); $board->set_piece_at($ssq, $piece); $piece->set_current_square($ssq); if ($piece->_firstmoved() == $movenum) { $piece->set_moved(0); } } elseif ($move->is_short_castle()) { $king_sq = $player == $player1 ? "e1" : "e8"; $rook_sq = $player == $player1 ? "h1" : "h8"; $king_curr_sq = $player == $player1 ? "g1" : "g8"; $rook_curr_sq = $player == $player1 ? "f1" : "f8"; $rook = $board->get_piece_at($rook_curr_sq); $board->set_piece_at($king_curr_sq, NULL); $board->set_piece_at($rook_curr_sq, NULL); $board->set_piece_at($king_sq, $piece); $board->set_piece_at($rook_sq, $rook); $rook->set_current_square($rook_sq); $piece->set_current_square($king_sq); $rook->set_moved(0); $piece->set_moved(0); } elseif ($move->is_long_castle()) { $king_sq = $player == $player1 ? "e1" : "e8"; $rook_sq = $player == $player1 ? "a1" : "a8"; $king_curr_sq = $player == $player1 ? "c1" : "c8"; $rook_curr_sq = $player == $player1 ? "d1" : "d8"; $rook = $board->get_piece_at($rook_curr_sq); $board->set_piece_at($king_curr_sq, NULL); $board->set_piece_at($rook_curr_sq, NULL); $board->set_piece_at($king_sq, $piece); $board->set_piece_at($rook_sq, $rook); $rook->set_current_square($rook_sq); $piece->set_current_square($king_sq); $rook->set_moved(0); $piece->set_moved(0); } else { $board->set_piece_at($dsq, NULL); $board->set_piece_at($ssq, $piece); $piece->set_current_square($ssq); if ($piece->_firstmoved() == $movenum) { $piece->set_moved(0); } } #delete last unset($this->player_has_moves[$player][$movenum]); } return $move; }