1
0
mirror of https://github.com/maaslalani/gambit.git synced 2024-11-28 08:38:36 +02:00
This commit is contained in:
Maas Lalani 2021-12-24 14:13:21 -05:00
parent 9a9bdeb471
commit 68263dd257
No known key found for this signature in database
GPG Key ID: 5A6ED5CBF1A0A000
4 changed files with 89 additions and 14 deletions

44
border.go Normal file
View File

@ -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")
}

45
game.go
View File

@ -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')
}

2
go.mod
View File

@ -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

12
style.go Normal file
View File

@ -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"))
)