-
Notifications
You must be signed in to change notification settings - Fork 0
/
enemy.go
235 lines (216 loc) · 5.53 KB
/
enemy.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
229
230
231
232
233
234
235
package main
import (
"math"
"math/rand"
"time"
termbox "github.com/nsf/termbox-go"
)
type iEnemy interface {
getPosition() (x, y int)
setPosition(x, y int)
getDisplayFormat() (rune, termbox.Attribute)
think(p *player) (int, int)
move(x, y int)
hasCaptured(p *player)
eval(p *player, x, y int) float64
}
type enemy struct {
x int
y int
char rune
color termbox.Attribute
waitingTime int
oneActionInN int
canMove func(int, int) bool
strategy
underRune
}
type hunter struct {
enemy
}
type ghost struct {
enemy
}
type assassin struct {
enemy
}
type strategy interface {
eval(p *player, x, y int) float64
}
type assault struct{}
type tricky struct{}
type ambush struct{}
type underRune struct {
char rune
fgColor termbox.Attribute
bgColor termbox.Attribute
}
func (e *enemy) getPosition() (x, y int) {
return e.x, e.y
}
func (e *enemy) setPosition(x, y int) {
e.x, e.y = x, y
}
func (e *enemy) getDisplayFormat() (rune, termbox.Attribute) {
return e.char, e.color
}
func control(s stage, p *player) error {
for _, e := range s.enemies {
e.move(e.think(p))
e.hasCaptured(p)
}
if err := termbox.Flush(); err != nil {
return err
}
time.Sleep(s.gameSpeed)
return nil
}
func (e *enemy) think(p *player) (int, int) {
x, y := e.getPosition()
// Calculate the evaluation value for movement
up := e.eval(p, x, y-1)
down := e.eval(p, x, y+1)
left := e.eval(p, x-1, y)
right := e.eval(p, x+1, y)
// The evaluation value is the distance between the player and the enemy
// So prefer lower (closer) values
if up <= down && up <= left && up <= right {
return x, y - 1 // up
} else if down <= left && down <= right {
return x, y + 1 // down
} else if left <= right {
return x - 1, y // left
} else {
return x + 1, y // right
}
}
func (e *enemy) move(x, y int) {
e.waitingTime--
if e.waitingTime <= 0 && e.canMove(x, y) {
winWidth, _ := termbox.Size()
// Set the original character in the original cell
termbox.SetCell(e.x, e.y, e.underRune.char, e.underRune.fgColor, e.underRune.bgColor)
// Retains destination cell information
// Because it is necessary to set the original character at the next move
cell := termbox.CellBuffer()[(winWidth*y)+x]
e.setPosition(x, y)
e.underRune.char = cell.Ch
e.underRune.fgColor = cell.Fg
e.underRune.bgColor = cell.Bg
termbox.SetCell(x, y, e.char, e.color, e.color)
e.waitingTime = e.oneActionInN
}
}
func (e *enemy) hasCaptured(p *player) {
if e.x == p.x && e.y == p.y {
p.state = lose
}
}
func (e *enemy) eval(p *player, x, y int) float64 {
if !e.canMove(x, y) {
// Returns a large enough value if it can't move
return 1000
}
return e.strategy.eval(p, x, y)
}
func (s *assault) eval(p *player, x, y int) float64 {
// Distance between two points
return math.Sqrt(math.Pow(float64(p.y-y), 2) + math.Pow(float64(p.x-x), 2))
}
func (s *tricky) eval(p *player, x, y int) float64 {
if random(0, 5) == 0 {
return float64(random(0, 30))
} else {
return math.Sqrt(math.Pow(float64(p.y-y), 2) + math.Pow(float64(p.x-x), 2))
}
}
func (s *ambush) eval(p *player, x, y int) float64 {
// Implement the logic for calculating the evaluation value.
return float64(random(0, 30))
}
// Return a value between min and max
// e.g. random(0, 3) returns 0,1,2,3
func random(min, max int) int {
rand.New(rand.NewSource(time.Now().UnixNano()))
return rand.Intn(max-min+1) + min
}
type iEnemyBuilder interface {
displayFormat(rune, string) iEnemyBuilder
speed(int) iEnemyBuilder
strategize(strategy) iEnemyBuilder
movable(func(int, int) bool) iEnemyBuilder
defaultHunter() iEnemyBuilder
defaultGhost() iEnemyBuilder
defaultAssassin() iEnemyBuilder
build() iEnemy
}
type enemyBuilder struct {
x int
y int
char rune
color termbox.Attribute
underRuneBgColor termbox.Attribute
waitingTime int
oneActionInN int
canMove func(int, int) bool
strategy strategy
}
func (eb *enemyBuilder) displayFormat(r rune, s string) iEnemyBuilder {
eb.char = r
switch s {
case "RED":
eb.color = termbox.ColorRed
case "CYAN":
eb.color = termbox.ColorCyan
case "DARK_GRAY":
eb.color = termbox.ColorDarkGray
}
return eb
}
func (eb *enemyBuilder) speed(i int) iEnemyBuilder {
eb.waitingTime = i
eb.oneActionInN = i
return eb
}
func (eb *enemyBuilder) movable(fn func(int, int) bool) iEnemyBuilder {
eb.canMove = fn
return eb
}
func (eb *enemyBuilder) strategize(s strategy) iEnemyBuilder {
eb.strategy = s
return eb
}
func (eb *enemyBuilder) defaultHunter() iEnemyBuilder {
fn := func(x, y int) bool {
return !isCharWall(x, y) && !isCharEnemy(x, y)
}
return eb.displayFormat(chHunter, "RED").speed(1).movable(fn).strategize(&assault{})
}
func (eb *enemyBuilder) defaultGhost() iEnemyBuilder {
fn := func(x, y int) bool {
return !isCharBoundary(x, y) && !isCharEnemy(x, y)
}
return eb.displayFormat(chGhost, "CYAN").speed(2).movable(fn).strategize(&assault{})
}
func (eb *enemyBuilder) defaultAssassin() iEnemyBuilder {
fn := func(x, y int) bool {
return !isCharWall(x, y) && !isCharEnemy(x, y)
}
return eb.displayFormat(chAssassin, "DARK_GRAY").speed(1).movable(fn).strategize(&assault{})
}
func newEnemyBuilder() iEnemyBuilder {
return &enemyBuilder{}
}
func (eb *enemyBuilder) build() iEnemy {
return &enemy{
x: eb.x,
y: eb.y,
char: eb.char,
color: eb.color,
waitingTime: eb.waitingTime,
oneActionInN: eb.oneActionInN,
canMove: eb.canMove,
strategy: eb.strategy,
underRune: underRune{bgColor: termbox.ColorBlack},
}
}