-1

My code remove randomly only one coin. How i can remove randomly from 1 to 3 coins?

@IBAction func endTurn(sender: UIButton!) {
    if coins.count > 0 { // @IBOutlet var coins: [UIButton]! (21 coins)
        let index: Int = Int(arc4random_uniform(UInt32(coins.count)))
        coins[index].hidden = true
        self.coins.removeAtIndex(index)
        if coins.isEmpty {
            println("GameOver")
        }
    }
}
Fantattitude
  • 1,842
  • 2
  • 18
  • 34
Spartak
  • 41
  • 6

2 Answers2

1

For randoms I recommend this extension:

extension Int {
    static func random(range: Range<Int> ) -> Int {
        var offset = 0

        if range.startIndex < 0 {
            offset = abs(range.startIndex)
        }

        let min = UInt32(range.startIndex + offset)
        let max = UInt32(range.endIndex   + offset)

        return Int(min + arc4random_uniform(max - min)) - offset
    }
}

And then:

var i = Int.random(1...5)
Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
Gun13
  • 103
  • 9
0

Try this

let numberToDelete = Int(arc4random_uniform(UInt32(3))) + 1

for i in 0..<numberToDelete{
    let indexToDelete = Int(arc4random_uniform(UInt32(coins.count)))
    coins.removeAtIndex(indexToDelete)
    if coins.isEmpty{
        break;
    }
}
if coins.isEmpty{
    println("GameOver")
}
nRewik
  • 8,958
  • 4
  • 23
  • 30