<gh_stars>0
import chess
import numpy as np
# centipawn scale
POINT_VAL = {
    1: 100,  # pawn
    2: 350,  # knight
    3: 350,  # bishop
    4: 525,  # rook
    5: 1000,  # queen
    6: 10000  # king
}

PIECE_TABLES = {
    # PAWN TABLE
    1: np.array([[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]]),
    # KNIGHT TABLE
    2: np.array([[-50, -40, -30, -30, -30, -30, -40, -50],
                 [-40, -20, 0, 5, 5, 0, -20, -40],
                 [-30, 5, 10, 15, 15, 10, 5, -30],
                 [-30, 0, 15, 20, 20, 15, 0, -30],
                 [-30, 5, 15, 20, 20, 15, 5, -30],
                 [-30, 0, 10, 15, 15, 10, 0, -30],
                 [-40, -20, 0, 0, 0, 0, -20, -40],
                 [-50, -40, -30, -30, -30, -30, -40, -50]]),
    # BISHOP TABLE
    3: np.array([[-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]]),
    # ROOK TABLE
    4: np.array([[0, 0, 0, 0, 0, 0, 0, 0],
                 [5, 10, 10, 10, 10, 10, 10, 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, 0, 0, 0, 0, 0, 0, -5],
                 [0, 0, 0, 5, 5, 0, 0, 0]]),
    # QUEEN TABLE
    5: np.array([[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]]),
    # KING TABLE
    6: np.array([[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]]),
    # KINGLATE TABLE
    7: np.array([[-50, -40, -30, -20, -20, -30, -40, -50],
                 [-30, -20, -10, 0, 0, -10, -20, -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, -30, 0, 0, 0, 0, -30, -30],
                 [-50, -30, -30, -30, -30, -30, -30, -50]])
}


def position_score(board: chess.Board, turn):
    if board.is_game_over():
        return POINT_VAL[chess.KING] * (1 if turn else -1)
    value_sum = 0
    for pos, piece in board.piece_map().items():
        pos_file = chess.square_file(pos)
        pos_rank = chess.square_rank(pos)
        if piece.color:
            value_sum += POINT_VAL[piece.piece_type]
            value_sum += PIECE_TABLES[piece.piece_type][pos_rank][pos_file]
        else:
            value_sum += POINT_VAL[piece.piece_type] * -1
            value_sum += list(reversed(PIECE_TABLES[piece.piece_type]))[
                pos_rank][pos_file] * -1

    return value_sum * (1 if turn else -1)

import chess
import numpy as np
# centipawn scale
POINT_VAL = {
    1: 100,  # pawn
    2: 350,  # knight
    3: 350,  # bishop
    4: 525,  # rook
    5: 1000,  # queen
    6: 10000  # king
}

PIECE_TABLES = {
    # PAWN TABLE
    1: np.array([[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]]),
    # KNIGHT TABLE
    2: np.array([[-50, -40, -30, -30, -30, -30, -40, -50],
                 [-40, -20, 0, 5, 5, 0, -20, -40],
                 [-30, 5, 10, 15, 15, 10, 5, -30],
                 [-30, 0, 15, 20, 20, 15, 0, -30],
                 [-30, 5, 15, 20, 20, 15, 5, -30],
                 [-30, 0, 10, 15, 15, 10, 0, -30],
                 [-40, -20, 0, 0, 0, 0, -20, -40],
                 [-50, -40, -30, -30, -30, -30, -40, -50]]),
    # BISHOP TABLE
    3: np.array([[-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]]),
    # ROOK TABLE
    4: np.array([[0, 0, 0, 0, 0, 0, 0, 0],
                 [5, 10, 10, 10, 10, 10, 10, 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, 0, 0, 0, 0, 0, 0, -5],
                 [0, 0, 0, 5, 5, 0, 0, 0]]),
    # QUEEN TABLE
    5: np.array([[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]]),
    # KING TABLE
    6: np.array([[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]]),
    # KINGLATE TABLE
    7: np.array([[-50, -40, -30, -20, -20, -30, -40, -50],
                 [-30, -20, -10, 0, 0, -10, -20, -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, -30, 0, 0, 0, 0, -30, -30],
                 [-50, -30, -30, -30, -30, -30, -30, -50]])
}


def position_score(board: chess.Board, turn):
    if board.is_game_over():
        return POINT_VAL[chess.KING] * (1 if turn else -1)
    value_sum = 0
    for pos, piece in board.piece_map().items():
        pos_file = chess.square_file(pos)
        pos_rank = chess.square_rank(pos)
        if piece.color:
            value_sum += POINT_VAL[piece.piece_type]
            value_sum += PIECE_TABLES[piece.piece_type][pos_rank][pos_file]
        else:
            value_sum += POINT_VAL[piece.piece_type] * -1
            value_sum += list(reversed(PIECE_TABLES[piece.piece_type]))[
                pos_rank][pos_file] * -1

    return value_sum * (1 if turn else -1)
# centipawn scale # pawn # knight # bishop # rook # queen # king # PAWN TABLE # KNIGHT TABLE # BISHOP TABLE # ROOK TABLE # QUEEN TABLE # KING TABLE # KINGLATE TABLE