1

For a little rpg game, I would create board that contains a specific number of a value.

Let me introduce an example :

board =[[0, 0, 0, 15, 0, 0, 0, 0, 0, 0],
[0, 0, 15, 0, 15, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 15, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 15, 0, 0, 0, 0, 15, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 15, 0, 0, 0, 0, 0, 15, 0, 0],
[0, 0, 0, 0, 0, 15, 0, 0, 15, 0],
[15, 0, 15, 15, 0, 0, 0, 0, 0, 0]]

Here is a 10 by 10 board with 13 time the value 15.

To have this result, I've use a program that give random coordinates on the board.

b2x = []
b2y = []
for i in range(B):
    x = random.randint(0,L-1)
    y = random.randint(0,H-1)
    b2x.append(x)
    b2y.append(y)
    board[x][y] = 15
    print('x : ',x, ' and y : ', y)

A obvious problem here is we can get two similar coordinates, that will reduce by one the whole number of value.

In fact, in the board of the first example, I won't have the number of values caused I asked 15 values to the program, and it return me 13.

So I try to solve this problem without a coordinates check, that seems not work properly now.

    for j in range(len(bo2x)):
        if (x == b2x[j-1]) and (y == b2y[j-1]):
            i -= 1 # affect the for i in range(B) loop

With the full code :

b2x = []
b2y = []
for i in range(B):
    x = random.randint(0,L-1)
    y = random.randint(0,H-1)
    for j in range(len(bo2x)):
        if (x == b2x[j-1]) and (y == b2y[j-1]):
            i -= 1 # affect the for i in range(B) loop
    b2x.append(x)
    b2y.append(y)
    board[x][y] = 15
    print('x : ',x, ' and y : ', y)

As a result, after multiple tries, of no changes at all :

random generation
x :  5  and y :  4
x :  1  and y :  3
x :  7  and y :  7
x :  7  and y :  5
x :  0  and y :  7
x :  0  and y :  1
x :  6  and y :  2
x :  3  and y :  6
x :  9  and y :  4
x :  5  and y :  9
x :  6  and y :  8
x :  6  and y :  7
x :  3  and y :  6
x :  3  and y :  7
x :  7  and y :  5
[Finished in 0.2s]

As you can see, the line x : 7 and y : 5 and the line x : 3 and y : 6 appears twice in the generation.

Can someone could help me to reach my expected result ?

EXPECTED RESULT (concept):

x :  5  and y :  4
x :  1  and y :  3
x :  7  and y :  7
x :  7  and y :  5
x :  0  and y :  7
x :  0  and y :  1
x :  6  and y :  2
x :  3  and y :  6
x :  9  and y :  4
x :  5  and y :  9
x :  6  and y :  8
x :  6  and y :  7
x :  3  and y :  6
this line already exist !
x :  3  and y :  7
x :  7  and y :  5
this line already exist !
x :  1  and y :  3 
x :  9  and y :  4

board :
[[0, 15, 15, 0, 0, 0, 0, 15, 0, 0],
[0, 0, 0, 15, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 15, 15, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 15, 0, 0, 0, 0, 15],
[0, 0, 15, 0, 0, 0, 0, 15, 15, 0],
[0, 0, 0, 0, 0, 15, 0, 15, 0, 0],
[0, 0, 0, 15, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 15, 0, 0, 0, 0, 0]]
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    You could shorten your question by 90% by asking how to generate a list of random numbers without repetition... And here is an answer for that: [your answer](https://stackoverflow.com/a/9755548/6045800) – Tomerikoo Jul 07 '19 at 16:27
  • Is it working for coordinates ? I don't find how it can solve the problem : it's create random number wihout repetition but not random coordinates wihout repetitions... –  Jul 07 '19 at 16:32
  • Possible duplicate of [How to generate random pairs of numbers in Python, including pairs with one entry being the same and excluding pairs with both entries being the same?](https://stackoverflow.com/questions/30890434/how-to-generate-random-pairs-of-numbers-in-python-including-pairs-with-one-entr) – Valentino Jul 07 '19 at 18:22
  • @Edhyjox OK, well here is [one](https://stackoverflow.com/questions/30610885/generate-a-large-list-of-points-with-no-duplicates), no [two](https://stackoverflow.com/questions/29999759/python-generating-a-non-repeating-random-pairs-of-numbers) answers that are exactly the same. My point was, search a little and don't try to be spoon-fed with answers – Tomerikoo Jul 07 '19 at 21:27

1 Answers1

2

You want to create a LxB board.

L, H = 10, 10

This can be achieved by one line of code:

board = [[0] * L for _ in range(H)] 

You want to place a certain number 15 times on the board, on different places.
Generate random coordinates, but skip the coordinates if the number is already assigned to the indexed field:

count = 15
number = 15
b = []
while count > 0:
    x, y = random.randint(0,L-1), random.randint(0,H-1)
    if board[y][x] != number:
        board[y][x] = number
        b.append((x, y))
        count -= 1

for row in board:
    print(row)
print(b)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • @Edhyjox I though you'll need the index tuples. I've you don't need them further, then `b` is useless. – Rabbid76 Jul 08 '19 at 11:28