So I am making a chess engine in nim and I am using python-chess module through nimpy and I get the following error in my code please help me if you can...
import nimpy
import std/algorithm
import options
import sequtils, strutils
import std/tables
let chess = pyImport("chess")
let py = pyBuiltinsModule()
let none = py.None
type
piece = enum
Pawn = 1, Rook = 2, Knight = 3, Bishop = 4, Queen = 5, King = 6
const
pieceValue = [
Pawn: 100,
Rook: 500,
Knight: 320,
Bishop: 330,
Queen: 900,
King: 20000
]
var
pawnEvalWhite = @[
0, 0, 0, 0, 0, 0, 0, 0,
5, 10, 10, -20, -20, 10, 10, 5,
5, -5, -10, 0, 0, -10, -5, 5,
0, 0, 0, 20, 20, 0, 0, 0,
5, 5, 10, 25, 25, 10, 5, 5,
10, 10, 20, 30, 30, 20, 10, 10,
50, 50, 50, 50, 50, 50, 50, 50,
0, 0, 0, 0, 0, 0, 0, 0]
var
pawnEvalBlack: seq[int] = pawnEvalWhite.reversed().toSeq
var knightEval = @[
-50, -40, -30, -30, -30, -30, -40, -50,
-40, -20, 0, 0, 0, 0, -20, -40,
-30, 0, 10, 15, 15, 10, 0, -30,
-30, 5, 15, 20, 20, 15, 5, -30,
-30, 0, 15, 20, 20, 15, 0, -30,
-30, 5, 10, 15, 15, 10, 5, -30,
-40, -20, 0, 5, 5, 0, -20, -40,
-50, -40, -30, -30, -30, -30, -40, -50]
var
bishopEvalWhite = @[
-20, -10, -10, -10, -10, -10, -10, -20,
-10, 5, 0, 0, 0, 0, 5, -10,
-10, 10, 10, 10, 10, 10, 10, -10,
-10, 0, 10, 10, 10, 10, 0, -10,
-10, 5, 5, 10, 10, 5, 5, -10,
-10, 0, 5, 10, 10, 5, 0, -10,
-10, 0, 0, 0, 0, 0, 0, -10,
-20, -10, -10, -10, -10, -10, -10, -20]
var
bishopEvalBlack: seq[int] = bishopEvalWhite.reversed().toSeq
var
rookEvalWhite = @[
0, 0, 0, 5, 5, 0, 0, 0,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
5, 10, 10, 10, 10, 10, 10, 5,
0, 0, 0, 0, 0, 0, 0, 0]
var
rookEvalBlack: seq[int] = rookEvalWhite.reversed().toSeq
var
queenEval = @[
-20, -10, -10, -5, -5, -10, -10, -20,
-10, 0, 0, 0, 0, 0, 0, -10,
-10, 0, 5, 5, 5, 5, 0, -10,
-5, 0, 5, 5, 5, 5, 0, -5,
0, 0, 5, 5, 5, 5, 0, -5,
-10, 5, 5, 5, 5, 5, 0, -10,
-10, 0, 5, 0, 0, 0, 0, -10,
-20, -10, -10, -5, -5, -10, -10, -20]
var
kingEvalWhite = @[
20, 30, 10, 0, 0, 10, 30, 20,
20, 20, 0, 0, 0, 0, 20, 20,
-10, -20, -20, -20, -20, -20, -20, -10,
20, -30, -30, -40, -40, -30, -30, -20,
-30, -40, -40, -50, -50, -40, -40, -30,
-30, -40, -40, -50, -50, -40, -40, -30,
-30, -40, -40, -50, -50, -40, -40, -30,
-30, -40, -40, -50, -50, -40, -40, -30]
var
kingEvalBlack: seq[int] = kingEvalWhite.reversed().toSeq
var
kingEvalEndGameWhite = @[
50, -30, -30, -30, -30, -30, -30, -50,
-30, -30, 0, 0, 0, 0, -30, -30,
-30, -10, 20, 30, 30, 20, -10, -30,
-30, -10, 30, 40, 40, 30, -10, -30,
-30, -10, 30, 40, 40, 30, -10, -30,
-30, -10, 20, 30, 30, 20, -10, -30,
-30, -20, -10, 0, 0, -10, -20, -30,
-50, -40, -30, -20, -20, -30, -40, -50]
var
kingEvalEndGameBlack: seq[int] = kingEvalEndGameWhite.reversed().toSeq
proc evaluatePiece(piece= chess.Piece, square= chess.Square, endGame: bool): int =
let pieceType = piece.pieceType
var mapping: seq[int]
if pieceType == chess.PAWN:
mapping = if piece.color == chess.WHITE: pawnEvalWhite else: pawnEvalBlack
if pieceType == chess.KNIGHT:
mapping = knightEval
if pieceType == chess.BISHOP:
mapping = if piece.color == chess.WHITE: bishopEvalWhite else: bishopEvalBlack
if pieceType == chess.ROOK:
mapping = if piece.color == chess.WHITE: rookEvalWhite else: rookEvalBlack
if pieceType == chess.QUEEN:
mapping = queenEval
if pieceType == chess.KING:
# use end game piece-square tables if neither side has a queen
if endGame:
mapping = if piece.color == chess.WHITE: kingEvalEndGameWhite else: kingEvalEndGameBlack
else:
mapping = if piece.color == chess.WHITE: kingEvalWhite else: kingEvalBlack
return mapping[square.to(int)]
proc evaluateCapture(board= chess.Board, move= chess.Move): float =
if board.is_en_passant(move).to(bool):
return float(pieceValue[Pawn])
let dest = board.piece_at(move.to_square) # We know that dest & src are of type PyObject
let src = board.piece_at(move.from_square)
if dest.isNone().to(bool) or src.isNone().to(bool):
echo("Pieces were expected at both " & $move.to_square & " and " & $move.from_square )
return float(pieceValue[dest.piece_type.to(int)] - pieceValue[src.piece_type.to(int)])
proc moveValue(board= chess.Board, move= chess.Move, endgame: bool): float =
if move.promotion != none:
return if board.turn == chess.BLACK: float(NegInf) else: float(Inf)
let piece = board.piece_at(move.from_square)
if piece != none:
let fromValue = float(evaluatePiece(piece.get, move.from_square, endgame))
let toValue = float(evaluatePiece(piece.get, move.to_square, endgame))
let positionChange = toValue - fromValue
else:
echo("A piece was expected at ", $move.from_square)
var captureValue = 0.0
if board.is_capture(move).to(bool):
captureValue = evaluateCapture(board, move)
var currentMoveValue = captureValue + positionChange
if board.turn == chess.BLACK:
currentMoveValue = -currentMoveValue
return currentMoveValue
proc evaluateBoard(board= chess.Board): float =
var total = 0.0
var endGame = checkEndGame(board)
for square in chess.SQUARES:
if let piece = board.piece_at(square):
let pieceType = piece.piece_type
let value = pieceValue[piece_type] + evaluatePiece(piece, square, endGame)
total += if piece.color == chess.WHITE: value else -value
return total
proc checkEndGame(board= chess.Board): bool =
var queens = 0
var minors = 0
for square in chess.SQUARES:
if let piece = board.piece_at(square):
case piece.piece_type
of chess.PieceType.QUEEN:
queens += 1
of chess.PieceType.BISHOP, chess.PieceType.KNIGHT:
minors += 1
if queens == 0 or (queens == 2 and minors <= 1):
return true
return false
var total = evaluateBoard(board)
var endgame = checkEndGame(board)
for square in chess.SQUARES:
if let piece = board.piece_at(square):
let value = pieceValue[piece.piece_type] + evaluatePiece(piece, square, endgame)
total += if piece.color == chess.WHITE: value else -value
return total
Error:
/home/runner/Engine/evaluate.nim:144:28 Error: type mismatch: got <array[1..6, int], int>
The specific line that causes this error as shown in the error Line no. 144:
return float(pieceValue[dest.piece_type.to(int)] - pieceValue[src.piece_type.to(int)])
/home/runner/Engine/evaluate.nim: 144:28 Error: t ype mismatch: got <array[1..6, int], int> but expected one of:
proc `[]`(s: string; i: BackwardsIndex): char first type mismatch at position: 0
proc `[]`(s: var string; i: BackwardsIndex): var char
first type mismatch at position: 0
proc `[]`[A, B](t: OrderedTableRef [A, B]; key: A ): var B
first type mismatch at position: 0
proc [][A, B](t: OrderedTable[A, B]; key: A):
B
first type mismatch at position: 0
proc `[]`[A, B](t: TableRef[A, B]; key: A): var
B
first type mismatch at position: 0
proc `[][A, B](t: Table[A, B]; key: A): B first type mismatch at position: 0
proc []`[A, B](t: var OrderedTable[A, B]; key: A): var B
first type mismatch at position: 0
proc `[]`[A, B](t: var Table[A, B]; key: A): var
B
first type mismatch at position: 0
proc [`[A](t: CountTableRef [A]; key: A): int first type mismatch at position: 0
proc [`[A](t: CountTable[A]; key: A): int first type mismatch at position: 0 proc [`[I: Ordinal; T](a: T; i: I): T first type mismatch at position: 0
proc `[]`[Idx, T; U, V: Ordinal](a: array[Idx, T ]; x: HSlice[U, V]): seq[T]
first type mismatch at position: 0
proc `[]`[Idx, T](a: array[Idx, T]; i: Backwards Index): T
first type mismatch at position: 0
proc `[]`[Idx, T](a: var array[Idx, T]; i: Backw ardsIndex): var T
first type mismatch at position: 0
proc [`[K](o: PyObject; idx: K): PyObject first type mismatch at position: 0
proc `[]`[T, U: Ordinal](s: string; x: HSlice[T, U]): string
first type mismatch at position: 0
first type mismatch at position: 0 proc `[]`[K](o: PyObject; idx: K): PyObject first type mismatch at position: 0
proc `[]`[T, U: Ordinal](s: string; x: HSlice[T, U]): string
first type mismatch at position: 0
proc `[]`[T; U, V: Ordinal](s: openArray[T]; x: HSlice[U, V]): seq[T]
first type mismatch at position: 0
proc `[]`[T](s: openArray[T]; i: BackwardsIndex) : T
first type mismatch at position: 0
proc `[]`[T](s: var openArray[T]; i: BackwardsIn dex): var T
first type mismatch at position: 0 template `[]`(s: string; i: int): char first type mismatch at position: 0
expression: `[]`(pieceValue, to(getAttr(dest, "p iece_type"), int))
For anyone who wants to see full error ^^^
The specific error at line 144 is because you need to convert integer to enum:
return float(pieceValue[dest.piece_type.to(int).piece] - pieceValue[src.piece_type.to(int).piece])
It's hard to say if there are more issues, until these are fixed.