Orphanage

It works.
apart from winning.

it has...
- selections
- colors
- undo!
- errors

pretty cool huh
This commit is contained in:
hedy 2022-03-28 17:43:05 +08:00
commit 09e2937101
Signed by: hedy
GPG Key ID: B51B5A8D1B176372
5 changed files with 225 additions and 0 deletions

15
.gitignore vendored Normal file
View File

@ -0,0 +1,15 @@
# ---> Go
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, build with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
tictactoe

2
README.md Normal file
View File

@ -0,0 +1,2 @@
Simple tictactoe TUI implementation
TODO: do getWinnre()

8
go.mod Normal file
View File

@ -0,0 +1,8 @@
module tictactoe
go 1.16
require (
github.com/charmbracelet/bubbletea v0.20.0 // indirect
github.com/charmbracelet/lipgloss v0.5.0 // indirect
)

31
go.sum Normal file
View File

@ -0,0 +1,31 @@
github.com/charmbracelet/bubbletea v0.20.0 h1:/b8LEPgCbNr7WWZ2LuE/BV1/r4t5PyYJtDb+J3vpwxc=
github.com/charmbracelet/bubbletea v0.20.0/go.mod h1:zpkze1Rioo4rJELjRyGlm9T2YNou1Fm4LIJQSa5QMEM=
github.com/charmbracelet/lipgloss v0.5.0 h1:lulQHuVeodSgDez+3rGiuxlPVXSnhth442DATR2/8t8=
github.com/charmbracelet/lipgloss v0.5.0/go.mod h1:EZLha/HbzEt7cYqdFPovlqy5FZPj0xFhg5SaqxScmgs=
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34=
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68/go.mod h1:Xk+z4oIWdQqJzsxyjgl3P22oYZnHdZ8FFTHAQQt5BMQ=
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.11.1-0.20220204035834-5ac8409525e0/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739 h1:QANkGiGr39l1EESqrE0gZw0/AJNYzIvoGLhIoVYtluI=
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210422114643-f5beecf764ed h1:Ei4bQjjpYUsS4efOUz+5Nz++IVkHk87n2zBA0NxBWc0=
golang.org/x/term v0.0.0-20210422114643-f5beecf764ed/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=

169
main.go Normal file
View File

@ -0,0 +1,169 @@
package main
import (
"fmt"
"os"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var (
colorRed = lipgloss.NewStyle().Foreground(lipgloss.Color("#ff0000"))
colorGreen = lipgloss.NewStyle().Foreground(lipgloss.Color("#76c165"))
colorBlue = lipgloss.NewStyle().Foreground(lipgloss.Color("#1a73e8"))
colorLemon = lipgloss.NewStyle().Foreground(lipgloss.Color("#e7ef80"))
)
type model struct {
board [][]int // 2-D game board. 0 for nothing, 1 for o, 2 for x
turn int // who's turn is it
cursor []int // coordinates of cursor position in the board
msg string
moves int // number of occupied cells
movesHistory [][]int // list of coords
}
func initialModel() model {
board := make([][]int, 3)
for i := range board {
board[i] = make([]int, 3)
}
return model{
board: board,
turn: 1,
cursor: make([]int, 2),
msg: "",
}
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) getWinner() int {
return 0
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.msg = ""
switch msg := msg.(type) {
case tea.KeyMsg:
key := msg.String()
switch key {
case "q", "ctrl+c":
m.msg = "Bye!"
return m, tea.Quit
case "u":
if m.moves == 0 {
m.msg = "Nothing to undo"
return m, nil
}
last := m.movesHistory[m.moves-1]
m.board[last[0]][last[1]] = 0
m.cursor[0] = last[0]
m.cursor[1] = last[1]
m.movesHistory = m.movesHistory[:m.moves-1]
m.turn = 1 - m.turn + 2 // change 1 to 2, 2 to 1
m.moves--
m.msg = "Undone"
return m, nil
case "enter", " ":
if m.board[m.cursor[0]][m.cursor[1]] == 0 {
m.board[m.cursor[0]][m.cursor[1]] = m.turn
m.turn = 1 - m.turn + 2 // change 1 to 2, 2 to 1
m.moves++
m.movesHistory = append(m.movesHistory, []int{m.cursor[0], m.cursor[1]})
} else {
m.msg = "That cell is already occupied!"
}
case "up", "k":
if m.cursor[0] > 0 {
m.cursor[0]--
}
case "down", "j":
if m.cursor[0] < 2 {
m.cursor[0]++
}
case "left", "h":
if m.cursor[1] > 0 {
m.cursor[1]--
}
case "right", "l":
if m.cursor[1] < 2 {
m.cursor[1]++
}
}
}
winner := m.getWinner()
if winner != 0 {
if winner == 1 {
m.msg = "o wins!"
} else {
m.msg = "x wins!"
}
return m, tea.Quit
}
if m.moves == 9 {
m.msg = "It's a tie!"
m.turn = 1 - m.turn + 2 // show last turn
return m, tea.Quit
}
return m, nil
}
func (m model) View() (s string) {
s = "\n"
// s += fmt.Sprintf("%v", m.cursor)
indent := " "
for x, row := range m.board {
var v string
s += indent
for y, cell := range row {
switch cell {
case 0:
v = "_"
case 1:
v = colorGreen.Render("o")
case 2:
v = colorBlue.Render("x")
}
if x == m.cursor[0] && y == m.cursor[1] {
s += fmt.Sprintf(" [%s] ", v)
} else {
s += fmt.Sprintf(" %s ", v)
}
}
s += "\n\n"
}
if m.turn == 1 {
s += fmt.Sprintf("\n %s turn\n", colorGreen.Render("o"))
} else {
s += fmt.Sprintf("\n %s turn\n", colorBlue.Render("x"))
}
s += "\n " + colorLemon.Render(m.msg) + "\n\n"
s += "[enter/space] select; [hjkl/arrows] move around; [u]ndo; [q/ctrl-c] quit"
return
}
func main() {
p := tea.NewProgram(initialModel())
if err := p.Start(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err)
os.Exit(1)
}
}