feat: got logic to run.

This commit is contained in:
CronyAkatsuki 2025-03-03 11:48:02 +01:00
parent 9f7e566d28
commit 4e3ed9cf2a

36
main.go
View File

@ -20,6 +20,9 @@ type gol struct {
buffer [ROW][COL]int
}
// Create array that holds field's to check for neighbours
var CHECK_ARRAY = [8][2]int{{-1, -1}, {-1, 0}, {-1, +1}, {0, -1}, {0, +1}, {+1, -1}, {+1, 0}, {+1, +1}}
// Initialize empty game of life
func initGol() gol {
start := [ROW][COL]int{}
@ -44,6 +47,36 @@ func (g gol) Init() tea.Cmd {
return nil
}
// Check neighobours
func (g gol) countNeighbours(x int, y int) int {
n := 0
for i := 0; i < 8; i++ {
if g.buffer[x+CHECK_ARRAY[i][0]][y+CHECK_ARRAY[i][1]] == 1 {
n += 1
}
}
return n
}
// Life logic
func (g gol) Life() tea.Model {
for i := 1; i < ROW-1; i++ {
for j := 1; j < COL-1; j++ {
n := g.countNeighbours(i, j)
if g.buffer[i][j] == 1 {
if n < 2 || n > 3 {
g.buffer[i][j] = 0
}
} else {
if n == 3 {
g.buffer[i][j] = 1
}
}
}
}
return g
}
// Simple update
func (g gol) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
@ -52,6 +85,9 @@ func (g gol) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.String() {
case "ctrl+c", "q":
return g, tea.Quit
case " ":
g.Life()
return g, nil
}
}
return g, nil