-
Notifications
You must be signed in to change notification settings - Fork 0
/
predator.go
89 lines (70 loc) · 2.03 KB
/
predator.go
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
package main
import (
"fmt"
"math"
"math/rand"
"slices"
)
type PredatorType AnimalType
const (
Wolf PredatorType = iota + 4
Lion
)
func (p PredatorType) String() string {
return [...]string{"Wolf", "Lion"}[p-4]
}
type Predator struct {
kind PredatorType
canHunt []PreyType
Hunter
Animal
}
func NewPredator(kind PredatorType, canHunt []PreyType, unit int, gender bool) *Predator {
return &Predator{kind, canHunt, *NewHunter(unit), *NewAnimal(gender)}
}
func (p *Predator) Kind() string {
return p.kind.String()
}
// TODO Write a general method to reduce code repetition in Hunt and Breed methods
func (p *Predator) Breed() (animal IAnimal, born bool) {
foundPredators := Scan(p.x, p.y, p.unitRange, func(m IMover) (*Predator, bool) {
v, ok := m.(*Predator)
return v, ok && v.kind == p.kind && v.gender != p.gender
})
if len(foundPredators) == 0 {
return
}
slices.SortFunc(foundPredators, func(a, b *Predator) int {
return int(
math.Abs(float64(p.x-(*a).X())) + math.Abs(float64(p.y-(*a).Y())) -
math.Abs(float64(p.x-(*b).X())) + math.Abs(float64(p.y-(*b).Y())),
)
})
nearest := foundPredators[0]
fmt.Printf("%ss breed at %v,%v -> %v,%v. ", p.kind, p.x, p.y, nearest.x, nearest.y)
animal = NewPredator(p.kind, p.canHunt, p.unitRange, rand.Intn(2) == 0)
born = true
fmt.Printf("newborn is at %v,%v\n", animal.X(), animal.Y())
predatorsBreeded[p.kind]++
return
}
func (p *Predator) Hunt() (prey IAnimal, hunted bool) {
foundPreys := Scan(p.x, p.y, p.unitRange, func(m IMover) (*Prey, bool) {
v, ok := m.(*Prey)
return v, ok && slices.Contains(p.canHunt, v.kind)
})
if len(foundPreys) == 0 {
return
}
slices.SortFunc(foundPreys, func(a, b *Prey) int {
return int(
math.Abs(float64(p.x-(*a).X())) + math.Abs(float64(p.y-(*a).Y())) -
math.Abs(float64(p.x-(*b).X())) + math.Abs(float64(p.y-(*b).Y())),
)
})
prey = foundPreys[0]
hunted = true
fmt.Printf("%s hunts %s at %v,%v -> %v,%v\n", p.kind, prey.Kind(), p.x, p.y, prey.X(), foundPreys[0].Y())
predatorsHunted[foundPreys[0].kind]++
return
}