Bug 430403 - A bug or a feature in castling?
Summary: A bug or a feature in castling?
Status: RESOLVED FIXED
Alias: None
Product: knights
Classification: Applications
Component: general (show other bugs)
Version: unspecified
Platform: Other Linux
: NOR normal
Target Milestone: ---
Assignee: Alexander Semke
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-12-14 23:01 UTC by DIMIT
Modified: 2022-04-15 08:36 UTC (History)
2 users (show)

See Also:
Latest Commit:
Version Fixed In: 22.08
Sentry Crash Report:


Attachments
A bug or a feature in castling? (1.32 MB, video/mp4)
2020-12-14 23:01 UTC, DIMIT
Details
test case O-O-O (203 bytes, application/vnd.chess-pgn)
2021-12-29 21:01 UTC, Dave Kaye
Details
test case O-O (256 bytes, application/vnd.chess-pgn)
2021-12-29 21:01 UTC, Dave Kaye
Details
Patch file Bug 430403 (888 bytes, text/plain)
2022-03-21 18:51 UTC, Dave Kaye
Details

Note You need to log in before you can comment on or make changes to this bug.
Description DIMIT 2020-12-14 23:01:14 UTC
Created attachment 134081 [details]
A bug or a feature in castling?

SUMMARY


STEPS TO REPRODUCE
1. 
2. 
3. 

OBSERVED RESULT


EXPECTED RESULT


SOFTWARE/OS VERSIONS
Windows: 
macOS: 
Linux/KDE Plasma: ArcoLInux
(available in About System)
KDE Plasma Version: 
KDE Frameworks Version: 
Qt Version: 

ADDITIONAL INFORMATION
Comment 1 Dave Kaye 2021-12-29 21:01:12 UTC
Created attachment 144945 [details]
test case O-O-O
Comment 2 Dave Kaye 2021-12-29 21:01:55 UTC
Created attachment 144946 [details]
test case O-O
Comment 3 Dave Kaye 2021-12-29 21:04:18 UTC
This is a bug in the castling code for Knights.  It is reproducible for both king side and queen side castling.

The rules for castling are:
* king has not moved
* rook has not moved
* no pieces between the king and rook
* king is not in check
* king does not cross over a square that is attacked by the opponent's pieces
* king does not end the move on a square attacked by the opponent's pieces

All of these rules are passed in the attached game.  The unusual situation is that the rook was captured - but the rook never "moved" so it passed the rules tests.  A human would recognize the piece was not a rook and therefore not castle.  An additional rule must be formulated specifically for a computer to test if the piece is a rook.  It would reside in Knights castling routines.

Attached are game files that can be used to test the code.  The NEXT move in the game will be the castling move, and will demonstrate the error.
Comment 4 Dave Kaye 2022-03-21 18:51:29 UTC
Created attachment 147653 [details]
Patch file Bug 430403

Attached is a patch to fix this bug.

The bug allows the castling rook to be captured by either a knight or bishop.  If the rook has not moved, then the present code assumes the position is occupied by a rook and will castle with the piece that is in that position.  The present code will not allow castling if the capturing piece is a rook or a queen, since these will immediately put the king into check and prevent castling.

The patch is a simple check to see if the rook's position actually contains a rook.  The color could also be checked, but this is not necessary.  If the location is occupied by an opponent's rook (different color) it will not allow castling due to that rook putting the king into check.
Comment 5 Alexander Semke 2022-04-15 08:36:59 UTC
Git commit ce7a9b46041295c61f9fbd07724e5c739ef8a923 by Alexander Semke, on behalf of David Kaye.
Committed on 15/04/2022 at 08:35.
Pushed by asemke into branch 'master'.

Don't allow to perform castling if the non-moved rook was already
captured and its position is occupied by another piece.
FIXED-IN: 22.08

M  +2    -2    src/rules/chessrules.cpp

https://invent.kde.org/games/knights/commit/ce7a9b46041295c61f9fbd07724e5c739ef8a923