0

I am currently trying to implement Conway's Game of Life in a Code, and therefore built a function which generates the coordinates depending of the size of the window.

def coords_maker(num_x, num_y):

    num_x += 1  
    num_y += 1  
    coords = []
    for i in range (0,num_y, 1):    
        for n in range (0,num_x,1):
            coords.append ('x'+str(n)+'y'+str(i))   
    return coords

Yet, I would like to randomly assign values to the resulting strings, to mark them either as alive (1) or dead (0). However they only way to convert a string to a variable name known to me is via a dict and var(), but however, it is essential for the further code that the coordinates stay sorted, as I want to be able to iterate over the ordered items and put the cursor accordingly to the coordinates name. Something like:

print ('\033['+X_COORD+';'+Y_COORD+'f'+ x1y5) 

if e.g. x1y5 is the corresponding value (0 or 1) of the variable

Is there a convenient method how to either do this via a dict or how to convert the name of the strings to variable names? Or probably. If I keep one dict and one list and store the coordinate names in the list and the values in the dict?

Thank you in advance! kyril

1
  • 1
    Why not use a 2 dimensional array or list of lists? Commented Jun 20, 2012 at 15:26

1 Answer 1

1

You use a dictionary:

def coords_maker(num_x, num_y):

    num_x += 1  
    num_y += 1  
    coords = {}
    for i in range (0,num_y, 1):    
        for n in range (0,num_x,1):
            coords['x'+str(n)+'y'+str(i)] = 0
    return coords

You then access the value with

 coords[x][y]

And change it like so:

 coords[x][y] = 1

Now, of course this converting of coordinates to strings is completely pointless. Simply use a list of lists:

def coords_maker(num_x, num_y):

    num_x += 1  
    num_y += 1  
    coords = [[0]*num_x for x in range(num_y)]
    return coords

And I don't know why you add 1 to the coordinates either:

def coords_maker(num_x, num_y):
    return [[0]*num_x for x in range(num_y)]
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your fast reply. But how do i solve the problem that the dict is unsorted?
@kyril: Why would that be a problem? But anyway, I added some less complicated ways of doing this.
Thanks, the last part actually does solve the problem. I just did not know how to do it otherwise. I added +1 to the values so that if I want the window with cells 5 chars wide, range would not stop at 4. One last question: what stands the [0]*num_x in the list comprehension for? in particular the [0]. thanks!
@kyril: [0] is a list with a 0 in it. I suggest you go through some Python tutorials to get the basics of the language. It really helps.
@kyril: It's a list. In that context. It doesn't do anything. Multiplying it will create a list with N elements, so [0]*5 creates a list like this: [0, 0, 0, 0, 0]. Python is interactive, open a prompt and type it in and see what happens. Best way.

Your Answer

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.