How to shuffle text from array without repeating a word and going through the whole array? (Swift)

Viewed 165

I'm trying to shuffle my array and display all the words without repeating them. Iv tried a while and for a loop. I thought I could use the enum but it's not exactly working, not sure why...any ideas?

My Array

    var fruitOptions = [
        
        Fruit(id: 1, fruit:"", name: "KIWI"),
        Fruit(id: 2, fruit:"", name: "APPLE"),
        Fruit(id: 3, fruit:"", name: "PEAR"),
        Fruit(id: 4, fruit:"", name: "ORANGE"),
        Fruit(id: 5, fruit:"", name: "STRAWBERRY"),
        Fruit(id: 6, fruit:"", name: "WATERMELON"),
        Fruit(id: 7, fruit:"", name: "GRAPES"),
        Fruit(id: 8, fruit:"", name: "BANANA"),
        Fruit(id: 9, fruit:"", name: "CHERRY")
        
    ]

The function that shuffles and displays the next fruit.

 mutating func nextFruit() {
        
        fruitOptions.shuffle()
        
        while fruitOptions.count <= 8 {
            if fruitNumber + 1 < fruitOptions.count {
            
            
              //  if !fruitOptions.contains(fruitOptions.capacity) {
           
                fruitNumber += 1
            
//
//            for (index, fruitOptions) in fruitOptions.enumerated() {
//                if case fruitOptions.id = index + 0 {
//                    fruitNumber += 1
                    
                          
                } else {
                    
                    // fruitNumber = 0     //  <- Makes a never ending app.!? :/
                    score = 0
                }
            }
        }
    }
1 Answers

If I understood it correctly you shouldn't shuffle your collection every time you call nextFruit. Just shuffle it once before iterating your collection. The simplest solution in my opinion is to store the index of the current fruit. In your next fruit method check if the index is equal to zero and if true shuffle your fruits. Get the fruit at the current index and increment it. If the index is equal to the number of fruits just reset it to zero. Something like:

struct Fruit {
    let id: Int
    let fruit: Character
    let name: String
}

Playground testing:

var fruitOptions = [
    Fruit(id: 1, fruit:"", name: "KIWI"),
    Fruit(id: 2, fruit:"", name: "APPLE"),
    Fruit(id: 3, fruit:"", name: "PEAR"),
    Fruit(id: 4, fruit:"", name: "ORANGE"),
    Fruit(id: 5, fruit:"", name: "STRAWBERRY"),
    Fruit(id: 6, fruit:"", name: "WATERMELON"),
    Fruit(id: 7, fruit:"", name: "GRAPES"),
    Fruit(id: 8, fruit:"", name: "BANANA"),
    Fruit(id: 9, fruit:"", name: "CHERRY")]

var index = 0

func nextFruit() {
    if index == 0 { fruitOptions.shuffle() }
    print(fruitOptions[index])
    index += 1
    if index == fruitOptions.count  {
        print("end of fruits")
        index = 0
    }
}

nextFruit()
nextFruit()
nextFruit()
nextFruit()
nextFruit()
nextFruit()
nextFruit()
nextFruit()
nextFruit()
nextFruit()
nextFruit()
nextFruit()
nextFruit()

This will print:

Fruit(id: 5, fruit: "", name: "STRAWBERRY")
Fruit(id: 2, fruit: "", name: "APPLE")
Fruit(id: 4, fruit: "", name: "ORANGE")
Fruit(id: 9, fruit: "", name: "CHERRY")
Fruit(id: 3, fruit: "", name: "PEAR")
Fruit(id: 6, fruit: "", name: "WATERMELON")
Fruit(id: 8, fruit: "", name: "BANANA")
Fruit(id: 1, fruit: "", name: "KIWI")
Fruit(id: 7, fruit: "", name: "GRAPES")
end of fruits
Fruit(id: 2, fruit: "", name: "APPLE")
Fruit(id: 3, fruit: "", name: "PEAR")
Fruit(id: 1, fruit: "", name: "KIWI")
Fruit(id: 4, fruit: "", name: "ORANGE")

Related