vendredi 16 octobre 2020

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

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
                }
            }
        }
    }



Aucun commentaire:

Enregistrer un commentaire