1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
| package main
import "fmt"
type Fighter interface { Name() string Health() int TakeDamage(damage int) IsAlive() bool }
type Action interface { Execute(actor, target Fighter) Description() string }
type ActionSelector interface { SelectAction() Action SelectTarget(opponents []Fighter) Fighter }
type BaseCharacter struct { name string health int }
func (c *BaseCharacter) Name() string { return c.name }
func (c *BaseCharacter) Health() int { return c.health }
func (c *BaseCharacter) TakeDamage(damage int) { c.health -= damage if c.health < 0 { c.health = 0 } }
func (c *BaseCharacter) IsAlive() bool { return c.health > 0 }
type Warrior struct { BaseCharacter attack int }
func NewWarrior(name string, health, attack int) *Warrior { return &Warrior{ BaseCharacter: BaseCharacter{name: name, health: health}, attack: attack, } }
type Mage struct { BaseCharacter spellPower int }
func NewMage(name string, health, spellPower int) *Mage { return &Mage{ BaseCharacter: BaseCharacter{name: name, health: health}, spellPower: spellPower, } }
type AttackAction struct { damage int }
func (a AttackAction) Execute(actor, target Fighter) { target.TakeDamage(a.damage) }
func (a AttackAction) Description() string { return fmt.Sprintf("attacks for %d damage", a.damage) }
type SpellAction struct { damage int spellName string }
func (s SpellAction) Execute(actor, target Fighter) { target.TakeDamage(s.damage) }
func (s SpellAction) Description() string { return fmt.Sprintf("casts %s for %d damage", s.spellName, s.damage) }
type HealAction struct { amount int }
func (h HealAction) Execute(actor, target Fighter) { target.(*BaseCharacter).health += h.amount }
func (h HealAction) Description() string { return fmt.Sprintf("heals for %d health", h.amount) }
type WarriorActionSelector struct { warrior *Warrior }
func (w WarriorActionSelector) SelectAction() Action { return AttackAction{damage: w.warrior.attack} }
func (w WarriorActionSelector) SelectTarget(opponents []Fighter) Fighter { for _, opp := range opponents { if opp.IsAlive() { return opp } } return nil }
type MageActionSelector struct { mage *Mage }
func (m MageActionSelector) SelectAction() Action { actions := []Action{ SpellAction{damage: m.mage.spellPower, spellName: "Fireball"}, HealAction{amount: 20}, } return actions[0] }
func (m MageActionSelector) SelectTarget(opponents []Fighter) Fighter { for _, opp := range opponents { if opp.IsAlive() { return opp } } return nil }
type BattleSystem struct{}
func (bs BattleSystem) PerformTurn(actor Fighter, selector ActionSelector, opponents []Fighter) { if !actor.IsAlive() { return } action := selector.SelectAction() target := selector.SelectTarget(opponents) if target == nil { return } fmt.Printf("%s %s at %s! ", actor.Name(), action.Description(), target.Name()) action.Execute(actor, target) fmt.Printf("(%s health: %d)\n", target.Name(), target.Health()) }
func main() { warrior := NewWarrior("Arthur", 100, 15) mage := NewMage("Merlin", 80, 25) warriorSelector := WarriorActionSelector{warrior: warrior} mageSelector := MageActionSelector{mage: mage} battleSystem := BattleSystem{} fmt.Println("===== Battle Start =====") battleSystem.PerformTurn(warrior, warriorSelector, []Fighter{mage}) battleSystem.PerformTurn(mage, mageSelector, []Fighter{warrior}) battleSystem.PerformTurn(warrior, warriorSelector, []Fighter{mage}) battleSystem.PerformTurn(mage, mageSelector, []Fighter{warrior}) fmt.Println("===== Battle End =====") fmt.Printf("%s health: %d\n", warrior.Name(), warrior.Health()) fmt.Printf("%s health: %d\n", mage.Name(), mage.Health()) }
|