-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprey.go
More file actions
62 lines (49 loc) · 1.19 KB
/
prey.go
File metadata and controls
62 lines (49 loc) · 1.19 KB
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
package main
import (
"fmt"
"math"
"math/rand"
"slices"
)
type PreyType AnimalType
const (
Sheep PreyType = iota + 1
Cow
Chicken
)
func (p PreyType) String() string {
return [...]string{"Sheep", "Cow", "Chicken"}[p-1]
}
type Prey struct {
kind PreyType
LivingThing
Animal
}
func NewPrey(kind PreyType, unit int, gender bool) *Prey {
return &Prey{kind, *NewLivingThing(unit), *NewAnimal(gender)}
}
func (p *Prey) Kind() string {
return p.kind.String()
}
func (p *Prey) Breed() (animal IAnimal, born bool) {
foundPreys := Scan(p.x, p.y, p.unitRange, func(m IMover) (*Prey, bool) {
v, ok := m.(*Prey)
return v, ok && v.kind == p.kind && v.gender != p.gender
})
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())),
)
})
nearest := foundPreys[0]
fmt.Printf("%ss breed at %v,%v -> %v,%v ", p.kind, p.x, p.y, nearest.x, nearest.y)
animal = NewPrey(p.kind, p.unitRange, rand.Intn(2) == 0)
born = true
fmt.Printf("newborn is at %v,%v\n", animal.X(), animal.Y())
preysBreeded[p.kind]++
return
}