Practice Design Patterns through implementing a text based adventure game
This collaborative project aims at getting a feel for design patterns and what problems they try to solve.
If you're interested in joining us on this adventure, you can find us at https://codebuddies.org/ (we're in the #python and #text-adventure channels in Slack).
- Fork the code. (Or, if you've requested collaboration access in the #text-adventures channel on the CodeBuddies Slack, you can
git clonethe repo directly.)
- Checkout the 0.1 tag by typing
git checkout tags/0.1.
- Create a new branch based on this tag by typing
git checkout -b [YOUR_NAME]_character. Make your changes while in this new branch.
- Commit your changes. Once you're happy with your code, create a pull request -- or, if you've been added as a collaborator, simply
git pushup this branch. (The first time you push, you'll have to type
git push --set-upstream [branchname]).
Your task is to implement the
MyCharacter class found in
update method contains print statements
explaining how the character should behave, and Workflow.png will give you
some hints on what order things should be happening in.
WARNING: This code is going to throw an exception initially - there are some things missing.
Your mission is to find and fix those items!
You can find an example of what the output might be like in
Our FIrst Design Pattern: Finite State Machine
After you've implemented your solution, check out the
state_machine branch and try to see if you can rework your code following that pattern.
Hangout reportback notes
24/7 hangout link: https://codebuddies.org/hangout/9rasmyL5rp9igiDyZ
June 3rd, 2017
We reviewed together the naive way of implementing the problem and then we went through the Finite State Machine pattern. It allows us to remove a level of
ifstatements, and keep all the logic about one state in a single place.
I've pushed 2 branches. One is https://github.com/codebuddiesdotorg/text_adventure/tree/state_machine and shows the basic implementation of the State machine for Python. It could be made more formal by using an Interface Class from which all the states derive. But as Python is a dynamic language, it's not really worth the effort.
I've pushed in another branch https://github.com/codebuddiesdotorg/text_adventure/tree/alt_implementation an alternate implementation using the fact that Python being dynamic, you can change the class of the
Entitydynamically to have the desired implementation depending on the current state. I would not necessarily recommend doing it this way. I've discovered it while reading the Python Cookbook, 3rd edition. http://chimera.labs.oreilly.com/books/1230000000393
What I'm going now to do is merge the
state_machinebranch into master. From there we decided for practice to implement one or two more states. As we will do it all differently, we will rename our class whatever we want. Currently it was
MyCharacter. I would suggest you take a name describing the kind of character you're going to implement. It could be a
Vampireor whatever floats your boat. As a fun experiment we will then merge all these classes and their states together and instantiate one of each character type to see them living in the world.
After that I think we will move on to really work on the foundation of the text based adventure game.