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 clone
the 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 push
up this branch. (The first time you push, you'll have to typegit push --set-upstream [branchname]
).
Your task is to implement the MyCharacter
class found in
text_adventure/entity.py. The 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
expected_output.txt
.
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.
24/7 hangout link: https://codebuddies.org/hangout/9rasmyL5rp9igiDyZ
@dangillet:
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
if
statements, 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
Entity
dynamically 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_machine
branch 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 wasMyCharacter
. I would suggest you take a name describing the kind of character you're going to implement. It could be aDog
, aVampire
or 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.