-
Notifications
You must be signed in to change notification settings - Fork 0
/
antwar.go
117 lines (93 loc) · 1.96 KB
/
antwar.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
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
package antwar
import (
"image/color"
)
type Action int
type AntBrain interface {
Decide(env *Tile, brains []AntBrain) (Action, bool)
}
type Ant struct {
brain AntBrain
team *Team
tile *Tile
}
type AntSpawner (func() AntBrain)
type AntSet map[*Ant]bool
func (s AntSet) Put(a *Ant) {
s[a] = true
}
func (s AntSet) Remove(a *Ant) {
delete(s, a)
}
func (s AntSet) Do(f func(a *Ant)) {
for ant, isPresent := range s {
if isPresent {
f(ant)
}
}
}
func (s AntSet) Len() int {
return len(s)
}
func (set AntSet) brainsExcept(exception *Ant) []AntBrain {
brains := make([]AntBrain, 0, set.Len()-1)
set.Do(func(ant *Ant) {
if ant != exception {
brains = append(brains, ant.brain)
}
})
return brains
}
func NewAntSet(capacity int) AntSet {
s := make(AntSet, capacity)
return s
}
type AntHill struct {
team *Team
ants AntSet
tile *Tile
}
func (h *AntHill) spawnAnt() *Ant {
ant := &Ant{h.team.spawn(), h.team, h.tile}
h.tile.putAnt(ant)
h.tile.board.ants.Put(ant)
ant.team.ants.Put(ant)
return ant
}
func (hill *AntHill) spawnAnts() {
for 0 < hill.tile.food {
hill.spawnAnt()
hill.tile.removeFood(1)
}
hill.tile.update()
}
var colorIndex = 0
type Team struct {
name string
ants AntSet
spawn AntSpawner
color color.Color
}
type Teams []*Team
func (self *Team) RanksLowerThan(other *Team) bool {
return self.ants.Len() < other.ants.Len()
}
type ByRank struct {
Teams
}
func (s Teams) Len() int { return len(s) }
func (s Teams) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s ByRank) Less(i, j int) bool { return s.Teams[i].RanksLowerThan(s.Teams[j]) }
func NewTeam(name string, spawn AntSpawner) *Team {
teamColors := [...]color.RGBA{
color.RGBA{255, 128, 128, 255},
color.RGBA{128, 255, 128, 255},
color.RGBA{128, 128, 255, 255},
color.RGBA{127, 128, 127, 255},
color.RGBA{127, 127, 128, 255},
color.RGBA{128, 127, 127, 255},
}
color := teamColors[colorIndex]
colorIndex++
return &Team{name, NewAntSet(5000), spawn, color}
}