I am making a CBS where the characters are on a 4x4 grid. At the start of the battle I am trying to randomize the x and y positions of the characters and then check to see if any of the character's positions are the same, and if they are, randomize them again until they are all on different spots on the grid.
It seemed simple enough but I guess it was harder than I thought. Here is my code:
It doesn't seem to yield the results I want. This is really driving me nuts and if anyone could give some help I would appreciate it.
It seemed simple enough but I guess it was harder than I thought. Here is my code:
Code:
@char = charlist # array holding the character data
@match = false
for i in 0..@char.size - 1 do
@char[i].position_x = rand(1...4) #randomize x position
@char[i].position_y = rand(1...4) #randomize y position
if @char[i].index != 0
for ii in 0..@char[i].index - 1 do
if @char[i].position_x = @char[ii].position_x #if x matches
and @char[i].position_y = @char[ii].position_y #and if y matches
@match = true #a match was found
end
end
if @match = true #if a match was found
redo #redo the for loop
end
end
end
It doesn't seem to yield the results I want. This is really driving me nuts and if anyone could give some help I would appreciate it.