bugfix and added text mode

This commit is contained in:
ston1th 2021-06-03 20:34:33 +02:00
commit 50d5af714d

62
main.go
View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"image" "image"
"image/color" "image/color"
@ -114,8 +115,7 @@ func max(i int) int {
return i return i
} }
func (g *GOL) check(x, y int) { func (g *GOL) n(x, y int) (n int) {
n := 0
for i := min(x - 1); i <= max(x+1); i++ { for i := min(x - 1); i <= max(x+1); i++ {
for j := min(y - 1); j <= max(y+1); j++ { for j := min(y - 1); j <= max(y+1); j++ {
if g.grid1.G[i][j] { if g.grid1.G[i][j] {
@ -123,6 +123,11 @@ func (g *GOL) check(x, y int) {
} }
} }
} }
return
}
func (g *GOL) check(x, y int) {
n := g.n(x, y)
if g.grid1.G[x][y] { if g.grid1.G[x][y] {
n-- n--
switch n { switch n {
@ -136,6 +141,8 @@ func (g *GOL) check(x, y int) {
} else { } else {
if n == 3 { if n == 3 {
g.grid2.G[x][y] = true g.grid2.G[x][y] = true
} else {
g.grid2.G[x][y] = false
} }
} }
} }
@ -158,9 +165,57 @@ func (g *GOL) Calc() (gr *Grid) {
g.Gen++ g.Gen++
return g.grid() return g.grid()
} }
var text bool
func main() { func main() {
flag.BoolVar(&text, "text", false, "text mode")
flag.Parse()
gol := NewGOL() gol := NewGOL()
gc := make(chan *Grid, 10) gc := make(chan *Grid, 10)
if text {
go func() {
buf := make([]byte, (size*size)+size*6)
for {
buf[0] = '+'
c := 1
for ; c < size+1; c++ {
buf[c] = '-'
}
buf[c] = '+'
c++
buf[c] = '\n'
c++
grid := <-gc
for i := 0; i < size; i++ {
buf[c] = '|'
c++
for j := 0; j < size; j++ {
if grid.G[j][i] {
buf[c] = '#'
} else {
buf[c] = ' '
}
c++
}
buf[c] = '|'
c++
buf[c] = '\n'
c++
}
buf[c] = '+'
c++
for i := 0; i < size; i++ {
buf[c] = '-'
c++
}
buf[c] = '+'
c++
buf[c] = '\n'
fmt.Printf("\r%s", string(buf))
}
}()
} else {
ic := make(chan image.Image, 10) ic := make(chan image.Image, 10)
go func() { go func() {
o := jpeg.Options{100} o := jpeg.Options{100}
@ -205,10 +260,13 @@ func main() {
ic <- img ic <- img
} }
}() }()
}
for { for {
gc <- gol.Calc() gc <- gol.Calc()
if !text {
fmt.Fprintf(os.Stderr, "\rGeneration: %d", gol.Gen) fmt.Fprintf(os.Stderr, "\rGeneration: %d", gol.Gen)
}
time.Sleep(time.Second / gps) time.Sleep(time.Second / gps)
} }
} }