<commit_before># DESCRIPTION: Contains the heavy-lifting of the engine: search & evaluation.

# 50726f6772616d6d696e6720697320627265616b696e67206f66206f6e652062696720696d706f
# 737369626c65207461736b20696e746f207365766572616c207665727920736d616c6c20706f73
# 7369626c65207461736b732e

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~MAIN~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
from copy import copy, deepcopy
from lib import chessboard, core, movegenerator, pieces

class Evaluator:
    """Evaluate a board as a "chess score" in order to pick the best moves."""

    def __init__(self):
        return None

    def materialscore(self, board):
        """Looks at the material advantage present on the board."""
        def piecediff(piecetype):
            """Find if one side is up pieces (+ for white, - for black)"""
            whitepieces = len(board.findpiece(piecetype, 'white'))
            blackpieces = len(board.findpiece(piecetype, 'black'))
            return whitepieces - blackpieces

        positionvalue = (
            piecediff(pieces.KingPiece)*100000 +
            piecediff(pieces.QueenPiece)*9000 +
            piecediff(pieces.RookPiece)*5000 +
            piecediff(pieces.BishopPiece)*2500 +
            piecediff(pieces.KnightPiece)*2500 +
            piecediff(pieces.PawnPiece)*1000
        )
        return positionvalue

    def mobilityscore(self, board):
        """Determines how mobile each side is and returns a net score."""
        generator = movegenerator.MoveGenerator(board)
        whitemobility = len(generator.generatemovelist('white'))*100
        blackmobility = len(generator.generatemovelist('black'))*100
        return whitemobility - blackmobility

    def evaluate(self, board):
        """Evaluates the board passed, considering lots of factors."""
        netscore = 0
        netscore += self.materialscore(board)
        netscore += self.mobilityscore(board)
        return None


class ChessEngine:
    """The brains of the computer. It searches and evaluates positions."""

    def __init__(self):
        self.movegenerator = movegenerator.MoveGenerator()
        self.evaluate = Evaluator.evaluate
        return None

    def netattackers(self, board, position):
        """Looks at the position on the board and sees the number of attackers
        and defenders. This is used to see if there is a possibiliy of exchange."""
        try:
            piececolour = board[position].colour
        except AttributeError:
            raise core.EmptySquareError(position)

        endmoves = lambda l: map(lambda x: x[1], l)

        defenderendmoves = endmoves(
            self.movegenerator.basicmoves(piececolour, defendingmoves=True)
        )
        attackerendmoves = endmoves(
            self.movegenerator.basicmoves(core.oppositecolour(piececolour))
        )
        return attackerendmoves.count(position) - defenderendmoves.count(position)<commit_msg>[FIX] Small fixes & made evaluate return netscore instead of None<commit_after># DESCRIPTION: Contains the heavy-lifting of the engine: search & evaluation.

# 50726f6772616d6d696e6720697320627265616b696e67206f66206f6e652062696720696d706f
# 737369626c65207461736b20696e746f207365766572616c207665727920736d616c6c20706f73
# 7369626c65207461736b732e

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~MAIN~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
from copy import copy, deepcopy
from lib import chessboard, core, movegenerator, pieces

class Evaluator:
    """Evaluate a board as a "chess score" in order to pick the best moves."""

    def __init__(self):
        return None

    def materialscore(self, board):
        """Looks at the material advantage present on the board."""
        def piecediff(piecetype):
            """Find if one side is up pieces (+ for white, - for black)"""
            whitepieces = len(board.findpiece(piecetype, 'white'))
            blackpieces = len(board.findpiece(piecetype, 'black'))
            return whitepieces - blackpieces

        positionvalue = (
            piecediff(pieces.KingPiece)*100000 +
            piecediff(pieces.QueenPiece)*9000 +
            piecediff(pieces.RookPiece)*5000 +
            piecediff(pieces.BishopPiece)*2500 +
            piecediff(pieces.KnightPiece)*2500 +
            piecediff(pieces.PawnPiece)*1000
        )
        return positionvalue

    def mobilityscore(self, board):
        """Determines how mobile each side is and returns a net score."""
        generator = movegenerator.MoveGenerator(board)
        whitemobility = len(generator.generatemovelist('white'))*100
        blackmobility = len(generator.generatemovelist('black'))*100
        return whitemobility - blackmobility

    def evaluate(self, board):
        """Evaluates the board passed, considering lots of factors."""
        netscore = 0
        netscore += self.materialscore(board)
        netscore += self.mobilityscore(board)
        return netscore


class ChessEngine:
    """The brains of the computer. It searches and evaluates positions."""

    def __init__(self):
        self.movegenerator = movegenerator.MoveGenerator
        self.evaluate = Evaluator.evaluate
        return None

    def netattackers(self, board, position):
        """Looks at the position on the board and sees the number of attackers
        and defenders. This is used to see if there is a possibiliy of exchange."""
        try:
            piececolour = board[position].colour
        except AttributeError:
            raise core.EmptySquareError(position)

        endmoves = lambda l: map(lambda x: x[1], l)
        movegen = self.movegenerator(board)

        defenderendmoves = endmoves(
            movegen.basicmoves(piececolour, defendingmoves=True))
        attackerendmoves = endmoves(
            movegen.basicmoves(core.oppositecolour(piececolour)))
        return attackerendmoves.count(position) - defenderendmoves.count(position)
{'fraction_non_alphanumeric': 0.10652395514780835, 'fraction_numerical': 0.06489976214746856, 'mean_word_length': 3.0516173434273917, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '564919', 'n_tokens_mistral': 1947, 'n_tokens_neox': 1631, 'n_words': 422}