Esempio n. 1
0
def play(grids_to_win: int, grid_width: int, grid_height: int,
         min_distance: int, max_distance: int,
         branch_probability: float) -> None:
    """Play the game.

    Args:
        grids_to_win: The number of grids that need to be completed to win the game. Increasing this makes the game more
            difficult.
        grid_width: The number of columns in the grid. Increasing this makes the game more difficult.
        grid_height: The number of rows in the grid. Increasing this makes the game more difficult.
        min_distance: The minimum length for a segment of the generated path through the maze grid. Decreasing this
            makes the game more difficult.
        max_distance: The maximum length for a segment of the generated path through the maze grid. Decreasing this
            makes the game more difficult.
        branch_probability: The probability for each tile that a dead end branch will be generated. Increasing this
            makes the game more difficult.
    """
    for _ in range(grids_to_win):
        grid = MazeGrid.create_random(grid_width, grid_height, min_distance,
                                      max_distance, branch_probability)

        interface = GameInterface(grid)
        interface.app.run()

    print_correct_message()
Esempio n. 2
0
def play(rows_to_win: int, starting_rows: int, columns: int) -> None:
    """Play the game.

    Args:
        columns: The number of columns in the grid. Increasing this makes the game more difficult.
        starting_rows: The number of rows in the grid when the game starts. Increasing this makes the game more
            difficult.
        rows_to_win: The total number of rows that must be in the grid to win the game. Increasing this makes the game
            more difficult.
    """
    char_grid = create_grid(starting_rows, columns)
    usernames = random.sample(USERNAMES, rows_to_win)
    pad_width = len(max(usernames, key=len))

    # Format and print the initial grid.
    starting_grid = "\n".join(
        format_line(usernames.pop(), pad_width, line)
        for line in char_grid.format().splitlines())
    print(starting_grid)

    # Create the prompt session.
    validator = Validator.from_callable(char_grid.check_row,
                                        error_message="Invalid row",
                                        move_cursor_to_end=True)
    session = PromptSession(validator=validator,
                            validate_while_typing=False,
                            mouse_support=True,
                            style=GUI_STYLE)

    # Prompt the user until they complete enough lines.
    while len(char_grid.rows) < rows_to_win:
        session.prompt(format_line(usernames.pop(), pad_width, ""))

    print_correct_message()
Esempio n. 3
0
def play(challenges_to_win: int, number_of_examples: int,
         max_section_number: int) -> None:
    """Play the game.

    Args:
        challenges_to_win: The number of challenges that must be completed to win the game. Increasing this makes the
            game more difficult.
        number_of_examples: The number of completed IP addresses that come before each challenge. Decreasing this makes
            the game more difficult.
        max_section_number: The maximum possible number for each section of the IP address. Increasing this makes the
            game more difficult.
    """
    session = PromptSession(validate_while_typing=False, mouse_support=True)

    for _ in range(challenges_to_win):
        example = AddressChallenge.create_random(max_section_number)
        challenge = AddressChallenge.create_random(max_section_number,
                                                   template=example)

        # Print examples.
        for _ in range(number_of_examples):
            example_copy = AddressChallenge.create_random(max_section_number,
                                                          template=example)
            print(example_copy.format_socket(include_solution=True))

        # Prompt for the challenge.
        session.prompt(challenge.format_socket(include_solution=False),
                       validator=SolutionValidator(challenge.solution))

        print_filler(challenge.format_address(), challenge.format_port())

    print_correct_message()
Esempio n. 4
0
def play(challenges_to_win: int, rows: int, continuous_columns: int,
         discrete_columns: int) -> None:
    """Play the game.

    Args:
        challenges_to_win: The number of challenges the user has to complete to win the game. Increasing this makes the
            game more difficult.
        rows: The number of rows in the table. Increasing this makes the game more difficult.
        continuous_columns: The number of continuous columns in the table. These are columns that have a range of
            possible values. Increasing this makes the game more difficult.
        discrete_columns: The number of discrete columns in the table. These are columns that have a limited number of
            possible values. Increasing this makes the game more difficult. Increasing this relative to
            `continuous_columns` also makes the game more difficult.
    """
    completed_challenges = 0

    while completed_challenges < challenges_to_win:
        table = Table(rows, continuous_columns, discrete_columns)
        table.create_table()

        # Prompt the user.
        interface = GameInterface(table)
        answer_is_correct = interface.app.run()

        if answer_is_correct:
            completed_challenges += 1
        else:
            print_incorrect_message()

    print_correct_message()
Esempio n. 5
0
def play(commands_to_win: int, min_args: int, max_args: int,
         redirect_probability: float, pipe_probability: float) -> None:
    """Play the game.

    Args:
        commands_to_win: The number of commands that must be correctly entered to win the game. Increasing this makes
            the game more difficult.
        min_args: The minimum number of non-required arguments that a command can have. Increasing this makes the game
            more difficult.
        max_args: The maximum number of non-required arguments that a command can have. Increasing this makes the game
            more difficult.
        redirect_probability: The probability that a command will send its output to a pipe or file. Increasing this
            makes the game more difficult.
        pipe_probability: The probability that a command will use a pipe when redirecting its output. Increasing this
            makes the game more difficult.
    """
    session = PromptSession(validate_while_typing=False,
                            mouse_support=True,
                            style=GUI_STYLE)

    command_generator = CommandGenerator(
        commands=COMMANDS,
        input_names=INPUT_FILE_NAMES,
        output_names=OUTPUT_FILE_NAMES,
        min_args=min_args,
        max_args=max_args,
        redirect_probability=redirect_probability,
        pipe_probability=pipe_probability,
    )

    # Print random commands and prompt the user to type them in until they type them in correctly.
    for _ in range(commands_to_win):
        command_string = command_generator.get_random()
        print(COMMAND_PROMPT + command_string)

        validator = Validator.from_callable(
            lambda x: x == command_string,
            error_message="Commands do not match",
            move_cursor_to_end=True)
        session.prompt(COMMAND_PROMPT, validator=validator)

        print()

    print_correct_message()
Esempio n. 6
0
def play(challenges_to_win: int, tree_depth: int, min_branches: int,
         max_branches: int, total_nodes: int) -> None:
    """Play the game.

    Args:
        challenges_to_win: The number of trees that need to be completed to win the game. Increasing this makes the
            game more difficult.
        tree_depth: The number of levels in the generated tree. Increasing this makes the game more difficult.
        min_branches: The minimum number of branches at each level of the tree. The effect on the difficulty varies.
        max_branches: The maximum number of branches at each level of the tree. The effect on the difficulty varies.
        total_nodes: The number of nodes that the tree will have. Increasing this makes the game more difficult.
    """
    for _ in range(challenges_to_win):
        tree = TreeNode.create_random(tree_depth, min_branches, max_branches,
                                      total_nodes,
                                      random.choice(NODE_VALUE_SETS))
        closure_table = ClosureTable(tree)

        interface = GameInterface(tree, closure_table)
        interface.app.run()

    print_correct_message()