Compare commits

..

No commits in common. "84e9e8fdea5d2da93740ffe1e410f0f0b1e6f03c" and "4e3ed9cf2a92e0b310672e8955e3c6e430dcfb25" have entirely different histories.

14
main.go
View File

@ -18,7 +18,6 @@ const (
type gol struct { type gol struct {
grid [ROW][COL]int grid [ROW][COL]int
buffer [ROW][COL]int buffer [ROW][COL]int
step int
} }
// Create array that holds field's to check for neighbours // Create array that holds field's to check for neighbours
@ -48,11 +47,11 @@ func (g gol) Init() tea.Cmd {
return nil return nil
} }
// Check neighbours // Check neighobours
func (g gol) countNeighbours(x int, y int) int { func (g gol) countNeighbours(x int, y int) int {
n := 0 n := 0
for i := 0; i < 8; i++ { for i := 0; i < 8; i++ {
if g.grid[x+CHECK_ARRAY[i][0]][y+CHECK_ARRAY[i][1]] == 1 { if g.buffer[x+CHECK_ARRAY[i][0]][y+CHECK_ARRAY[i][1]] == 1 {
n += 1 n += 1
} }
} }
@ -64,7 +63,7 @@ func (g gol) Life() tea.Model {
for i := 1; i < ROW-1; i++ { for i := 1; i < ROW-1; i++ {
for j := 1; j < COL-1; j++ { for j := 1; j < COL-1; j++ {
n := g.countNeighbours(i, j) n := g.countNeighbours(i, j)
if g.grid[i][j] == 1 { if g.buffer[i][j] == 1 {
if n < 2 || n > 3 { if n < 2 || n > 3 {
g.buffer[i][j] = 0 g.buffer[i][j] = 0
} }
@ -75,8 +74,6 @@ func (g gol) Life() tea.Model {
} }
} }
} }
g.grid = g.buffer
g.step++
return g return g
} }
@ -89,7 +86,8 @@ func (g gol) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case "ctrl+c", "q": case "ctrl+c", "q":
return g, tea.Quit return g, tea.Quit
case " ": case " ":
return g.Life(), nil g.Life()
return g, nil
} }
} }
return g, nil return g, nil
@ -97,7 +95,7 @@ func (g gol) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// Let's draw this all simply // Let's draw this all simply
func (g gol) View() string { func (g gol) View() string {
s := fmt.Sprintf("Step: %d\n", g.step) var s string
// Making sure to not draw the empty field around the array // Making sure to not draw the empty field around the array
for i := 1; i < ROW-1; i++ { for i := 1; i < ROW-1; i++ {
for j := 1; j < COL-1; j++ { for j := 1; j < COL-1; j++ {