diff --git a/Makefile b/Makefile index b20eada..9511555 100644 --- a/Makefile +++ b/Makefile @@ -85,3 +85,12 @@ show-versions: buildlocal # lint: # golangci-lint run -p bugs -p unused + +buildwasm: + env GOOS=js GOARCH=wasm go build -o $(tool).wasm $(LDFLAGS) . + +zipwasm: + zip -r openquell-$(SHORTVERSION).zip index.html $(tool).wasm wasm_exec.js + +wasm: buildwasm zipwasm + @ls -l $(tool)-$(SHORTVERSION).zip diff --git a/game.go b/game.go index 105d195..25c7b28 100644 --- a/game.go +++ b/game.go @@ -116,6 +116,12 @@ func (game *Game) UpdateCells() { game.TicksElapsed = 0 } +func (game *Game) Reset() { + game.Paused = true + game.InitGrid(nil) + game.Paused = false +} + // check user input func (game *Game) CheckInput() { if inpututil.IsKeyJustPressed(ebiten.KeyQ) { @@ -152,6 +158,10 @@ func (game *Game) CheckInput() { game.SaveState() } + if inpututil.IsKeyJustPressed(ebiten.KeyR) { + game.Reset() + } + if game.Paused { if inpututil.IsKeyJustPressed(ebiten.KeyN) { game.RunOneStep = true @@ -349,6 +359,31 @@ func (game *Game) InitPattern() { } // initialize playing field/grid +func (game *Game) _InitGrid(grid *Grid) { + if grid != nil { + // use pre-loaded grid + game.Grids = []*Grid{ + grid, + NewGrid(grid.Width, grid.Height), + } + + game.History = NewGrid(grid.Width, grid.Height) + + return + } + + grida := NewGrid(game.Width, game.Height) + + grida.FillRandom(game) + + game.Grids = []*Grid{ + grida, + NewGrid(grida.Width, grida.Height), + } + + game.History = grida.Clone() +} + func (game *Game) InitGrid(grid *Grid) { if grid != nil { // use pre-loaded grid diff --git a/go.mod b/go.mod index 246e0d8..d93f07f 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( ) require ( + github.com/alecthomas/repr v0.4.0 // indirect github.com/ebitengine/gomobile v0.0.0-20240518074828-e86332849895 // indirect github.com/ebitengine/hideconsole v1.0.0 // indirect github.com/ebitengine/purego v0.7.0 // indirect diff --git a/go.sum b/go.sum index 3b0eded..0fa22ed 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc= +github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4= github.com/ebitengine/gomobile v0.0.0-20240518074828-e86332849895 h1:48bCqKTuD7Z0UovDfvpCn7wZ0GUZ+yosIteNDthn3FU= github.com/ebitengine/gomobile v0.0.0-20240518074828-e86332849895/go.mod h1:XZdLv05c5hOZm3fM2NlJ92FyEZjnslcMcNRrhxs8+8M= github.com/ebitengine/hideconsole v1.0.0 h1:5J4U0kXF+pv/DhiXt5/lTz0eO5ogJ1iXb8Yj1yReDqE= diff --git a/grid.go b/grid.go index e1c7797..5694653 100644 --- a/grid.go +++ b/grid.go @@ -4,6 +4,7 @@ import ( "bufio" "errors" "fmt" + "math/rand" "os" "strconv" "strings" @@ -40,6 +41,26 @@ func (grid *Grid) Clone() *Grid { return newgrid } +func (grid *Grid) Clear() { + for y := range grid.Data { + for x := range grid.Data[y] { + grid.Data[y][x] = 0 + } + } +} + +func (grid *Grid) FillRandom(game *Game) { + if !game.Empty { + for y := range grid.Data { + for x := range grid.Data[y] { + if rand.Intn(game.Density) == 1 { + grid.Data[y][x] = 1 + } + } + } + } +} + func GetFilename(generations int64) string { now := time.Now() return fmt.Sprintf("dump-%s-%d.gol", now.Format("20060102150405"), generations)