added reset

This commit is contained in:
2024-05-24 17:53:38 +02:00
parent 743a18c696
commit 757b48232f
5 changed files with 68 additions and 0 deletions

View File

@@ -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

35
game.go
View File

@@ -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

1
go.mod
View File

@@ -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

2
go.sum
View File

@@ -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=

21
grid.go
View File

@@ -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)