-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhunter.go
More file actions
48 lines (39 loc) · 987 Bytes
/
hunter.go
File metadata and controls
48 lines (39 loc) · 987 Bytes
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
package main
import (
"fmt"
"math"
"slices"
)
type Hunter struct {
LivingThing
}
func NewHunter(unit int) *Hunter {
return &Hunter{*NewLivingThing(unit)}
}
// Search is a method that searches for prey and retruns the first prey found
//
// if there is no prey found it returns nil
func (h *Hunter) Hunt() (hunt IAnimal, hunted bool) {
foundAnimals := Scan(h.x, h.y, h.unitRange, func(m IMover) (IAnimal, bool) {
v, ok := m.(IAnimal)
return v, ok
})
slices.SortFunc(foundAnimals, func(a, b IAnimal) int {
return int(
math.Abs(float64(h.x-a.X())) + math.Abs(float64(h.y-a.Y())) -
math.Abs(float64(h.x-b.X())) + math.Abs(float64(h.y-b.Y())),
)
})
if len(foundAnimals) == 0 {
return
}
hunt = foundAnimals[0]
hunted = true
fmt.Printf("The hunter hunts %s at %v,%v -> %v,%v\n", hunt.Kind(), h.x, h.y, hunt.X(), hunt.Y())
if p, ok := hunt.(*Prey); ok {
huntersHuntedPreys[p.kind]++
} else {
huntersHuntedPreds[hunt.(*Predator).kind]++
}
return
}