#!/usr/bin/python

def read_data():
    with open("adventure.data") as f:
        grid = []
        s = f.readline()
        while len(s.strip()):
            grid.append(s.strip())
            s = f.readline()
        contents = map(lambda s: s.strip(), f.readlines())
    return grid, contents

def explore(grid, contents):
    directions = {"west": (-1,0),
                  "north": (0,-1),
                  "east": (1,0),
                  "south": (0,1)}
    x = 1
    y = 1
    goal_x = len(grid[0])-2
    goal_y = len(grid)-2
    width = (len(grid[0])-1)/2
    height = (len(grid)-1)/2
    while x!=goal_x or y!=goal_y:
        cell_x = (x-1)/2
        cell_y = (y-1)/2
        here = contents[cell_x+cell_y*width]
        print("You are in the %s." % here)
        print("You can go: ")
        legal_directions = []
        for name in directions:
            (dx,dy) = directions[name]
            there = contents[cell_x+dx+(cell_y+dy)*width]
            if grid[y+dy][x+dx] == '.':
                print("  %s to the %s" % (name, there))
                legal_directions.append(name)
        choice = raw_input("Which way? ")
        while choice not in legal_directions and choice != "jump":
            print("That is not a valid choice.")
            choice = raw_input("Which way? ")
        if choice == "jump":
            location = raw_input("Jump to where? ")
            nx = 1
            ny = 1
            found = False
            for place in contents:
                if place == location:
                    print "Going to the %s..." % location
                    x = nx
                    y = ny
                    found = True
                    break
                else:
                    nx += 2
                    if nx > goal_x:
                        nx = 1
                        ny += 2
            if not found:
                print("That place does not exist!")
        else:
            (dx,dy) = directions[choice]
            x += dx*2
            y += dy*2
    print("You have found the secret treasure room!")
    print("Don't you feel accomplished?")

def main():
    grid, contents = read_data()
    explore(grid, contents)
    
main()
