<reponame>mikhas/cheviz
# AUTOGENERATED! DO NOT EDIT! File to edit: 00_core.ipynb (unless otherwise specified).

__all__ = ['dimension', 'reshaped', 'show', 'makeThreatenedSquares', 'defence', 'attack']

# Cell
from typing import Callable
import matplotlib.pyplot as plt
import numpy as np
import chess


def dimension()->int:
    return 8


def reshaped(data:np.ndarray)->np.ndarray:
    dim = dimension()
    return data.reshape(dim, dim)


def show(data:np.ndarray, ax=None):
    dim = dimension()
    if ax is None:
        plt.matshow(reshaped(data))
    else:
        ax.matshow(reshaped(data))


def makeThreatenedSquares(**kwargs)->Callable:
    board = kwargs['board'] if 'board' in kwargs else chess.Board()
    if 'fen' in kwargs:
        board.set_fen(kwargs['fen'])

    max_index = dimension()**2 - 1
    dim = dimension()

    def threatenedSquares(side:bool, filter_by:Callable=None)->np.ndarray:
        data = np.zeros((max_index + 1,))

        for i in range(max_index + 1):
            if filter_by == None or filter_by(board, side, i):
                row_offset = (dim - 1 - (i // dim)) * dim
                data[row_offset + i % dim] = len(board.attackers(side, i))

        return data

    return threatenedSquares

# Cell
def defence(board:chess.Board, side:bool, square:int)->bool:
    return board.piece_at(square).color == side if board.piece_at(square) else False

def attack(board:chess.Board, side:bool, square:int)->bool:
    return not defence(board, side, square)

# AUTOGENERATED! DO NOT EDIT! File to edit: 00_core.ipynb (unless otherwise specified).

__all__ = ['dimension', 'reshaped', 'show', 'makeThreatenedSquares', 'defence', 'attack']

# Cell
from typing import Callable
import matplotlib.pyplot as plt
import numpy as np
import chess


def dimension()->int:
    return 8


def reshaped(data:np.ndarray)->np.ndarray:
    dim = dimension()
    return data.reshape(dim, dim)


def show(data:np.ndarray, ax=None):
    dim = dimension()
    if ax is None:
        plt.matshow(reshaped(data))
    else:
        ax.matshow(reshaped(data))


def makeThreatenedSquares(**kwargs)->Callable:
    board = kwargs['board'] if 'board' in kwargs else chess.Board()
    if 'fen' in kwargs:
        board.set_fen(kwargs['fen'])

    max_index = dimension()**2 - 1
    dim = dimension()

    def threatenedSquares(side:bool, filter_by:Callable=None)->np.ndarray:
        data = np.zeros((max_index + 1,))

        for i in range(max_index + 1):
            if filter_by == None or filter_by(board, side, i):
                row_offset = (dim - 1 - (i // dim)) * dim
                data[row_offset + i % dim] = len(board.attackers(side, i))

        return data

    return threatenedSquares

# Cell
def defence(board:chess.Board, side:bool, square:int)->bool:
    return board.piece_at(square).color == side if board.piece_at(square) else False

def attack(board:chess.Board, side:bool, square:int)->bool:
    return not defence(board, side, square)
# AUTOGENERATED! DO NOT EDIT! File to edit: 00_core.ipynb (unless otherwise specified). # Cell # Cell