def performance_test():
    ai.transpositionTable = dict()
    ai.total_moves = 0
    _board = boards['difficultPosition']
    test_start_time = now()
    for depth in range(1, 6):
        start_time = now()
        best_move, _ = ai.search(_board, depth, ai.evaluate(_board), True, -99999, 99999)
        print('{}\t\t\t{}\t\t{:.3f}\t{}'.format(ai.total_moves, depth, now() - start_time, len(ai.transpositionTable)))
        # print('\n'.join(' '.join(piece for piece in row) for row in best_move.__reversed__()) + '\n')
    print('{} moves made per second'.format(int(ai.total_moves / (now() - test_start_time))))
def print_state(_turn, board, run_time, white_time_remaining,
                black_time_remaining, white, black, repeat):
    ai.recalculate_position_values(ai.to_array(board))
    print(
        f'----- {white.__name__} vs {black.__name__} match {repeat} move {_turn} -----'
    )
    print('\n'.join(' '.join(piece for piece in row)
                    for row in board.__reversed__()) + '\n')
    print('{} took: {:.3f} seconds'.format('white' if _turn % 2 else 'black',
                                           run_time))
    print('white time: {:.3f}'.format(white_time_remaining))
    print('black time: {:.3f}'.format(black_time_remaining))
    print('score: {:.1f}'.format(ai.evaluate(ai.to_array(board))))
def main(given_history, _, __):
    history = ai.to_array(given_history)
    player_is_white = len(history) % 2 == 1
    current_board = history[-1]
    best_score = -10**10
    for move, diff in ai.legal_moves(current_board, player_is_white):
        score = ai.evaluate(move)
        if not player_is_white:
            score = -score
        if score > best_score:
            best_score = score
            best_move = move
    print(f'search depth: 1')
    print(f'expected score: {best_score}')
    return ai.from_array(best_move)
. . . R . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . p
. . . . . . . P
P . P . . P P .
. . . . . R K .'''
}

for key in boards:
    board = boards[key].replace(' ', '').split().__reversed__()
    board = array('u', ''.join(row + '_' * 8 for row in board))
    assert len(board) == 128
    boards[key] = board
    # print(len(list(ai.moves(position, True))))
assert abs(ai.evaluate(boards['initialPosition'])) < 0.000001
assert len(list(ai.moves(boards['initialPosition'], True))) == 20
assert len(list(ai.moves(boards['difficultPosition'], True))) == 42
assert len(list(ai.moves(boards['pawnTakePosition1'], True))) == 2
assert len(list(ai.moves(boards['pawnTakePosition1'], False))) == 3
assert len(list(ai.moves(boards['pawnTakePosition2'], True))) == 3
assert len(list(ai.moves(boards['pawnTakePosition2'], False))) == 2
assert len(list(ai.moves(boards['castlingPosition'], True))) == 16
assert len(list(ai.moves(boards['castlingPosition'], False))) == 16
assert not ai.is_check(boards['kingSavePosition'], True)
assert not ai.is_check(boards['kingSavePosition'], False)
assert not ai.is_check(boards['kingThreat'], True)
assert ai.is_check(boards['kingThreat'], False)
assert ai.position_value['N'][3 + 4 * 16] == ai.position_value['N'][4 + 3 * 16]
assert ai.position_value['N'][3 + 4 * 16] == -ai.position_value['n'][3 + 4 * 16]
assert ai.position_value['P'][16] < ai.position_value['P'][5 * 16]