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:]]
I am trying to make a simple chess engine in Python. The list board
shows how the board is in the position the program is trying to evaluate. Part of my evaluation involves going through each piece on the board and adding a certain value to the score that colour has.
For example, the first piece in the list at the moment (the starting position) is a black rook so I want the program to add 500
to materialScoreBlack
. However, I keep getting this error:
materialScoreBlack += "b" + pieceScores[each[0:]]
TypeError: unhashable type: 'list'
How can I fix this?
You're trying to access pieceScores with
each[0:]
, which is a "slice" of the array each, which is just a shorter list. You can't access a dictionary at a list! I think what you meant to do is something likeWhich accesses the dictionary correctly. Also note that I changed your slice starting at 0 to a slice starting at 1. This is to remove the w/b at the beginning of the entries in your board, so that the dictionary can be accessed. I didn't make it
because I wasn't sure what you were trying to do with the "b" -- materialScoreBlack looks and sounds like it should be a number, so don't try to add strings to it
even if Python lets you do it.