From 27bdbcabb25f4bbdb7cd2454ceb463b97dd1a9a1 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Tue, 10 May 2022 12:43:38 +0200 Subject: [PATCH] game starts automatically with first cell click --- bar.go | 13 ++----------- grid.go | 4 ++-- state.go | 9 ++------- state_reducer.go | 21 +++++++-------------- 4 files changed, 13 insertions(+), 34 deletions(-) diff --git a/bar.go b/bar.go index 0cbfcec..8f1c5f5 100644 --- a/bar.go +++ b/bar.go @@ -35,11 +35,7 @@ func NewBar(th *material.Theme) *Bar { func (b *Bar) Layout(gtx C) D { // Handle events. if b.restart.Clicked() { - if state.Running || state.Finished { - ResetGame() - } else { - StartGame() - } + ResetGame() } return layout.Stack{Alignment: layout.Center}.Layout(gtx, @@ -91,12 +87,7 @@ func (b *Bar) Layout(gtx C) D { }), layout.Rigid(func(gtx C) D { return layout.Center.Layout(gtx, func(gtx C) D { - t := "Start" - if state.Running || state.Finished { - t = "Reset" - } - - return material.Button(b.th, &b.restart, t).Layout(gtx) + return material.Button(b.th, &b.restart, "Reset").Layout(gtx) }) }), layout.Flexed(1, func(gtx C) D { diff --git a/grid.go b/grid.go index 3f387a2..292d627 100644 --- a/grid.go +++ b/grid.go @@ -40,8 +40,8 @@ func NewGrid(th *material.Theme) *Grid { } func (g *Grid) Layout(gtx C) D { - if !state.Running { - // Disable events if game is not running. + if state.Finished { + // Disable events if game is finished. gtx = gtx.Disabled() } diff --git a/state.go b/state.go index 0609df2..d44711c 100644 --- a/state.go +++ b/state.go @@ -6,7 +6,7 @@ import ( ) const ( - defaultMines = 20 + defaultMines = 40 defaultSecondsLeft = 180 defaultGridRows = 20 defaultGridCols = 15 @@ -64,8 +64,7 @@ func dispatch(a Action) { type ActionType int const ( - startGame ActionType = iota - clickedCell + clickedCell ActionType = iota resetGame decrementTimer ) @@ -75,10 +74,6 @@ type Action struct { Data interface{} } -func StartGame() { - dispatch(Action{Type: startGame}) -} - type clickedCellData struct { idx int rightClick bool diff --git a/state_reducer.go b/state_reducer.go index 70011d8..2e0c4fe 100644 --- a/state_reducer.go +++ b/state_reducer.go @@ -13,8 +13,6 @@ func reducer(a Action) { copy(s.Cells, state.Cells) switch a.Type { - case startGame: - s = startGameReducer(s) case clickedCell: s = clickedCellReducer(s, a.Data.(clickedCellData)) case resetGame: @@ -29,21 +27,16 @@ func reducer(a Action) { state = s } -func startGameReducer(s State) State { - if s.Running || s.Finished { +func clickedCellReducer(s State, data clickedCellData) State { + // Do nothing, if game is finished. + if s.Finished { return s } - s.Running = true - - atomic.StoreUint32(s.timerActive, 1) - return s -} - -func clickedCellReducer(s State, data clickedCellData) State { - // Do nothing, if game is not running. - if !s.Running || s.Finished { - return s + // If game is not yet running, start it. + if !s.Running { + s.Running = true + atomic.StoreUint32(s.timerActive, 1) } cell := s.Cells[data.idx]