jeudi 13 août 2020

How can i append to list randomly

i am making a small game that has a map and Tower in it denoted by "T". I am trying to place the 4 towers excluding the one thats alrady in the list, randomly with at least 3 spaces apart but i cant figure out how to. This is the list that would print out the map:

world_map = [['T', ' ', ' ', ' ', ' ', ' ', ' ', ' '],\
             [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],\
             [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],\
             [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],\
             [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],\
             [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],\
             [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],\
             [' ', ' ', ' ', ' ', ' ', ' ', ' ', 'K']]

All i can do is this:

def random_tower():
    for i in range(4):
        row = randint(0,len(world_map))
        col = randint(0,len(world_map))
        world_map[row][col] = "T"



Aucun commentaire:

Enregistrer un commentaire