solitaire-tui/pkg/card.go

76 lines
1.4 KiB
Go
Raw Normal View History

2023-01-03 06:24:59 +01:00
package pkg
import (
"strings"
"github.com/charmbracelet/lipgloss"
)
var (
values = []string{"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}
suits = []string{"♠", "♦", "♥", "♣"}
)
const (
2023-01-17 02:13:36 +01:00
Width = 6
Height = 5
2023-01-03 06:24:59 +01:00
)
type Card struct {
Value int
Suit int
IsVisible bool
IsSelected bool
}
func NewCard(value, suit int) *Card {
return &Card{
Value: value,
Suit: suit,
}
}
func (c *Card) View() string {
2023-01-16 22:17:39 +01:00
color := "#000000"
if c.IsSelected {
color = "#FFFF00"
}
2023-01-03 06:24:59 +01:00
if !c.IsVisible {
2023-01-16 22:17:39 +01:00
return viewCard("", "", color)
2023-01-03 06:24:59 +01:00
}
style := lipgloss.NewStyle().Foreground(lipgloss.Color(c.Color()))
2023-01-16 22:17:39 +01:00
return viewCard(" ", style.Render(c.String()), color)
2023-01-03 06:24:59 +01:00
}
func (c *Card) Flip() {
c.IsVisible = !c.IsVisible
}
func (c *Card) Color() string {
if c.Suit == 1 || c.Suit == 2 {
return "#FF0000"
} else {
return "#000000"
}
}
func (c *Card) String() string {
return values[c.Value] + suits[c.Suit]
}
2023-01-16 22:17:39 +01:00
func viewCard(design, shorthand, color string) string {
style := lipgloss.NewStyle().Foreground(lipgloss.Color(color))
2023-01-17 02:13:36 +01:00
padding := strings.Repeat("─", Width-2-lipgloss.Width(shorthand))
2023-01-03 06:24:59 +01:00
view := style.Render("╭") + shorthand + style.Render(padding+"╮") + "\n"
2023-01-17 02:13:36 +01:00
for i := 1; i < Height-1; i++ {
view += style.Render("│"+strings.Repeat(design, Width-2)+"│") + "\n"
2023-01-03 06:24:59 +01:00
}
view += style.Render("╰"+padding) + shorthand + style.Render("╯")
return view
}