Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

randomizing and compairing

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:


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.
 
hi, i dont know too much of that rgss script, but i may be of a lil help,

the redo is a function i dot understand, so why dont use a while (counter < @char.size -1) and you increase the counter in the else section of the if statement, then it will pass the while when it get the characters in different position. Be sure to set the counter to 0 before the while and to know that @char doesnt have garbage for the comparisson, when in the first loop, you set random positions to char[0] and compare with the others, but do the others have valid coordinates by that time?

also when doing it for too many characters it will become very uneffective bacause you would loop a lot of times if the coordinates it gets match the coordinates of previous characters, maybe you can have an array of valid coordinates and just chose randomly an index and take out that element of the array, them the next loop chosse another randomly so that you get valid different coordinates in just one loop.

Again i dont know too much of that script but will learn soon, when i have some tme':|
 
There's an easiest way to do this actually :

Code:
size = $game_party.actors.size
indexes = []
for i in 0...size
  indexes.push(i)
end
indexes.sort! {|a,b| a + rand(size) <=> b + rand(size)}

for i in 0...size
  actor = $game_party.actors[i]
  actor.position_x = indexes[i]
  actor.position_y = indexes[i]
  # or whatever method you defined or use...
end
 
Thanks Squall, I really appreciate it.

but I really dont understand this line:
Code:
indexes.sort! {|a,b| a + rand(size) <=> b + rand(size)}

I know what the sort! and rand functions do, but I dont understand the way you fed sort! the arguments.

2) in the second "for" loop you used "$game_party.actors.size" instead of "size". Was there a reason for this?

3) in the second "for" loop you said "actor = $game_party.actors[1]" instead of "actor = $game_party.actors". Was this a typo?

I am still learning RGSS so sorry for the detailed questions. I want to make sure I get it right.

And also thanks Zero, that was a much better way to approach the logic.
 
1) actually it make the program sort the items that way :
if item_a + a random number is greater than item_b + a random number item_a is placed before item_b in the array.
it's a simple way of randomizing numbers in an array.

2) no, I just forgot, I stored it into a variable three lines above XD

3) err, yeah that's a typo too. I'm writing things too quickly sometimes :P


to take an example, the first loop make an array filled with numbers from 0 to $game_party.actors.size

and the sort method just mess their position in the array.

ary = [0, 1, 2, 3]

# sort blahblah

ary # [2, 1, 0, 3]

that's just an example, the output will be completely random
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top