From 68263dd257bf9f9e2e373692901e93a1f6b6f8af Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Fri, 24 Dec 2021 14:13:21 -0500 Subject: [PATCH] tidy --- border.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ game.go | 45 ++++++++++++++++++++++++++++++++------------- go.mod | 2 +- style.go | 12 ++++++++++++ 4 files changed, 89 insertions(+), 14 deletions(-) create mode 100644 border.go create mode 100644 style.go diff --git a/border.go b/border.go new file mode 100644 index 0000000..07e9054 --- /dev/null +++ b/border.go @@ -0,0 +1,44 @@ +package main + +import "strings" + +const ( + firstCol = 0 + firstRow = 0 + + lastCol = 7 + lastRow = 7 + + vertical = "│" + horizontal = "─" +) + +const marginLeft = 3 + +// withMarginLeft returns a string with a prepended left margin +func withMarginLeft(s string) string { + return strings.Repeat(" ", marginLeft) + s +} + +// buildBorder returns a string with a border for a given row (top, middle, bottom) +func buildBorder(left, middle, right string) string { + border := left + horizontal + strings.Repeat(horizontal+horizontal+middle+horizontal, lastRow) + border += horizontal + horizontal + right + "\n" + return withMarginLeft(border) +} + +func topBorder() string { + return buildBorder("┌", "┬", "┐") +} + +func middleBorder() string { + return buildBorder("├", "┼", "┤") +} + +func bottomBorder() string { + return buildBorder("└", "┴", "┘") +} + +func bottomLabels() string { + return withMarginLeft(" A B C D E F G H\n") +} diff --git a/game.go b/game.go index b9b2d01..8ede604 100644 --- a/game.go +++ b/game.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "math/rand" "strings" tea "github.com/charmbracelet/bubbletea" @@ -23,27 +24,34 @@ func (m model) Init() tea.Cmd { return nil } func (m model) View() string { var s strings.Builder - ranks := strings.Split(strings.Split(m.board.ToFen(), " ")[0], "/") + + fen := m.board.ToFen() + board := strings.Split(fen, " ")[0] + ranks := strings.Split(board, "/") + for r, rank := range ranks { - if r == 0 { - s.WriteString(" ┌───┬───┬───┬───┬───┬───┬───┬───┐\n") + if r == firstRow { + s.WriteString(topBorder()) } + for c, cell := range rank { - if c == 0 { - s.WriteString(fmt.Sprintf(" %d │", r)) + if c == firstCol { + label := fmt.Sprintf(" %d ", lastRow-r+1) + s.WriteString(Faint.Render(label) + vertical) } - if cell >= '1' && cell <= '8' { - s.WriteString(strings.Repeat(" │", int(cell-'0'))) + + if isNumeric(cell) { + s.WriteString(strings.Repeat(" "+vertical, runeToInt(cell))) } else { - s.WriteString(" " + Display[string(cell)] + " │") + s.WriteString(" " + Display[string(cell)] + " " + vertical) } } s.WriteRune('\n') - if r == 7 { - s.WriteString(" └───┴───┴───┴───┴───┴───┴───┴───┘\n") - s.WriteString(" A B C D E F G H\n") + + if r != lastRow { + s.WriteString(middleBorder()) } else { - s.WriteString(" ├───┼───┼───┼───┼───┼───┼───┼───┤\n") + s.WriteString(bottomBorder() + Faint.Render(bottomLabels())) } } return s.String() @@ -55,7 +63,8 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case tea.KeyMsg: switch msg.String() { case " ": - move := m.board.GenerateLegalMoves()[0] + moves := m.board.GenerateLegalMoves() + move := moves[rand.Intn(len(moves))] m.board.Apply(move) case "ctrl+c", "q": return m, tea.Quit @@ -64,3 +73,13 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, nil } + +// isNumeric returns true if the current rune is a number +func isNumeric(r rune) bool { + return r >= '0' && r <= '9' +} + +// runeToInt converts a rune to an int +func runeToInt(r rune) int { + return int(r - '0') +} diff --git a/go.mod b/go.mod index 97670b6..1d0fea8 100644 --- a/go.mod +++ b/go.mod @@ -5,11 +5,11 @@ go 1.17 require ( github.com/charmbracelet/bubbletea v0.19.1 github.com/charmbracelet/lipgloss v0.4.0 + github.com/dylhunn/dragontoothmg v0.0.0-20170905201839-b0146de1e275 ) require ( github.com/containerd/console v1.0.2 // indirect - github.com/dylhunn/dragontoothmg v0.0.0-20170905201839-b0146de1e275 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/mattn/go-isatty v0.0.13 // indirect github.com/mattn/go-runewidth v0.0.13 // indirect diff --git a/style.go b/style.go new file mode 100644 index 0000000..6dd66c4 --- /dev/null +++ b/style.go @@ -0,0 +1,12 @@ +package main + +import ( + lg "github.com/charmbracelet/lipgloss" +) + +var ( + Faint = lg.NewStyle().Foreground(lg.Color("8")) + White = lg.NewStyle().Foreground(lg.Color("7")) + Black = lg.NewStyle().Foreground(lg.Color("4")) + Selected = lg.NewStyle().Foreground(lg.Color("2")) +)