-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
48 lines (41 loc) · 993 Bytes
/
utils.go
File metadata and controls
48 lines (41 loc) · 993 Bytes
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
package main
import (
"math/rand"
"time"
tea "github.com/charmbracelet/bubbletea"
)
func tickEvery(d time.Duration) tea.Cmd {
return tea.Tick(d, func(t time.Time) tea.Msg {
return tickMsg(t)
})
}
func isHorizontal(d direction) bool {
return d == left || d == right
}
func getRandomCoord(exclude []coord) coord {
for {
x := rand.Intn(cols)
y := rand.Intn(rows)
if !contains(exclude, coord{x: x, y: y}) {
return coord{x: x, y: y}
}
}
}
func contains(s []coord, e coord) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func appendHeadingIfLegal(headings []direction, newHeading direction) []direction {
// It's only legal to turn 90 degrees at a time. If the direction being
// appended would result in an illegal turn then discard it and return
// headings unchanged.
lastHeading := headings[len(headings)-1]
if isHorizontal(lastHeading) != isHorizontal(newHeading) {
return append(headings, newHeading)
}
return headings
}