简体   繁体   中英

Typecasting arc4random_uniform() in Swift

I have a method in an app I'm creating to play War, the card game, that is malfunctioning. It is supposed to randomly generate a number with arc4random_uniform and then use that number to determine the suit, rank, and value of the card that the player has. Following this, the suit and rank are used to show the card image. This works except that it only shows cards from 8 through Ace. I think it has something to do with my typecasting of randCard because I can't find anything else wrong with it logically.

@IBAction func showPlayerCard(sender: AnyObject) {
        var randCard = arc4random_uniform(52)+1
        var suit = ""
        var rank = ""
        var value = 0

        if(randCard <= 13){
            suit = " of Clubs"
        }else if(randCard <= 26){
            suit = " of Diamonds"
            value = (Int)(randCard)/2
        }else if(randCard <= 39){
            suit = " of Hearts"
            value = (Int)(randCard)/3
        }else if(randCard <= 52){
            suit = " of Spades"
            value = (Int)(randCard)/4
        }

        switch value {

        case 1:
            rank = "2"
            value = 2

        case 2:
            rank = "3"
            value = 3

        case 3:
            rank = "4"
            value = 4

        case 4:
            rank = "5"
            value = 5

        case 5:
            rank = "6"
            value = 6

        case 6:
            rank = "7"
            value = 7

        case 7:
            rank = "8"
            value = 8

        case 8:
            rank = "9"
            value = 9

        case 9:
            rank = "10"
            value = 10

        case 10:
            rank = "Jack"
            value = 11

        case 11:
            rank = "Queen"
            value = 12

        case 12:
            rank = "King"
            value = 13

        case 13:
            rank = "Ace"
            value = 14

        default:
            rank = ""
            value = 0
        }

        var cardName = rank + suit

        if(rank == ""){
            cardName = "Ace" + suit
        }

        self.firstCardImageView.image = UIImage(named: cardName)

If anyone has suggestions on how to fix this it would be much appreciated.

Oh and I forgot to add, the if(rank == "") at the bottom I put in because sometimes the randomly generated card would be a blank; I believe as a result of the default case being triggered.

The problem is unrelated to type casting. Your logic to compute value from a random number in the range 1 ... 52 is wrong. Instead of dividing by 1, 2, 3, or 4 you would have to subtract an offset. (Just imagine what the possible outcomes for value = (Int)(randCard)/4 are if randCard is in the range 40 ... 52.)

A simpler method would be to use the "remainder operator" % :

let randCard = Int(arc4random_uniform(52)) // 0, 1, ..., 51
let suit = randCard / 13 + 1 // 1, 2, 3, 4
let value = randCard % 13 + 1 // 1, 2, ..., 13

or just

let suit = Int(arc4random_uniform(4)) + 1  // 1, 2, 3, 4
let value = Int(arc4random_uniform(13)) + 1 // 1, 2, ..., 13

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM