initial commit
This commit is contained in:
commit
a5408e152b
4 changed files with 246 additions and 0 deletions
210
main.go
Normal file
210
main.go
Normal file
|
|
@ -0,0 +1,210 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/jpeg"
|
||||
"math/rand"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
//size = 150
|
||||
size = 30
|
||||
zoom = 8
|
||||
fps = 2
|
||||
)
|
||||
|
||||
type Grid struct {
|
||||
G [size][size]bool
|
||||
}
|
||||
|
||||
func (g *Grid) Blinker() {
|
||||
g.G[14][13] = true
|
||||
g.G[14][14] = true
|
||||
g.G[14][15] = true
|
||||
}
|
||||
|
||||
func (g *Grid) Bipole() {
|
||||
g.G[14][14] = true
|
||||
g.G[14][15] = true
|
||||
g.G[15][14] = true
|
||||
g.G[16][17] = true
|
||||
g.G[17][16] = true
|
||||
g.G[17][17] = true
|
||||
}
|
||||
|
||||
func (g *Grid) Tripole() {
|
||||
g.G[14][14] = true
|
||||
g.G[14][15] = true
|
||||
g.G[15][14] = true
|
||||
g.G[16][15] = true
|
||||
g.G[16][17] = true
|
||||
g.G[17][18] = true
|
||||
g.G[18][17] = true
|
||||
g.G[18][18] = true
|
||||
}
|
||||
|
||||
func (g *Grid) Pulsator() {
|
||||
for i := 10; i < 20; i++ {
|
||||
g.G[i][14] = true
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Grid) Glider() {
|
||||
g.G[1][0] = true
|
||||
g.G[2][1] = true
|
||||
g.G[2][2] = true
|
||||
g.G[1][2] = true
|
||||
g.G[0][2] = true
|
||||
}
|
||||
|
||||
func (g *Grid) Random() {
|
||||
rand.Seed(size)
|
||||
for i := 0; i < size; i++ {
|
||||
for j := 0; j < size; j++ {
|
||||
g.G[i][j] = rand.Intn(2) == 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
func (g *Grid) Pentomino() {
|
||||
g.G[75][74] = true
|
||||
g.G[76][74] = true
|
||||
g.G[74][75] = true
|
||||
g.G[75][75] = true
|
||||
g.G[75][76] = true
|
||||
}
|
||||
*/
|
||||
|
||||
type GOL struct {
|
||||
grid1 *Grid
|
||||
grid2 *Grid
|
||||
Gen int
|
||||
}
|
||||
|
||||
func NewGOL() (g *GOL) {
|
||||
g = &GOL{grid1: new(Grid), grid2: new(Grid)}
|
||||
g.grid1.Blinker()
|
||||
//g.grid1.Bipole()
|
||||
//g.grid1.Tripole()
|
||||
//g.grid1.Pulsator()
|
||||
//g.grid1.Random()
|
||||
//g.grid1.Pentomino()
|
||||
return
|
||||
}
|
||||
|
||||
func min(i int) int {
|
||||
if i == -1 {
|
||||
return i + 1
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
func max(i int) int {
|
||||
if i == size {
|
||||
return i - 1
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
func (g *GOL) Check(x, y int) {
|
||||
n := 0
|
||||
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] {
|
||||
if !(i == x && j == y) {
|
||||
n++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if g.grid1.G[x][y] {
|
||||
switch n {
|
||||
case 0, 1:
|
||||
g.grid2.G[x][y] = false
|
||||
case 2, 3:
|
||||
g.grid2.G[x][y] = true
|
||||
default:
|
||||
g.grid2.G[x][y] = false
|
||||
}
|
||||
} else {
|
||||
if n == 3 {
|
||||
g.grid2.G[x][y] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GOL) Grid() (gr *Grid) {
|
||||
gr = new(Grid)
|
||||
for i := 0; i < size; i++ {
|
||||
gr.G[i] = g.grid1.G[i]
|
||||
}
|
||||
g.grid1, g.grid2 = g.grid2, new(Grid)
|
||||
return
|
||||
}
|
||||
|
||||
func main() {
|
||||
gol := NewGOL()
|
||||
gc := make(chan *Grid, 10)
|
||||
ic := make(chan image.Image, 10)
|
||||
go func() {
|
||||
o := jpeg.Options{100}
|
||||
img := <-ic
|
||||
for {
|
||||
select {
|
||||
case img = <-ic:
|
||||
default:
|
||||
}
|
||||
err := jpeg.Encode(os.Stdout, img, &o)
|
||||
if err == nil {
|
||||
os.Stdout.Sync()
|
||||
}
|
||||
time.Sleep(time.Millisecond * 16)
|
||||
}
|
||||
}()
|
||||
go func() {
|
||||
s := size * zoom
|
||||
r := image.Rect(0, 0, s, s)
|
||||
for {
|
||||
img := image.NewNRGBA(r)
|
||||
grid := <-gc
|
||||
x := 0
|
||||
y := 0
|
||||
for i := 0; i < size; i++ {
|
||||
for j := 0; j < size; j++ {
|
||||
var c color.Color
|
||||
if grid.G[i][j] {
|
||||
c = color.Black
|
||||
} else {
|
||||
c = color.White
|
||||
}
|
||||
for a := 0; a < zoom; a++ {
|
||||
for b := 0; b < zoom; b++ {
|
||||
img.Set(x+a, y+b, c)
|
||||
}
|
||||
}
|
||||
y += zoom
|
||||
}
|
||||
x += zoom
|
||||
y = 0
|
||||
}
|
||||
ic <- img
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
for i := 0; i < size; i++ {
|
||||
for j := 0; j < size; j++ {
|
||||
gol.Check(i, j)
|
||||
}
|
||||
}
|
||||
gc <- gol.Grid()
|
||||
gol.Gen++
|
||||
fmt.Fprintf(os.Stderr, "\rGeneration: %d", gol.Gen)
|
||||
time.Sleep(time.Second / fps)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue