[ Web Proxy ]
URL:
Viewing: https://python-course.eu/python-tutorial/adventure-game-with-structural-pattern-matching.php [Back]  [Original]

18. Adventure Game with Structural Pattern Matching | Python Tutorial
python-course.eu
Search site:

Live Python classes by highly experienced instructors:

instructor-led training course [instructor-led training course]
Instructor-led training courses by Bernd Klein

  1. Intro to Python Tutorial
  2. History and Philosophy of Python
  3. The Interpreter, an Interactive Shell
  4. Execute a Script
  5. Structuring with Indentation
  6. Assignment Expressions
  7. Data Types and Variables
  8. Type Annotations
  9. Operators
  10. Sequential Data Types
  11. List Manipulation
  12. Shallow and Deep Copy
  13. Dictionaries
  14. Sets and Frozen Sets
  15. Sets Examples
  16. Keyboard Input
  17. Conditional Statements
  18. Structural Pattern Matching
  19. Adventure Game with Structural Pattern Matching
  20. While Loops
  21. For Loops
  22. Output with Print
  23. Formatted Output
  24. Working with Dictionaries and while Loops
  25. Functions
  26. Passing Arguments
  27. Parameters And Arguments
  28. Namespaces
  29. Global vs. Local Variables and Namespaces
  30. File Management
  31. Modular Programming and Modules
  32. Packages
  33. Errors and Exception Handling

This website contains a free and extensive online tutorial by Bernd Klein, using material from his classroom Python training courses.

If you are interested in an instructor-led classroom training course, have a look at these Python classes:

instructor-led training course [instructor-led training course]
Instructor-led training course by Bernd Klein at Bodenseo

Image kabliczech - Fotolia.com

This page was written by Bernd Klein.

Bernd is an experienced computer scientist with a history of working in the education management industry and is skilled in Python, Perl, Computer Science, and C++. He has a Dipl.-Informatiker / Master Degree focused in Computer Science from Saarland University.

PDF versions of this site [PDF versions of this site]

PDF logo [PDF logo] PDF version of this site

This website is free of annoying ads. We want to keep it like this. You can help with your donation:

[Go]

The need for donations

18. Adventure Game with Structural Pattern Matching

By Bernd Klein. Last modified: 10 Nov 2023.

Introduction

Adventure Game image [Adventure Game image]

In this section of our Python tutorial, we introduce structural pattern matching through an intriguing scenarioa hypothetical text-based adventure game.

Text adventure games, often referred to as interactive fiction, are a type of computer game that relies primarily on text-based descriptions and typed language commands for user input, rather than graphics and sound. Typical user input for most of these games look like:

A notable example of such a game is "Hack," a text-based role-playing game developed in the 1980s at the Massachusetts Institute of Technology (MIT) and more commonly recognized as "NetHack."

We will showcase Python code snippets that simulate scenarios within a text adventure game, while also elucidating significant aspects and features of structural pattern matching.

command = input("What are you doing next? ")
action, object = command.split()
print(f"{action=}, {object=}")

OUTPUT:

action='take', object='sword'

What if the user types less or more than 2 words?

command = input("What are you doing next? ")
words = command.split()
no_of_words = len(words)
if no_of_words == 1:
    print(f"action without object: {action=}")
elif no_of_words == 2:
    print(f"{action=}, {object=}")
else:
    print(words)

OUTPUT:

action='take', object='sword'

Now with structural pattern recognition. Matching multiple patterns:

command = input("What are you doing next? ")
match command.split():
    case [action]:
        print(f"action without object: {action=}")
    case [action, obj]:
        print(f"{action=}, {obj=}")

OUTPUT:

action without object: action='look'

Live Python training

instructor-led training course [instructor-led training course]

Enjoying this page? We offer live Python training courses covering the content of this site.

See our Python training courses

See our Machine Learning with Python training courses

Matching specific patterns:

command = input("What are you doing next? ")
match command.split():
    case ["quit"]:
        print("Goodbye!")
        quit_game()
    case ["look"]:
        print("You are in cold and dark room ....")
    case ["get", obj]:
        print(f"You grab the {obj}")
    case ["go", direction]:
        print(f"You will go {direction} if possible")
    case _:
        print("Sorry, I do not understand!")

OUTPUT:

You grab the apple

Warrior in a Python adventure game [Warrior in a Python adventure game]

Matching Multiple Values:

command = "drop apple armour shield"

match command.split():
    case ["drop", *objects]:
        for obj in objects:
            print(f"You drop a{'n' if obj[0] in 'aeiou' else ''} {obj}")

OUTPUT:

You drop an apple
You drop an armour
You drop a shield

Live Python training

instructor-led training course [instructor-led training course]

Enjoying this page? We offer live Python training courses covering the content of this site.

Upcoming online Courses

See our Python training courses

See our Machine Learning with Python training courses

Adding a wildcard

In the following code, if you use an input that doesn't match any of the defined patterns, Python doesn't raise an exception. Instead, the code will simply continue without entering any of the case blocks like in the following Python example:

command = 'fight monster'
match command.split():
    case ["help"]:
        print("""You can use the following commands:
        """)
    case ["go", direction]: 
        print('going', direction)
    case ["drop", "all", "weapons"]: 
        for weapon in weapons:
            inventory.remove(weapon)
    case ["drop", item]:
        print('dropping', item)
    

To handle unexpected or unmatched input gracefully, you can add a wildcard case using an underscore _ as a wildcard to catch any input that doesn't match the specific patterns.

commands = ['go north','go west', 'drop potion', 'drop all weapons', 'drop shield', 'fight monster']
weapons = ['axe','sword','dagger']
shield = True
inventory = ['apple','wood','potion'] + weapons + ['shield']
for command in commands: 
    match command.split():
        case ["help"]:
            print("""You can use the following commands:
            """)
        case ["go", direction]: 
            print('going', direction)
        case ["drop", "all", "weapons"]: 
            for weapon in weapons:
                inventory.remove(weapon)
        case ["drop", item]:
            print('dropping', item)
            inventory.remove(item)
        case ["drop shield"]:
            shield = False 
        case _:
            print(f"Sorry, I couldn't understand {command!r}")

OUTPUT:

going north
going west
dropping potion
dropping shield
Sorry, I couldn't understand 'fight monster'

Several Patterns Resulting in the Same Outcome

command = input("What are you doing next? ")

match command.split():
    # Other cases
    # .....
    case ["north"] | ["go", "north"]:
        print("Let's go north")
    case ["look"] | ["look", "around"]:
        print("You are in a cold and dark room ....")
    case ["get", obj] | ["pick", "up", obj] | ["pick", obj, "up"]:
        print(f"Picking up {obj}")

OUTPUT:

You are in a cold and dark room ....

Live Python training

instructor-led training course [instructor-led training course]

Enjoying this page? We offer live Python training courses covering the content of this site.

See our Python training courses

See our Machine Learning with Python training courses

Capturing Matched Subpatterns

command = input("What are you doing next? ")
match command.split():
    case ["go", ("north" | "south" | "east" | "west") as direction]:
        print(f"You go {direction}")

OUTPUT:

You go north

Patterns mit Conditions

We can also add conditions to a branch of a structural pattern matching. So like checking if an exit exists in a certain direction of a room.

Condition: Two doors to choose from [Condition: Two doors to choose from]

command = input("What are you doing next? ")
possible_direction = ['north', 'south']
match command.split():
    case ["go", direction] if direction in possible_direction:
        print(f"So, let's go {direction}!")

OUTPUT:

So, let's go south!

Live Python training

instructor-led training course [instructor-led training course]

Enjoying this page? We offer live Python training courses covering the content of this site.

Upcoming online Courses

See our Python training courses

See our Machine Learning with Python training courses

top


Web Proxy Viewer  |  New URL  |  Original Page