~eliasnaur/gio-example

ref: 8a3a65c8c0340253beaba1c2e5687a000fa74789 gio-example/x/outlay/fan/playing/card.go -rw-r--r-- 1.3 KiB
8a3a65c8Chris Waldon gio-extras/*,x: rename gio-extras demos and change imports 2 months ago
                                                                                
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
Package playing provides types for modeling a deck of conventional
playing cards.
*/
package playing

type Suit uint8
type Rank uint8
type Color bool

const (
	Spades Suit = iota
	Clubs
	Hearts
	Diamonds
	UnknownSuit
)

const (
	Ace Rank = iota
	Two
	Three
	Four
	Five
	Six
	Seven
	Eight
	Nine
	Ten
	Jack
	Queen
	King
	UnknownRank
)

const (
	Red   Color = true
	Black Color = false
)

type Card struct {
	Suit
	Rank
}

func Deck() []Card {
	d := make([]Card, 0, 52)
	for i := 0; i < 4; i++ {
		for k := 0; k < 13; k++ {
			d = append(d, Card{
				Suit: Suit(i),
				Rank: Rank(k),
			})
		}
	}
	return d
}

func (r Rank) String() string {
	switch r {
	case Ace:
		return "A"
	case Two:
		return "2"
	case Three:
		return "3"
	case Four:
		return "4"
	case Five:
		return "5"
	case Six:
		return "6"
	case Seven:
		return "7"
	case Eight:
		return "8"
	case Nine:
		return "9"
	case Ten:
		return "10"
	case Jack:
		return "J"
	case Queen:
		return "Q"
	case King:
		return "K"
	default:
		return "?"
	}
}

func (s Suit) String() string {
	switch s {
	case Spades:
		return "♠"
	case Hearts:
		return "♥"
	case Diamonds:
		return "♦"
	case Clubs:
		return "♣"
	default:
		return "?"
	}
}

func (s Suit) Color() Color {
	switch s {
	case Spades, Clubs:
		return Black
	case Hearts, Diamonds:
		return Red
	default:
		return Black
	}
}