Example #1
0
def start_woodcutter_game(players_info):
    """
        Функция запускает каждую стратегию с каждой.
    """

    utils.redirect_ctypes_stdout()

    points = [players_info[i][1] for i in range(len(players_info))]

    for i in range(len(players_info) - 1):
        if players_info[i][0] != "NULL":
            player_lib = ctypes.CDLL(players_info[i][0])

            for j in range(i + 1, len(players_info)):
                if players_info[j][0] != "NULL":
                    rival_lib = ctypes.CDLL(players_info[j][0])

                    count_nodes = randint(Woodcutter.min_count_nodes,
                                          Woodcutter.max_count_nodes)

                    tree = create_tree(count_nodes)
                    tree_copy = create_tree(count_nodes)

                    fill_tree(tree, count_nodes)
                    copy_tree(tree, tree_copy, count_nodes)

                    round_info = woodcutter_round(
                        player_lib, rival_lib, tree, count_nodes,
                        (players_info[i][0], players_info[j][0]))
                    points = scoring(points, i, j, round_info)

                    round_info = woodcutter_round(
                        rival_lib, player_lib, tree_copy, count_nodes,
                        (players_info[j][0], players_info[i][0]))
                    points = scoring(points, j, i, round_info)

                else:
                    points[j] = utils.GameResult.no_result
        else:
            points[i] = utils.GameResult.no_result

    utils.print_score_results(points, players_info, len(players_info))

    return points
Example #2
0
def start_sequence_game(players_libs):
    """
        Открытие функции с библиотеками игроков, запуск их функций, печать результатов.
    """

    utils.redirect_ctypes_stdout()
    game_conditions = generate_game_conditions()
    results = []

    for lib in players_libs:
        if lib != "NULL":
            player_lib = ctypes.CDLL(lib)
            player_lib.sequence_game.restype = ctypes.c_longlong
            results.append(player_results(game_conditions, player_lib))
        else:
            results.append((utils.GameResult.no_result, 0, 0))

    utils.print_time_results(results, players_libs)
    return results
Example #3
0
def start_xogame_competition(players_info, field_size):
    """
        Функция запускает каждую стратегию с каждой,
        результаты для каждого игрока записываются в массив points.
    """

    if field_size == 3:
        utils.redirect_ctypes_stdout()

    points = [players_info[i][1] for i in range(len(players_info))]

    for i in range(len(players_info) - 1):
        if players_info[i][0] != "NULL":
            player_lib = ctypes.CDLL(players_info[i][0])

            for j in range(i + 1, len(players_info)):
                if players_info[j][0] != "NULL":
                    opponent_lib = ctypes.CDLL(players_info[j][0])

                    round_info = xogame_round(
                        player_lib,
                        opponent_lib,
                        field_size,
                        (players_info[i][0], players_info[j][0])
                    )
                    points = scoring(points, i, j, round_info)

                    round_info = xogame_round(
                        opponent_lib,
                        player_lib,
                        field_size,
                        (players_info[j][0], players_info[i][0])
                    )
                    points = scoring(points, j, i, round_info)

                else:
                    points[j] = utils.GameResult.no_result
        else:
            points[i] = utils.GameResult.no_result

    utils.print_score_results(points, players_info, len(players_info))

    return points
Example #4
0
def start_travel_game(players_info, test_path):
    """
       Открытие библиотеки с функциями игроков.
       Подсчет времени выполнения их функций.
       Получение результатов.
    """
    utils.redirect_ctypes_stdout()

    with open(test_path + FILE_FLIGHTS, "r") as file_flights:
        test_data = create_test(file_flights)
        file_flights.seek(0)
        array_flights = solution(file_flights, test_data)

    print_conditions(test_data, array_flights)

    fopen, rewind, fclose = get_c_functions()

    mode = init_string("r")
    file_name = init_string(test_path + FILE_FLIGHTS)

    c_pointer = ctypes.POINTER(ctypes.c_int)()
    file_pointer = fopen(file_name, mode)
    route = Flight(test_data)

    results = []

    for player_lib in players_info:
        if player_lib == "NULL":
            results.append((utils.GameResult.no_result, 0, 0))
            continue

        rewind(file_pointer)

        results.append(
            player_results(player_lib, test_path, c_pointer, file_pointer,
                           route, array_flights, rewind))

    fclose(file_pointer)

    utils.print_time_results(results, players_info)

    return results
Example #5
0
def start_numbers_game(players_info):
    """
        Открытие библиотеки с функциями игроков, подсчёт времени исполнения их функций,
        печать результатов.
    """

    utils.redirect_ctypes_stdout()
    intervals = round_intervals()
    print_conditions(intervals)
    results = []

    for player_lib in players_info:
        if player_lib != "NULL":
            lib = ctypes.CDLL(player_lib)
            results.append(player_results(lib, intervals))
        else:
            results.append((utils.GameResult.no_result, 0, 0))

    utils.print_time_results(results, players_info)
    return results
Example #6
0
def start_teen48game_competition(players_info, field_size):
    """
        Создание игрового поля и запуск игры для каждого
        игрока, подсчёт его очков. Если количество очков менее, чем
        было набрано в прошлый раз, то очки не обновляются.
    """

    if field_size == 4:
        utils.redirect_ctypes_stdout()

    results = []

    for player in players_info:
        if player[0] == "NULL":
            results.append(utils.GameResult.no_result)
            continue

        player_lib = ctypes.CDLL(player[0])
        player_lib.teen48game.argtypes = [Matrix]
        player_lib.teen48game.restype = ctypes.c_char

        game_field = Matrix(field_size, field_size)
        game_field_copy = Matrix(field_size, field_size)

        fill_random_cell(game_field.matrix, get_random_numb(), game_field.rows,
                         game_field.columns)
        fill_random_cell(game_field.matrix, get_random_numb(), game_field.rows,
                         game_field.columns)
        game_is_end = False
        prev_move = "_"

        while not game_is_end:
            copy_field(game_field, game_field_copy)

            move = utils.call_libary(player_lib, ctypes_wrapper,
                                     ctypes.c_wchar, utils.Error.char_segfault,
                                     game_field)

            game_field, is_done = make_move(move, game_field)

            if is_done:
                rand_numb = get_random_numb()
                fill_random_cell(game_field.matrix, rand_numb, game_field.rows,
                                 game_field.columns)

            game_is_end = check_end_game(game_field)

            if move == utils.Error.char_segfault:
                print("▼ This player caused segmentation fault. ▼")
                game_is_end = True

            if prev_move == move and not is_done:
                print(
                    f"Two identical moves that do not change the field. Move: {move}"
                )
                game_is_end = True

            prev_move = move

        score = scoring(game_field)
        results.append(score if score > player[1] else player[1])
        print_field(game_field, utils.parsing_name(player[0]), score,
                    field_size)

    return results
Example #7
0
def start_tetris_competition(players_info):
    """
        Создание игрового поля.
        Запуск игры для каждого игрока.
        Подсчет очков.
    """

    utils.redirect_ctypes_stdout()

    results = []

    for player in players_info:
        if player[0] == "NULL":
            results.append(utils.GameResult.no_result)
            continue

        player_lib = ctypes.CDLL(player[0])

        c_strings, c_strings_copy, gamefield = create_c_objects()
        angle = ctypes.c_int()
        game = True
        points = 0

        while game:

            figure, matrix_figure = get_figure()
            c_figure = ctypes.c_char(figure.encode(utils.Constants.utf_8))
            print_figure(matrix_figure)

            move = utils.call_libary(
                player_lib, ctypes_wrapper, 'i', utils.Error.segfault, gamefield, c_figure,
                ctypes.byref(angle))

            if move == utils.Error.segfault:
                print("▼ This player caused segmentation fault. ▼")
                break

            move = player_lib.tetris_game(gamefield, c_figure, ctypes.byref(angle))

            if check_player_move(move, c_strings, c_strings_copy):

                if not move_figure(move, angle, matrix_figure, c_strings):
                    print_now_score(player[0], points)
                    break

                points += Tetris.bonus
                copy(c_strings_copy, c_strings)

                count_full_line = find_filled_lines(c_strings)

                if count_full_line:
                    copy(c_strings_copy, c_strings)
                    points += scoring(count_full_line)

                print_now_score(player[0], points)
                print_gamefield(c_strings)

                if points >= Tetris.max_score:
                    game = False

            else:
                print_now_score(player[0], points)
                game = False

        results.append(points if points > player[1] else player[1])

    print(f"\033[33mRESULTS: {results}\033[0m")

    return results
Example #8
0
def start_reagent_competition(players_info, field_size):
    """
        Запуск игры для каждого игрока.
        Подсчет очков.
    """

    if field_size == 10:
        utils.redirect_ctypes_stdout()

    results = []

    for player in players_info:
        if player[0] == "NULL":
            results.append(utils.GameResult.no_result)
            continue

        player_lib = ctypes.CDLL(player[0])

        c_strings, c_strings_copy, gamefield = create_c_objects(field_size)
        game = True
        count_moves = Reagent.max_count_moves
        points = 0

        while game and count_moves:

            count_moves -= 1

            print_round_info(player[0], points, c_strings, field_size)

            move = utils.call_libary(player_lib, ctypes_wrapper, 'i',
                                     utils.Error.segfault, gamefield,
                                     field_size)

            if move == utils.Error.segfault:
                count_moves = 0
                print("▼ This player caused segmentation fault. ▼")
                break

            field = ""
            for i in range(field_size):
                field += c_strings[i].value.decode(utils.Constants.utf_8)

            memory_leak_check_res = utils.memory_leak_check(
                SAMPLE_PATH, player[0], [field, str(field_size)])

            if memory_leak_check_res:
                count_moves = 0
                points = Reagent.leakage_fee
                print("▼ This player caused memory leaks. ▼")
                break

            move = player_lib.reagent_game(gamefield, field_size)
            print(f"\033[37mPLAYER MOVE: {str(move)}\033[0m")
            count_explosions = 0

            if check_player_move(move, c_strings, c_strings_copy, field_size):

                if not position_is_empty(move, c_strings, field_size):
                    count_explosions += splash_bomb(move, c_strings,
                                                    field_size)

                points += count_explosions - 1
                copy(c_strings_copy, c_strings, field_size)

                if check_end_game(c_strings, field_size):
                    game = False
            else:
                game = False

        points += add_empty_field_points(c_strings, field_size)
        points += count_moves

        results.append(points if points > player[1] else player[1])

    print(f"\033[32mRESULTS: {results}\033[0m")

    return results