-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
168 lines (153 loc) · 2.72 KB
/
main.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
package main
import (
"bytes"
"math/rand"
"time"
tea "github.com/charmbracelet/bubbletea"
)
var fps = 5
const alive = "😂"
const dead = "🌊"
func main() {
model := model{}
p := tea.NewProgram(model, tea.WithAltScreen())
p.Run()
}
type board [][]bool
type model struct {
board board
ready bool
lastUpdate time.Time
ticked bool
last tea.Msg
}
type TickMsg time.Time
// Init implements tea.Model
func (m model) Init() tea.Cmd {
return doTick()
}
func doTick() tea.Cmd {
return tea.Tick(time.Second/time.Duration(fps), func(t time.Time) tea.Msg {
return TickMsg(t)
})
}
// Update implements tea.Model
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.last = msg
m.lastUpdate = time.Now()
m.ticked = false
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.board = make([][]bool, msg.Height)
for i := range m.board {
m.board[i] = make([]bool, msg.Width)
}
m.seed()
m.ready = true
case TickMsg:
if m.ready {
m.board = m.tick()
}
m.ticked = true
return m, doTick()
case tea.KeyMsg:
switch msg.String() {
case "+", "=":
fps++
case "-", "_":
fps--
if fps == 0 {
fps = 1
}
case "q", "esc", "ctrl+c":
return m, tea.Quit
default:
m.seed()
}
}
return m, nil
}
func (m model) seed() {
liveRatio := .50 + rand.Float64()*0.25
for i := range m.board {
for j := range m.board[i] {
if rand.Float64() < liveRatio {
m.board[i][j] = true
} else {
m.board[i][j] = false
}
}
}
}
func (m model) tick() board {
res := make([][]bool, len(m.board))
for i := range res {
res[i] = make([]bool, len(m.board[i]))
}
for i := range m.board {
for j := range m.board[i] {
alive := m.board[i][j]
res[i][j] = m.board[i][j]
switch {
case alive:
n := m.countLiveNeighbors(i, j)
if n < 2 || n > 3 {
res[i][j] = false
}
default:
n := m.countLiveNeighbors(i, j)
if n == 3 {
res[i][j] = true
}
}
}
}
return res
}
func (m model) countLiveNeighbors(i, j int) int {
var count int
for x := i - 1; x <= i+1; x++ {
for y := j - 1; y <= j+1; y++ {
if x == i && y == j {
continue
}
cx, cy := x, y
if cx < 0 {
cx = len(m.board) - 1
}
if cx >= len(m.board) {
cx = 0
}
if cy < 0 {
cy = len(m.board[cx]) - 1
}
if cy >= len(m.board[cx]) {
cy = 0
}
if m.board[cx][cy] {
count++
}
}
}
return count
}
// View implements tea.Model
func (m model) View() string {
if !m.ready {
return ""
}
buf := new(bytes.Buffer)
for i := range m.board {
for j := range m.board[i] {
if m.board[i][j] {
buf.WriteString(alive)
} else {
buf.WriteString(dead)
}
}
if i != len(m.board)-1 {
buf.WriteString("\n")
}
}
return buf.String()
}