Playground
gnomod.toml
module = "gno.land/r/nym-moultest123/x/daily/life/v0" gno = "0.9" [addpkg] creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv" height = 186407 max_deposit = "7000000ugnot"
life.gno
// Conway's Game of Life on Gno — the entire board lives on-chain.
// Anyone can call Step() to tick the simulation forward, load classic
// patterns, or sculpt cells one by one.
package life
import (
"strconv"
"strings"
)
const (
MaxWidth = 50
MaxHeight = 40
)
var (
width = 20
height = 15
cells []bool
generation int
population int
)
func init() {
initBoard(width, height)
LoadPreset("glider")
}
func initBoard(w, h int) {
if w < 1 {
w = 1
}
if w > MaxWidth {
w = MaxWidth
}
if h < 1 {
h = 1
}
if h > MaxHeight {
h = MaxHeight
}
width = w
height = h
cells = make([]bool, width*height)
generation = 0
population = 0
}
func cellIdx(x, y int) int {
return y*width + x
}
func alive(x, y int) bool {
if x < 0 || x >= width || y < 0 || y >= height {
return false
}
return cells[cellIdx(x, y)]
}
func countNeighbors(x, y int) int {
n := 0
for dy := -1; dy <= 1; dy++ {
for dx := -1; dx <= 1; dx++ {
if dx == 0 && dy == 0 {
continue
}
if alive(x+dx, y+dy) {
n++
}
}
}
return n
}
// Step advances the simulation by exactly one generation.
func Step() {
next := make([]bool, width*height)
newPop := 0
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
n := countNeighbors(x, y)
var live bool
if alive(x, y) {
live = n == 2 || n == 3
} else {
live = n == 3
}
next[cellIdx(x, y)] = live
if live {
newPop++
}
}
}
cells = next
population = newPop
generation++
}
// MultiStep advances the simulation by n generations (capped at 100).
func MultiStep(n int) {
if n < 1 {
n = 1
}
if n > 100 {
n = 100
}
for i := 0; i < n; i++ {
Step()
}
}
// SetCell sets or clears a specific cell.
func SetCell(x, y int, live bool) {
if x < 0 || x >= width || y < 0 || y >= height {
return
}
i := cellIdx(x, y)
was := cells[i]
cells[i] = live
if live && !was {
population++
} else if !live && was {
population--
}
}
// Reset clears the board and resizes it to w×h.
func Reset(w, h int) {
initBoard(w, h)
}
// LoadPreset loads a classic Game of Life pattern onto the current board.
// Names: glider, blinker, toad, beacon, block, pulsar.
func LoadPreset(name string) {
for i := range cells {
cells[i] = false
}
generation = 0
population = 0
cx := width / 2
cy := height / 2
switch name {
case "glider":
// .O.
// ..O
// OOO
SetCell(1, 0, true)
SetCell(2, 1, true)
SetCell(0, 2, true)
SetCell(1, 2, true)
SetCell(2, 2, true)
case "blinker":
// OOO (period-2 oscillator)
SetCell(cx-1, cy, true)
SetCell(cx, cy, true)
SetCell(cx+1, cy, true)
case "toad":
// period-2 oscillator
SetCell(cx, cy-1, true)
SetCell(cx+1, cy-1, true)
SetCell(cx+2, cy-1, true)
SetCell(cx-1, cy, true)
SetCell(cx, cy, true)
SetCell(cx+1, cy, true)
case "beacon":
// period-2 oscillator
SetCell(cx-2, cy-2, true)
SetCell(cx-1, cy-2, true)
SetCell(cx-2, cy-1, true)
SetCell(cx, cy, true)
SetCell(cx+1, cy, true)
SetCell(cx, cy+1, true)
SetCell(cx+1, cy+1, true)
case "block":
// 2×2 still life
SetCell(cx, cy, true)
SetCell(cx+1, cy, true)
SetCell(cx, cy+1, true)
SetCell(cx+1, cy+1, true)
case "pulsar":
// period-3 oscillator (needs at least 17×17 board)
for _, d := range []int{-4, -3, -2, 2, 3, 4} {
SetCell(cx+d, cy-6, true)
SetCell(cx+d, cy-1, true)
SetCell(cx+d, cy+1, true)
SetCell(cx+d, cy+6, true)
SetCell(cx-6, cy+d, true)
SetCell(cx-1, cy+d, true)
SetCell(cx+1, cy+d, true)
SetCell(cx+6, cy+d, true)
}
default:
LoadPreset("glider")
}
}
// Render returns a Markdown snapshot of the current board.
func Render(path string) string {
out := "# Conway's Game of Life\n\n"
out += "> A classic cellular automaton, ported to Gno — board state lives on-chain.\n\n"
out += "**Generation:** " + strconv.Itoa(generation)
out += " | **Population:** " + strconv.Itoa(population)
out += " | **Board:** " + strconv.Itoa(width) + "×" + strconv.Itoa(height) + "\n\n"
border := "+" + strings.Repeat("-", width) + "+\n"
out += "```\n"
out += border
for y := 0; y < height; y++ {
row := "|"
for x := 0; x < width; x++ {
if alive(x, y) {
row += "O"
} else {
row += "."
}
}
row += "|\n"
out += row
}
out += border
out += "```\n\n"
out += "## Transactions\n\n"
out += "| Call | Effect |\n"
out += "|---|---|\n"
out += "| `Step()` | Advance one generation |\n"
out += "| `MultiStep(n)` | Advance *n* generations (max 100) |\n"
out += "| `SetCell(x, y, alive)` | Flip a single cell |\n"
out += "| `Reset(w, h)` | Clear and resize (max " + strconv.Itoa(MaxWidth) + "×" + strconv.Itoa(MaxHeight) + ") |\n"
out += "| `LoadPreset(name)` | `glider` · `blinker` · `toad` · `beacon` · `block` · `pulsar` |\n\n"
out += "## Rules\n\n"
out += "1. A live cell with 2 or 3 live neighbours survives.\n"
out += "2. A dead cell with exactly 3 live neighbours becomes alive.\n"
out += "3. All other cells die or remain dead.\n"
return out
}
life_test.gno
package life
import (
"testing"
)
func TestBlinker(t *testing.T) {
initBoard(7, 7)
// Horizontal blinker centred at (3,3)
SetCell(2, 3, true)
SetCell(3, 3, true)
SetCell(4, 3, true)
if population != 3 {
t.Errorf("initial population: want 3, got %d", population)
}
Step()
// Step 1: vertical at (3,2),(3,3),(3,4)
if !alive(3, 2) {
t.Error("want (3,2) alive after step 1")
}
if !alive(3, 3) {
t.Error("want (3,3) alive after step 1")
}
if !alive(3, 4) {
t.Error("want (3,4) alive after step 1")
}
if alive(2, 3) || alive(4, 3) {
t.Error("want (2,3) and (4,3) dead after step 1")
}
if population != 3 {
t.Errorf("population after step 1: want 3, got %d", population)
}
Step()
// Step 2: horizontal again
if !alive(2, 3) || !alive(3, 3) || !alive(4, 3) {
t.Error("want horizontal blinker after step 2")
}
if alive(3, 2) || alive(3, 4) {
t.Error("want (3,2) and (3,4) dead after step 2")
}
if generation != 2 {
t.Errorf("generation: want 2, got %d", generation)
}
}
func TestBlockStillLife(t *testing.T) {
initBoard(6, 6)
// 2×2 block at (2,2)
SetCell(2, 2, true)
SetCell(3, 2, true)
SetCell(2, 3, true)
SetCell(3, 3, true)
for i := 0; i < 5; i++ {
Step()
if population != 4 {
t.Errorf("step %d: block changed, population=%d", i+1, population)
}
if !alive(2, 2) || !alive(3, 2) || !alive(2, 3) || !alive(3, 3) {
t.Errorf("step %d: block cell died", i+1)
}
}
}
func TestGliderSurvives(t *testing.T) {
initBoard(20, 20)
LoadPreset("glider")
if population != 5 {
t.Errorf("glider initial population: want 5, got %d", population)
}
// A glider has period 4 and shifts (1,1) every 4 steps; it should
// survive many generations on a large-enough board.
for i := 0; i < 20; i++ {
Step()
}
if population != 5 {
t.Errorf("glider after 20 steps: want population 5, got %d", population)
}
if generation != 20 {
t.Errorf("generation: want 20, got %d", generation)
}
}
func TestSetCell(t *testing.T) {
initBoard(5, 5)
SetCell(2, 2, true)
if !alive(2, 2) {
t.Error("want (2,2) alive after SetCell true")
}
if population != 1 {
t.Errorf("population: want 1, got %d", population)
}
SetCell(2, 2, false)
if alive(2, 2) {
t.Error("want (2,2) dead after SetCell false")
}
if population != 0 {
t.Errorf("population: want 0, got %d", population)
}
// Out-of-bounds should be a no-op
SetCell(100, 100, true)
if population != 0 {
t.Error("out-of-bounds SetCell must not change population")
}
}
func TestReset(t *testing.T) {
initBoard(10, 10)
LoadPreset("block")
if population == 0 {
t.Error("want non-zero population after loading block preset")
}
Reset(5, 5)
if population != 0 {
t.Errorf("want 0 population after reset, got %d", population)
}
if generation != 0 {
t.Errorf("want 0 generation after reset, got %d", generation)
}
if width != 5 || height != 5 {
t.Errorf("want 5×5 board after reset, got %d×%d", width, height)
}
}
func TestCountNeighbors(t *testing.T) {
initBoard(5, 5)
// Live cells at (1,1),(2,1),(3,1) — blinker
SetCell(1, 1, true)
SetCell(2, 1, true)
SetCell(3, 1, true)
// Centre cell (2,1) has 2 live neighbours: (1,1) and (3,1)
if n := countNeighbors(2, 1); n != 2 {
t.Errorf("center of blinker: want 2 neighbours, got %d", n)
}
// Cell below center (2,2) has 3 live neighbours
if n := countNeighbors(2, 2); n != 3 {
t.Errorf("cell below center: want 3 neighbours, got %d", n)
}
}
func TestMultiStep(t *testing.T) {
initBoard(20, 20)
LoadPreset("glider")
MultiStep(4)
if generation != 4 {
t.Errorf("generation after MultiStep(4): want 4, got %d", generation)
}
if population != 5 {
t.Errorf("glider population after 4 steps: want 5, got %d", population)
}
}
render_test.gno
package life
import (
"strconv"
"strings"
"testing"
"gno.land/p/nt/uassert/v0"
)
// ExampleRender pins the whole page on a deliberately small board so the
// expected output stays readable. initBoard resets the grid, so this example
// is independent of the tests that run before it.
func ExampleRender() {
initBoard(5, 3)
SetCell(1, 1, true)
SetCell(2, 1, true)
SetCell(3, 1, true)
print(Render(""))
// Output:
// # Conway's Game of Life
//
// > A classic cellular automaton, ported to Gno — board state lives on-chain.
//
// **Generation:** 0 | **Population:** 3 | **Board:** 5×3
//
// ```
// +-----+
// |.....|
// |.OOO.|
// |.....|
// +-----+
// ```
//
// ## Transactions
//
// | Call | Effect |
// |---|---|
// | `Step()` | Advance one generation |
// | `MultiStep(n)` | Advance *n* generations (max 100) |
// | `SetCell(x, y, alive)` | Flip a single cell |
// | `Reset(w, h)` | Clear and resize (max 50×40) |
// | `LoadPreset(name)` | `glider` · `blinker` · `toad` · `beacon` · `block` · `pulsar` |
//
// ## Rules
//
// 1. A live cell with 2 or 3 live neighbours survives.
// 2. A dead cell with exactly 3 live neighbours becomes alive.
// 3. All other cells die or remain dead.
}
// TestRenderTracksGeneration checks the header counters follow the simulation.
func TestRenderTracksGeneration(t *testing.T) {
initBoard(5, 3)
SetCell(1, 1, true)
SetCell(2, 1, true)
SetCell(3, 1, true)
uassert.True(t, strings.Contains(Render(""), "**Generation:** 0 | **Population:** 3"),
"fresh board should report generation 0")
Step() // blinker rotates to vertical; on a 3-row board only the centre survives
out := Render("")
uassert.True(t, strings.Contains(out, "**Generation:** 1"), "generation should advance")
uassert.True(t, strings.Contains(out, "**Population:** "+strconv.Itoa(population)),
"rendered population should match the counter")
}
// TestRenderDrawsLiveCells checks the ASCII grid maps cells to O/. correctly.
func TestRenderDrawsLiveCells(t *testing.T) {
initBoard(3, 3)
SetCell(0, 0, true)
SetCell(2, 2, true)
out := Render("")
uassert.True(t, strings.Contains(out, "+---+\n|O..|\n|...|\n|..O|\n+---+"),
"corners should render as live cells")
}
// TestRenderBoardDimensions checks the border and header follow a resize.
func TestRenderBoardDimensions(t *testing.T) {
initBoard(8, 2)
out := Render("")
uassert.True(t, strings.Contains(out, "**Board:** 8×2"), "header should report the new size")
uassert.True(t, strings.Contains(out, "+"+strings.Repeat("-", 8)+"+"), "border should match width")
}
// TestRenderIgnoresPath documents that Render has no sub-routes.
func TestRenderIgnoresPath(t *testing.T) {
initBoard(4, 4)
uassert.Equal(t, Render(""), Render("/whatever"))
}