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.

Random variable with destroy instance?

Script Request Template
This is a request for a new script.

Script Title:
Random Variable with Destroy Instance
RMXP:
Detailed Description:
What I'm trying to do is have twelve cards lying face down. When the player clicks the card, it turns face up. I don't want the player to be able to get the same results from turning over one card, as they would for another. I Know how to set the variable to a random value, but what I DON'T know is how to make that value no longer an option in the random selection. It will allow me to select randomly from a range, ie 0-11, but won't give me an option to disclude one value in that range, ie 0-11 but not 5, or 0-4 and 6-11.
 

Zeriab

Sponsor

For doing that I would consider the range as a set where I would pick a random piece from the set and remove it. This way you don't have to worry about ranges. You can use ranges to populate the initial range.

In a more usable form I would suggest converting the range to an array and then delete values when you use them. To make it easier you can extend the Array class with a method for this:

[rgss]class Array
  def destroy_random!
    delete_at(rand(length))
  end
end
 
# Example of how it can be used
array = (0..11).to_a
p array.destroy_random!, array
p array.destroy_random!, array
[/rgss]

You just have to keep track of the array.
Note that the picked number is returned from array.delete_random!

[rgss]class Array
  def delete_random!(times = 1)
    return delete_at(rand(length)) if times == 1
    arr = []
    times.times {arr << delete_at(rand(length))}
    return *arr
  end
end
 
# Example of how it can be used
array = (0..11).to_a
p array.delete_random!, array
p array.delete_random!(5), array
[/rgss]

*hugs*
- Zeriab
 
I'm afraid I'm still having trouble. Could you possible send me a simple example in game format? Or, does anyone out there have a copy of Trickster's blackjack game?
Thank you SO much!!!
 

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