Skip to content

Commit

Permalink
game starts automatically with first cell click
Browse files Browse the repository at this point in the history
  • Loading branch information
skaldesh committed May 10, 2022
1 parent 05c2b24 commit 27bdbca
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 34 deletions.
13 changes: 2 additions & 11 deletions bar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions grid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
9 changes: 2 additions & 7 deletions state.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

const (
defaultMines = 20
defaultMines = 40
defaultSecondsLeft = 180
defaultGridRows = 20
defaultGridCols = 15
Expand Down Expand Up @@ -64,8 +64,7 @@ func dispatch(a Action) {
type ActionType int

const (
startGame ActionType = iota
clickedCell
clickedCell ActionType = iota
resetGame
decrementTimer
)
Expand All @@ -75,10 +74,6 @@ type Action struct {
Data interface{}
}

func StartGame() {
dispatch(Action{Type: startGame})
}

type clickedCellData struct {
idx int
rightClick bool
Expand Down
21 changes: 7 additions & 14 deletions state_reducer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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]
Expand Down

0 comments on commit 27bdbca

Please sign in to comment.