I'm stuck with some Python script i found at https://python4kids.brendanscott.com/2014/12/02/hooking-up-the-sunfish-chess-engine-advanced/ : i followed the instructions of Brendan Scott and build the little Python script as he described, to get a TKinter GUI for sunfish.py, a nifty little chess application. But the code contains some bugs, although his article and explanations are very clear and well set up.
First, this gave a "KeyError" error :
def location_to_algebraic(board_location):
return "%s%s"%(ALGEBRAIC_DICT[7-board_location.j],8-board_location.i)
which i simply solved by:
def location_to_algebraic(board_location):
return "%s%s"%(ALGEBRAIC_DICT[math.ceil(7-board_location.j)],math.ceil(8-board_location.i))
Explanation : the point on the screen where the user clicked, somewhere on a chess board square, seems to give x,y float numbers while integers are expected, because they are the index of a dictionary. Just by rounding, using math.ceil(), we get the proper integer and it works as intended. Strange, it seems the author did not test the final script.
But another (simple?) bug in this script i can not solve :
move, score = sunfish.search(pos)
gives this error : AttributeError: module 'sunfish' has no attribute 'search'
It seems the search() function is not called properly, while it DOES exist in module 'sunfish' : in its Class 'Searcher'. So i tried to fix it by:
move, score = sunfish.Searcher.search(pos)
but then i get another error:
TypeError: search() missing 2 required positional arguments: 'pos' and 'secs'
The search() function is now called, but with to few arguments !? When i try to fix this by:
move, score = sunfish.Searcher.search(pos, secs=2)
i get another error:
TypeError: search() missing 1 required positional argument: 'pos'
I stuck now .. Here is the concerning search function, inside the sunfish.Searcher class, which is very simple:
def search(self, pos, secs):
start = time.time()
for _ in self._search(pos):
if time.time() - start > secs:
break
return self.tp_move.get(pos), self.tp_score.get((pos, self.depth, True)).lower
How can i call search() properly ?
The init of the Searcher class is like this :
class Searcher:
def __init__(self):
self.tp_score = LRUCache(TABLE_SIZE)
self.tp_move = LRUCache(TABLE_SIZE)
self.nodes = 0
__init__?move, score = sunfish.Searcher().search(pos, secs=2)won't work ?