def examine(self, objects): self.first_examine = False visible_items = [ i for i in objects['Inventory'][self.__class__.__name__] if i.visible ] msg = None for i in visible_items: # For 1 item if i.visible and len(visible_items) == 1: msg = ('{0} has a {1}.'.format(self.name[0], i.name[0])) # For 2 items elif i.visible and len(visible_items) == 2: if visible_items.index(i) == 0: msg = ('{0} has a {1}, '.format(i.name[0])) else: msg += ('and a {0}.'.format(i.name[0])) # For more than 2 items elif i.visible and len(visible_items) > 1: if visible_items.index(i) == 0: msg = ('{0} has a {1}, '.format(i.name[0])) elif visible_items.index(i) != len(visible_items) - 1: msg += (' a {0}, '.format(i.name[0])) else: msg += (' and a {0}.'.format(i.name[0])) if msg == None: print('''{0} doesn't appear to have anything of interest.'''.format( self.name[0])) else: print(msg)
def read(self, objects): print('''+2: Look at the top card of target player's library. You may put that card on the bottom of that player's library. 0: Draw three cards, then put two cards from your hand on top of your library in any order. −1: Return target creature to its owner's hand. −12: Exile all cards from target player's library, then that player shuffles his or her hand into his or her library.''')
def throw(self, objects): current_room_obj = objects['Game'].current_room.__class__.__name__ if self in objects['Inventory']['Actor']: print("You throw the {}.".format(self.name[0])) self.drop() elif self in obj.inventory_map[current_room_obj]: print("You do not have the {}.".format(self.name[0]))
def examine(self, objects): self.first_examine = False self.display_name = ['"Eternal Masters" booster pack'] + \ self.display_name print('''It's a booster pack of Eternal Masters, hot off the presses! It is currently sealed. Who knows what cards could be found if it were to be opened?!''')
def take(self, objects): message = "You shouldn't see this message... item.py take function" if not self.takeable: message = "You cannot take the {}.".format(self.name[0]) elif self in objects['Inventory']['Actor']: message = "You already have the {}.".format(self.name[0]) elif self.contained or self.placed: # Remove the item from the ITEM that contains it, add to inventory room_name = objects['CurrentRoom'].__class__.__name__ for i in objects['Inventory'][room_name]: if i.can_contain: containing_item = i.__class__.__name__ if self in objects['Inventory'][containing_item]: objects['Inventory'][containing_item].remove(self) i.contains_item = False objects['Inventory']['Actor'].append(self) message = "You have taken the {}.".format(self.name[0]) break self.placed = False objects[containing_item].has_item = False self.is_taken = True self.taken() else: room_name = objects['CurrentRoom'].__class__.__name__ objects['Inventory'][room_name].remove(self) objects['Inventory']['Actor'].append(self) message = "You have taken the {}.".format(self.name[0]) print(message)
def quit(objects): ''' Prompts user for verification to exit the game.''' prompt = input("Are you sure you want to quit? " + objects['Game'].prompt_char) if prompt.lower() == 'yes' or prompt.lower() == 'y': print ("It's game over, man! GAME OVER!!\n") raise SystemExit else: print ("OK, nevermind then.")
def token_npc(tokenized_input, objects): ''' Process a command referring to a non-playable character.''' npc = get_value(tokenized_input[0]) increment_moves(objects) if is_present(npc, objects): print('''{} is here.'''.format(npc[0].upper() + npc[1:].lower())) else: print('''There's no one here by that name.''')
def token_noun(tokenized_input, objects): ''' Process a single noun-command.''' noun = get_value(tokenized_input[0]) increment_moves(objects) if is_available(noun, objects): print('''What would you like to do with the {}?'''.format(noun)) else: print('''There is no {} here.'''.format(noun))
def savePrompt(objects): ''' Save a serialized copy of the setup.objects dict to a file.''' file_name = input("Enter a file name > ") file_name += '.tbs' if fileExists(file_name): print("File already exists! Specify a unique name when saving.") else: saveGame(file_name, objects)
def brief(objects): ''' Activates brief descriptions for rooms. Deactivates verbose descriptions.''' if objects['Game'].brief_msg: print("Brief descriptions are already activated.") else: objects['Game'].brief_msg = True objects['Game'].verbose_msg = False print("Brief descriptions are now on.")
def token_verb(tokenized_input, objects): ''' Process a single verb-command. "look" is unique as it can be used with or without an item.''' verb = get_value(tokenized_input[0]) increment_moves(objects) if verb == 'look' or verb == 'l': objects['CurrentRoom'].look(objects) else: print('''What do you want to {}?'''.format(verb))
def token_adjective(tokenized_input, objects): ''' Process a single adjective-command. Similar to the token_noun function.''' adjective = get_value(tokenized_input[0]) increment_moves(objects) if is_available(adjective, objects): print('''What would you like to do with the {}?'''.format(adjective)) else: print('''There is no {} here.'''.format(adjective))
def verbose(objects): ''' Activates verbose descriptions for rooms. Deactivates brief descriptions.''' if objects['Game'].verbose_msg: print("Verbose is already on.") else: objects['Game'].brief_msg = False objects['Game'].verbose_msg = True print("Maximum verbosity.")
def talk(self, objects, item_obj=None): if item_obj == None: print('''What do you want to talk to {0} about?'''.format( self.name[0])) else: try: self.talk_subjects[item_obj.__class__.__name__](objects) except KeyError: print('''"Sorry, I don't know much about the {0}."'''.format( item_obj.name))
def token_adjective_noun(tokenized_input, objects): ''' Process a noun preceeded by an adjective.''' increment_moves(objects) adjective = get_value(tokenized_input[0]) noun = get_value(tokenized_input[1]) noun_obj = is_available(noun, objects) if noun_obj and adjective in noun_obj.adjective: print("What would you like to do with the {}?".format(\ adjective + " " + noun)) else: print('''There is no {} here.'''.format(adjective + " " + noun))
def token_verb_noun_adjective(tokenized_input, objects): ''' Same as token_verb_noun, but includes an adjective before the noun.''' increment_moves(objects) verb = get_value(tokenized_input[0]) noun = get_value(tokenized_input[1]) adjective = get_value(tokenized_input[2]) noun_obj = is_available(noun, objects) if noun_obj and adjective in noun_obj.adjective: command = methodcaller(verb, objects) command(noun_obj) else: print('''There is no {0} {1} here.'''.format(adjective, noun))
def token_verb_noun(tokenized_input, objects): ''' Process an action upon a noun. First, determine if the item is present, then-- if so-- execute it's corresponding verb command.''' increment_moves(objects) noun = get_value(tokenized_input[1]) noun_obj = is_available(noun, objects) if noun_obj: verb = get_value(tokenized_input[0]) command = methodcaller(verb, objects) command(noun_obj) else: print('''There is no {} here.'''.format(noun))
def saveGame(file_name, objects): ''' Save the game file to the 'Saved Games' directory.''' if not os.path.exists('Saved Games'): os.makedirs('Saved Games') with open(os.path.join('Saved Games', file_name), 'wb') as f: pickle.dump(objects, f) if os.path.exists(os.path.join('Saved Games', file_name)): print("Game saved as {0}".format(file_name)) else: print("Unknown error encountered when saving file {0}".format(\ file_name))
def drop(self, objects): ''' Remove the object from the actor's inventory and place it in the current room's inventory.''' if self in objects['Inventory']['Actor']: objects['Inventory']['Actor'].remove(self) room_name = objects['CurrentRoom'].__class__.__name__ objects['Inventory'][room_name].append(self) self.is_taken = False self.dropped = True print("You have dropped the {}.".format(self.name[0])) else: print("You are not carrying the {}.".format(self.name[0]))
def token_verb_direction(tokenized_input, objects): ''' Processes verbs associated with movement, but ultimately just executes a lone direction command.''' increment_moves(objects) verb = get_value(tokenized_input[0]) if verb in ['move']: direction = [] direction.append(list(tokenized_input)[1]) current_game = objects['Game'] token_dir(direction, current_game) else: print('''You used the word {} in a way I don't understand.'''.format(verb))
def token_dir(tokenized_input, objects): ''' Process a single direction command.''' get_direction = methodcaller(get_value(tokenized_input[0]), objects) direction = get_direction(objects['CurrentRoom']) if get_key(direction) is 'message': print(get_value(direction)) elif get_key(direction) is 'movement': increment_moves(objects) objects['CurrentRoom'] = get_value(direction) current_game = objects['Game'] if current_game.verbose_msg: objects['CurrentRoom'].look(objects)
def tutorial_choice(self): print('''Welcome to the game demo! If you haven't played this before then there are a few things you'll need to know. If you have played before then you can totally skip this intro stuff.''') while self.tutorial_mode == None: prompt = input('''Have you played this game before? (y/n) ''' + \ self.prompt_char) if prompt.lower() in ['yes', 'y']: self.tutorial_mode = False elif prompt.lower() in ['no', 'n']: self.tutorial_mode = True else: print('''I didn't understand your input. Type 'yes' or 'no' and press Enter.''')
def token_combined_nouns(tokenized_input, objects): ''' Uses a 'combine' command to allow two items to interact.''' increment_moves(objects) verb = get_value(tokenized_input[0]) noun1 = get_value(tokenized_input[1]) action = get_value(tokenized_input[2]) noun2 = get_value(tokenized_input[3]) noun_obj1 = is_available(noun1, objects) noun_obj2 = is_available(noun2, objects) if noun1 == noun2: print('''There is no... wait, what?''') elif noun_obj1 == None: print('''There is no {} here.'''.format(noun1)) elif noun_obj2 == None: print('''There is no {} here.'''.format(noun2)) else: function_name = '_'.join([verb, action]) m = [mtd for mtd in dir(noun_obj1) if callable(getattr(noun_obj1, mtd))] if function_name in m: command = methodcaller(function_name, noun_obj2, objects) command(noun_obj1) else: print('''You combined the words '{0}' and '{1}' in way I didn't understand'''.format(verb, action))
def print_header(self): ''' Prints the current room's name and number of moves.''' move_msg = "moves: " + str(self.moves) spaces = self.console_width -len(self.current_room.name) -len(move_msg) print(); print() print(self.current_room.name + (" " * spaces) + move_msg) print("-" * self.console_width)
def token_verb_npc(tokenized_input, objects): ''' Process a single verb command when applied to a present NPC.''' increment_moves(objects) verb_cmd = get_value(tokenized_input[0]) npc_cmd = get_value(tokenized_input[1]) npc_obj = is_present(npc_cmd, objects) if npc_obj: if verb_cmd == 'give': print('''What would you like to give to {0}?'''.format( npc_obj.name[0])) else: command = methodcaller(verb_cmd, objects) command(npc_obj) else: print('''There's no one here by that name.''')
def token_verb_npc_noun(tokenized_input, objects): increment_moves(objects) verb_cmd = get_value(tokenized_input[0]) npc_cmd = get_value(tokenized_input[1]) noun_cmd = get_value(tokenized_input[2]) noun_obj = is_available(noun_cmd, objects) npc_obj = is_present(npc_cmd, objects) if noun_obj: if npc_obj: command = methodcaller(verb_cmd, objects, noun_obj) command(npc_obj) else: print('''There's no one here by that name.''') else: print('''There is no {0} here.'''.format(noun_cmd))
def inventory(objects): ''' Formats and prints the current items in the actor's inventory.''' if objects['Inventory']['Actor'] == []: print("You don't have anything in your inventory.") else: item_list = [i for i in objects['Inventory']['Actor']] message = "You are currently carrying " for i in item_list: message += "a {}".format(i.name[0]) if item_list.index(i) + 1 == len(item_list): message += "." elif item_list.index(i) + 2 == len(item_list): message += ", and " else: message += ", " print(message)
def print_header(self): ''' Prints the current room's name and number of moves.''' move_msg = "moves: " + str(self.moves) spaces = self.console_width - len( self.current_room.name) - len(move_msg) print() print() print(self.current_room.name + (" " * spaces) + move_msg) print("-" * self.console_width)
def take_from(self, containing_item, objects): if not self.containable: print("The {0} might be a little too big to be placed in\ another item.".format(self.name[0])) elif not containing_item.can_contain: print("The {0} does not allow anything to be stored in \ it.".format(containing_item.name[0])) elif not containing_item.is_open: print("The {0} does not appear to be open.".format(\ containing_item.name[0])) elif not containing_item.contains_item: print("The doesn't appear to be anything in the {0}."\ .format(containing_item.name[0])) elif containing_item.contains_item: for i in objects['Inventory'][containing_item.__class__.__name__]: if i.name == self.name: self.take() break else: print("The {0} is not inside the {1}".format(\ self.name[0], containing_item.name[0]))
def place_on(self, containing_item, objects): ''' Similar to the place_inside function. Updates the inventory of an item to show that it is "holding" another item.''' if self.taken == False: message = "You are not currently carrying the {0}".format( self.name[0]) elif containing_item.can_contain == False: message = "You cannot place items on the {0}".format(\ containing_item.name[0]) elif self.size > containing_item.size: message = "The {0} is too large to fit on the {1}.".format(\ self.name[0], containing_item.name[0]) elif containing_item.has_item == True: message = "The {0} already has the {1} on top of it.".format(\ containing_item.name[0], self.name[0]) else: containing_item_name = containing_item.__class__.__name__ objects['Inventory'][containing_item_name].append(self) objects['Inventory']['Actor'].remove(self) containing_item.has_item = True self.placed = True message = "The {0} is now on top of the {1}.".format(\ self.name[0], containing_item.name[0]) print(message)
def close(self, objects): ''' Allow door to open if the booster pack has been opened''' if objects['Booster'].is_open == False: print('''The door is already closed, as it should be, unless you have something important to do.''') elif self.is_open == False: print('''The door is already closed.''') else: print('''You close the door.''') self.is_open = False
def give(self, item_obj, objects): ''' Give an item to a NPC. Availability of both is checked in processInput.py''' if item_obj == None: print('''What do you want to give to {0}?'''.format(self.name[0])) elif item_obj.__class__.__name__ in self.accept_items: objects['Inventory']['Actor'].remove(item_obj) objects['Inventory'][self.__class__.__name__].append(item_obj) print('''You have given {0} the {1}.'''.format( self.name, item_obj.name)) else: print('''{0} doesn't seem to want the {1}.'''.format( self.name[0], item_obj.name))
def give(self, objects, item_obj=None): ''' Give an item to a NPC. Availability of both is checked in processInput.py''' if item_obj == None: print('''What do you want to give to {0}?'''.format(self.name[0])) elif item_obj.__class__.__name__ in self.accept_items: objects['Inventory']['Actor'].remove(item_obj) objects['Inventory'][self.__class__.__name__].append(item_obj) try: self.give_events[item_obj.__class__.__name__](objects) except KeyError: print('''You have given {0} the {1}.'''.format( self.name, item_obj.name)) else: print('''"I don't have any need for that thing, you can keep it."''')
def gameLoop(objects): ''' Main game loop. Executes while the Actor is still alive. Get the user's input, create tokens out of it, and process it.''' user_input = [word for word in input(objects['Game'].prompt_char).split()] if user_input: tokenized_input = tokenizer.get_token(user_input) if 'unknown_word' not in tokenized_input[0].keys(): valid_pattern = tokenizer.pattern_match(tokenized_input) if valid_pattern: processInput.executeCommand(tokenized_input, valid_pattern, objects) else: print("I didn't understand that sentence.") else: print("I don't recognize the word {0}.".format( tokenized_input[0]['unknown_word'])) else: print("Pardon?")
def game_over(self, message): print(message, await_input = "\nGame Over!") raise SystemExit
def talk(self, objects): print('''"..."''')
def moves(objects): ''' Displays the number of successful moves that have been made.''' print("You have executed {} valid commands.".format(\ objects['Game'].completed_moves))
def win(self): print("Congrats! You've completed the game!") raise SystemExit
def look(self, objects): ''' Prints the current room's description, and any available items.''' msg = objects['CurrentRoom'].description current_room = objects['CurrentRoom'].__class__.__name__ visible_items = [ i for i in objects['Inventory'][current_room] if i.visible ] for i in visible_items: # For 1 item if i.visible and len(visible_items) == 1: msg += (' There is a {} here.'.format(i.name[0])) # For 2 items elif i.visible and len(visible_items) == 2: if visible_items.index(i) == 0: msg += (' There is a {} '.format(i.name[0])) else: msg += (' and a {} here.'.format(i.name[0])) # For more than 2 items elif i.visible and len(visible_items) > 1: if visible_items.index(i) == 0: msg += ('There is a {}, '.format(i.name[0])) elif visible_items.index(i) != len(visible_items) - 1: msg += (' a {}, '.format(i.name[0])) else: msg += (' and a {} here.'.format(i.name[0])) # For any item(s) found within another item if i.contains_item: stored_item = visible_items if stored_item.visible: msg += (' There is a {0} contained in the {1}.'.format(\ stored_item.name[0], i.name[0])) visible_npcs = [ n for n in objects['Npc_Location'][current_room] if n.visible ] for n in visible_npcs: # For 1 npc if n.visible and len(visible_npcs) == 1: msg += (' {} is here.'.format(n.name[0])) # For 2 npc's elif n.visible and len(visible_npcs) == 2: if visible_npcs.index(n) == 0: msg += ('{} and '.format(n.name[0])) else: msg += (' {} are here.'.format(n.name[0])) # For more than 2 npc's elif n.visible and len(visible_npcs) > 1: if visible_npcs.index(n) == 0: msg += ('{}, '.format(n.name[0])) elif visible_npcs.index(i) != len(visible_npcs) - 1: msg += ('{}, '.format(n.name[0])) else: msg += (' and {} are here.'.format(n.name[0])) print(msg)