随机数打印数组(Swift)

时间:2022-09-06 13:51:16

First of all here's my previous question so you see what I'm trying to make.

首先,这是我之前的问题,所以你看看我想要做什么。

We are trying to make a Small game. We made a sort of monster fighting game. We made items to the game but I want them to drop by the monster. made different types and would like to know how to code to get a Drop Chance on the Items... like

我们正在尝试制作一款小游戏。我们制作了一种怪物格斗游戏。我们为游戏制作了物品,但我希望它们能够被怪物击落。做了不同的类型,并想知道如何编码,以获得项目的丢失机会...喜欢

So now I know how that works I'm stuck to get a random item from my list.

所以现在我知道这是如何工作的我不得不从我的列表中获取一个随机项目。

So what I actually want is to get a Random item of my "NormalType" list when i print it..

所以我真正想要的是当我打印它时获得我的“NormalType”列表的随机项目..

protocol NormalType {
    var name: String { get }
}

class knife: NormalType {
    let name = "Knife"
    let Str = 10
}
class sword: NormalType {
    let name = "Sword"
    let Str = 20
}
class katana: NormalType {
    let name = "Katana"
    let Str = 30
}


class RareType {
    class Knife: RareType {
        var Str = 10
        var Hp = 10
    }
    class sword: RareType {
        var Str = 20
        var HP = 15
    }
    class Katana: RareType {
        var Str = 30
        var Hp = 20
    }
}

class LegendaryType {
    class Knife: LegendaryType {
        var Str = 10
    }
    class sword: LegendaryType {
        var Str = 20
    }
    class Katana: LegendaryType {
        var Str = 30
    }
}
var Knife = knife()
var Sword = sword()
var Katana = katana()

var Items: [NormalType] = [Knife, Sword, Katana]
var randomnumber = (arc4random_uniform(2))

print(Items[randomnumber])

1 个解决方案

#1


1  

So you made a little mistake here's the code:

所以你在这里犯了一个小错误的代码:

You have forgot to convert to Int the : (arc4random_uniform(2))

您忘了转换为Int :( arc4random_uniform(2))

import UIKit

protocol NormalType {
    var name: String { get }
}

class knife: NormalType {
    let name = "Knife"
    let Str = 10
}
class sword: NormalType {
    let name = "Sword"
    let Str = 20
}
class katana: NormalType {
    let name = "Katana"
    let Str = 30
}



class RareType {
    class Knife: RareType {
        var Str = 10
        var Hp = 10
    }
    class sword: RareType {
        var Str = 20
        var HP = 15
    }
    class Katana: RareType {
        var Str = 30
        var Hp = 20
    }

}

class LegendaryType {
    class Knife: LegendaryType {
        var Str = 10
    }
    class sword: LegendaryType {
        var Str = 20
    }
    class Katana: LegendaryType {
        var Str = 30
    }

}
var Knife = knife()
var Sword = sword()
var Katana = katana()

var Items: [NormalType] = [Knife, Sword, Katana]
var randomnumber =   Int(arc4random_uniform(2))



print(Items[randomnumber])

#1


1  

So you made a little mistake here's the code:

所以你在这里犯了一个小错误的代码:

You have forgot to convert to Int the : (arc4random_uniform(2))

您忘了转换为Int :( arc4random_uniform(2))

import UIKit

protocol NormalType {
    var name: String { get }
}

class knife: NormalType {
    let name = "Knife"
    let Str = 10
}
class sword: NormalType {
    let name = "Sword"
    let Str = 20
}
class katana: NormalType {
    let name = "Katana"
    let Str = 30
}



class RareType {
    class Knife: RareType {
        var Str = 10
        var Hp = 10
    }
    class sword: RareType {
        var Str = 20
        var HP = 15
    }
    class Katana: RareType {
        var Str = 30
        var Hp = 20
    }

}

class LegendaryType {
    class Knife: LegendaryType {
        var Str = 10
    }
    class sword: LegendaryType {
        var Str = 20
    }
    class Katana: LegendaryType {
        var Str = 30
    }

}
var Knife = knife()
var Sword = sword()
var Katana = katana()

var Items: [NormalType] = [Knife, Sword, Katana]
var randomnumber =   Int(arc4random_uniform(2))



print(Items[randomnumber])