sdl2-life

Conway's Game of Life with go-sdl2
git clone https://www.brianlane.com/git/sdl2-life
Log | Files | Refs | README

commit 9da919078554247fba0dcd1ff7640e5d525aa9e8
parent 1d58bf4f94cc457209db8618b369094fa4a927d1
Author: Brian C. Lane <bcl@brianlane.com>
Date:   Wed, 15 Apr 2020 06:05:25 -0700

Add parsing plaintext files

This will fall back to plaintext parsing if it cannot detect the type of
file, it assumes . is a dead cell and any other character is the live
cell.

Diffstat:
Mmain.go | 40+++++++++++++++++++++++++++++++++++++---
1 file changed, 37 insertions(+), 3 deletions(-)

diff --git a/main.go b/main.go @@ -144,8 +144,8 @@ func (g *LifeGame) InitializeCells() { g.cells, err = ParseLife105(scanner) } else if strings.HasPrefix(header, "#Life 1.06") { log.Fatal("Life 1.06 file format is not supported") - } else if strings.HasPrefix(header, "!Name:") { - g.cells, err = ParsePlaintext(scanner) + } else { + g.cells, err = ParsePlaintext(header, scanner) } if err != nil { @@ -274,9 +274,43 @@ func ParseLife105(scanner *bufio.Scanner) ([][]*Cell, error) { // ParsePlaintext pattern file // The header has already been read from the buffer when this is called -func ParsePlaintext(scanner *bufio.Scanner) ([][]*Cell, error) { +// This is a bit more generic than the spec, skip lines starting with ! +// and assume the pattern is . for dead cells any anything else for live. +func ParsePlaintext(name string, scanner *bufio.Scanner) ([][]*Cell, error) { cells := make([][]*Cell, cfg.Rows, cfg.Columns) + // Fill it with dead cells first + for y := 0; y < cfg.Rows; y++ { + for x := 0; x < cfg.Columns; x++ { + c := &Cell{x: x, y: y} + cells[y] = append(cells[y], c) + } + } + + var x, y int + + // Move x, y to center of field + x = cfg.Columns / 2 + y = cfg.Rows / 2 + + for scanner.Scan() { + line := scanner.Text() + if strings.HasPrefix(line, "!") { + continue + } else { + // Parse the line, . is dead, anything else is alive. + xLine := x + for _, c := range line { + if c != '.' { + cells[y][xLine].alive = true + cells[y][xLine].aliveNext = true + } + xLine = xLine + 1 + } + y = y + 1 + } + } + return cells, nil }