so having read this a little deeper..here are my thoughts:

in chess960, the rooks might start right next to your king..therefore, it
is possible that you move your king only one square and it IS a legal
castling attempt, if that happens to be the column where your rook started.

so this check IS necessary and needed for chess960 behave properly.

regarding legality of the move...i believe the way this library works is
that as long as the king is moving to a legal square one space adjacent to
it, that is the only check necessary as later on in the code it tries to
see if any exceptions are thrown (or pear exceptions) and if so then it
reads the exception and at that point rolls back the move as being illegal
b/c you moved into check or whatever.

the way this library works is awkard because it tries to complete a move
and then if it runs into a pear exception it rolls the move back using a
makeshift transactional code approach. so it actually needs and relies on
those pear exceptions to behave correctly and find illegal chess moves.
this is why when we replaced the pear stuff with real php exceptions, the
whole library breaks and goes out of whack.


On Sat, Dec 21, 2013 at 10:50 AM, Jay Severson <jay.severson@gmail.com>wrote:

> playing with fire!
>
> this logic is obviously very tricky. what i do know is that u have to
> account for chess960 where the pieces can be anywhere.
>
> this is why you can either castle by moving your king to the final
> destination square OR by moving it to the rook location.
>
> if there was a bug in this logic, it would have been revealed by now in
> one of our millions of games, so i doubt it is buggy. if it is, then i
> challenge u to reproduce the bug on production
>
>
>
> On Saturday, December 21, 2013, Ben Curtis wrote:
>
>> In ChessGame.php:
>>
>> > @@ -3122,11 +3124,41 @@ function getPossibleKingMoves($square, $color = null, $returnCastleMoves = true)
>> >          $ret = $this->_getKingSquares($square);
>> >          if ($returnCastleMoves) {
>> >              $castleret = $this->_getCastleSquares($square);
>> > +            // verify castling is allowed to each square due to
>> > +            // check rules (cannot move the king out of, through, or into check)
>> > +            // and that no pieces exist between the rook and king
>> > +            // http://en.wikipedia.org/wiki/Castling#Requirements
>> > +            foreach ($castleret as $idx => $moveTo) {
>> > +                list($colFrom,$row) = str_split($square);
>> > +                list($colTo,$row) = str_split($moveTo);
>> > +
>> > +                // let's check for interfering pieces first, since that's less expensive
>> > +                // find all the columns that are between the king and the rook
>> > +                $betweenCols = array_slice(range($colFrom, $colTo > $colFrom ? 'g':'b'), 1);
>> > +                foreach($traverseCols as $col) {
>>
>> Cut/paste error — thanks!
>>
>> —
>> Reply to this email directly or view it on GitHub<https://github.com/ChessCom/Chess-Game/pull/15/files#r8518051>
>> .
>>
>