-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (48 loc) · 978 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"fmt"
"os"
tea "github.com/charmbracelet/bubbletea"
"github.com/desertfox/go-2048/game"
)
type model struct {
game game.Game
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "up", "w":
m.game.ProcessAction(game.Action("up"))
case "down", "s":
m.game.ProcessAction(game.Action("down"))
case "left", "a":
m.game.ProcessAction(game.Action("left"))
case "right", "d":
m.game.ProcessAction(game.Action("right"))
case "ctrl+c", "q":
return m, tea.Quit
}
}
return m, nil
}
func (m model) View() string {
if m.game.State != "playing" {
return m.game.State
}
return m.game.BoardString()
}
func main() {
g := game.Game{}
g.Start()
p := tea.NewProgram(model{
game: g,
})
if _, err := p.Run(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err)
os.Exit(1)
}
}