def get_valid_direction(position): """ :param position: a tuple with the coordinates of where the player currently is :return: the list of directions that are attainable from where the player currently is """ valid_directions = {} for k, v in directions.iteritems(): possible_position = (position[0] + v[0], position[1] + v[1]) possible_location = locations.get(possible_position) if possible_location: valid_directions[k] = possible_position return valid_directions
def got_to(directions, position): direction_error = directions position_error = position location = locations[position] print('you are at the %s' % location) valid_directions = {} for k, v in directions.items(): possible_position = (position[0] + v[0], position[1] + v[1]) possible_location = locations.get(possible_position) if possible_location: print('to the %s is a %s' % (k, possible_location)) valid_directions[k] = possible_position direction = input('which direction do you wanna go\n') try: position = valid_directions[direction] got_to(directions, position) except KeyError: print('invalid location entered! Try again\n') got_to(direction_error, position_error)
'west': (-1, 0), 'east': (1, 0), 'north': (0, -1), 'south': (0, 1), } position = (0, 0) new_position = {} while True: location = locations[position] print 'you are at the %s' % location valid_directions = {} for k, v in directions.iteritems(): possible_position = (position[0] + v[0], position[1] + v[1]) possible_location = locations.get(possible_position) if possible_location: print 'to the %s is a %s' % (k, possible_position) valid_directions[k] = possible_position direction = raw_input('which direction do you want to go? \n') new_position = valid_directions.get(direction) if new_position: position = new_position else: print "Sorry, you are lost!"
'west': (-1, 0), 'east': (1, 0), 'north': (0, -1), 'south': (0, 1), } position = (0, 0) while True: location = locations[position] print 'you are at the %s' % location valid_directions = {} for k, v in directions.iteritems(): possible_position = (position[0] + v[0], position[1] + v[1]) possible_location = locations.get(possible_position) if possible_location: print 'to the %s is a %s' % (k, possible_location) valid_directions[k] = possible_position # print map for k, v in valid_directions.iteritems(): print locations.get(v) direction = raw_input('which direction do you want to go?\n') new_position = valid_directions.get(direction) if new_position: position = new_position else: print "sorry, that isn't a valid direction"
from data import locations directions = { 'west': (-1, 0), 'east': (1, 0), 'north': (0, -1), 'south': (0, 1), } position = (0, 0) while True: location = locations[position] print "you are at the %s" % location valid_directions = {} for k, v in directions.iteritems(): possible_position = (position[0] + v[0], position[1] + v[1]) possible_location = locations.get(possible_position) if possible_location: print "to the %s is a %s" % (k, possible_location) valid_directions[k] = possible_position direction = raw_input("which direction do you want to go?\n") new_position = valid_directions.get(direction) if new_position: position = new_position else: print "sorry, that isn't a valid direction"
from data import locations direction = { 'west': (-1, 0), 'east': (1, 0), 'north': (0, -1), 'south': (0, 1), } position = (0,0) while True locations= locatiomns[position] print 'you are at the %s' % location valid_directions = {} for k, v in directions.interitems(): possible_position = (position[0] + v[0], posistion[1] + v[1]) possible_locations = locations.get(possoble_postion) if possible_location: print 'to the %s is a %s' % (k, possible_location) valid_directions[k] = posible_posistion direction = raw_input('which direction do you want go?\n') postion = valid directions[direction]
'south': (0, 1), } position = (0, 0) # starting position while True: location = locations[ position] # find the name of our location by using locations dictionary we imported print 'you are at the %s' % location valid_directions = {} for k, v in directions.iteritems( ): # when iterating through dictionary its helped to read both keys and values possible_position = (position[0] + v[0], position[1] + v[1] ) # for each direction we calculate new position possible_location = locations.get( possible_position ) # check to see if position is in list of locations if possible_location: # if possible locations exists we print it and add it to dicitonary print 'to the %s is a %s' % (k, possible_location) valid_directions[k] = possible_position direction = raw_input('Which direction do you want to go?\n' ) # we ask user for a direction and use new_position = valid_directions.get(direction) if new_position: position = new_position else: print "sorry, that isn't a valid direction"
from data import locations # dictionary of directions if we are currently at (1, 1) moving east will result in (1+1,1+0) = (2,1) directions = { 'west': (-1, 0), 'east': (1, 0), 'north': (0, -1), 'south': (0, 1), } position = (0, 0) # starting position while True: location = locations[position] # returns the actual name of our location imported from data so starts at house print 'you are at the %s' % location # prints the location you are currently at valid_directions = {} for k, v in directions.iteritems(): # k=key v=value. possible_position = (position[0] + v[0], position[1] + v[1]) # calcs the new position if we were to go that way possible_location = locations.get(possible_position) # checks if the new position is in locations if possible_location: # if the possible location exists print 'to the %s a %s' % (k, possible_location) # prints the key from directions & location from data valid_directions[k] = possible_position # add the location to a dictionary direction = raw_input('which direction do you want to go?\n') # ask the user for a direction if direction in valid_directions: position = valid_directions[direction] # passes the direction to the function else: print 'this is an invalid direction'