-
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 8, 2024
1 parent
0ec491a
commit 41d04df
Showing
5 changed files
with
122 additions
and
95 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,25 @@ | ||
package main | ||
|
||
import "github.com/maxence-charriere/go-app/v9/pkg/app" | ||
|
||
type button struct { | ||
app.Compo | ||
|
||
id int64 | ||
} | ||
|
||
func NewButton(id int64) *button { | ||
return &button{ | ||
id: id, | ||
} | ||
} | ||
|
||
func (b *button) Render() app.UI { | ||
return app.Button(). | ||
Class("simon-button"). | ||
Body(app.Span().Text("")). | ||
ID("button%d", b.id). | ||
OnClick(func(ctx app.Context, _ app.Event) { | ||
ctx.NewActionWithValue(click, b.id) | ||
}) | ||
} |
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
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 |
---|---|---|
@@ -1,58 +1,117 @@ | ||
package main | ||
|
||
import ( | ||
"crypto/rand" | ||
"fmt" | ||
"math/big" | ||
"time" | ||
|
||
"github.com/maxence-charriere/go-app/v9/pkg/app" | ||
) | ||
|
||
type events = string | ||
|
||
const ( | ||
click events = "click" | ||
click events = "click" | ||
playSequence events = "playSequence" | ||
) | ||
|
||
func NewSimonSaysUI() *simonSaysUI { | ||
return &simonSaysUI{} | ||
func NewGame() *game { | ||
return &game{ | ||
buttonIds: []string{ | ||
"firstButton", | ||
"secondButton", | ||
"thirdButton", | ||
"fourthButton", | ||
}, | ||
sequence: GenerateSequence(4), | ||
} | ||
} | ||
|
||
type simonSaysUI struct { | ||
type game struct { | ||
app.Compo | ||
|
||
buttonIds []string | ||
sequence []int64 | ||
clicks int | ||
} | ||
|
||
func (h *game) OnMount(ctx app.Context) { | ||
ctx.Handle(playSequence, h.playSequence) | ||
} | ||
|
||
func (h *simonSaysUI) Render() app.UI { | ||
t := app.Div().Class("game-field") | ||
|
||
t.Body( | ||
app.Button(). | ||
Class("simon-button"). | ||
Body(app.Span().Text("")).OnClick(h.onClickFirst), | ||
app.Button(). | ||
Class("simon-button"). | ||
Body(app.Span().Text("")).OnClick(h.onClickSecond), | ||
app.Button(). | ||
Class("simon-button"). | ||
Body(app.Span().Text("")).OnClick(h.onClickThird), | ||
app.Button(). | ||
Class("simon-button"). | ||
Body(app.Span().Text("")).OnClick(h.onClickFourth), | ||
func (h *game) Render() app.UI { | ||
gameField := app.Div().Class("game-field") | ||
|
||
firstButton := NewButton(0) | ||
secondButton := NewButton(1) | ||
thirdButton := NewButton(2) | ||
fourthButton := NewButton(3) | ||
|
||
gameField.Body( | ||
firstButton, | ||
secondButton, | ||
thirdButton, | ||
fourthButton, | ||
) | ||
|
||
return app.Div().Class("fill", "background").Body( | ||
t, | ||
gameField, | ||
app.Button().Text("New Game").OnClick(h.handleNewGame), | ||
) | ||
} | ||
|
||
func (h *simonSaysUI) onClickFirst(ctx app.Context, e app.Event) { | ||
ctx.NewActionWithValue(click, int64(0)) | ||
func (h *game) playSequence(ctx app.Context, a app.Action) { | ||
sequence, ok := a.Value.([]int64) | ||
if !ok { | ||
fmt.Println("wrong type") | ||
return | ||
} | ||
fmt.Println("sequence:", sequence) | ||
for _, btnIndex := range sequence { | ||
btn := app.Window().GetElementByID(h.buttonIds[btnIndex]) | ||
fmt.Println(btn) | ||
ctx.After(time.Second, func(ctx app.Context) { | ||
}) | ||
} | ||
} | ||
|
||
func (h *simonSaysUI) onClickSecond(ctx app.Context, e app.Event) { | ||
ctx.NewActionWithValue(click, int64(1)) | ||
func (s *game) handleNewGame(ctx app.Context, a app.Event) { | ||
fmt.Println("New Game") | ||
} | ||
|
||
func (h *simonSaysUI) onClickThird(ctx app.Context, e app.Event) { | ||
ctx.NewActionWithValue(click, int64(2)) | ||
func (s *game) handleClick(_ app.Context, a app.Action) { | ||
click, ok := a.Value.(int64) | ||
if !ok { | ||
fmt.Println("wrong type") | ||
return | ||
} | ||
|
||
fmt.Println("received click:", click) | ||
if s.sequence[s.clicks] != click { | ||
fmt.Println("YOU LOSE!") | ||
s.clicks = 0 | ||
s.sequence = GenerateSequence(4) | ||
return | ||
} | ||
s.clicks++ | ||
if len(s.sequence) == s.clicks { | ||
fmt.Println("YOU WIN!") | ||
s.clicks = 0 | ||
s.sequence = GenerateSequence(4) | ||
} | ||
} | ||
|
||
func (h *simonSaysUI) onClickFourth(ctx app.Context, e app.Event) { | ||
ctx.NewActionWithValue(click, int64(3)) | ||
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()) | ||
|
||
} | ||
fmt.Println(seq) | ||
return seq | ||
} |