Exemple #1
0
def main():
    random.seed()
    print("Maze generator")
    width = input_int("Width? ", 3, 500)
    height = input_int("Height? ", 3, 500)
    maze = generate_maze(width, height)
    print("Maze generated.")
    filename = input("File name? ")
    bitmap = convert_to_bitmap(maze)
    save_bitmap(bitmap, filename)
    print(f"Maze saved to {filename}")
Exemple #2
0
def get_burn(fuel):
    if fuel > 0:
        burn = input_int("? ", 0, MAX_BURN)
        burn = min(burn, fuel)
        if fuel == burn:
            print("*** FUEL EXHAUSTED ***")
    else:
        burn = 0
    return burn
Exemple #3
0
def play_game():
    """
    Plays one round of the Two Heaps game.
    """

    left = right = START_SIZE

    # The game is over when both heaps are at zero
    def is_win():
        return left == right == 0

    # Update the game status
    def show_status():
        print(f"Left heap has {left}. Right heap has {right}.")

    # Do first player prompt out of the main loop to enforce not allowing Both on first move.
    show_status()
    player_move = input_letter("Will you take from Left or Right? ", "LR")
    while not is_win():
        if player_move == "L":
            max_count = left
        elif player_move == "R":
            max_count = right
        else:
            max_count = left if left < right else right
        player_count = input_int("How many? ", 1, max_count)
        if player_move == "L":
            left -= player_count
        elif player_move == "R":
            right -= player_count
        else:
            left -= player_count
            right -= player_count

        show_status()
        if is_win():
            print("You win!")
            break
        left_move, right_move = optimal_move(left, right)
        print(
            f"Computer takes {left_move} from Left and {right_move} from Right."
        )
        left, right = left - left_move, right - right_move
        if is_win():
            print("Computer wins.")
            break
        show_status()
        player_move = input_letter("Will you take from Left, Right, or Both? ",
                                   "LRB")
Exemple #4
0
def play_game():
    total = 0
    while total < 100:
        player_move = input_int("Your move? ", 1, 10)
        total += player_move
        print(f"Total is {total}.")
        if total >= 100:
            print("You win!")
            break
        cpu_move = optimal_move(total)
        total += cpu_move
        print(f"Computer plays {cpu_move}.  Total is {total}.")
        if total >= 100:
            print("Computer wins.")
            break
Exemple #5
0
def play_game():
    """
    Plays one round of the guessing game.  The user can guess as many times as needed to guess a
    number between MIN and MAX.
    """
    # TODO: implement move limit... ceiling of lg(range)... 7 moves for 1-100
    number = random.randint(MIN, MAX)
    guess = 0
    print(f"I am thinking of a number between {MIN} and {MAX}.")
    while number != guess:
        guess = input_int("What's your guess? ", MIN, MAX)
        if number > guess:
            print("My number is higher than that.")
        elif number < guess:
            print("My number is less than that.")
    print("That's it - you got it!")
Exemple #6
0
def play_game():
    """
    Plays one round of the stars guessing game.  The user can guess as many times as needed to guess a
    number between MIN and MAX.  On each round, the user is shown stars depending on how close they are.
    The distance is based on a power of two: 7 stars for a guess 1 number away, 6 stars for 2 away, 5 stars
    for 3 away... and so on.
    """
    # TODO: implement move limit... 7 moves for 1 to 100
    number = random.randint(MIN, MAX)
    guess = 0
    print(f"I am thinking of a number between {MIN} and {MAX}.")
    while number != guess:
        guess = input_int("What's your guess? ", MIN, MAX)
        if number != guess:
            diff = abs(number - guess)
            star_count = 8
            while diff >= 1:
                diff //= 2
                star_count -= 1
            print(star_count * "*")
    print("That's it - you got it!")