-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.go
More file actions
194 lines (163 loc) · 3.22 KB
/
model.go
File metadata and controls
194 lines (163 loc) · 3.22 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
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
package main
import (
tea "github.com/charmbracelet/bubbletea"
"math/rand"
"strconv"
"time"
)
type Direction int
const (
DirectionUp Direction = iota
DirectionDown
DirectionLeft
DirectionRight
)
type coord struct {
x, y int
}
type model struct {
controller InterfaceLEDController
grid [][]string
direction Direction
body []coord
growNext bool
foodLocation coord
speed time.Duration
}
func (m model) placeFood() coord {
for {
x := rand.Intn(len(m.grid[0]))
y := rand.Intn(len(m.grid))
c := coord{x, y}
occupied := false
for _, b := range m.body {
if b == c {
occupied = true
break
}
}
if !occupied {
if err := m.controller.SetStatus(m.grid[y][x], InterfaceLEDStatusGreen); err != nil {
panic(err)
}
return c
}
}
}
func (m model) Init() tea.Cmd {
return m.tick()
}
func (m model) tick() tea.Cmd {
return tea.Tick(m.speed, func(_ time.Time) tea.Msg {
return moveMsg{}
})
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case moveMsg:
return m.move()
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
case "up", "k":
m.direction = DirectionUp
case "down", "j":
m.direction = DirectionDown
case "left", "h":
m.direction = DirectionLeft
case "right", "l":
m.direction = DirectionRight
}
}
return m, nil
}
func (m model) move() (tea.Model, tea.Cmd) {
head := m.body[0]
var newHead coord
switch m.direction {
case DirectionUp:
if head.y <= 0 {
return m, tea.Quit
}
newHead = coord{head.x, head.y - 1}
case DirectionDown:
if head.y >= len(m.grid)-1 {
return m, tea.Quit
}
newHead = coord{head.x, head.y + 1}
case DirectionLeft:
if head.x <= 0 {
return m, tea.Quit
}
newHead = coord{head.x - 1, head.y}
case DirectionRight:
if head.x >= len(m.grid[0])-1 {
return m, tea.Quit
}
newHead = coord{head.x + 1, head.y}
}
ateFood := newHead == m.foodLocation
if ateFood {
m.growNext = true
}
if err := m.controller.SetStatus(m.grid[newHead.y][newHead.x], InterfaceLEDStatusOrange); err != nil {
panic(err)
}
if !m.growNext {
tail := m.body[len(m.body)-1]
if err := m.controller.SetStatus(m.grid[tail.y][tail.x], InterfaceLEDStatusOff); err != nil {
panic(err)
}
m.body = m.body[:len(m.body)-1]
} else {
m.growNext = false
}
for _, c := range m.body {
if c == newHead {
return m, tea.Quit
}
}
m.body = append([]coord{newHead}, m.body...)
if ateFood {
m.foodLocation = m.placeFood()
}
return m, m.tick()
}
func (m model) View() string {
bodyMap := make(map[coord]bool)
for _, c := range m.body {
bodyMap[c] = true
}
var s string
for y, row := range m.grid {
for x, name := range row {
c := coord{x, y}
var char string
switch {
case m.body[0] == c:
switch m.direction {
case DirectionUp:
char = "^"
case DirectionDown:
char = "v"
case DirectionLeft:
char = "<"
case DirectionRight:
char = ">"
}
case bodyMap[c]:
char = "x"
case c == m.foodLocation:
char = "o"
default:
char = "-"
}
s += char + ": " + name + "\t"
}
s += "\n"
}
s += "\nScore: " + strconv.Itoa(len(m.body)-1)
return s
}
type moveMsg struct{}