Created at PyCon PL as a coding dojo challenge
Last active
December 25, 2015 22:08
-
-
Save honzakral/7047096 to your computer and use it in GitHub Desktop.
Coding Dojo
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
class WTF(Exception): | |
pass | |
class Player(object): | |
def __init__(self, starting_room): | |
starting_room.enter(self) | |
def input(self, text): | |
if ' ' in text: | |
item_name, action = text.split(' ', 1) | |
for i in self.location.items: | |
if i.name == item_name: | |
item = i | |
break | |
else: | |
raise WTF() | |
else: | |
item = self.location | |
action = text | |
item.action(self, action) | |
def act(self, item, action): | |
item.action(self, action) | |
class Item(object): | |
def __init__(self, name): | |
self.name = name | |
def __str__(self): | |
return self.name | |
def action(self, player, action): | |
if hasattr(self, 'do_' + action): | |
getattr(self, 'do_' + action)(player) | |
else: | |
raise WTF() | |
def do_help(self, player): | |
print('I am not helpring you!!') | |
for x in dir(self): | |
if x.startswith('do_'): | |
print(x[3:]) | |
class Room(Item): | |
def __init__(self, name, description, items=None): | |
super().__init__(name) | |
self.description = description | |
self.items = items or [] | |
def enter(self, player): | |
player.location = self | |
print("Welcome to %s\n\n%s" % (self.name, self.description) ) | |
def do_list(self, player): | |
for item in self.items: | |
print(item) | |
class Door(Item): | |
def __init__(self, name, from_room, to_room): | |
super().__init__(name) | |
self.from_room, self.to_room = from_room, to_room | |
self.from_room.items.append(self) | |
self.to_room.items.append(self) | |
def do_enter(self, player): | |
if player.location is self.from_room: | |
self.to_room.enter(player) | |
else: | |
self.from_room.enter(player) | |
def test_walk_through_a_door(): | |
r1, r2 = Room('hallway', 'Dark'), Room('living room', 'warm') | |
d = Door('littledoor', r1, r2) | |
player = Player(r1) | |
d.action(player, "enter") | |
assert player.location is r2 | |
def test_player_can_act_on_items(): | |
r1 = Room('hallway', 'Dark') | |
p = Player(r1) | |
class Movable(Item): | |
def do_move(self, player): | |
self.moved_by = player | |
i = Movable('stone') | |
p.act(i, 'move') | |
assert i.moved_by is p | |
def test_door_adds_iteself_to_both_rooms(): | |
r1, r2 = Room('hallway', 'Dark'), Room('living room', 'warm') | |
d = Door('door', r1, r2) | |
assert d in r1.items | |
assert d in r2.items | |
def test_player_can_interpret_command(): | |
r1, r2 = Room('hallway', 'Dark'), Room('living room', 'warm') | |
Door('littledoor', r1, r2) | |
player = Player(r1) | |
player.input('littledoor enter') | |
assert player.location is r2 | |
class Street(Room): | |
def enter(self, player): | |
super().enter(player) | |
print('Welcome to the game, type `help` to get no help.') | |
street = Street('street', 'In warsaw') | |
hallway = Room('hallway', 'Dark and gloomy') | |
entrance = Door('entrance', street, hallway) | |
def main_loop(): | |
p = Player(street) | |
while True: | |
command = input('What to do? ') | |
p.input(command) | |
if __name__ == '__main__': | |
main_loop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment