## A simple treasure hunt game by Prof. Patterson
#

# Define a table which has the map in it
# Players can walk on spaces and are trying to find the O
# Notice that when you index the maze you specify columns first
# so it's maze[y][x] not (x,y)

maze = [
["+","-","-","-","-","-","-","-","-","-","+"],
["|"," "," "," "," "," "," "," "," "," ","|"],
["|","-","-","+"," ","+","+"," ","|"," ","|"],
["|"," "," ","|"," "," ","|"," ","|"," ","|"],
["|"," ","+","+"," ","-","+"," ","|"," ","|"],
["|"," ","|"," "," "," "," "," ","|"," ","|"],
["|"," ","+","*"," ","+","+"," ","|"," ","|"],
["|"," "," "," "," "," "," "," ","|"," ","|"],
["+","-","-","-","-","-","-","-","+"," ","|"],
["|","O"," "," "," "," "," "," "," "," ","|"],
["+","-","-","-","-","-","-","-","-","-","+"],
        ]

## This function returns true if the spot indicated in the maze
# is able to be walked on by the character
def goodLocation(maze,x,y):

    if(maze[y][x] == " "):
        #Spaces are okay to walk on
        return True
    elif(maze[y][x] == "O"):
        #The treasure is okay to walk on
        return True
    else:
        #Everything else is a wall
        print("blocked at %d %d"%(x,y))
        return False


## This function will draw a version of the maze that the player
# should see.  The maze and the player's current position are the inputs
def printMaze(maze,x,y):

    #This is just for debugging
    print("You are at (%d,%d)" %(x,y))

    #The outer loop goes through the columns or "y"
    for i in range(len(maze)):

        #The inner loop goes through the rows or "x"
        for j in range(len(maze[0])):
            #If the current spot we are drawing is where
            # the player is, the print an "e"
            if(i == y and j == x):
                print("e",end="")
            else:
                #If the position is the treasure
                #hide it
                if(maze[i][j] != "O"):
                    print(maze[i][j],end="")
                else:
                    #All other cases just print what's in the maze table
                    print(" ",end="")
        #At the end of each row print a newline
        print()


#Start execution with player at position 1,1
myX =1 
myY =1

printMaze(maze,myX,myY)
print("which way? asdw")  #Priming prompt
direction = input("")     #Priming read

#sentinel value is "q" to quit
while(direction != "q") :

    # Up which is subtracting 1 from y because of the way we draw
    if(direction == "w"):
        if(goodLocation(maze,myX,myY-1)):
            myY = myY-1
        else:
            print("blocked")

    # Down which adds 1 to y because of the way we draw
    if(direction == "s"):
        if(goodLocation(maze,myX,myY+1)):
            myY = myY+1
        else:
            print("blocked")

    # Left which subtracts 1 from x because of the way we draw
    if(direction == "a"):
        if(goodLocation(maze,myX-1,myY)):
            myX = myX-1
        else:
            print("blocked")

    # Right which adds 1 to x because of the way we draw
    if(direction == "d"):
        if(goodLocation(maze,myX+1,myY)):
            myX = myX+1
        else:
            print("blocked")

    # Show maze after processing input
    printMaze(maze,myX,myY)

    #Check for a win
    if(maze[myY][myX] == "O") :
        direction = "q"
    else :
        #modification read
        direction = input("")

#After loop check to see if the reason we quit was a win
if(maze[myY][myX] == "O") :
    print("You found the hidden treasure!")



