0

Everytime the app starts I want to shuffle the elements that are in my array, also the elements should be placed on the screen without repeating, how I can do this? I tried several extensions to shuffle my array but didn't work

I'm using swift 4.1 and Here is my code:

import SpriteKit

struct ContainerSprite{
    let block : SKSpriteNode!

    let containers = ["Container_Circle_Blue","Container_Hexagone_Blue",
"Container_Square_Blue","Container_Star_Blue","Container_Triangle_Blue",
"Container_Circle_DBlue","Container_Hexagone_DBlue","Container_Square_DBlue",
"Container_Star_DBlue","Container_Triangle_DBlue","Container_Circle_Green",
"Container_Hexagone_Green","Container_Square_Green","Container_Star_Green",
"Container_Triangle_Green"]

    init (numContainer: Int, row: Int, col: Int , inThisScene: GameScene) {
        block = SKSpriteNode(imageNamed: containers[numContainer])
        let numberOfBlocks = 12.5
        let blockScale = 0.6   
     }
}

This is one of the extensions I used but when I tried, it was showing elements repeated randomly

extension MutableCollection {
    /// Shuffles the contents of this collection.
    mutating func shuffle() {
        let c = count
        guard c > 1 else { return }

        for (firstUnshuffled, unshuffledCount) in zip(indices, stride(from: c, to: 1, by: -1)) {
            // Change Int in the next line to IndexDistance in < Swift 4.1
            let d: Int = numericCast(arc4random_uniform(numericCast(unshuffledCount)))
            let i = index(firstUnshuffled, offsetBy: d)
            swapAt(firstUnshuffled, i)
        }
    }
}

extension Sequence {
    /// Returns an array with the contents of this sequence, shuffled.
    func shuffled() -> [Element] {
        var result = Array(self)
        result.shuffle()
        return result
    }
}
Phantom_strike
  • 149
  • 2
  • 10
  • Do you want to randomize the elements of `containers`? Show what you tried. There are plenty of examples of shuffling an array in Swift. – rmaddy Aug 23 '18 at 03:11
  • @rmaddy Yes I want to randomize the elements of "containers" I updated my question and added the extension I used – Phantom_strike Aug 23 '18 at 03:21
  • And what problem exactly are you having? – rmaddy Aug 23 '18 at 03:24
  • @rmaddy the problem with this extension is that puts repeated elements of my array randomly, and I want each element to be unique at different place every time my app starts – Phantom_strike Aug 23 '18 at 03:29
  • It looks like you got that code from the duplicate I just linked. If that specific code doesn't work then look at one of the many other solutions there. – rmaddy Aug 23 '18 at 03:32
  • what is wrong with https://developer.apple.com/documentation/swift/array/2994757-shuffled – pds Aug 23 '18 at 04:54
  • Are you sure you are not constantly shuffling after you read each index? that would cause duplicates – Knight0fDragon Aug 23 '18 at 17:48
  • @pds that does not exist yet (That comes with XCode 10) – Knight0fDragon Aug 23 '18 at 17:49

0 Answers0