-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.go
More file actions
87 lines (76 loc) · 1.43 KB
/
player.go
File metadata and controls
87 lines (76 loc) · 1.43 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
package snake
import (
"math/rand"
"time"
)
type Move struct {
Move []float64
ID ID
}
type choice string
const (
left choice = "left"
right choice = "right"
straight choice = "straight"
)
func (m Move) getChoice() choice {
if len(m.Move) != 3 {
return straight
}
if m.Move[0] > m.Move[1] && m.Move[0] > m.Move[2] {
return left
} else if m.Move[1] > m.Move[2] && m.Move[1] > m.Move[0] {
return straight
} else if m.Move[2] > m.Move[0] && m.Move[2] > m.Move[1] {
return right
}
return straight
}
type Human struct {
ID ID
Input chan rune
Framerate time.Duration
}
func (h *Human) Play(gameState GameState) Move {
var key rune
select {
case key = <-h.Input:
case <-time.After(h.Framerate):
key = '0'
}
switch key {
case 'a':
return Move{Move: []float64{1, 0, 0}, ID: h.ID}
case 'w':
return Move{Move: []float64{0, 1, 0}, ID: h.ID}
case 'd':
return Move{Move: []float64{0, 0, 1}, ID: h.ID}
default:
return Move{Move: []float64{0, 1, 0}, ID: h.ID}
}
}
func (h *Human) SetID(id ID) {
h.ID = id
}
type Random struct {
ID ID
}
func (r *Random) Play(gameState GameState) Move {
v := gameState.Vision(r.ID)
left := rand.Float64()
straight := rand.Float64()
right := rand.Float64()
if v[4] >= wall {
left = 0
}
if v[5] >= wall {
straight = 0
}
if v[6] >= wall {
right = 0
}
return Move{Move: []float64{left, straight, right}, ID: r.ID}
}
func (r *Random) SetID(id ID) {
r.ID = id
}