pyved-engine
pyved-engine copied to clipboard
Fix rules for chess game template, see game of chess template issue #27
I have modified the code for the chess rules to fix the issue with the castling move. Please check and let me know if it is in line with your vision for the engine. Also, I did not update the version number in this change for the setup.py as I am not sure on how you handle the versioning for the package deployment.
Thank you very much for your suggestion!
It is a nice start you have written there.
Unfortunately, the code of that prototype is quite messy, game rules have been encoded in a rather bad manner. The fix you suggested could work in theory, but in practice because of how exsiting functions have been defined, the fix you suggested causes an infinite recursion loop, that makes the game crash.
You can see this phenomenon if you try to run the game with two A.I. s playing.
Error message after crash:
...
File "cartridge\chess_rules.py", line 321, in is_player_in_check
if ChessRules.is_legal_move(board, enemyColorFull, (row, col), kingTuple):
File "cartridge\chess_rules.py", line 193, in is_legal_move
return ChessRules.is_valid_king_move(board, pl_chesscolor, from_tuple, to_tuple)
File "cartridge\chess_rules.py", line 235, in is_valid_king_move
if not boardref.bK_moved and not ChessRules.is_player_in_check(boardref, chesscolor):
File "cartridge\chess_rules.py", line 321, in is_player_in_check
if ChessRules.is_legal_move(board, enemyColorFull, (row, col), kingTuple):
File "cartridge\chess_rules.py", line 193, in is_legal_move
return ChessRules.is_valid_king_move(board, pl_chesscolor, from_tuple, to_tuple)
File "cartridge\chess_rules.py", line 219, in is_valid_king_move
if not boardref.wK_moved and not ChessRules.is_player_in_check(boardref, chesscolor):
File "cartridge\chess_rules.py", line 321, in is_player_in_check
if ChessRules.is_legal_move(board, enemyColorFull, (row, col), kingTuple):
File "cartridge\chess_rules.py", line 193, in is_legal_move
return ChessRules.is_valid_king_move(board, pl_chesscolor, from_tuple, to_tuple)
File "cartridge\chess_rules.py", line 235, in is_valid_king_move
if not boardref.bK_moved and not ChessRules.is_player_in_check(boardref, chesscolor):
File "cartridge\chess_rules.py", line 321, in is_player_in_check
if ChessRules.is_legal_move(board, enemyColorFull, (row, col), kingTuple):
File "cartridge\chess_rules.py", line 193, in is_legal_move
return ChessRules.is_valid_king_move(board, pl_chesscolor, from_tuple, to_tuple)
File "cartridge\chess_rules.py", line 210, in is_valid_king_move
if boardref.square_has(to_sq, C_EMPTY_SQUARE) or boardref.square_ctrled_by(to_sq, enemy(chesscolor)):
File "cartridge\model.py", line 149, in square_ctrled_by
adhoc_sym = colorsym(chesscolor)
File "cartridge\model.py", line 76, in colorsym
if x == C_BLACK_PLAYER:
RecursionError: maximum recursion depth exceeded in comparison
To fix this requires quite a lot of refactoring, but once a few new functions are available, it will become feasible to rewrite rules properly without falling into the infinite recursion loop trap.
I've pushed a few commits to ease a final problem resolution :)
Information after files that still require modifications, after the small refactoring:
All in the file chess_rules.py
- line 386
- line 275
- line 98
comments that start with # TODO
pinpoint what lines require attention to find a final problem resolution
linked to issue #27 . Handy idea: to ensure the issue will be solved, it would be nice to have a way of testing a specific configuration of the chessboard and print out all valid moves computed by the game
Pull request was rejected❌ Proposed changes did introduce a critical bug (infinite recursion bug, see my previous comments. Note that this bug did not exist in the original version)
Now it seems that the dev who opened the pull request is not active anymore to suggest a better modification