2

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
                }
            }
        }
    }
4
  • Sorry but I am not sure what is your actual goal here. Can you elaborate ? Commented Oct 17, 2020 at 1:38
  • I want to be able to shuffle the items and display 1 and go through the array. Commented Oct 17, 2020 at 1:40
  • Example - When number 5 shows up as the shuffled item it goes through all 9 items then stops. At the moment it repeats words and doesn't go through all the words. Commented Oct 17, 2020 at 1:42
  • 1
    So just shuffle it once, start from the startIndex and just increment the index when you call nextFruit. You can shuffle it only if the index is at 0 Commented Oct 17, 2020 at 1:43

1 Answer 1

4

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")

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.