-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
231 lines (195 loc) · 4.49 KB
/
Copy pathmain.go
File metadata and controls
231 lines (195 loc) · 4.49 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"fmt"
"math/rand"
"slices"
)
const AREA_SIZE = 500
var (
area *Area
huntersHuntedPreds = make(map[PredatorType]int)
huntersHuntedPreys = make(map[PreyType]int)
predatorsHunted = make(map[PreyType]int)
predatorsBreeded = make(map[PredatorType]int)
preysBreeded = make(map[PreyType]int)
)
type Area struct {
preys []*Prey
predators []*Predator
hunter *Hunter
action int
matrix [][]IMover
count int
}
func NewArea(hunter *Hunter, predators []*Predator, preys []*Prey) *Area {
newArea := new(Area)
newArea.hunter = hunter
newArea.predators = predators
newArea.preys = preys
matrix := make([][]IMover, AREA_SIZE)
for i := range matrix {
matrix[i] = make([]IMover, AREA_SIZE)
}
newArea.matrix = matrix
newArea.add(hunter)
for _, p := range predators {
newArea.add(p)
}
for _, p := range preys {
newArea.add(p)
}
return newArea
}
func (a *Area) add(i IMover) {
for a.At(i.X(), i.Y()) != nil {
fmt.Println("Found a collision")
i.SetX(rand.Intn(AREA_SIZE))
i.SetY(rand.Intn(AREA_SIZE))
}
a.matrix[AREA_SIZE-i.Y()-1][i.X()] = i
}
func (a *Area) calculateTotal() int {
count := 0
for y := range AREA_SIZE {
for x := range AREA_SIZE {
if a.At(x, y) != nil {
count++
}
}
}
a.count = count
return count
}
func (a *Area) At(x, y int) IMover {
return a.matrix[AREA_SIZE-y-1][x]
}
func (a *Area) AddAnimal(animal IAnimal) {
switch animal := animal.(type) {
case *Predator:
a.predators = append(a.predators, animal)
case *Prey:
a.preys = append(a.preys, animal)
default:
panic("Unknown animal")
}
a.add(animal)
}
func (a *Area) Print() {
fmt.Printf("\ntotal number of living things in the area: %v\n", a.calculateTotal())
printMap("hunters hunts", huntersHuntedPreys)
for k, v := range huntersHuntedPreds {
fmt.Printf("\t%v: %v, ", k, v)
}
fmt.Printf("\n")
printMap("predators hunts", predatorsHunted)
printMap("predators breed", predatorsBreeded)
printMap("preys breed", preysBreeded)
}
func printMap[T comparable](str string, iterate map[T]int) {
fmt.Printf("%s : \n", str)
if len(iterate) == 0 {
fmt.Printf("\tnothing\n")
} else {
for k, v := range iterate {
fmt.Printf("\t%v: %v, ", k, v)
}
fmt.Printf("\n")
}
}
func (a *Area) Delete(x, y int) {
a.matrix[AREA_SIZE-y-1][x] = nil
}
func Move[T IMover](movers ...T) {
for _, m := range movers {
if area.action >= 1000 {
return
}
oldX, oldY := m.X(), m.Y()
m.Move()
area.Delete(oldX, oldY)
area.add(m)
area.action += m.Unit()
}
}
func Hunt[T IHunter](hunters ...T) {
for _, hunter := range hunters {
hunt, hunted := hunter.Hunt()
if hunted {
area.Delete(hunt.X(), hunt.Y())
switch hunt := hunt.(type) {
case *Predator:
area.predators = slices.DeleteFunc(area.predators, func(a *Predator) bool {
return a.x == hunt.X() && a.y == hunt.Y()
})
case *Prey:
area.preys = slices.DeleteFunc(area.preys, func(a *Prey) bool {
return a.x == hunt.X() && a.y == hunt.Y()
})
default:
panic("Unknown hunt")
}
}
}
}
func Breed[T IAnimal](breeders ...T) {
for _, breeder := range breeders {
breed, born := breeder.Breed()
if born {
area.AddAnimal(breed)
}
}
}
func Scan[T IMover](px, py, gap int, fn func(m IMover) (T, bool)) []T {
found := make([]T, 0)
for xr, xl, a := px-gap, px+gap, 0; xr <= xl; xr, xl, a = xr+1, xl-1, a+1 {
for y := py + a; y >= py-a; y-- {
if xr < 0 || xl < 0 || y < 0 || xr >= AREA_SIZE || xl >= AREA_SIZE || y >= AREA_SIZE {
continue
}
aleft := area.At(xl, y)
aright := area.At(xl, y)
if v, ok := fn(aleft); ok {
found = append(found, v)
}
if v, ok := fn(aright); ok {
found = append(found, v)
}
}
}
return found
}
func init() {
hunter := NewHunter(1)
preys := make([]*Prey, 60)
for i := 0; i < 30; i++ {
preys[i] = NewPrey(Sheep, 2, i%2 == 0)
}
for i := 30; i < 40; i++ {
preys[i] = NewPrey(Cow, 2, i%2 == 0)
}
for i := 40; i < 60; i++ {
preys[i] = NewPrey(Chicken, 1, i%2 == 0)
}
predators := make([]*Predator, 18)
for i := 0; i < 10; i++ {
predators[i] = NewPredator(Wolf, []PreyType{Sheep, Chicken}, 3, i%2 == 0)
}
for i := 10; i < 18; i++ {
predators[i] = NewPredator(Lion, []PreyType{Cow, Sheep}, 4, i%2 == 0)
}
area = NewArea(hunter, predators, preys)
}
func main() {
for area.action < 1000 {
Move(area.hunter)
Move(area.predators...)
Move(area.preys...)
// first love
Breed(area.preys...)
Breed(area.predators...)
// then fight
Hunt(area.hunter)
Hunt(area.predators...)
}
area.Print()
}