-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jan Steinke
committed
Feb 9, 2024
1 parent
f7a66b7
commit 8a95a98
Showing
3 changed files
with
142 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package main | ||
|
||
import ( | ||
"crypto/rand" | ||
"fmt" | ||
"math/big" | ||
"time" | ||
|
||
"github.com/maxence-charriere/go-app/v9/pkg/app" | ||
) | ||
|
||
func NewLogic() *logic { | ||
return &logic{ | ||
sequence: []int64{}, | ||
} | ||
} | ||
|
||
type logic struct { | ||
app.Compo | ||
|
||
sequence []int64 | ||
clicks int | ||
stage int | ||
state gameState | ||
} | ||
|
||
func (g *logic) simonSays(ctx app.Context, a app.Action) { | ||
sequence, ok := a.Value.([]int64) | ||
if !ok { | ||
fmt.Println("wrong type") | ||
return | ||
} | ||
fmt.Println("sequence:", sequence) | ||
|
||
go func() { | ||
// TODO: This is so weird. I somehow need to wait before I can do the next | ||
// action, otherwise it just won't update the DOM. | ||
<-time.After(200 * time.Millisecond) | ||
for _, btnIndex := range sequence { | ||
fmt.Println("sending", btnIndex) | ||
ctx.NewAction(fmt.Sprintf(playButton, btnIndex)) | ||
<-time.After(time.Second) | ||
} | ||
g.state = gameStatePlayerSays | ||
ctx.NewActionWithValue(stateChange, g.state) | ||
}() | ||
} | ||
|
||
func (g *logic) handleNewGame(ctx app.Context, a app.Action) { | ||
fmt.Println("New Game") | ||
g.clicks = 0 | ||
// TODO: allow setting difficulty | ||
g.sequence = GenerateSequence(4) | ||
g.stage = 1 | ||
g.state = gameStateSimonSays | ||
ctx.NewActionWithValue(stateChange, g.state) | ||
ctx.NewActionWithValue(simonSays, g.sequence[:1]) | ||
} | ||
|
||
func (g *logic) handleClick(ctx app.Context, a app.Action) { | ||
if g.state != gameStatePlayerSays { | ||
fmt.Println("no game") | ||
return | ||
} | ||
click, ok := a.Value.(int64) | ||
if !ok { | ||
fmt.Println("wrong type") | ||
return | ||
} | ||
|
||
fmt.Println("received click:", click) | ||
if g.sequence[g.clicks] != click { | ||
g.state = gameStateLost | ||
ctx.NewActionWithValue(stateChange, g.state) | ||
return | ||
} | ||
g.clicks++ | ||
if len(g.sequence) == g.clicks { | ||
g.state = gameStateWon | ||
ctx.NewActionWithValue(stateChange, g.state) | ||
return | ||
} | ||
if g.clicks == g.stage { | ||
g.clicks = 0 | ||
g.stage++ | ||
g.state = gameStateSimonSays | ||
ctx.NewActionWithValue(stateChange, g.state) | ||
ctx.After(1*time.Second, func(ctx app.Context) { | ||
ctx.NewActionWithValue(simonSays, g.sequence[:g.stage]) | ||
}) | ||
} | ||
} | ||
|
||
func GenerateSequence(l int) []int64 { | ||
seq := []int64{} | ||
for i := 0; i < l; i++ { | ||
n, err := rand.Int(rand.Reader, big.NewInt(4)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
seq = append(seq, n.Int64()) | ||
|
||
} | ||
return seq | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters