marcusbuffett / command-line-chess

A python program to play chess against an AI in the terminal.
MIT License
500 stars 162 forks source link

Fix problems of promotions #13

Closed mathjudo closed 2 years ago

mathjudo commented 6 years ago

Hello everyone, There were some problems of promotion when pawns had to be promoted when they took. Sorry I don't find how to open correctly a pull request (with correct comparaisons) so I jus put modified files here. Changes are in methods undoLastMove and makeMove of Board and in line 74 of Pawn Have a nice day

ddugovic commented 6 years ago

Can you comment in this comment section about how to duplicate the problem and how to fix it?

mathjudo commented 6 years ago

An error is raised when a Pawn can take another piece in the last line ( line 8 for whites, 1 for blacks ) because it have to be promoted in the same time. Indeed the possibility of promotion when a pawn takes another piece had an error. To fix it we have to modify methods undoLastMove and makeMove of Board.py and to modify the move yielded by the Pawn when it takes on the last line. like in files I uploaded above. Sorry for my English, but if you need more precisions don't hesitate to ask me

mathjudo commented 6 years ago

For instance :

8  R . . . . . . .  
7  . p . . K . . .  
6  . . . . . . . .  
5  . . . . . . . .  
4  . . . . . . . .  
3  . . . . . . K .  
2  . . . . . . . .  
1  . . . . . . . .  

   a b c d e f g h

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/mathjudo/Downloads/command-line-chess-master-2/build/lib/chess/main.py", line 139, in main
    startGame(board, playerSide, opponentAI)
  File "/Users/mathjudo/Downloads/command-line-chess-master-2/build/lib/chess/main.py", line 88, in startGame
    if board.isCheckmate():
  File "/Users/mathjudo/Downloads/command-line-chess-master-2/build/lib/chess/Board.py", line 114, in isCheckmate
    if len(self.getAllMovesLegal(self.currentSide)) == 0:
  File "/Users/mathjudo/Downloads/command-line-chess-master-2/build/lib/chess/Board.py", line 400, in getAllMovesLegal
    if self.moveIsLegal(move):
  File "/Users/mathjudo/Downloads/command-line-chess-master-2/build/lib/chess/Board.py", line 392, in moveIsLegal
    self.undoLastMove()
  File "/Users/mathjudo/Downloads/command-line-chess-master-2/build/lib/chess/Board.py", line 91, in undoLastMove
    self.pieces.remove(promotedPiece)
  File "/Users/mathjudo/Downloads/command-line-chess-master-2/build/lib/chess/Piece.py", line 40, in __eq__
    if self.board == other.board and \
AttributeError: 'NoneType' object has no attribute 'board'
ddugovic commented 6 years ago

@mathjudo I attempted the same change db9d95ba9bd40c0c53db9bec6c35046ef3286d30 you made, but it doesn't work (White pawn on a7)?

8  R N B Q K B N R
7  P P P P P P P P
6  
5  
4  
3  
2  P P P P P P P P
1  R N B Q K B N R

   a b c d e f g h

Traceback (most recent call last):
  File "src/main.py", line 130, in <module>
    startGame(board, playerSide, opponentAI)
  File "src/main.py", line 78, in startGame
    if board.isCheckmate():
  File "/home/lila/command-line-chess/src/Board.py", line 132, in isCheckmate
    if len(self.getAllMovesLegal(self.currentSide)) == 0:
  File "/home/lila/command-line-chess/src/Board.py", line 458, in getAllMovesLegal
    if self.moveIsLegal(move):
  File "/home/lila/command-line-chess/src/Board.py", line 450, in moveIsLegal
    self.undoLastMove()
  File "/home/lila/command-line-chess/src/Board.py", line 103, in undoLastMove
    self.pieces.remove(promotedPiece)
  File "/home/lila/command-line-chess/src/Piece.py", line 41, in __eq__
    if self.board == other.board and \
AttributeError: 'NoneType' object has no attribute 'board'
mathjudo commented 6 years ago

Did you also modified the Pawn.py file as I sent ? (Replace line 74 move = Move(self, newPosition) by move = Move(self, newPosition, pieceToCapture=pieceToTake) and why did you modifie the method makeStringRep ? (line 183)

ddugovic commented 6 years ago

Thanks, I missed that line 74.

I modified makeStringRep to revert a previous formatting change which caused the 8th rank to display without left-padding spaces.

ddugovic commented 6 years ago

@mathjudo Please try latest master and advise if you're still observing this issue. Thanks!

marcusbuffett commented 4 years ago

Coming in just over a year late on this one! This diff looks odd though, @mathjudo should this go in or no? I haven't really been paying much attention to this project, as you can tell

ClasherKasten commented 2 years ago

Closed for the following reasons:

If anyone still has problems with this, please leave a comment so the community can discuss about it.