我被一些python脚本困住了
//12/02/hooking-up-the-sunfish-chess-engine-advanced/
:我按照Brendan Scott的指示,按照他的描述构建了小Python脚本,以获得sunfish.py的TKinter GUI,这是一个漂亮的小棋类应用程序但是代码中包含了一些错误,尽管他的文章和解释非常清楚,并且设置得很好。
首先,这给出了一个“keyerror”错误:
def location_to_algebraic(board_location):
return "%s%s"%(ALGEBRAIC_DICT[7-board_location.j],8-board_location.i)
我简单地解决了这个问题:
def location_to_algebraic(board_location):
return "%s%s"%(ALGEBRAIC_DICT[math.ceil(7-board_location.j)],math.ceil(8-board_location.i))
说明:屏幕上用户点击的点,在棋盘格的某个地方,似乎给出x,y浮点数,而整数是预期的,因为它们是字典的索引。通过舍入,使用math.ceil(),我们就可以得到正确的整数,并且它可以按预期工作。奇怪的是,作者似乎没有测试最后的剧本。
但是另一个(简单的?)我无法解决此脚本中的错误:
move, score = sunfish.search(pos)
出现此错误:
attributeerror:模块'sunfish'没有'search'属性
SKECHO()函数似乎没有被正确调用,而它确实存在于模块“太阳鱼”中:在它的类“搜索器”中。所以我试着通过:
move, score = sunfish.Searcher.search(pos)
但后来我又犯了一个错误:
TypeError:search()缺少2个必需的位置参数:“pos”和“secs”
现在调用了search()函数,但参数很少!?当我试图通过以下方式解决这个问题时:
move, score = sunfish.Searcher.search(pos, secs=2)
我得到另一个错误:
typeerror:search()缺少1个必需的位置参数:“pos”
我现在卡住了。
下面是sunfish.Searcher类中的相关搜索函数,非常简单:
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
如何正确调用search()?
这个
初始
搜索者类的如下:
class Searcher:
def __init__(self):
self.tp_score = LRUCache(TABLE_SIZE)
self.tp_move = LRUCache(TABLE_SIZE)
self.nodes = 0