cgol/main.go

82 lines
1.2 KiB
Go

package main
import (
"fmt"
"math/rand"
"os"
tea "github.com/charmbracelet/bubbletea"
)
// Create simple constants
const (
ROW = 35
COL = 70
)
// Just need a grid and buffer
type gol struct {
grid [ROW][COL]int
buffer [ROW][COL]int
}
// Initialize empty game of life
func initGol() gol {
start := [ROW][COL]int{}
for i := 0; i < ROW; i++ {
for j := 0; j < COL; j++ {
if rand.Intn(2) == 1 {
start[i][j] = 1
} else {
start[i][j] = 0
}
}
}
return gol{
grid: start,
buffer: start,
}
}
// Simple init
func (g gol) Init() tea.Cmd {
return nil
}
// Simple update
func (g gol) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
// Just quit when using ctrl+c or q
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return g, tea.Quit
}
}
return g, nil
}
// Let's draw this all simply
func (g gol) View() string {
var s string
for i := 0; i < ROW; i++ {
for j := 0; j < COL; j++ {
if g.grid[i][j] == 1 {
s += "*"
} else {
s += " "
}
}
s += "\n"
}
return s
}
func main() {
p := tea.NewProgram(initGol())
if _, err := p.Run(); err != nil {
fmt.Printf("Oh no, an error: %v", err)
os.Exit(1)
}
}