<commit_before>import random
from engine.evaluate_state import evaluate_state
from engine.check_detection import white_in_check, black_in_check
from engine.gen_moves import get_all_moves_at_state
from engine.move import move_at_state
from engine.constants import TURN_INDEX, WHITE, BOARD_INDEX, WK_SQ_INDEX, BK_SQ_INDEX


def best_move(state, depth=2, randomness=True):
    moves = get_all_moves_at_state(state)
    if len(moves) == 0:
        return None

    if state[1] == 0:
        current_score = -9999.0
    else:
        current_score = 9999.0
    move_n = -1
    turn = state[1]
    for i in range(len(moves)):
        new_state = move_at_state(state, moves[i])
        score = minimax(depth, new_state, -999, 999)
        if turn == 0:
            if score > current_score:
                move_n = i
                current_score = score
            elif score == current_score:
                if randomness and random.randint(1, 5) == 3:
                    move_n = i
                    current_score = score
        elif turn == 1:
            if score < current_score:
                move_n = i
                current_score = score
            elif score == current_score:
                if randomness and random.randint(1, 5) == 3:
                    move_n = i
                    current_score = score

    return moves[move_n]


def minimax(depth, state, alpha, beta):
    if depth == 0:
        b_val = evaluate_state(state)
        return b_val
    legal_moves = get_all_moves_at_state(state)
<commit_msg>end game one more layer<commit_after>import random
from engine.evaluate_state import evaluate_state
from engine.check_detection import white_in_check, black_in_check
from engine.gen_moves import get_all_moves_at_state
from engine.move import move_at_state
from engine.constants import TURN_INDEX, WHITE, BOARD_INDEX, WK_SQ_INDEX, BK_SQ_INDEX


def best_move(state, depth=2, randomness=True):
    moves = get_all_moves_at_state(state)
    n_moves = len(moves)
    if n_moves == 0:
        return None

    if n_moves < 40:  # search one layer deeper for end game
        depth += 1

    if state[1] == 0:
        current_score = -9999.0
    else:
        current_score = 9999.0
    move_n = -1
    turn = state[1]
    for i in range(n_moves):
        new_state = move_at_state(state, moves[i])
        score = minimax(depth, new_state, -999, 999)
        if turn == 0:
            if score > current_score:
                move_n = i
                current_score = score
            elif score == current_score:
                if randomness and random.randint(1, 5) == 3:
                    move_n = i
                    current_score = score
        elif turn == 1:
            if score < current_score:
                move_n = i
                current_score = score
            elif score == current_score:
                if randomness and random.randint(1, 5) == 3:
                    move_n = i
                    current_score = score

    return moves[move_n]


def minimax(depth, state, alpha, beta):
    if depth == 0:
        b_val = evaluate_state(state)
        return b_val
    legal_moves = get_all_moves_at_state(state)

{'fraction_non_alphanumeric': 0.07498424700693132, 'fraction_numerical': 0.020478890989287964, 'mean_word_length': 2.0151946818613484, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, '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': '7272335', 'n_tokens_mistral': 1042, 'n_tokens_neox': 965, 'n_words': 266}