gpt4 book ai didi

python - 国际象棋引擎类型错误 : unhashable type: 'list'

转载 作者:太空宇宙 更新时间:2023-11-04 05:49:39 28 4
gpt4 key购买 nike

board = [["bRook", "bKnight", "bBishop", "bQueen", "bKing", "bBishop", "bKnight", "bRook"],
["bPawn", "bPawn", "bPawn", "bPawn", "bPawn", "bPawn", "bPawn", "bPawn"],
["", "", "", "", "", "", "", ""],
["", "", "", "", "", "", "", ""],
["", "", "", "", "", "", "", ""],
["", "", "", "", "", "", "", ""],
["wPawn", "wPawn", "wPawn", "wPawn", "wPawn", "wPawn", "wPawn", "wPawn"],
["wRook", "wKnight", "wBishop", "wQueen", "wKing", "wBishop", "wKnight", "wRook"]]


def evaluation():
#Material Score
materialScoreWhite = 0
materialScoreBlack = 0
#Main Material
pieceScores = {"Pawn": 100,
"Knight": 320,
"Bishop": 330,
"Rook": 500,
"Queen": 900,
"King": 20000}

for each in board:
for f in each:
if f[0] == "w":
materialScoreWhite += "w" + pieceScores[each[0:]]
elif f[0] == "b":
materialScoreBlack += "b" + pieceScores[each[0:]]

我正在尝试用 Python 制作一个简单的国际象棋引擎。列表 board 显示了程序试图评估的棋盘位置。我的部分评估涉及遍历棋盘上的每一 block ,并为该颜色的分数增加一定的值(value)。

例如,此时列表中的第一 block (起始位置)是黑车,因此我希望程序将500 添加到materialScoreBlack。但是,我不断收到此错误:

    materialScoreBlack += "b" + pieceScores[each[0:]]
TypeError: unhashable type: 'list'

我该如何解决这个问题?

最佳答案

您正在尝试使用 each[0:] 访问 pieceScores,每个都是数组的“切片”,只是一个较短的列表。您无法访问列表中的字典!我想你的意思是像

materialScoreBlack += pieceScores[f[1:]]

正确访问字典。另请注意,我将您从 0 开始的切片更改为从 1 开始的切片。这是为了删除您的板中条目开头的 w/b,以便可以访问字典。我没有成功

materialScoreBlack += "b" + pieceScores[f[1:]]

因为我不确定您要用“b”做什么——materialScoreBlack 看起来和听起来应该是一个数字,所以不要尝试向它添加字符串 ,即使 Python 允许你做到了

关于python - 国际象棋引擎类型错误 : unhashable type: 'list' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30867375/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com