-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
194 lines (169 loc) · 3.97 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
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 (
"bufio"
"flag"
"fmt"
"math/rand"
"os"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var width, height int
var inputFile string
type cell struct {
alive bool
}
type model struct {
grid [][]cell
running bool
}
func readInputFile(filename string) ([][]cell, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
var grid [][]cell
scanner := bufio.NewScanner(file)
lineNum := 0
for scanner.Scan() {
line := scanner.Text()
line = strings.TrimSpace(line)
// Skip empty lines and comments
if line == "" || strings.HasPrefix(line, "!") {
continue
}
if lineNum == 0 {
width = len(line)
} else if len(line) != width {
return nil, fmt.Errorf("line %d has length %d, expected %d", lineNum+1, len(line), width)
}
row := make([]cell, width)
for i, char := range line {
row[i] = cell{alive: char == '*' || char == '1'}
}
grid = append(grid, row)
lineNum++
}
if err := scanner.Err(); err != nil {
return nil, err
}
height = len(grid)
return grid, nil
}
func initialModel() model {
var grid [][]cell
var err error
if inputFile != "" {
grid, err = readInputFile(inputFile)
if err != nil {
fmt.Printf("Error reading input file: %v\n", err)
os.Exit(1)
}
} else {
grid = make([][]cell, height)
for i := 0; i < height; i++ {
grid[i] = make([]cell, width)
for j := 0; j < width; j++ {
grid[i][j].alive = rand.Intn(2) == 1
}
}
}
return model{
grid: grid,
}
}
func (m model) Init() tea.Cmd {
return tickCmd()
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "q", "ctrl+c":
return m, tea.Quit
case "r":
m.running = !m.running
}
case tickMsg:
if m.running {
m.grid = nextGeneration(m.grid)
}
return m, tickCmd()
}
return m, nil
}
func (m model) View() string {
var b strings.Builder
for i := 0; i < height; i++ {
for j := 0; j < width; j++ {
if m.grid[i][j].alive {
b.WriteString(lipgloss.NewStyle().Foreground(lipgloss.Color("#212829")).Background(lipgloss.Color("#3b7ed2")).Render(" "))
} else {
b.WriteString(" ")
}
}
if i < height-1 {
b.WriteRune('\n')
}
}
return b.String() + "\n\nPress 'r' to toggle simulation, 'q' or 'ctrl+c' to quit."
}
func nextGeneration(grid [][]cell) [][]cell {
newGrid := make([][]cell, height)
for i := 0; i < height; i++ {
newGrid[i] = make([]cell, width)
}
for i := 0; i < height; i++ {
for j := 0; j < width; j++ {
aliveNeighbors := countAliveNeighbors(grid, i, j)
if grid[i][j].alive && (aliveNeighbors == 2 || aliveNeighbors == 3) {
newGrid[i][j] = cell{alive: true}
} else if !grid[i][j].alive && aliveNeighbors == 3 {
newGrid[i][j] = cell{alive: true}
} else {
newGrid[i][j] = cell{alive: false}
}
}
}
return newGrid
}
func countAliveNeighbors(grid [][]cell, x, y int) int {
aliveCount := 0
for i := -1; i <= 1; i++ {
for j := -1; j <= 1; j++ {
if i == 0 && j == 0 {
continue
}
neighborX, neighborY := x+i, y+j
if neighborX >= 0 && neighborX < height && neighborY >= 0 && neighborY < width {
if grid[neighborX][neighborY].alive {
aliveCount++
}
}
}
}
return aliveCount
}
type tickMsg struct{}
func tickCmd() tea.Cmd {
return func() tea.Msg {
time.Sleep(250 * time.Millisecond)
return tickMsg{}
}
}
func main() {
flag.IntVar(&width, "width", 40, "width of the grid (default 40, ignored if input file is provided)")
flag.IntVar(&height, "height", 25, "height of the grid (default 25, ignored if input file is provided)")
flag.StringVar(&inputFile, "input", "", "path to input file for initial state")
var seed int64
flag.Int64Var(&seed, "seed", time.Now().UnixNano(), "seed for random generation")
flag.Parse()
rand.Seed(seed)
p := tea.NewProgram(initialModel(), tea.WithAltScreen())
if err := p.Start(); err != nil {
fmt.Printf("Error running program: %v", err)
}
}