Created
October 18, 2013 21:02
-
-
Save wuub/7048227 to your computer and use it in GitHub Desktop.
great team, great game :) @PyConPL #Dojo entry team #2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from __future__ import with_statement, division, absolute_import, print_function | |
import sys | |
ROOMS = { | |
'start': { | |
'desc': """You are in Szczyrk in a very beutiful hotel. It is very modern and | |
it is famous for its delicious food. You are at RECEPTION. Where do | |
you want to go?""", | |
'directions': { | |
'upstairs': 'corridor1', | |
'downstairs': 'drinking_hall', | |
'left': 'corridor0', | |
}, | |
}, | |
'corridor1': { | |
'desc': """You hear voices somewhere on your left. But it's dark there. On right it is light and cosy. Which way do you want to go? | |
""", | |
'directions': { | |
'left': 'php_room', | |
'right': 'corridor2', | |
}, | |
}, | |
'php_room' : { | |
'desc': """You found a PHP programmers party. They invited you to join. | |
You drink with them and die. | |
""", | |
'directions': { | |
} | |
}, | |
'win': { | |
'desc': """You suddenly feel very good. Warm and nice voices everywhere. | |
You hear words like "python", "django", "pip". You finally feel | |
that you are at home. | |
""", | |
'directions': { | |
} | |
}, | |
'drinking_hall' : { | |
"desc": "You are in a place that smells of alcohol. Probably someone had aprty here. " | |
"There isn't much to do here. You see a door to the left and a one to the right", | |
"directions": { | |
'left' : 'merge_hell', | |
'right': 'messy_toilet' | |
} | |
}, | |
'merge_hell': { | |
"desc": "You left merging unattented for weeks and now it gets you. It's a Merge Hell." | |
"You die. You should not be doing this ever. Care for your code. A big merge" | |
"conflict jums at you from the darkness and strangles you.", | |
"directions": {} | |
}, | |
'messy_toilet' : { | |
"desc": "Phew, what a messy place! There is a drunk programmer lying on the floor." | |
"He smells of php. Do you really want to stay here? The stench is unbearable." | |
"You can only go back.", | |
"directions": { | |
'back': 'drinking_hall' | |
} | |
}, | |
"corridor0" : { | |
"desc": "This is just a crowded corridor. There are admins, PMs and... PHP programmers " | |
"run away!!! upstairs or downstairs?", | |
"directions": { | |
"upstairs": "php_room", | |
"downstairs": "drinking_hall" | |
} | |
}, | |
'sauna': { | |
'desc': 'Here is very hot, and steam is around you. You can stay here and relax or go to the next room.', | |
'directions' : { | |
'left': 'drinking_hall', | |
'right': 'messy_toilet', | |
} | |
}, | |
'corridor2': { | |
'desc': 'Empty corridor, but you hear Ruby programers, you must select next step fast.', | |
'directions' : { | |
'left': 'drinking_hall', | |
'right': 'win' | |
} | |
} | |
} | |
for name, room in ROOMS.items(): | |
for direction in room['directions'].values(): | |
if direction not in ROOMS: | |
print (name, direction) | |
def win(player): | |
if player.location in ["win"]: | |
return True | |
return False | |
def lose(player): | |
if player.location == "death": | |
return True | |
elif not ROOMS[player.location].get("directions"): | |
return True | |
return False | |
class Player(object): | |
def __init__(self): | |
self.location = "start" | |
def _describe_full(self): | |
self._describe_room() | |
print() | |
self._describe_movements() | |
def _describe_room(self): | |
print(ROOMS[self.location]['desc']) | |
def _describe_movements(self): | |
print ("You can go: {}".format(", ".join(ROOMS[self.location]['directions']))) | |
def help(self, *_): | |
print ("Available commands:") | |
print ("\tgo [SOMEWHERE]") | |
print ("\tdescribe") | |
print ("\tquit") | |
print ("\thelp") | |
def describe(self, what): | |
self._describe_full() | |
def go(self, where): | |
try: | |
self.location = ROOMS[self.location]['directions'][where.lower()] | |
self._describe_full() | |
except: | |
print("you cannot go there, sorry") | |
def quit(self, *_): | |
print("Goodbye") | |
sys.exit(1) | |
player = Player() | |
player._describe_full() | |
while True: | |
if win(player): | |
print ("YOU WIN") | |
break | |
elif lose(player): | |
print ("YOU WERE EATEN BY A GRUE") | |
break | |
action_text = raw_input("> ") | |
if not action_text: | |
continue | |
action, _, attrs = action_text.partition(' ') | |
try: | |
method = getattr(player, action) | |
except: | |
print("Unknown action. Type: 'help' for help") | |
continue | |
method(attrs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment