2019-11-08 03:30:41 +00:00
|
|
|
package main
|
|
|
|
|
2019-11-08 17:52:19 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Battle struct {
|
|
|
|
immune []*Army
|
|
|
|
infect []*Army
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBattle(immune, infect []*Army) *Battle {
|
|
|
|
return &Battle{
|
|
|
|
immune: immune,
|
|
|
|
infect: infect,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Battle) PrintStatus() {
|
|
|
|
fmt.Println("Immune System:")
|
|
|
|
for _, v := range b.immune {
|
|
|
|
fmt.Printf("Group %d contains %d units\n", v.id, v.units)
|
|
|
|
}
|
|
|
|
fmt.Println("Infection:")
|
|
|
|
for _, v := range b.infect {
|
|
|
|
fmt.Printf("Group %d contains %d units\n", v.id, v.units)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-08 03:30:41 +00:00
|
|
|
// All Battle Logic is in here
|
2019-11-08 17:52:19 +00:00
|
|
|
func (b *Battle) Fight() {
|
|
|
|
allCombatants := append(b.immune, b.infect...)
|
|
|
|
sort.Sort(ByPower(allCombatants))
|
|
|
|
for k := len(allCombatants) - 1; k >= 0; k-- {
|
|
|
|
b.FindTargetFor(allCombatants[k])
|
|
|
|
}
|
|
|
|
if Debug {
|
|
|
|
fmt.Println("")
|
|
|
|
}
|
|
|
|
for k := len(allCombatants) - 1; k >= 0; k-- {
|
|
|
|
allCombatants[k].Fight()
|
|
|
|
}
|
|
|
|
i := 0
|
|
|
|
for _, x := range b.immune {
|
|
|
|
if x.units > 0 {
|
|
|
|
b.immune[i] = x
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.immune = b.immune[:i]
|
|
|
|
i = 0
|
|
|
|
for _, x := range b.infect {
|
|
|
|
if x.units > 0 {
|
|
|
|
b.infect[i] = x
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.infect = b.infect[:i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Battle) FindTargetFor(a *Army) {
|
|
|
|
if a.tp == ArmyTypeImmune {
|
|
|
|
a.FindTarget(b.infect)
|
|
|
|
} else if a.tp == ArmyTypeInfection {
|
|
|
|
a.FindTarget(b.immune)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Battle) IsOver() bool {
|
|
|
|
return len(b.immune) == 0 || len(b.infect) == 0
|
2019-11-08 03:30:41 +00:00
|
|
|
}
|