Swift中的角色游戏效果/药水生成器

时间:2018-11-28 16:19:29

标签: swift macos dictionary cocoa generator

首先,请原谅我(有时)英语不好,我是法国人。我实际上正在从事角色游戏项目。目标是使用Swift和Xcode创建一个小的macOS软件(一个生成器),该软件可以通过多个参数创建一个药水(一种与药效相关的效果)(例如:“ Fortify Speed 7/10”)。该应用程序允许用户输入两个参数:配料(在弹出菜单中选择)和骰子卷(在文本字段中输入)。如果这两种(或多种)成分具有共同作用,则软函数将这两种作用的值相加,然后返回:““共同作用” +(第一种成分作用的值+第二种成分作用的值) ”。在操作过程中,结果可能会因骰子掷骰而改变,但此处无关紧要。 这是一个例子,可以理解我的意思:

第一成分:“猫爪树皮”->抗闪电(4/10);抵抗通灵(3/10);酸伤害(3/10);恐惧(3/10);隐形(1/10)。 || 第二个成分:“葡萄冬青”->治愈疾病(5/10);抗寒(4/10); 增强药剂(4/10;抵抗闪电(3/10);力量伤害(2/10。

因为这两种成分具有共同的效果(抗闪电),所以柔和的效果会增加两种效果的效力。 因此它返回:“ Resist Lightning(7/10)”

这是我的第一个问题:如何将其转换为Swift代码?我已经尝试了很多方法,但是没有用。我试图使用数组,字典,数组到字典中,但是我仍然面临着同样的问题:如何为多个参数(在这种情况下为效果)和类型(效果的名称为String,效力为Int)赋予属性到一个实例? (如果是正确的问题)。我的第二个问题是:如何比较字典的键(不是值)以匹配结果?

也许像这样,使用字典,但是我需要在结果中不仅显示字典中键的值,还需要显示键本身。另外,我需要比较键,而不是值:

let grapeHolly: [String: Int] = ["Cure Disease": 5, "Resist Cold": 4, "Heighten Medicine": 4, "Resist Lightning": 3, "Force Damage": 2]

这样做,我收到“无法在属性初始化程序中使用实例成员'grapeHolly'”错误。而且,如果我将该行放在“ viewDidLoad()”函数的主体内,则不在函数内部的代码将无法识别该行。

The UI of the app

为了清楚易懂,我希望用户在第二个弹出菜单中选择两种或多种配料(弹出的第一行将配料分为三类:“常见/不常见” / Rare”),然后当他按下“ Make a potion”按钮时,结果文本字段将显示两个效果值(如果它们具有共同的效果)的组合结果。

告诉我如果您需要我更精确的信息,我将非常乐意寻求帮助:)

这是代码(不在乎成分表的长度):

import Cocoa

class ViewController: NSViewController {

@IBOutlet weak var ingredientRarityNorthlands: NSPopUpButton!
@IBOutlet weak var ingredientListNorthlands: NSPopUpButton!

    @IBAction func ingredientRarityNorthlands(_ sender: NSPopUpButton) {
    if ingredientRarityNorthlands.titleOfSelectedItem == "Common" {
        ingredientListNorthlands.removeAllItems()
        ingredientListNorthlands.addItems(withTitles: ingredientListNorthlandsCommon)
    }
    if  ingredientRarityNorthlands.titleOfSelectedItem == "Uncommon" {
        ingredientListNorthlands.removeAllItems()
        ingredientListNorthlands.addItems(withTitles: ingredientListNorthlandsUncommon)
    }
    if ingredientRarityNorthlands.titleOfSelectedItem == "Rare" {
        ingredientListNorthlands.removeAllItems()
        ingredientListNorthlands.addItems(withTitles: ingredientListNorthlandsRare)
    }
}

let ingredientListNorthlandsCommon = ["Black Pepper", "Bolean Violet", "Brittlebush Flower", "Brittlebush Resin", "Butterfly Moss", "Cardamom", "Cinnamon",  "Cloves", "Colic Yellowcress", "Comb Mint", "Coriander (Leaves)", "Coriander (Seeds)", "Cork Tree Bark", "Crow Hood", "Cumin (Seeds)", "Desert Shrub", "Eucalyptus (Oil)", "Eucalyptus (Gum)", "Eucalyptus (Leaves)", "Eucalyptus (Seeds)", "Fire Itchweed", "Four-Leafed Colwort", "Frukmoot", "Ginger (Root)", "Jasmine (Flower)", "Life's Collard", "Lotus (Petals)", "Lotus (Seeds)", "Mango (Leaves)", "Mango (Taproot)", "Marigold (Flower)", "Marigold (Leaves)", "Mustard (Flower)", "Mustard (Greens)", "Mustard (Seeds)", "Nutmeg (Oil)", "Nutmeg (Seed)", "Radiant Azolla", "Red Chili (Seeds)", "Wall Snakeberry"]
let ingredientListNorthlandsUncommon = ["Aloe Vera", "Ash Gallberry", "Baby Zinnia", "Black Salt", "Buzzard Bile", "Corea Mint", "Dead Sage", "Dog Tongue", "Fennec Fox Droppings", "Giant Lizard Venom", "Marsupial Wolf Blood", "Noxious Laurel", "Ocort", "Orange Thyme", "Pale Penny Gilliflower", "Phoenix Briar", "Pocan Hemp", "Prickly Cane", "Serpent Rye", "Shivering Laurel", "Snage", "Two-Tongued Scorpion Tail", "Viper Venom"]
let ingredientListNorthlandsRare = ["Blackheart Flower", "Burrowing Vee Spider", "Jee Redzin Scorpion Eggs", "Kreetlee Leaves", "Olaart Buds", "Smouse Oil", "Wretch Salts", "Zettin Seeds"]


    @IBAction func makeAPotion(_ sender: NSButton) {
    if potionButton.isEnabled {
        resultField.stringValue = result()
    }

}

func result() -> String {
    return "\(effect) \(value)"
}
}

Ps:我认为没有必要,但我将掷骰子系统放在这里:

var firstEffect = 10..<12
var secondEffect = 12..<15
var thirdEffect = 15..<20
var fourthEffect = 20..<25
var fifthEffect = 25...

var firstEffectEnabler: Bool = false
var secondEffectEnabler: Bool = false
var thirdEffectEnabler: Bool = false
var fourthEffectEnabler: Bool = false
var fifthEffectEnabler: Bool = false

@IBAction func diceRoll(_ sender: NSTextField) {
    if diceRoll.stringValue == "\(firstEffect)" {
        firstEffectEnabler = true
    }
    if diceRoll.stringValue == "\(secondEffect)" {
        secondEffectEnabler = true
    }
    if diceRoll.stringValue == "\(thirdEffect)" {
        thirdEffectEnabler = true
    }
    if diceRoll.stringValue == "\(fourthEffect)" {
        fourthEffectEnabler = true
    }
    if diceRoll.stringValue == "\(fifthEffect)" {
        fifthEffectEnabler = true
    }
}

编辑:因此,我尝试了此操作,但我认为它有点混乱并且没有非常优化,并且我真的不知道它是否可以工作。此外,仍然存在两个问题:我不知道如何在成分列表数组和弹出菜单中而不是“ grapeHolly”中显示字符串“ Grape Holly”,以及如何解决“ potency = grapeHolly [? ]”行:

lazy var grapeHolly = ["Cure Disease": 5, "Resist Cold": 4, "Heighten Medicine": 4, "Resist Lightning": 3, "Force Damage": 2]

lazy var effect = Array(grapeHolly.keys)
var result: String = ""
lazy var potency = grapeHolly[]

func compare()  {
func resultNorthlands() {
    if ingredientListNorthlands.selectedItem?.doesContain(effect) == ((ingredientListMettina.selectedItem?.doesContain(effect))! || (ingredientListIsjord.selectedItem?.doesContain(effect))! || (ingredientListKaedmyr.selectedItem?.doesContain(effect))! || (ingredientListCouchant.selectedItem?.doesContain(effect))! || (ingredientListBlastlands.selectedItem?.doesContain(effect))!) {
        var result = "\(effect)"
    }
}

我认为某些方法可行,但出现错误 “无法用索引类型为[[String]”的下标'[String:Int]'下标” “ lazy var potency = grapeHolly [effect]” 行上的“ strong>”。我不太明白为什么... “ dictionary [key]” 不是在字典中获取键值的方法吗?另外,我需要在配料表中输入“ grapeHolly” 而不是“ Grape Holly” 字符串,有人知道解决此问题的方法吗? (我编写了一个带有拼写错误的'grapeHollys'字典,除了“ Cure Disease”效果与'compare()'函数中所示的两条效果线相匹配之外) / p>

关于'compare()'函数:我重复了成分列表编号的次数,但是每次函数重复都更改了第一个成分列表的名称(在这种情况下为ingredientListNorthlands) ,将'=='运算符之前的每个成分列表与其他列表进行比较,因此它有点长且重复,而且我知道程序员重复自己并不是一个好习惯,但这我所找到的全部。

但是目前由于错误而无法构建,因此我无法测试代码的效率,这就是为什么我要向您寻求帮助的原因:) 这是代码:

//Properties
var result: String = ""
lazy var potency = grapeHolly[effect]
lazy var effect = Array(grapeHolly.keys)

lazy var grapeHolly = ["Cure Disease": 5, "Resist Cold": 4, "Heighten     Medicine": 4, "Resist Lightning": 3, "Force Damage": 2]
lazy var grapeHollys = ["Cure Disease": 5, "Resist Cld": 4, "Heighten Mediine":     4, "Resist Lightnng": 3, "Force Damge": 2]

// Compare Function
func compare()  {
func resultNorthlands() {
    if ingredientListNorthlands.selectedItem?.doesContain(effect) == ((ingredientListMettina.selectedItem?.doesContain(effect))! || (ingredientListIsjord.selectedItem?.doesContain(effect))! || (ingredientListKaedmyr.selectedItem?.doesContain(effect))! || (ingredientListCouchant.selectedItem?.doesContain(effect))! || (ingredientListBlastlands.selectedItem?.doesContain(effect))!) {
        var result = "\(effect) \(potency1 + potency2)"

    }
}

    //Potion Button
@IBAction func makeAPotion(_ sender: NSButton) {
    compare()
    if potionButton.isEnabled {
        resultField.stringValue = result
    }

}

1 个答案:

答案 0 :(得分:0)

问题解决了!解决方案是如此明显,以至于我没有直接考虑它,for-in循环!我希望这可以帮助面临相同问题的任何人。这是相应的代码:

var selection = [
    catClawBark: ["Resist Lightning": 5, "Resist Damage": 8],
    grapeHolly: ["Resist Lightning": 6, "Resist Damage": 2]
]

for (ingredient, keys) in selection {
for key in keys {
    for (ingredient2, keys2) in selection {
        for key2 in keys2 {
            if ingredient != ingredient2 {
                if key.key == key2.key {
                    result = "You created a potion !\n---> \(key.key) \(key.value + key2.value) <---"
                }
                else {
                    result = "No match. Check the ingredients selected or retry when you have more !"
                }
            }
        }
    }
}

}