Chess Engine TypeError: unhashable type: 'list'

134 views Asked by At
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?

2

There are 2 answers

2
Curmudgeon On

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 like

materialScoreBlack += pieceScores[f[1:]]

Which 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

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

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.

0
Dportology On

not sure what you're trying to do with the line

materialScoreWhite += "w" + pieceScores[each[0:]]

adding "w" or "b" doesn't seem to make sense to me, and you can't access your dictionary like this. Just tried the code, starting at second element in f and it works fine

def evaluation():

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 len(f) > 0:
            if f[0] == "w":
                materialScoreWhite += pieceScores[f[1:]]
            elif f[0] == "b":
                materialScoreBlack += pieceScores[f[1:]]

print materialScoreBlack
print materialScoreWhite