Code journal #3 - First eval function
Today I have been struggling with the writing of the evaluation function. This function is a first approximation that assigns a punctuation to a specific board, given only the amount of alive pieces of the enemy and its value. The use of this function implies that each player will always pick the move which leads to their own material advantage, i.e., each player will always capture the highest ranked piece possible.
Other than that, when several moves share the same evaluation and therefore lead to the same material advantage, the final move is picked randomly. This is the code of the function:
The variable pieces is a list of tuples, containing the piece and its value according to the most accepted theory:
pieces = [(chess.PAWN, 1), (chess.KNIGHT, 3), (chess.BISHOP, 3), (chess.ROOK, 5), (chess.QUEEN, 9), (chess.KING, 0)]
Reminder:
- A list is defined by brackets:
a_list = [1,2]
- A tuple is defined by parenthesis:
a_tuple = (1,2)
The most important difference between them being that lists are mutable, while tuples are not.
But going back to the function, I had a hard time with chess.BaseBoard.pieces(). As commented in the image, the function has to calculate the value of the pieces of the enemy, which is the contrary of the player who makes the move. That would make the parameter color to be:
color=not(board.turn)
However, to evaluate the board I have made a move on the board (later on I undo it), so the turn has also changed and that is another negation:
color=not(not(board.turn))
It took me almost an hour of print debugging to figure out why the algorithm wasn't calculating the correct evaluation, and it was because of that negation missing.
Finally though, it all worked out and the program produced an acceptable outcome. Yesterday, both players were two blind people hitting each other unknowingly, resulting in the death of one of them or in draw after hundreds of movements. Today, the players are crazy psychopaths that kill whenever they can, leading to the fight ending faster:
This is one final board example, with a beautiful checkmate in only 41 moves, but all the simulations I have made do not go beyond the 100-150 moves, contrasting with the 400+ moves of yesterday.
Aaand that's all for today! Tomorrow I might want to spend some time working on the representation of SVGs outside notebooks, looking for new libraries, since debugging is hell in this environment (or I don't know how to do it).
Congratulations @neutra! You received a personal award!
Click here to view your Board
Congratulations @neutra! You received a personal award!
You can view your badges on your Steem Board and compare to others on the Steem Ranking
Vote for @Steemitboard as a witness to get one more award and increased upvotes!