How to identify the values of indexes around a specific array coordinate?
<p>If I had a 2-D array, representing a world, that looked something like: </p>

<pre><code>. . . . . . . . . . . . . . .
. P . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . t . . . . . . . . .
. . . . . t . . . . . . . . .
. . . . . . t . . . . . . . .
. . . . . . . t . . . . . . .
. . . . . . . t t . . . . . .
. . . . . m . . . . . . . . .
. . . . . . m m . . . . . . .
. . . . . . . . m m . . . . .
. . . . . . . . . . m . . . .
. . . . . . . . . . m . . . .
. . . . . . . . . . . m . . .
. . . . . . . . . . . . . . .
</code></pre>

<p><code>m</code> stands for <code>mountains</code>, <code>t</code> for <code>trees</code> and <code>P</code> for <code>Player</code>.</p>

<p>Now, let's say we have a <code>Player</code> class that tracks the <code>userpos</code> or user position in an array, which is currently <code>(1, 1)</code>:</p>

<pre><code>class Player(object):
    def __init__(self, x, y, array):
        self.x = x
        self.y = y
        self.userpos = array[x][y] 
</code></pre>

<p>If I were to write a method inside of the <code>Player</code> class that deals with basic movement controls to edit the location of the <code>P</code> value inside of the array, how could I go about tracking the 8 tiles around the <code>P</code> value? For example, we can see all of the tiles around the current <code>player</code> <code>userpos</code> is all <code>.</code>'s. </p>

<p>I want to restrict the player from moving onto <code>m</code> tiles, being that they are mountains. It is easy to identify the current tile with the <code>userpos</code> variable, since the <code>P</code> value is only added to a string version of the map, and not the actual array. However, the issue comes from trying to move the player to a previous position. I want to be able to return the player to the nearest <code>empty</code> or <code>.</code> tile, but do not know how to decide what index inside of the array that the player should be moved to.  </p>

<p>Is there an easier way of doing this, or would tracking the tiles around the player be the easiest way?</p>

<p>I think a better solution would be to have a method which tries to move the player in the desired direction and have that method be outside the player class since the player class shouldn't know about the map (code not tested):</p>

<pre><code>def try_to_move_player(direction,map,player):
    desired_x = player.x
    desired_y = player.y

    if direction == "Left":
        desired_x -= 1
    elif direction == "Right":
        desired_x += 1
    #etc

    #Prevent player from moving off map left or right
    if desired_x &lt; 0 or desired_x &gt; len(map): #&lt;-- map length might not be right
        return False
    #do the same for going above or below

    #Prevent the player from moving into mountains
    if map[desired_x][desired_y] == "m":
        print "Tried to walk into a mountain"
        return False

    #After all incorrect moving is taken care of
    player.x = desired_x
    player.y = desired_y
    return True

class Player(object):
    def __init__(self, x, y, array):
        self.x = x
        self.y = y
        self.userpos = array[x][y] 

#Try to move the player left
moved_successfully = try_to_move_player("Left",map,player)
if moved_successfully:
    print "The player was moved successfully w/ an updated position"
else:
    print "The player was not moved correctly"
</code></pre>

python arrays python-2.7 multidimensional-array