Sha256: 60f4e98e321f28a720bccaccc71f407e003b2ca6e81b95d066e06ca6311a9c6c

Contents?: true

Size: 1.54 KB

Versions: 137

Compression:

Stored size: 1.54 KB

Contents

package main

import (
	"log"
	"strings"
	"text/template"

	"../../../gen"
)

func main() {
	t := template.New("").Funcs(template.FuncMap{
		"pokerHands": PokerHands,
	})
	t, err := t.Parse(tmpl)
	if err != nil {
		log.Fatal(err)
	}
	var j js
	if err := gen.Gen("poker", &j, t); err != nil {
		log.Fatal(err)
	}
}

// The JSON structure we expect to be able to unmarshal into
type js struct {
	Cases []struct {
		Description string
		Input       []string
		Expected    []string
	}
}

var suitCharToSymbol = map[byte]string{'C': "♧", 'D': "♢", 'H': "♡", 'S': "♤"}

// PokerHands converts hands using C/D/H/S as the suit
// to one which uses the symbols ♧ / ♢ / ♡ / ♤ instead.
// For example, "2H 3C 4D 5S 9S" is converted to "2♡ 3♧ 4♢ 5♤ 9♤".
func PokerHands(inputHands []string) []string {
	hands := make([]string, len(inputHands))
	for i, inputHand := range inputHands {
		cards := strings.Fields(inputHand)
		handStr := ""
		for j, c := range cards {
			rankStr := c[0 : len(c)-1]
			handStr += rankStr + suitCharToSymbol[c[len(c)-1]]
			if j != len(cards)-1 {
				handStr += " "
			}
		}
		hands[i] = handStr
	}
	return hands
}

// template applied to above data structure generates the Go test cases
var tmpl = `package poker

{{.Header}}

type validTestCase struct {
	name  string
	hands []string
	best  []string
}

var validTestCases = []validTestCase {
{{range .J.Cases}}{
	name:  "{{.Description}}",
	hands: []string{
		{{range pokerHands .Input}} {{printf "%q" .}},
{{end}} },
	best:  {{pokerHands .Expected | printf "%#v"}},
},
{{end}}}
`

Version data entries

137 entries across 137 versions & 1 rubygems

Version Path
trackler-2.2.1.101 tracks/go/exercises/poker/.meta/gen.go
trackler-2.2.1.100 tracks/go/exercises/poker/.meta/gen.go
trackler-2.2.1.99 tracks/go/exercises/poker/.meta/gen.go
trackler-2.2.1.98 tracks/go/exercises/poker/.meta/gen.go
trackler-2.2.1.97 tracks/go/exercises/poker/.meta/gen.go
trackler-2.2.1.96 tracks/go/exercises/poker/.meta/gen.go
trackler-2.2.1.95 tracks/go/exercises/poker/.meta/gen.go
trackler-2.2.1.94 tracks/go/exercises/poker/.meta/gen.go
trackler-2.2.1.93 tracks/go/exercises/poker/.meta/gen.go
trackler-2.2.1.92 tracks/go/exercises/poker/.meta/gen.go
trackler-2.2.1.91 tracks/go/exercises/poker/.meta/gen.go
trackler-2.2.1.90 tracks/go/exercises/poker/.meta/gen.go
trackler-2.2.1.89 tracks/go/exercises/poker/.meta/gen.go
trackler-2.2.1.88 tracks/go/exercises/poker/.meta/gen.go
trackler-2.2.1.87 tracks/go/exercises/poker/.meta/gen.go
trackler-2.2.1.86 tracks/go/exercises/poker/.meta/gen.go
trackler-2.2.1.85 tracks/go/exercises/poker/.meta/gen.go
trackler-2.2.1.84 tracks/go/exercises/poker/.meta/gen.go
trackler-2.2.1.83 tracks/go/exercises/poker/.meta/gen.go
trackler-2.2.1.82 tracks/go/exercises/poker/.meta/gen.go