Ejemplo n.º 1
0
 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;
 }
Ejemplo n.º 2
0
 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;
 }
Ejemplo n.º 3
0
 /**
  * Takes two parameters containing the name of the square to move 
  * from and the name of the square to move to. They should be validated 
  * with "square_is_valid()" in Board prior to calling. Returns true 
  * if the provided move is legal within the context of the current game.
  * 
  * @param string $sq1 current position of the piece
  * @param string $sq2 destination of the piece
  * 
  * @return bool true if the move is legal or false otherwise
  */
 public function is_move_legal($sq1, $sq2)
 {
     #check squares are on the board
     if (Board::square_is_valid($sq1) == false) {
         return false;
     }
     if (Board::square_is_valid($sq2) == false) {
         return false;
     }
     $player1 = $this->players[0];
     $player2 = $this->players[1];
     $board = $this->board;
     $piece = $board->get_piece_at($sq1);
     #trying to move invisible piece
     if ($piece == NULL) {
         return -1;
     }
     $player = $piece->get_player();
     $movelist = $this->movelist;
     $last_moved = $movelist->get_last_moved();
     #check whose turn is
     if ($last_moved != NULL && $last_moved == $player || $last_moved == NULL && $player != $player1) {
         $this->message = "Not your turn";
         return false;
     }
     #square not reacheable for this piece
     if ($piece->can_reach($sq2) == false) {
         return false;
     }
     #if capturing anything
     $capture = $board->get_piece_at($sq2);
     if ($capture) {
         if ($capture->get_player() == $player) {
             $this->message = "You can't capture your own piece";
             return false;
         }
         #Pawn try to capture
         if ($piece instanceof Pawn) {
             if (abs(Board::horz_distance($sq1, $sq2)) != 1) {
                 $this->message = "Pawns may only capture diagonally";
                 return false;
             }
         } elseif ($piece instanceof King) {
             if (abs(Board::horz_distance($sq1, $sq2)) >= 2) {
                 $this->message = "You can't capture while castling";
                 return false;
             }
         }
     } else {
         #Check "en passant" capture
         if ($piece instanceof Pawn) {
             $ml = $piece->movelist;
             if (Board::horz_distance($sq1, $sq2) != 0 && $this->_is_valid_en_passant($piece, $sq1, $sq2) == false) {
                 $this->message = "Pawns must capture on a diagonal move";
                 return false;
             }
         }
     }
     $valid_castle = 0;
     $clone = clone $this;
     $king = $clone->kings[$player == $player1 ? 0 : 1];
     #Piece is a King
     if ($piece instanceof King) {
         $hdist = Board::horz_distance($sq1, $sq2);
         if (abs($hdist) == 2) {
             self::_mark_threatened_kings($clone);
             if ($king->threatened()) {
                 $this->message = "Can't castle out of check";
                 return false;
             }
             if ($hdist > 0) {
                 if ($this->_is_valid_short_castle($piece, $sq1, $sq2) == false) {
                     return false;
                 }
                 $valid_castle = self::__MOVE_CASTLE_SHORT__;
             } else {
                 if ($this->_is_valid_long_castle($piece, $sq1, $sq2) == false) {
                     return false;
                 }
                 $valid_castle = self::__MOVE_CASTLE_LONG__;
             }
         }
     } elseif (!$piece instanceof King) {
         $board_c = clone $board;
         $board_c->set_piece_at($sq1, NULL);
         $board_c->set_piece_at($sq2, NULL);
         if ($board_c->line_is_open($sq1, $sq2) == false) {
             $this->message = "Line '{$sq1}' - '{$sq2}' is blocked";
             return false;
         }
     }
     #move is not a castle
     if (!$valid_castle) {
         $clone->make_move($sq1, $sq2, false);
         self::_mark_threatened_kings($clone);
         if ($king->threatened()) {
             $this->message = "Move leaves your king in check";
             return false;
         }
     } else {
         #Short castle move
         if ($valid_castle == self::__MOVE_CASTLE_SHORT__) {
             $tsq = Board::square_right_of($sq1);
             $clone->make_move($sq1, $tsq, 0);
             self::_mark_threatened_kings($clone);
             if ($king->threatened()) {
                 $this->message = "Can't castle through check";
                 return false;
             }
             $clone->make_move($tsq, $sq2, 0);
             self::_mark_threatened_kings($clone);
             if ($king->threatened()) {
                 $this->message = "Move leaves your king in check";
                 return false;
             }
         } else {
             $tsq = Board::square_left_of($sq1);
             $clone->make_move($sq1, $tsq, 0);
             self::_mark_threatened_kings($clone);
             if ($king->threatened()) {
                 $this->message = "Can't castle through check";
                 return false;
             }
             $clone->make_move($tsq, $sq2, 0);
             self::_mark_threatened_kings($clone);
             if ($king->threatened()) {
                 die;
                 $this->message = "Move leaves your king in check";
                 return false;
             }
         }
     }
     $this->message = '';
     return true;
 }