2018 day 24 More work
This commit is contained in:
@@ -1,10 +1,72 @@
|
||||
package main
|
||||
|
||||
// All Battle Logic is in here
|
||||
func Battle(army1, army2 []Army) {
|
||||
allCombatants = append(army1, army2...)
|
||||
sort.Sort(ByPower(allCombatants))
|
||||
for _, v := range allCombatants {
|
||||
fmt.Print("%d %d %d (%d)\n", v.tp, v.units, v.hp, v.Power())
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
// All Battle Logic is in here
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user