1

I have looked around but cannot find anybody asking what I'm trying to do:

Let me give you a bit of background:

I am making an game in python where the player moves around the grid searching for treasure chests, and I am trying to randomly generate 10 chest locations in a 8x8 grid.

My grid was created by doing the following:

grid = []

I then fill the grid with 'subgrids'

Because my game is based around a grid design, this allowed me to separate the individual rows.

for i in range(8):
    grid.append([])

This makes me 8 empty lists inside the main 'grid' list.

What I am trying to do next is randomly generate the chest locations, and map them to another list called 'chestLocations', which also uses 10 subgrids (one for each unique chest location) . This is so I can create Y and X variables, which are relative to the grid list.

Here is my GenerateChestLocations() function:

def GenerateChestLocations():
global chestY
global chestX
counter = 10

chestY = []
chestX = []

while counter > 0:

    posY = random.randint(0,7)
    posX = random.randint(1,8)

    value = GetValue(posY,posX)

    if value == gridChar:
        pass
    elif value == playerChar:
        continue

    chestY.append(posY)
    chestX.append(posX)
    counter -= 1

for a in range(len(chestY)):
    chestLocations[a].append(chestY[a])
    visitedChests[a].append(chestY[a])
for i in range(len(chestX)):
    chestLocations[i].append(chestX[i])
    visitedChests[i].append(chestX[i])

for subItem in range(len(visitedChests)):
    visitedChests[subItem].append(0)

return

(BTW, the variables used in this are defined at the start of my program, and are as follows:)

The GetValue() function just returns the value of the grid item for those Y and X coordinates.

visitedChests is another grid, which needs to be an exact duplicate of chestLocations, but with an extra item in each 'subgrid' to hold the number of times that the user has landed on the chest.

My problem is I cannot workout how to detect whether the randomly generated posY and posX integers are already existing in the chestLocations list.

How do I create detection so if it already finds an item with the same coordinates, it will just 'continue' to run the whole while loop again?

Thanks for reading btw ;)

3
  • Can't you use if posY in chestY and posX in chestX: It's already used? Commented Feb 3, 2016 at 22:20
  • Dayum, guess I was looking to global. That helps. Thanks a lot. Maybe when I generate the bandits as well (in a different function) I will just use a for loops to feed the items back out of the chestLocations array and split X and Y back up. Commented Feb 3, 2016 at 22:24
  • Just tried and it doesn't work, because it is checking for the Y and X axis on their own, instead of together - so the max chests it can create is 8. Need to check the array together to check the posY and posX as one item. Commented Feb 3, 2016 at 22:26

2 Answers 2

1

Use the stdlib:

import random
from itertools import product

num_bandits = 5
num_chests = 10

all_locns = list(product(range(0,8), range(1,9)))
chest_locns = random.sample(all_locns, num_chests)

unused_locns = [loc for loc in all_locns if loc not in chest_locns]
bandit_locns = random.sample(unused_locns, num_bandits)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you a lot. How would I go about doing the same thing for making the bandits, checking existing chest locations?
0

Try:

index = 0
while posX in chestX[index:]:
    position = chestX[index:].index(posX)
    if posY == chestY[index + position]:
        #Can't be used.  (maybe say used = True)
        break
    index += position + 1

1 Comment

For some reason it still only makes a maximum of 8 chests - 1 per Y

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.