Reputation: 61
I have managed get my code to randomly pick objects and display the Images on my view controller but sometimes the randomly selected object will be a duplicate so I need to be able to remove the selected ones that get stored in the second array from the first array so they can not be chosen for the second time but I am unsure how to get this to happen in my code, Here is my code that works but will potentially select duplicates:
@IBAction func drawCardsButtonPressed(_ sender: Any) {
cardsDrawnArray = []
if cardsDrawn == 1 {
let randomCards = cardsArray[Int(arc4random_uniform(UInt32(cardsArray.count)))]
cardsDrawnArray.append(randomCards.tarotImage)
tarotCardFive.image = cardsDrawnArray[0]
} else if cardsDrawn == 3 {
for _ in 0...2 {
let randomCards = cardsArray[Int(arc4random_uniform(UInt32(cardsArray.count)))]
cardsDrawnArray.append(randomCards.tarotImage)
}
tarotCardFour.image = cardsDrawnArray[0]
tarotCardFive.image = cardsDrawnArray[1]
tarotCardSix.image = cardsDrawnArray[2]
}
else if cardsDrawn == 5 {
for _ in 0...4 {
let randomCards = cardsArray[Int(arc4random_uniform(UInt32(cardsArray.count)))]
cardsDrawnArray.append(randomCards.tarotImage)
}
tarotCardTwo.image = cardsDrawnArray[0]
tarotCardFour.image = cardsDrawnArray[1]
tarotCardFive.image = cardsDrawnArray[2]
tarotCardSix.image = cardsDrawnArray[3]
tarotCardEight.image = cardsDrawnArray[4]
} else if cardsDrawn == 9 {
for _ in 0...8 {
let randomCards = cardsArray[Int(arc4random_uniform(UInt32(cardsArray.count)))]
cardsDrawnArray.append(randomCards.tarotImage)
}
tarotCardOne.image = cardsDrawnArray[0]
tarotCardTwo.image = cardsDrawnArray[1]
tarotCardThree.image = cardsDrawnArray[2]
tarotCardFour.image = cardsDrawnArray[3]
tarotCardFive.image = cardsDrawnArray[4]
tarotCardSix.image = cardsDrawnArray[5]
tarotCardSeven.image = cardsDrawnArray[6]
tarotCardEight.image = cardsDrawnArray[7]
tarotCardNine.image = cardsDrawnArray[8]
} else {
print(" Invalid Number")
}
}
Any help to fix this is much appreciated.
Upvotes: 0
Views: 119
Reputation: 77433
Instead of executing code to get a random card each time, a much better approach is to "shuffle" your array of cards, then just draw them in the shuffled order. Much more efficient, and you never have to even consider duplicates.
Basically, if your array was the numbers 1 to 10, it would start like this:
let cards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
If you run a loop, selecting a "random slot" each time, you will frequently get duplicates (as you've seen). So...
let cards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let shuffledCards = myShuffleFunc(cards)
and you end up with something like:
[9, 2, 5, 6, 3, 1, 8, 10, 4, 7]
At that point, you can easily do:
tarotCardOne.image = shuffledCards[0]
tarotCardTwo.image = shuffledCards[1]
tarotCardThree.image = shuffledCards[2]
tarotCardFour.image = shuffledCards[3]
tarotCardFive.image = shuffledCards[4]
Or however you're assigning them.
Take a look at this post for some good shuffling code: How do I shuffle an array in Swift?
Upvotes: 1