/
naive_ant.go
134 lines (121 loc) · 2.4 KB
/
naive_ant.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
package naive_ant
import (
"github.com/KaptajnKold/antwar"
"math/rand"
)
type pos struct {
x, y int
}
type naiveAnt struct {
direction antwar.Action
pos
turn int
}
func flipCoin() bool {
if rand.Intn(2) == 0 {
return true
}
return false
}
func (me *naiveAnt) directionHome() (d antwar.Action) {
horizontal := antwar.HERE
vertical := antwar.HERE
if me.x > 0 {
horizontal = antwar.WEST
}
if me.x < 0 {
horizontal = antwar.EAST
}
if me.y > 0 {
vertical = antwar.NORTH
}
if me.y < 0 {
vertical = antwar.SOUTH
}
if horizontal == antwar.HERE && vertical == antwar.HERE {
d = antwar.HERE
} else if horizontal == antwar.HERE {
d = vertical
} else if vertical == antwar.HERE {
d = horizontal
} else if flipCoin() {
d = vertical
} else {
d = horizontal
}
return
}
func (me *naiveAnt) directionOut() (d antwar.Action) {
var horizontal, vertical antwar.Action
if me.x < 0 {
horizontal = antwar.WEST
} else if me.x > 0 {
horizontal = antwar.EAST
} else if flipCoin() {
horizontal = antwar.WEST
} else {
horizontal = antwar.EAST
}
if me.y < 0 {
vertical = antwar.NORTH
} else if me.y > 0 {
vertical = antwar.SOUTH
} else if flipCoin() {
vertical = antwar.NORTH
} else {
vertical = antwar.SOUTH
}
if flipCoin() {
d = horizontal
} else {
d = vertical
}
return
}
func (a *naiveAnt) update(decision antwar.Action) {
switch decision {
case antwar.NORTH:
a.y--
case antwar.SOUTH:
a.y++
case antwar.EAST:
a.x++
case antwar.WEST:
a.x--
}
a.turn++
}
func oppositeDirectionOf(d antwar.Action) (opposite antwar.Action) {
switch d {
case antwar.NORTH:
opposite = antwar.SOUTH
case antwar.SOUTH:
opposite = antwar.NORTH
case antwar.EAST:
opposite = antwar.WEST
case antwar.WEST:
opposite = antwar.EAST
default:
opposite = (antwar.Action)(rand.Intn(4) + 1)
}
return
}
func (a *naiveAnt) Decide(env *antwar.Tile, brains []antwar.AntBrain) (decision antwar.Action, bringFood bool) {
if env.FoodCount() > 0 {
decision = a.directionHome()
} else if env.North().FoodCount() > 0 {
decision = antwar.NORTH
} else if env.East().FoodCount() > 0 {
decision = antwar.EAST
} else if env.South().FoodCount() > 0 {
decision = antwar.SOUTH
} else if env.West().FoodCount() > 0 {
decision = antwar.WEST
} else {
decision = a.directionOut()
}
a.update(decision)
bringFood = env.FoodCount() > 0
return
}
func Spawn() antwar.AntBrain { return new(naiveAnt) }