Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rules for chess game template, see game of chess template issue #27 #29

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

sahild91
Copy link

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.

@wkta
Copy link
Member

wkta commented May 29, 2024

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 :)

@wkta
Copy link
Member

wkta commented May 29, 2024

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

@wkta wkta changed the title Updating the rules for chess. Fix rules for chess game template, see game of chess template issue #27 May 30, 2024
@wkta
Copy link
Member

wkta commented May 30, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants