我正在尝试Swift,试图创建一个基本的游戏引擎。
我有一个Struct
来定义战斗机,其中有许多用于定义战斗机技能的结构。
我想遍历每个“战士”并汇总每个单独的技能集,基本上返回3个值-standUp, clinch, ground
的总数
我可以遍历Fighter
结构,但是我是他们只能注销那些嵌套结构。
我应该如何遍历该结构,然后遍历嵌套的结构,返回每个循环的合计值呢?也许作为元组的一部分?
import UIKit
struct StandUp {
let boxing: Int
let kickBoxing: Int
}
struct Clinch {
let judo: Int
let freestyle: Int
}
struct Ground {
let bjj: Int
let judo: Int
}
struct Fighter {
let standUp: StandUp
let clinch: Clinch
let ground: Ground
}
let striker = Fighter(
standUp: StandUp(boxing: 8, kickBoxing: 7),
clinch: Clinch(judo: 5, freestyle: 4),
ground: Ground(bjj: 6, judo: 5)
)
let bjj = Fighter(
standUp: StandUp(boxing: 5, kickBoxing: 4),
clinch: Clinch(judo: 7, freestyle: 8),
ground: Ground(bjj: 8, judo: 7)
)
class FightEngine {
private let fOne: Fighter
private let fTwo: Fighter
init(fighterOne: Fighter, fighterTwo: Fighter) {
fOne = fighterOne
fTwo = fighterTwo
}
private func sumSkillSet(fighter: Fighter) -> Int {
var total: Int
let mirror = Mirror(reflecting: fighter)
for skill in mirror.children {
print(skill.value)
}
return 1 // Only returning this so the code will run
}
func getTotals() -> (Int, Int) {
let totalOne = sumSkillSet(fighter: fOne)
let totalTwo = sumSkillSet(fighter: fTwo)
return (1, 2) // Only returning this so the code will run
}
}
let fE = FightEngine(fighterOne: striker, fighterTwo: bjj)
fE.getTotals() // This should be a dictionary of each fighters totals, 3 values for each fighter
理想情况下,我希望从fE.getTotals()
获得某种回报,例如
["fighterOne":(1,2,3), "fighterTwo":(4,5,6)]
答案 0 :(得分:2)
我认为这可能是您想要的。
struct StandUp {
let boxing: Int
let kickBoxing: Int
func sum() -> Int {
return boxing + kickBoxing
}
}
struct Clinch {
let judo: Int
let freestyle: Int
func sum() -> Int {
return judo + freestyle
}
}
struct Ground {
let bjj: Int
let judo: Int
func sum() -> Int {
return bjj + judo
}
}
struct Fighter {
let standUp: StandUp
let clinch: Clinch
let ground: Ground
func sum() -> [Int] {
return [standUp.sum(), clinch.sum(), ground.sum()]
}
}
let striker = Fighter(
standUp: StandUp(boxing: 8, kickBoxing: 7),
clinch: Clinch(judo: 5, freestyle: 4),
ground: Ground(bjj: 6, judo: 5)
)
let bjj = Fighter(
standUp: StandUp(boxing: 5, kickBoxing: 4),
clinch: Clinch(judo: 7, freestyle: 8),
ground: Ground(bjj: 8, judo: 7)
)
class FightEngine {
private let fOne: Fighter
private let fTwo: Fighter
init(fighterOne: Fighter, fighterTwo: Fighter) {
fOne = fighterOne
fTwo = fighterTwo
}
func getTotals() -> [String: [Int]] {
return ["fighterOne": fOne.sum(), "fighterTwo": fTwo.sum()]
}
}
注意:我建议您在name
上添加一个参数Fighter
并将其用作键,而不是“ fighterOne”和“ fighterTwo”。
[fOne.name: totalOne, fTwo.name: totalTwo]
答案 1 :(得分:1)
您可以使用一个枚举来执行此操作,其中每种情况都代表一个技能集,该技能集具有用于计算技能集总分的init参数。
enum SkillSet {
case standUp(boxing: Int, kickBoxing: Int)
case clinch(judo: Int, freestyle: Int)
case ground(bjj: Int, judo: Int)
var score: Int {
switch self {
case .standUp(let boxing, let kickBoxing): return boxing + kickBoxing
case .clinch(let judo, let freestyle): return judo + freestyle
case .ground(let bjj, let judo): return bjj + judo
}
}
}
一名战士可以拥有SkillSet
的数组,这样我们就可以通过将每个技能集的分数相加来降低总分数。
struct Fighter {
let skillSets: [SkillSet]
var total: Int {
return skillSets.reduce(0) { (sum, skillSet) in
return sum + skillSet.score
}
}
}
您的引擎可以简化,只关心发动战斗并给出分数。
class FightEngine {
let fighters: (Fighter, Fighter)
init(one: Fighter, two: Fighter) { self.fighters = (one, two) }
func getTotals() -> (Int, Int) { return (fighters.0.total, fighters.1.total) }
}
叮叮
let fightEngine = FightEngine(
one: Fighter(
skillSets: [
.standUp(boxing: 8, kickBoxing: 7),
.clinch(judo: 5, freestyle: 4),
.ground(bjj: 6, judo: 5)
]
),
two: Fighter(
skillSets: [
.standUp(boxing: 5, kickBoxing: 4),
.clinch(judo: 7, freestyle: 8),
.ground(bjj: 8, judo: 7)
]
)
)
print(fightEngine.getTotals())