Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7749419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:02:47+00:00 2026-06-01T11:02:47+00:00

A little background: I’m writing a small word based maze game starting with a

  • 0

A little background:

I’m writing a small word based maze game starting with a simple maze made of X’s, O’s and F.

My maze is a list of lists representing the maze itself where X is a wall, O is an open spot and F is the finish line.

I’m trying to write a function that takes the name of the maze and the user’s current position and returns a list of all legal moves from that position (N, S, E, or W).

Here’s my practice maze and function.

def get_legal_directions(maze, position):
    x = position[0]
    y = position[1]
    legal = []
    if maze[x-1][y] == 'O' or maze[x-1][y] == 'F':
        legal.append('N')
    if maze[x+1][y] == 'O' or maze[x+1][y] == 'F':
        legal.append('S')  
    if maze[x][y+1] == 'O' or maze[x][y+1] == 'F':
        legal.append('E')
    if maze[x][y-1] == 'O' or maze[x][y-1] == 'F':
        legal.append('W')
    return legal


>>> maze1 = [['X', 'X', 'X', 'X', 'X'], ['X', 'O', 'X', 'F', 'X'], ['X', 'O', 'X', 'O', 'X'], ['X', 'O', 'O', 'O', 'X'], ['X', 'X', 'X', 'X', 'X'], ['X', 'X', 'X', 'X', 'X']]
>>>

input: get_legal_directions(maze1, (1,1))

output: ['S']

so the function appears to work normally here, however when I use the function within another function in order for the user to interact with the maze, I get this:

def interact():
    maze = raw_input('Maze File: ')
    x = maze[0]
    y = maze[1]
    pos = (1,1)
    history = [pos]
    while 1:
        print 'You are at position', pos
        command = raw_input('Command: ')
        if command == 'Q':
            com = raw_input('Are you sure you want to quit? [y] or [n]: ')
            if com =='y':
                print 'Thank you for playing - Goodbye!'
                break
            else: continue
        elif command == 'L':
            get_legal_directions(maze, pos)
        else: print 'invalid command'

I get the following:

>>> interact()

Maze File: maze1

You are at position (1, 1)

Command: L

"   if maze[x-1][y] == 'O' or maze[x-1][y] == 'F':
IndexError: string index out of range"

Is there something wrong with the way I’ve written the if statement or is it something else? Thanks to anyone who can help.

How can I get the command L to call the function get_legal_directions()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:02:48+00:00Added an answer on June 1, 2026 at 11:02 am

    This makes maze a string:

    maze = raw_input('Maze File: ')
    

    Your function expects a list of lists instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok guys so a little background... I'm writing a small application that will assign
For a little background, I work for a firm that develops web-based enterprise social
A little background: I'm using Spring and Hibernate to create a really simple domain/dao/service
A little background: I'm a C# developer starting to mess with the iPhone (have
A little background : I'm writing a set of C# classes to wrap a
First a little background. The company I work for writes web based software that
Little background.. I'm in the process of making an OpenGL game using Java and
A little background: I'm creating a set of adapters to allow communication with mobile
A little background: I am loading a WPF UI from a database which is
A little background first. I've been tasked with encrypting files with a Powershell script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.