Swift:卡片游戏中数组的isEmpty属性

时间:2017-10-24 18:41:35

标签: arrays swift is-empty

我正在开发我的战争卡游戏应用程序,并提出了这种方法。这是两个阵列,第一个填充52张卡,第二个是空的。下面有一个循环,它从第一个数组中删除一个随机卡,将其添加到第二个数组。

 var cardsImg: [String] = ["2_of_clubs","2_of_hearts","2_of_diamonds","2_of_spades","3_of_clubs","3_of_diamonds","3_of_spades","3_of_hearts","4_of_diamonds","4_of_clubs","4_of_spades","4_of_hearts","5_of_spades","5_of_diamonds","5_of_hearts","5_of_clubs","6_of_clubs","6_of_diamonds","6_of_spades","6_of_hearts","7_of_clubs","7_of_diamonds","7_of_spades","7_of_hearts","8_of_hearts","8_of_clubs","8_of_spades","8_of_diamonds","9_of_spades","9_of_diamonds","9_of_clubs","9_of_hearts","10_of_hearts","10_of_clubs","10_of_diamonds","10_of_spades","jack_of_spades","jack_of_hearts","jack_of_diamonds","jack_of_clubs","queen_of_spades","queen_of_clubs","queen_of_hearts","queen_of_diamonds","king_of_spades","king_of_clubs","king_of_diamonds","king_of_hearts","ace_of_spades","ace_of_hearts","ace_of_clubs","ace_of_diamonds"]
var cardsImgEmpty: [String] = []

var upperLimit: UInt32 = 52
var randomNum: Int

for _ in 1...52 {
    randomNum = Int((arc4random_uniform(upperLimit)))
    cardsImgEmpty.append(cardsImg[randomNum])
    cardsImg.remove(at: randomNum)
    upperLimit -= 1
}

player1Card.image = UIImage(named: cardsImgEmpty[0])
player2Card.image = UIImage(named: cardsImgEmpty[1])

这个想法是,当使用来自cardsImg的所有牌时,这个阵列是空的,游戏结束并且标签根据玩家之间的点数显示赢家。下面是我创建的函数,它应该在第二个数组填充52个对象并将获胜者打印到标签后禁用Draw按钮。但是在进行测试时,游戏会根据第一次抽奖的结果结束。应该更改或添加什么才能在所有卡片用完一次后才能结束游戏?

 func gameOver() {
        if cardsImg.isEmpty {
        draw.isEnabled = false
            let player1FinalScore = Int(player1Score.text!)!
            let player2FinalScore = Int(player2Score.text!)!
            if player1FinalScore > player2FinalScore {
                winner.text = "Player 1 wins!"
            } else {
                winner.text = "Player 2 wins!"
            }
        }
    }
gameOver()

0 个答案:

没有答案