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
import (
"flag"
"fmt"
"image"
"image/color"
@ -114,8 +115,7 @@ func max(i int) int {
return i
}
func (g *GOL) check(x, y int) {
n := 0
func (g *GOL) n(x, y int) (n int) {
for i := min(x - 1); i <= max(x+1); i++ {
for j := min(y - 1); j <= max(y+1); 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] {
n--
switch n {
@ -136,6 +141,8 @@ func (g *GOL) check(x, y int) {
} else {
if n == 3 {
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++
return g.grid()
}
var text bool
func main() {
flag.BoolVar(&text, "text", false, "text mode")
flag.Parse()
gol := NewGOL()
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)
go func() {
o := jpeg.Options{100}
@ -205,10 +260,13 @@ func main() {
ic <- img
}
}()
}
for {
gc <- gol.Calc()
if !text {
fmt.Fprintf(os.Stderr, "\rGeneration: %d", gol.Gen)
}
time.Sleep(time.Second / gps)
}
}