1

I'm trying to test a simple 2d map coordinate generator in python. It creates a Tile objects with x and y array so that I can access the coordinates and modify their properties.

This creates the map object and fills it with tiles in a 2D coordinate plane

map = [[ Tile(True)
 for y in range(MAP_HEIGHT) ]
 for x in range(MAP_WIDTH) ]

The tile class:

class Tile:
 #a tile of the map and its properties
 def __init__(self, blocked, type, owner, block_sight = None):
 self.blocked = blocked
 self.type = type
 self.owner = owner 
 if block_sight is None: block_sight = blocked
 self.block_sight = block_sight

I attempted to have the program read a text file character by character to create a map. It would insert an object at the coordinates provided by mapx and mapy into the map.

mapx = 0
mapy = 0
filename = str(mapn) + '.txt'
new_map = [[ Tile(True, 0, 0)
 for y in range(MAP_HEIGHT) ]
 for x in range(MAP_WIDTH) ]
with open(filename) as f:
 while True:
 c = f.read(1)
 if not c:
 return new_map
 elif (c == '#'):
 new_map[mapx][mapy].blocked = False
 new_map[mapx][mapy].block_sight = True
 new_map[mapx][mapy].type = 0
 new_map[mapx][mapy].owner = 0

(After several more elifs)

if(mapx < MAP_WIDTH):
 mapx += 1
elif(mapy < MAP_HEIGHT):
 mapy += 1
 mapx = 0

When running this, I get this error: IndexError: list index out of range. It says the line

 new_map[mapx][mapy].blocked = False 

is to fault for this. Any idea what I'm doing wrong?

asked Mar 21, 2013 at 20:16
4
  • Can you show a minimal example that shows your problem? Otherwise the only thing we can say is that you're failing to correctly check when the index is in range. Commented Mar 21, 2013 at 20:21
  • Are you sure that your file has exactly mapx * mapy characters? Commented Mar 21, 2013 at 20:22
  • @tom Oh yes. If the file has more lines, then mapx is never resetted to 0, and with the next loop its value is MAP_WIDTH which is out of range. Adding an else: break or else: mapx = 0 should solve the IndexError (even though I think there is a smarter way to read that file to avoid this thing). Commented Mar 21, 2013 at 20:25
  • This has to be a duplicate of some other question. Commented Mar 21, 2013 at 20:53

2 Answers 2

2

The after initialization the outer list will have have MAP_WIDTH elements with indices from 0 to MAP_WIDTH-1. Similarly, each inner list will have indices from 0 to MAP_HEIGHT-1.

Consider what happens when mapx = MAP_WIDTH-1; ie is at the end of the list using your code:

if(mapx < MAP_WIDTH):
 mapx += 1
....

The value of mapx will become larger than the upper bound of the list.

The test should be mapx < MAP_WIDTH-1 to resolve this. The test in the elif also needs to be changed accordingly.

answered Mar 21, 2013 at 20:29

Comments

0

It would be more pythonic if you first read the file and create a list [ ... (xi, yi) ... ] and then create a loop over the (xi,yi) to instanciate the Tile objects. This way, you would avoid to have to think about the list indices.

Just my 2 cts...

answered Mar 21, 2013 at 20:45

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.