Skip to content

Commit

Permalink
GH-5: Working printGame() function wired up to postCmd.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesboehmer committed May 28, 2020
1 parent 58c2d8f commit 31507c3
Showing 1 changed file with 76 additions and 3 deletions.
79 changes: 76 additions & 3 deletions cmd/klondike/klondike.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,95 @@ package main
import (
"fmt"
"github.com/jamesboehmer/gopatience/internal/cmd"
"github.com/jamesboehmer/gopatience/pkg/cards"
"github.com/jamesboehmer/gopatience/pkg/cards/suit"
"github.com/jamesboehmer/gopatience/pkg/games/solitaire"
"strings"
)

type KlondikeCmd struct {
cmd.Cmd
klondike *solitaire.KlondikeGame
klondike *solitaire.KlondikeGame
lastError error
}

const (
StyleBright = "\033[1m"
StyleReset = "\033[0m"
ColorForeBlack = "\033[30m"
ColorForeRed = "\033[31m"
ColorBackRed = "\033[41m"
ClearScreen = "\033[H\033[J"
)

func (cmd *KlondikeCmd) printGame() {
fmt.Println("print game...")
paintSuit := func(s suit.Suit) string {
var color = ""
if s.Color() == suit.Red {
color = ColorForeRed
}
return fmt.Sprintf("%s%s%s%s", StyleBright, color, s, StyleReset)
}

max := func (x, y int) int {
if x > y {
return x
}
return y
}
paintCard := func(card cards.Card, leftPad int, rightPad int) string {
cardString := card.String()
if !card.Revealed {
cardString = "#"
}
length := len([]rune(cardString))
left := strings.Repeat(" ", max(leftPad-length, 0))
right := strings.Repeat(" ", max(rightPad-length-len(left), 0))

if !card.Revealed || card.Pip == "" {
return fmt.Sprintf("%s%s%s%s%s", left, StyleBright, cardString, StyleReset, right)
}
return fmt.Sprintf("%s%s%s%s%s%s", left, StyleBright, card.Pip, paintSuit(card.Suit), StyleReset, right)

}
buffer := strings.Builder{}
buffer.WriteString(ClearScreen)
var status string
if cmd.klondike.IsSolved() {
status = "Solved!"
} else if cmd.lastError != nil {
status = cmd.lastError.Error()
cmd.lastError = nil
} else {
status = ""
}
buffer.WriteString(fmt.Sprintf("%s%s%s%s%s\n", StyleBright, ColorBackRed, ColorForeBlack, status, StyleReset))

buffer.WriteString(fmt.Sprintf("Score: %d\n", cmd.klondike.Score))

buffer.WriteString(fmt.Sprintf("Stock: %d\n", cmd.klondike.Stock.Remaining()))

var paintedCards []string
for _, c := range cmd.klondike.Waste {
paintedCards = append(paintedCards, paintCard(c, 0, 0))
}
waste := fmt.Sprintf("[%s]", strings.Join(paintedCards, ", "))
buffer.WriteString(fmt.Sprintf("Waste: %s\n", waste))

//TODO: foundation
//TODO: tableau

buffer.WriteString(strings.Repeat("\n", 19))
fmt.Println(buffer.String())

}

func (cmd *KlondikeCmd) doQuit(_ string) (bool, error) {
return true, nil
}

func (cmd *KlondikeCmd) doDeal(_ string) (bool, error) {
cmd.klondike.Deal()
cmd.CommandPrompt = fmt.Sprintf("klondike[deal]> ")
return false, nil
}
Expand Down Expand Up @@ -67,6 +139,7 @@ func (cmd *KlondikeCmd) doTableau(_ string) (bool, error) {
func (cmd *KlondikeCmd) Init() *KlondikeCmd {
cmd.PostCmd = cmd.postCmd
cmd.LastCmd = ""
cmd.PreLoop = func () {cmd.printGame()}
cmd.CommandPrompt = "klondike> "
cmd.FunctionMap = map[string]func(string) (bool, error){
"d": cmd.doDeal,
Expand Down Expand Up @@ -94,7 +167,7 @@ func (cmd *KlondikeCmd) Init() *KlondikeCmd {
}

func (cmd *KlondikeCmd) postCmd(stop bool, line string) bool {
fmt.Println("KlondikeCmd.postCmd")
cmd.printGame()
return stop
}

Expand Down

0 comments on commit 31507c3

Please sign in to comment.