samedi 13 novembre 2021

How to make a function that lays mines to random squares on nested list?

The field is created like this:

field = []
for row in range(10):
   field.append([])
   for col in range(15):
      field[-1].append(" ")

Tuples represent free squares where mines can be layed

free = []
for x in range(15):
   for y in range(10):
      free.append((x, y))

I have to lay the mines trough this function:

def lay_mines(field, free, number_of_mines):

    for _ in number_of_mines:
        mines = random.sample(free, number_of_mines)
        field(mines) = ["x"]

I was thinking using random.sample() or random.choice(). I just can't get it to work. How can I place the string "x" to a certain random coordinate?




Aucun commentaire:

Enregistrer un commentaire