nathan tech wrote:

> Hi there,
> 
> So I am running over some coding ideas in my head for creating a map for
> a game.
> 
> This map would expand based on how far the user explores.
> 
> I figure there are two ways to do this:
> 
> 1: the list method:
> 
> map=[]
> 
> for x in range(3):
> 
>  temp=[]
> 
>  for y in range(3):
> 
>  temp.append(default_grid_format)
> 
>  map.append(temp)
> 
> 
> then when ever the user explores a square not on the current map, it
> would do this:
> 
> for x in range(len(map)):
> 
>  map[x].append(default_grid_format)
> 
> temp=[]
> 
> for x in range(len(map[0])):
> 
>  temp.append(default_grid_format)
> 
> map.append(temp)
> 
> Obviously, though, this creates a lot of data for squares that are still
> ultimately unexplored.
> 
> So here was my other idea:
> 
> 
> 2. the dictionary method:
> 
> map={}
> 
> for x in range(3):
> 
>  for y in range(3):
> 
>  key=str(x)+":"+str(y)
> 
>  map[key]=default_grid_format
> 
> 
> Then when user explores new square do:
> 
> key=str(player_x)+":"+str(player_y)
> 
> map[key]=default_grid_format
> 
> 
> Is this an efficient method compared to 1?
> 
> Is it, code wise, sound logic?
> 
> 
> 
> I guess I'm just looking for a second opinion from experienced peoples.
> 
> thanks everyone.

Forget about "efficiency", try to write clean code.
For the above samples this means

- no unused data
- no unnecessary stringification

If you base your code on a defaultdict cells spring into existence as 
needed:

$ cat tmp.py
from collections import defaultdict

def get_default_format():
   return "whatever"

def show_cell(x, y):
    print(f"x = {x}, y = {y}, format = {grid_formats[x,y]!r}")

grid_formats = defaultdict(get_default_format)
grid_formats[42, 42] = "this cell is special"

player_location = 3, 4
show_cell(*player_location)
show_cell(42, 42)

$ python3.6 tmp.py
x = 3, y = 4, format = 'whatever'
x = 42, y = 42, format = 'this cell is special'


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to