-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.go
228 lines (195 loc) · 4.53 KB
/
board.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
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
package antwar
import (
"fmt"
"math/rand"
"sort"
)
const (
HERE Action = iota
NORTH
EAST
SOUTH
WEST
)
func mod(n, d int) int {
return ((n % d) + d) % d
}
type Pos struct {
X, Y int
}
type column []*Tile
func (board *Board) NorthFrom(pos Pos) Pos {
return Pos{pos.X, mod(pos.Y-1, board.height)}
}
func (board *Board) SouthFrom(pos Pos) Pos {
return Pos{pos.X, mod(pos.Y+1, board.height)}
}
func (board *Board) EastFrom(pos Pos) Pos {
return Pos{mod(pos.X+1, board.width), pos.Y}
}
func (board *Board) WestFrom(pos Pos) Pos {
return Pos{mod(pos.X-1, board.width), pos.Y}
}
type Board struct {
width, height int
columns []column
onUpdate func(p Pos)
ants AntSet
antHills []*AntHill
teams []*Team
}
func (b *Board) randomPos() Pos {
return Pos{rand.Intn(b.width), rand.Intn(b.height)}
}
func (b *Board) randomTile() *Tile {
return b.At(b.randomPos())
}
func (b *Board) At(p Pos) *Tile {
return (*b).columns[p.X][p.Y]
}
func (board *Board) createStartingAntHills() {
numberOfTeams := len(board.teams)
board.antHills = make([](*AntHill), numberOfTeams)
for i, team := range board.teams {
board.antHills[i] = board.randomTile().createAntHill(team)
}
fmt.Printf("Created %v starting ant hills\n", len(board.antHills))
}
func (board *Board) CreateStartingAnts(startingNumberOfAnts int) {
// Add 1 ant at a time to each team to avoid one team's ants all get to move before the other teams' ants
for i := 0; i < startingNumberOfAnts; i++ {
for _, antHill := range board.antHills {
antHill.spawnAnt()
}
}
}
func (b *Board) SpawnFoodRandomly(ncolumns, nFood int) {
for i, j := 0, rand.Intn(ncolumns); i < j; i++ {
t := b.randomTile()
t.putFood(rand.Intn(nFood))
t.update()
}
}
func (board *Board) SpawnAnts() {
// FIXME: Spawning ants for one anthill at a time gives an advantage to the team which is listed first.
for _, antHill := range board.antHills {
antHill.spawnAnts()
}
}
func moveFood(fromTile, toTile *Tile) {
if fromTile.food > 0 {
fromTile.removeFood(1)
toTile.putFood(1)
}
}
func (b *Board) removeAntHill(h *AntHill) {
for index, candidate := range b.antHills {
if candidate == h {
b.antHills = append(b.antHills[:index], b.antHills[index+1:]...)
return
}
}
}
func findDestination(origin *Tile, decision Action) (destination *Tile) {
switch decision {
case NORTH:
destination = origin.North()
case SOUTH:
destination = origin.South()
case EAST:
destination = origin.East()
case WEST:
destination = origin.West()
case HERE:
destination = origin
}
return
}
func (board *Board) MoveAnts() {
board.ants.Do(func(ant *Ant) {
origin := ant.tile
brains := origin.ants.brainsExcept(ant)
decision, bringFood := ant.brain.Decide(origin, brains)
destination := findDestination(origin, decision)
if origin == destination {
return
}
origin.removeAnt(ant)
destination.putAnt(ant)
if bringFood {
moveFood(origin, destination)
}
origin.update()
destination.update()
})
}
func (b *Board) CheckForWin() bool {
nTeamsWithAnts := 0
for _, team := range b.teams {
if team.ants.Len() > 0 {
nTeamsWithAnts++
if nTeamsWithAnts > 1 {
return false
}
}
}
return true
}
func (b *Board) TeamsByRank() []*Team {
teams := make(Teams, len(b.teams))
copy(teams, b.teams)
sort.Sort(ByRank{teams})
return teams
}
func (b *Board) Update(t *Tile) {
if b.onUpdate != nil {
b.onUpdate(t.pos)
}
}
func (b *Board) Width() int {
return b.width
}
func (b *Board) Height() int {
return b.height
}
func (board *Board) createGrid() {
board.columns = make([]column, board.width)
for x := 0; x < board.width; x++ {
board.columns[x] = make([]*Tile, board.height)
}
}
func (b *Board) createTiles() {
for x := 0; x < b.width; x++ {
for y := 0; y < b.height; y++ {
tile := new(Tile)
tile.board = b
b.columns[x][y] = tile
tile.ants = NewAntSet(20)
tile.pos = Pos{x, y}
}
}
}
func (board *Board) linkTiles() {
for x := 0; x < board.width; x++ {
for y := 0; y < board.height; y++ {
pos := Pos{x, y}
here := board.At(pos)
here.north = board.At(board.NorthFrom(pos))
here.east = board.At(board.EastFrom(pos))
here.south = board.At(board.SouthFrom(pos))
here.west = board.At(board.WestFrom(pos))
}
}
}
func (board *Board) takeTurn() {
board.SpawnAnts()
board.SpawnFoodRandomly(nTilesToReceiveExtraFood, nFoodToPutOnTiles)
board.MoveAnts()
}
func NewBoard(width, height int) (board *Board) {
board = &Board{width: width, height: height, ants: NewAntSet(10000)}
board.createGrid()
board.createTiles()
board.linkTiles()
return
}