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.

HBGames

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.

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