-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.go
98 lines (82 loc) · 2.77 KB
/
ui.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"fmt"
"strings"
"github.com/muesli/termenv"
)
// termenv for color styling
var p = termenv.ColorProfile()
// renderCardSymbol returns a string symbol representation of a card with color
func renderCardSymbol(card Card) string {
var suitSymbol string
var suitColor termenv.Color
switch card.Suit {
case "Hearts":
suitSymbol = "♥"
suitColor = p.Color("#FF0000") // Red color
case "Diamonds":
suitSymbol = "♦"
suitColor = p.Color("#FF0000") // Red color
case "Clubs":
suitSymbol = "♣"
suitColor = p.Color("#FFFFFF") // Black color
case "Spades":
suitSymbol = "♠"
suitColor = p.Color("#FFFFFF") // Black color
}
coloredSymbol := termenv.String(suitSymbol).Foreground(suitColor).String()
return fmt.Sprintf("%s%s", card.Rank, coloredSymbol)
}
// renderHandSymbol returns a string representation of a hand of cards using symbols
func renderHandSymbol(hand []Card) string {
var cards []string
for _, card := range hand {
cards = append(cards, renderCardSymbol(card))
}
return strings.Join(cards, " ")
}
// renderGameState returns a string representation of the current game state with a table-like layout
func renderGameState(gs gameState, width int) string {
var sb strings.Builder
sb.WriteString("\n🃏 Blackjack Table 🃏\n\n")
sb.WriteString("Dealer's Hand:\n")
if gs.GameOver {
sb.WriteString(" " + renderHandSymbol(gs.DealerHand) + "\n")
} else {
sb.WriteString(" [🂠] " + renderCardSymbol(gs.DealerHand[1]) + "\n") // Hidden card
}
sb.WriteString("\nYour Hand:\n")
sb.WriteString(" " + renderHandSymbol(gs.PlayerHand) + "\n")
playerScore, isSoftPlayer := gs.CalculateScore(gs.PlayerHand)
scoreText := fmt.Sprintf("%d", playerScore)
if isSoftPlayer {
scoreText = fmt.Sprintf("Soft %s", scoreText)
}
sb.WriteString(fmt.Sprintf("\nYour Score: %s\n", scoreText))
if gs.GameOver {
dealerScore, isSoftDealer := gs.CalculateScore(gs.DealerHand)
dealerScoreText := fmt.Sprintf("%d", dealerScore)
if isSoftDealer {
dealerScoreText = fmt.Sprintf("Soft %s", dealerScoreText)
}
sb.WriteString(fmt.Sprintf("Dealer's Score: %s\n", dealerScoreText))
sb.WriteString("\n" + gameOutcomeMessage(gs) + "\n")
} else if playerScore == 21 {
sb.WriteString("\n21! Press 'S'.\n")
} else {
sb.WriteString("\nPress 'H' to hit or 'S' to stand.\n")
}
return sb.String()
}
// gameOutcomeMessage returns a string message about the game outcome
func gameOutcomeMessage(gs gameState) string {
var sb strings.Builder
if gs.Tie {
sb.WriteString("🤝 It's a tie!\n\nPress 'R' to restart.")
} else if gs.PlayerWins {
sb.WriteString("🎉 You win!\n\nPress 'R' to restart.")
} else {
sb.WriteString("💔 Dealer wins!\n\nPress 'R' to restart.")
}
return sb.String()
}