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]]