-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
100 lines (78 loc) · 1.73 KB
/
app.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
99
100
package main
import (
"context"
"math/rand"
)
// App struct
type App struct {
ctx context.Context
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
type CardSuit int
const (
Hearts CardSuit = 0
Diamonds CardSuit = 1
Clubs CardSuit = 2
Spades CardSuit = 3
)
var cardValueWords = [...]string{"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"}
var cardValueDisplayStrings = [...]string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}
func (a *App) CardValueToDisplayString(cv int) string {
if cv < 0 || cv > 13 {
return ""
}
return cardValueDisplayStrings[cv-1]
}
func (a *App) CardValueToWord(cv int) string {
if cv < 0 || cv > 13 {
return ""
}
return cardValueWords[cv-1]
}
type Card struct {
Value int
Suit CardSuit
IsRed bool
}
// Returns a random card
func (a *App) MakeRandomCard() Card {
// randInstance := rand.
thisVal := rand.Intn(12)
thisSuit := rand.Intn(4)
return Card{
Value: thisVal,
Suit: CardSuit(thisSuit),
IsRed: thisSuit < 2,
}
}
type Deck struct {
Cards []Card
}
// Returns a standard 52-card deck
func (a *App) NewStandardDeck() Deck {
// one of each value, in each suit
//n^2 moment
outDeck := Deck{}
outDeckContainer := []Card{}
// each value
for val := 1; val < 14; val++ {
// each suit
for suit := 0; suit < 4; suit++ {
outDeckContainer = append(outDeckContainer, Card{
Value: val,
Suit: CardSuit(suit),
IsRed: suit < 2,
})
}
}
outDeck.Cards = outDeckContainer
return outDeck
}