1
0
mirror of https://github.com/maaslalani/gambit.git synced 2024-12-26 20:54:07 +02:00
This commit is contained in:
Maas Lalani 2021-12-24 16:05:04 -05:00
parent 8abe072d85
commit c30e6351e5
No known key found for this signature in database
GPG Key ID: 5A6ED5CBF1A0A000
3 changed files with 59 additions and 53 deletions

View File

@ -1,45 +0,0 @@
package main
import (
"strings"
"github.com/maaslalani/gambit/board"
)
const (
cellHeight = 2
cellWidth = 4
marginLeft = 3
marginTop = 1
vertical = "│"
horizontal = "─"
)
// 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, board.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")
}

51
border/border.go Normal file
View File

@ -0,0 +1,51 @@
package border
import (
"strings"
"github.com/maaslalani/gambit/board"
)
const (
cellHeight = 2
cellWidth = 4
marginLeft = 3
marginTop = 1
Vertical = "│"
Horizontal = "─"
)
func Cell(x, y int) (int, int) {
col := (x - marginLeft) / cellWidth
row := board.LastRow - (y-marginTop)/cellHeight
return col, row
}
// withMarginLeft returns a string with a prepended left margin
func withMarginLeft(s string) string {
return strings.Repeat(" ", marginLeft) + s
}
// Build returns a string with a border for a given row (top, middle, bottom)
func Build(left, middle, right string) string {
border := left + Horizontal + strings.Repeat(Horizontal+Horizontal+middle+Horizontal, board.LastRow)
border += Horizontal + Horizontal + right + "\n"
return withMarginLeft(border)
}
func Top() string {
return Build("┌", "┬", "┐")
}
func Middle() string {
return Build("├", "┼", "┤")
}
func Bottom() string {
return Build("└", "┴", "┘")
}
func BottomLabels() string {
return withMarginLeft(" A B C D E F G H\n")
}

16
game.go
View File

@ -8,6 +8,7 @@ import (
"github.com/charmbracelet/lipgloss"
dt "github.com/dylhunn/dragontoothmg"
"github.com/maaslalani/gambit/board"
"github.com/maaslalani/gambit/border"
"github.com/maaslalani/gambit/pieces"
"github.com/maaslalani/gambit/position"
)
@ -39,14 +40,14 @@ func (m model) View() string {
for r, rank := range ranks {
if r == board.FirstRow {
s.WriteString(topBorder())
s.WriteString(border.Top())
}
count := 0
for c, cell := range rank {
if c == board.FirstCol {
label := fmt.Sprintf(" %d ", board.LastRow-r+1)
s.WriteString(Faint.Render(label) + vertical)
s.WriteString(Faint.Render(label) + border.Vertical)
}
if isNumeric(cell) {
@ -59,7 +60,7 @@ func (m model) View() string {
break
}
}
s.WriteString(display + vertical)
s.WriteString(display + border.Vertical)
count += 1
}
} else {
@ -75,16 +76,16 @@ func (m model) View() string {
}
}
}
s.WriteString(" " + style.Render(pieces.Display[string(cell)]) + " " + vertical)
s.WriteString(" " + style.Render(pieces.Display[string(cell)]) + " " + border.Vertical)
count += 1
}
}
s.WriteRune('\n')
if r != board.LastRow {
s.WriteString(middleBorder())
s.WriteString(border.Middle())
} else {
s.WriteString(bottomBorder() + Faint.Render(bottomLabels()))
s.WriteString(border.Bottom() + Faint.Render(border.BottomLabels()))
}
}
s.WriteRune('\n')
@ -99,8 +100,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil
}
col := (msg.X - marginLeft) / cellWidth
row := board.LastRow - (msg.Y-marginTop)/cellHeight
col, row := border.Cell(msg.X, msg.Y)
if m.selected != "" {
from := m.selected