diff --git a/common/data/Element/main.go b/common/data/Element/main.go new file mode 100644 index 000000000..40f9429d6 --- /dev/null +++ b/common/data/Element/main.go @@ -0,0 +1,163 @@ +package main + +import ( + "fmt" +) + +// 定义属性枚举 +type ElementType int + +const ( + FIRE ElementType = iota + 1 // 火系 + GRASS // 草系 + WATER // 水系 + ICE // 冰系 + FIGHTING // 战斗系 + PSYCHIC // 超能系 +) + +// 定义属性组合 +type ElementCombination struct { + primary ElementType + secondary *ElementType +} + +// 创建新的属性组合 +func NewElementCombination(primary ElementType, secondary *ElementType) (ElementCombination, error) { + if secondary != nil { + if primary > *secondary { + primary, *secondary = *secondary, primary + } + if primary == *secondary { + return ElementCombination{}, fmt.Errorf("双属性不能重复") + } + } + return ElementCombination{ + primary: primary, + secondary: secondary, + }, nil +} + +// 判断是否为双属性 +func (ec ElementCombination) IsDual() bool { + return ec.secondary != nil +} + +// 获取属性列表 +func (ec ElementCombination) Elements() []ElementType { + if ec.IsDual() { + return []ElementType{ec.primary, *ec.secondary} + } + return []ElementType{ec.primary} +} + +// 属性计算器 +type ElementCalculator struct{} + +// 单属性克制关系矩阵 +var singleMatrix = map[ElementType]map[ElementType]float64{ + FIRE: { + PSYCHIC: 0.5, + ICE: 2.0, + }, + GRASS: { + ICE: 1.0, + PSYCHIC: 1.0, + GRASS: 0.5, + WATER: 2.0, + }, + FIGHTING: { + ICE: 2.0, + PSYCHIC: 0.5, + FIGHTING: 0.5, + }, + WATER: { + ICE: 2.0, + PSYCHIC: 0.5, + FIGHTING: 0.5, + }, +} + +// 获取单属性克制系数 +func (ec ElementCalculator) getSingleEffectiveness(attacker ElementType, defender ElementType) float64 { + if attackerMatrix, exists := singleMatrix[attacker]; exists { + if value, ok := attackerMatrix[defender]; ok { + return value + } + } + return 1.0 +} + +// 计算克制系数 +func (ec ElementCalculator) CalculateEffectiveness(attacker, defender ElementCombination) float64 { + if !attacker.IsDual() { + return ec.handleSingleAttacker(attacker.primary, defender) + } + return ec.handleDualAttacker(attacker, defender) +} + +// 处理单属性攻击 +func (ec ElementCalculator) handleSingleAttacker(attacker ElementType, defender ElementCombination) float64 { + if !defender.IsDual() { + return ec.getSingleEffectiveness(attacker, defender.primary) + } + m1 := ec.getSingleEffectiveness(attacker, defender.primary) + m2 := ec.getSingleEffectiveness(attacker, *defender.secondary) + return (m1 + m2) / 2.0 +} + +// 处理双属性攻击 +func (ec ElementCalculator) handleDualAttacker(attacker, defender ElementCombination) float64 { + aElements := attacker.Elements() + a1, a2 := aElements[0], aElements[1] + + if !defender.IsDual() { + m1 := ec.getSingleEffectiveness(a1, defender.primary) + m2 := ec.getSingleEffectiveness(a2, defender.primary) + return (m1 + m2) / 2.0 + } + + dElements := defender.Elements() + d1, d2 := dElements[0], dElements[1] + + m11 := ec.getSingleEffectiveness(a1, d1) + m12 := ec.getSingleEffectiveness(a1, d2) + m21 := ec.getSingleEffectiveness(a2, d1) + m22 := ec.getSingleEffectiveness(a2, d2) + + return (m11 + m12 + m21 + m22) / 4.0 +} + +func main() { + calculator := ElementCalculator{} + + // 测试用例1 + attacker1, _ := NewElementCombination(FIGHTING, nil) + defender1, _ := NewElementCombination(ICE, nil) + result1 := calculator.CalculateEffectiveness(attacker1, defender1) + fmt.Printf("战斗->冰:%v → %v → 倍数:%.1f\n", attacker1, defender1, result1) + + // 测试用例2 + attacker2, _ := NewElementCombination(GRASS, &FIGHTING) + defender2, _ := NewElementCombination(FIGHTING, nil) + result2 := calculator.CalculateEffectiveness(attacker2, defender2) + fmt.Printf("草战斗->战斗:%v → %v → 倍数:%.1f\n", attacker2, defender2, result2) + + // 测试用例3 + attacker3, _ := NewElementCombination(GRASS, &FIGHTING) + defender3, _ := NewElementCombination(GRASS, &FIGHTING) + result3 := calculator.CalculateEffectiveness(attacker3, defender3) + fmt.Printf("草战斗->草战斗:%v → %v → 倍数:%.1f\n", attacker3, defender3, result3) + + // 测试用例4 + attacker4, _ := NewElementCombination(GRASS, nil) + defender4, _ := NewElementCombination(GRASS, &FIGHTING) + result4 := calculator.CalculateEffectiveness(attacker4, defender4) + fmt.Printf("草->草战斗:%v → %v → 倍数:%.1f\n", attacker4, defender4, result4) + + // 测试用例5 + attacker5, _ := NewElementCombination(GRASS, &FIGHTING) + defender5, _ := NewElementCombination(WATER, &FIGHTING) + result5 := calculator.CalculateEffectiveness(attacker5, defender5) + fmt.Printf("草战斗->水战斗:%v → %v → 倍数:%.1f\n", attacker5, defender5, result5) +}