Move with no figure is possible
Closed this issue · 4 comments
Description
Sometimes when I run my agent, I got an error that on after checking if the move is a capture the moving figure doesnt match the figure names.
After further investiagation I found out that when my Quiesce function do recursivly moves without checking if the game is over, you can create a move with '--' as playing figure.
Code Changes:
With the addition in ChessEngine.py:800
if(self.pieceMoved == "--"): raise ValueError('moving a piece that is not on the board')
Error Output
I got the following console output:
Process Process-1:
Traceback (most recent call last):
File "C:\Python39\lib\multiprocessing\process.py", line 315, in _bootstrap
self.run()
File "C:\Python39\lib\multiprocessing\process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "C:\Projekte\chess_ai\student_agents\template.py", line 107, in findBestMove
score = self.alphaBeta(copyGS, self.currentDepth, -float('inf'), float('inf'), True)
File "C:\Projekte\chess_ai\student_agents\template.py", line 284, in alphaBeta
score = self.alphaBeta(copyGS, depth - 1, alpha, beta, False)
File "C:\Projekte\chess_ai\student_agents\template.py", line 319, in alphaBeta
score = self.alphaBeta(copyGS, depth - 1, alpha, beta, True)
File "C:\Projekte\chess_ai\student_agents\template.py", line 264, in alphaBeta
return self.Quiesce(gs,alpha,beta,depth)
File "C:\Projekte\chess_ai\student_agents\template.py", line 388, in Quiesce
score = -self.Quiesce(gs, -beta, -alpha ,depth)
File "C:\Projekte\chess_ai\student_agents\template.py", line 388, in Quiesce
score = -self.Quiesce(gs, -beta, -alpha ,depth)
File "C:\Projekte\chess_ai\student_agents\template.py", line 388, in Quiesce
score = -self.Quiesce(gs, -beta, -alpha ,depth)
[Previous line repeated 1 more time]
File "C:\Projekte\chess_ai\student_agents\template.py", line 384, in Quiesce
validMoves = gs.getValidMoves()
File "C:\Projekte\chess_ai\ChessEngine.py", line 300, in getValidMoves
self.getKingMoves(kingRow, kingCol, moves)
File "C:\Projekte\chess_ai\ChessEngine.py", line 688, in getKingMoves
moves.append(Move((r, c), (endRow, endCol), self.board))
File "C:\Projekte\chess_ai\ChessEngine.py", line 800, in __init__
raise ValueError('moving a piece that is not on the board')
ValueError: moving a piece that is not on the board
There you can see that when you execute gs.getValidMoves()
on a Board without a king it returns a move where '--' is the moving Figure
I tried to reproduce this in the initial position without white's king and white's king's pawn; gs.getValidMoves() still returned no move with '--' as piece in this case. Therefore, I can't really be sure where this comes from.
However, GameState uses self.white/blackKingLocation for its getKingMoves-method and I don't know how this attribute is handled in your case.
Also, I'm a little puzzled on how you arrive at a position without a king; when you just call getValidMoves for your move generation it should be impossible to capture the opponents king or is it? If you create your own moves then anything can happen.
I think the problem was that I didnt check if I won in my Quiesce function, so there it try to moves the king where no king is anymore.
But I think a check in the Engine that there is no move possible with '--' would be a good addition
If it's okay I would like to open a pull request
yes, I agree. please open a PR :)
unfortunately the raising of this ValueError lead to some other bugs with the GUI, therefore I had to take it out again. However, I added some lines that check where king and rooks actually are in the constructor of GameState (in commit 2ce0824
) and I think these solve all thrown errors that we discussed in this thread.
If you still find some or see a way to include your changes again without causing other problems, I am open to hear it.