From 752242a2d25693fc5a8e65021655622ad9ad54bc Mon Sep 17 00:00:00 2001 From: rjuanpuj Date: Fri, 12 Jul 2019 11:12:48 +0200 Subject: [PATCH] Migration from Python2 to Python3 completed. --- Game/engine.py | 28 +++++++++++++-------------- Game/exits.py | 16 ++++++++-------- Game/inventory.py | 18 +++++++++--------- Game/items.py | 2 +- Game/map.py | 8 ++++---- Game/map_old.py | 38 ++++++++++++++++++------------------- Game/mobiles.py | 46 ++++++++++++++++++++++----------------------- Game/rooms.py | 20 ++++++++++---------- tests/Game_tests.py | 4 ++-- 9 files changed, 90 insertions(+), 90 deletions(-) diff --git a/Game/engine.py b/Game/engine.py index 8365181..ecd2249 100644 --- a/Game/engine.py +++ b/Game/engine.py @@ -35,8 +35,8 @@ def move_into(self, room_name): def prompt(self): #prints the prompt and returns the input - print "\nWhat do you want to do?" - action = raw_input("> ") + print("\nWhat do you want to do?") + action = input("> ") return action @@ -44,8 +44,8 @@ def menu_commands(self, command): #handles straightforward, always-available commands like "quit" and "inv" self.command = command if "quit" in self.command: - print "Are you sure? Press Y to quit, any other key to keep playing." - confirmation = raw_input("> ") + print("Are you sure? Press Y to quit, any other key to keep playing.") + confirmation = input("> ") if confirmation == "Y" or confirmation == "y": exit(1) else: @@ -62,7 +62,7 @@ def parse_fail(self): #this should be called as little as possible in favor of more specific errors #in that vein: tracking of commands that get this response so that I can #make better resposnes to common unrecognized phrases - print "I'm afraid I don't know what that means." + print("I'm afraid I don't know what that means.") def inventory_parse(self, command): #decides whether the command is to take, drop, or use an item and calls the appropriate @@ -76,14 +76,14 @@ def inventory_parse(self, command): self.item_to_try = self.mentioned_in(self.command, self.player.inventory.inv_list) self.player.drop(self.item_to_try) else: - print "Inventory parsing error" #should never happen + print("Inventory parsing error") #should never happen def look_fail(self, command): #called when a look command doesn't refer to anything #could be made more interesting by referring to the command self.command = command - print "You don't see anything like that." + print("You don't see anything like that.") def parse(self, action): #breaks commands into categories and then calls an appropriate function @@ -114,7 +114,7 @@ def parse(self, action): def use_words_lookup(self): self.use_words = ['use'] - for label, item in self.player.can_see().iteritems(): + for label, item in self.player.can_see().items(): if item.use_words != "": for word in item.use_words: self.use_words.append(word) @@ -133,7 +133,7 @@ def look_parse(self, command): self.looked_at = self.mentioned_in(self.command, self.player.can_see()) if self.looked_at.label != 'not_found': - print self.looked_at.description + print(self.looked_at.description) else: self.look_fail() @@ -148,7 +148,7 @@ def move_parse(self, command): self.move_into(self.player.new_location) def look_fail(self): - print "You don't see anything like that here." + print("You don't see anything like that here.") def use_parse(self, command): #this is triggered if the command starts with a possible use word @@ -161,7 +161,7 @@ def use_parse(self, command): self.player.use(self.use_item) def use_fail(self): - print "You don't see any way to do that." + print("You don't see any way to do that.") def mentioned_in(self, command, items_to_search): #to use on both exits and items @@ -176,7 +176,7 @@ def mentioned_in(self, command, items_to_search): self.items_to_search = items_to_search self.success = False - for i, item in self.items_to_search.iteritems(): + for i, item in self.items_to_search.items(): for word in self.command: if word in item.keywords: self.exit_to_try = item @@ -193,6 +193,6 @@ def simulate_play(self, command_list): self.parse(command) def victory(self): - print "With a touch of the ignition button, the snowmobile roars with way more power than it needs. You climb on and steer it out into the blinding snow, hoping you will reach civilization while you can still be described as civilized." - print "Congratulations! You are a winner!" + print("With a touch of the ignition button, the snowmobile roars with way more power than it needs. You climb on and steer it out into the blinding snow, hoping you will reach civilization while you can still be described as civilized.") + print("Congratulations! You are a winner!") exit(1) \ No newline at end of file diff --git a/Game/exits.py b/Game/exits.py index 53b54c3..dab7d16 100644 --- a/Game/exits.py +++ b/Game/exits.py @@ -1,4 +1,4 @@ -import items +from . import items import csv class Exit(items.Item): @@ -63,7 +63,7 @@ def shall_pass(self, player): if self.is_open == True: return(True) else: - print "The %s is closed." #shouldn't happen + print("The %s is closed.") #shouldn't happen return(False) #to create custom "shall pass" behavior, make a substitute method below @@ -76,23 +76,23 @@ def main_portal_special(self, player): self.is_open = True return(True) elif self.player.inventory.has('keycard'): - print """ + print(""" The reader beeps as the light turns green, and the door swings open. Outside, a howling wind whips across waist-deep drifts of snow. It's hard to see anything through the blizzard. You don't think you'd survive long out there without some protection from the cold. - """ + """) return(False) else: - print "You think you'll need a keycard to open that door." + print("You think you'll need a keycard to open that door.") return(False) def reactor_special(self, player): self.player = player if self.player.injected == False: - print "You really don't think it's a good idea to go in there unprotected." + print("You really don't think it's a good idea to go in there unprotected.") return False else: - print "You step through the door. You're pretty sure that you wouldn't experience radiation exposure as a slight subdermal tingle, so that's probably your imagination." + print("You step through the door. You're pretty sure that you wouldn't experience radiation exposure as a slight subdermal tingle, so that's probably your imagination.") return True def create_exit(config): @@ -151,7 +151,7 @@ def special_setup(all_exits): def populate(): all_exits = {} - f = open('data/exits.csv', 'rb') + f = open('data/exits.csv', 'r') reader = csv.DictReader(f) for config in reader: diff --git a/Game/inventory.py b/Game/inventory.py index cc42082..9b80b1f 100644 --- a/Game/inventory.py +++ b/Game/inventory.py @@ -22,7 +22,7 @@ def remove(self, item): def has(self, label): self.label = label - if self.label in self.inv_list.keys(): + if self.label in list(self.inv_list.keys()): return True else: return False @@ -30,24 +30,24 @@ def has(self, label): def describe(self, item): self.item = item if self.has(self.item.label): - print self.item.description + print(self.item.description) else: - print "You are not carrying a %s.\n" % self.item.name + print("You are not carrying a %s.\n" % self.item.name) def list(self): #lists the contents of the inventory if self.inv_list == {}: - print "You are not carrying anything." + print("You are not carrying anything.") else: - print "You are carrying:\n" - for i, item in self.inv_list.iteritems(): - print item.name + print("You are carrying:\n") + for i, item in self.inv_list.items(): + print(item.name) #these should probably never happen because of how add and remove are called #as part of mobile.take/drop, which should only be called in pre-screened circumstances def remove_error(self, item): - print "%s not found in inventory." % self.item.label + print("%s not found in inventory." % self.item.label) def add_error(self, item): - print "%s already in inventory" % self.item.label \ No newline at end of file + print("%s already in inventory" % self.item.label) \ No newline at end of file diff --git a/Game/items.py b/Game/items.py index 6873213..018dae0 100644 --- a/Game/items.py +++ b/Game/items.py @@ -68,7 +68,7 @@ def populate(): #and returns a dictionary of all the items all_items = {} - f = open('data/items.csv', 'rb') + f = open('data/items.csv', 'r') reader = csv.DictReader(f) for row in reader: diff --git a/Game/map.py b/Game/map.py index 3a6a8d3..4869478 100644 --- a/Game/map.py +++ b/Game/map.py @@ -1,6 +1,6 @@ -import items -import exits -import rooms +from . import items +from . import exits +from . import rooms class Map(object): def setup(self): @@ -8,6 +8,6 @@ def setup(self): self.all_exits = exits.populate() self.all_rooms = rooms.populate() - for label, room in self.all_rooms.iteritems(): + for label, room in self.all_rooms.items(): room.add_items(self.all_items) room.add_exits(self.all_exits) \ No newline at end of file diff --git a/Game/map_old.py b/Game/map_old.py index 3cd0199..51a71c5 100644 --- a/Game/map_old.py +++ b/Game/map_old.py @@ -1,8 +1,8 @@ -import items -import exits -import inventory -import mobiles -import rooms +from . import items +from . import exits +from . import inventory +from . import mobiles +from . import rooms class MainPortal(Exit): #seems like the best way to implement complicated exit behavior is by tacking on @@ -10,18 +10,18 @@ class MainPortal(Exit): #move_special attribute, then run it. def move_special(self, inventory): self.inventory = inventory - if ("keycard" in self.inventory.inventory.keys()) and ("parka" in self.inventory.inventory.keys()): + if ("keycard" in list(self.inventory.inventory.keys())) and ("parka" in list(self.inventory.inventory.keys())): self.is_open = True return("outside") - if ("keycard" in self.inventory.inventory.keys()): - print """ + if ("keycard" in list(self.inventory.inventory.keys())): + print(""" The reader beeps as the light turns green, and the door swings open. Outside, a howling wind whips across waist-deep drifts of snow. It's hard to see anything through the blizzard. You don't think you'd survive long out there without some protection from the cold. - """ + """) return("central_hallway_east") else: - print "You think you'll need a keycard to open that door." + print("You think you'll need a keycard to open that door.") return("central_hallway_east") @@ -43,7 +43,7 @@ def pick_up(self, item, room_items): self.item = item self.room_items = room_items - print "You pick up the %s." % self.item.name + print("You pick up the %s." % self.item.name) self.inventory[self.item.label]= self.item del self.room_items[self.item.label] @@ -53,7 +53,7 @@ def drop(self, item, room_items): self.item = item self.room_items = room_items - print "You drop the %s." % self.item.name + print("You drop the %s." % self.item.name) self.room_items[self.item.label]=self.item del self.inventory[self.item.label] @@ -65,23 +65,23 @@ def look(self, command): if self.item_to_describe == "not_found": return False elif self.item_to_describe.look_special == True: - print self.item_to_describe.description + print(self.item_to_describe.description) return True self.look_special(self.item_to_describe.label) #not necessary to pass the item label currently b/c there's only #one special event per room, but that could change. else: - print self.item_to_describe.description + print(self.item_to_describe.description) return True def list(self): #lists the contents of the inventory, responds appropriately if inventory is empty if self.inventory == {}: - print "You are not carrying anything." + print("You are not carrying anything.") else: - print "You are carrying:" - for i, item in self.inventory.iteritems(): - print item.name + print("You are carrying:") + for i, item in self.inventory.items(): + print(item.name) class Kitchen(Room): @@ -151,7 +151,7 @@ def mentioned_in(command, items_to_search): #the keyword list for each item success = False - for i, item in items_to_search.iteritems(): + for i, item in items_to_search.items(): for word in command: if word in item.keywords: exit_to_try = item diff --git a/Game/mobiles.py b/Game/mobiles.py index d19db24..7379b93 100644 --- a/Game/mobiles.py +++ b/Game/mobiles.py @@ -20,12 +20,12 @@ def move(self, exit): self.exit = exit # check to see if the exit is valid/open if self.exit.label == 'not_found': - print "You can't go that way." + print("You can't go that way.") elif self.exit.shall_pass(self): - print "You go through the %s to the %s.\n" % (self.exit.name, self.exit.direction) + print("You go through the %s to the %s.\n" % (self.exit.name, self.exit.direction)) self.new_location = self.exit.destination else: - print "You can't go that way." + print("You can't go that way.") def take(self, item): #called if the player is trying to pick something in the room up @@ -33,11 +33,11 @@ def take(self, item): self.item = item if self.item.label == "not_found": - print "I don't see one of those to pick up.\n" + print("I don't see one of those to pick up.\n") elif self.item.type != "carryable": - print "You can't pick that up.\n" + print("You can't pick that up.\n") else: - print "You pick up the %s.\n" % self.item.name + print("You pick up the %s.\n" % self.item.name) self.inventory.add(self.item) del self.location.items[self.item.label] @@ -47,7 +47,7 @@ def drop(self, item): self.item = item if self.item.label == 'not_found': - print "You're not carrying one of those.\n" + print("You're not carrying one of those.\n") else: self.inventory.remove(self.item) self.location.items[self.item.label] = self.item @@ -57,7 +57,7 @@ def can_see(self): #defines the items the mobile can see, for use with the "look" command #adds together the items and exits in the room along with inventory items - self.things_to_look_at = dict(self.location.items.items()) + self.things_to_look_at = dict(list(self.location.items.items())) self.things_to_look_at.update(self.location.exits) self.things_to_look_at.update(self.inventory.inv_list) #and here naming the dict of items "items" gets awkward @@ -81,7 +81,7 @@ def look_special(self, item): elif self.item.label == "fridge": self.location.items['syringe'].type = "carryable" else: - print "Look special failed." #shouldn't happen + print("Look special failed.") #shouldn't happen def use(self, item): #called if the parser thinks the player is trying to use an item. @@ -101,22 +101,22 @@ def use(self, item): self.use_fail() def use_fail(self): - print "You don't see how to do that." + print("You don't see how to do that.") def cut(self): if self.inventory.has('scalpel') == False: - print "You don't have anything to cut that with." + print("You don't have anything to cut that with.") elif self.can_cut_key() == False: - print "Cutting that doesn't seem like it would be a good idea." + print("Cutting that doesn't seem like it would be a good idea.") else: - print "You use the scalpel to cut the key free of the frozen, swollen fingers. The flesh is stiff and bloodless." + print("You use the scalpel to cut the key free of the frozen, swollen fingers. The flesh is stiff and bloodless.") self.location.items['key'].type = "carryable" def can_cut_key(self): if self.location.label != 'garage': return False - elif ('key' in self.location.items.keys()) == False: + elif ('key' in list(self.location.items.keys())) == False: return False elif self.location.items['key'].type == 'hidden': return False @@ -125,27 +125,27 @@ def can_cut_key(self): def unlock_core(self): if self.inventory.has('key') == False: - print "You don't have anything that fits in the lock." - elif self.location.label != 'reactor' or ("core" in self.location.items.keys()) == False: - print "You don't see anything that key would unlock." + print("You don't have anything that fits in the lock.") + elif self.location.label != 'reactor' or ("core" in list(self.location.items.keys())) == False: + print("You don't see anything that key would unlock.") else: - print "You put the key into the lock on top of the cylinder and twist. There's a hiss as the top angles open." + print("You put the key into the lock on top of the cylinder and twist. There's a hiss as the top angles open.") del self.location.items['core'] self.location.items['open_core'].type = "scenery" self.location.items['rod'].type = "carryable" def inject(self): if self.inventory.has('syringe') == False: - print "You don't have anything to inject." + print("You don't have anything to inject.") else: - print "It's clear what you have to do. You grit your teeth and plunge the syringe into your chest. You probably can't really feel the spread of the liquid burning through your arteries, but if feels like you can." + print("It's clear what you have to do. You grit your teeth and plunge the syringe into your chest. You probably can't really feel the spread of the liquid burning through your arteries, but if feels like you can.") self.injected = True def fix(self): if self.inventory.has('rod') and self.location.label == 'garage': - print "You slide the core into the cylinder on the side of the modified snowmobile. It slides into place snugly and the snowmobile's electronics blink to life." + print("You slide the core into the cylinder on the side of the modified snowmobile. It slides into place snugly and the snowmobile's electronics blink to life.") self.victory = True elif self.location.label == 'garage': - print "The snowmobile is totally inert. The gas tank and battery have both been removed, and a strange cylindrical assembly mounted on the side. It will need some other power source." + print("The snowmobile is totally inert. The gas tank and battery have both been removed, and a strange cylindrical assembly mounted on the side. It will need some other power source.") else: - print "You really don't think that really needs that kind of power." \ No newline at end of file + print("You really don't think that really needs that kind of power.") \ No newline at end of file diff --git a/Game/rooms.py b/Game/rooms.py index c54ac60..501b130 100644 --- a/Game/rooms.py +++ b/Game/rooms.py @@ -15,22 +15,22 @@ def setup(self, config): def extra_description(self): - print '\n' + print('\n') #prints description of items and exits - for i, item in self.items.iteritems(): + for i, item in self.items.items(): if item.type != 'hidden': - print "There is a %s here." % item.name - for i, exit in self.exits.iteritems(): - print "There is a %s to the %s." % (exit.name, exit.direction) + print("There is a %s here." % item.name) + for i, exit in self.exits.items(): + print("There is a %s to the %s." % (exit.name, exit.direction)) def describe_verbose(self): #prints the verbose room description - print "\n%s" % self.verbose_description + print("\n%s" % self.verbose_description) self.extra_description() def describe_terse(self): #prints the terse room description - print "\n%s" % self.terse_description + print("\n%s" % self.terse_description) self.extra_description() def describe(self): @@ -50,14 +50,14 @@ def add_items(self, item_list): #of items whose location matches the room self.item_list = item_list self.items = {} - for key, item in self.item_list.iteritems(): + for key, item in self.item_list.items(): if item.location == self.label: self.items[item.label] = item def add_exits(self, exit_list): self.exit_list = exit_list self.exits = {} - for key, exit in self.exit_list.iteritems(): + for key, exit in self.exit_list.items(): if exit.location == self.label: self.exits[exit.label] = exit @@ -69,7 +69,7 @@ def create_room(config): def populate(): all_rooms = {} - f = open("data/rooms.csv", "rb") + f = open("data/rooms.csv", "r") reader = csv.DictReader(f) for config in reader: diff --git a/tests/Game_tests.py b/tests/Game_tests.py index b46b97d..2e89b03 100644 --- a/tests/Game_tests.py +++ b/tests/Game_tests.py @@ -129,7 +129,7 @@ def test_engine(): engine.simulate_play(move_and_drop) assert_equal(engine.player.location.label, 'central_hallway_west') - assert_equal('scalpel' in engine.player.location.items.keys(), True) + assert_equal('scalpel' in list(engine.player.location.items.keys()), True) assert_equal(len(engine.player.inventory.inv_list), 0) #player is in west hall and has dropped scalpel @@ -185,7 +185,7 @@ def test_engine(): engine.simulate_play(find_key) assert_equal(engine.player.location.label, 'garage') - assert_equal('key' in engine.player.location.items.keys(), True) + assert_equal('key' in list(engine.player.location.items.keys()), True) assert_equal(engine.player.location.items['key'].type, 'scenery') #player is in garage, key is visible but can't be taken