6.1 - Is there a way to "shuffle" an array?
Цитата: Hanon Ondricek от 21.01.19, 03:47I'm still working in 6.1 and I'm learning how arrays work. I'm trying to make some arrays that work like a deck of cards - I need to choose an element from the array and remove it with slice.
It would make my life a little easier (and the game fairer) if I could specify an array like <<$deck = ["Ace","King","Queen","Jack","10"]>> and then shuffle it somehow (like <<$deck.randomize>>) to put them in a new order so I could then consistently say <<$carddrawn = $deck.slice(0,1)>> to get the "top" card.
Is there a way to do this? I've tried programmatically creating the deck array and then choosing a random number <<random $drawn = $deck.length -1>> and then <<$shuffleddeck.push ($deck.slice ($drawn, 1))>> (and other ways) but it doesn't seem to work any way I try it.
I'm still working in 6.1 and I'm learning how arrays work. I'm trying to make some arrays that work like a deck of cards - I need to choose an element from the array and remove it with slice.
It would make my life a little easier (and the game fairer) if I could specify an array like <<$deck = ["Ace","King","Queen","Jack","10"]>> and then shuffle it somehow (like <<$deck.randomize>>) to put them in a new order so I could then consistently say <<$carddrawn = $deck.slice(0,1)>> to get the "top" card.
Is there a way to do this? I've tried programmatically creating the deck array and then choosing a random number <<random $drawn = $deck.length -1>> and then <<$shuffleddeck.push ($deck.slice ($drawn, 1))>> (and other ways) but it doesn't seem to work any way I try it.
Цитата: UsualRussian от 15.12.20, 22:48There is at least one solution I can come up with. Take the random element between the first and the last and switch it with the last. Then, pop(). Not a slice, however, but nonetheless a pretty basic way to do this.
There is at least one solution I can come up with. Take the random element between the first and the last and switch it with the last. Then, pop(). Not a slice, however, but nonetheless a pretty basic way to do this.