-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.go
55 lines (46 loc) · 1.47 KB
/
draw.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
package deckofcards
import (
"fmt"
"strings"
jsoniter "github.com/json-iterator/go"
)
//UnmarshalDraw unmarshals a byte array into a pointer to a Draw for internal use.
func UnmarshalDraw(data []byte) (*Draw, error) {
var r *Draw
json := jsoniter.ConfigCompatibleWithStandardLibrary
err := json.Unmarshal(data, &r)
return r, err
}
//Marshal marshals a pointer to a Draw into a byte array for transmission.
func (z *Draw) Marshal() ([]byte, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(z)
}
//Draw is a type that implements the structure of a Draw.
type Draw struct {
Success bool `json:"success"`
Cards Cards `json:"cards"`
Remaining int `json:"remaining"`
}
//String function serializes the Draw struct into a representable string output.
func (z *Draw) String() string {
var printString []string
printString = append(printString, fmt.Sprintf("Success: %t", z.Success))
printString = append(printString, fmt.Sprintf("Remaining: %d", z.Remaining))
printString = append(printString, z.Cards.String())
return strings.Join(printString, "\n")
}
//AreAllCardsInThisDraw returns true if all the cards in the cards slice are in the draw.
func (z *Draw) AreAllCardsInThisDraw(cards Cards) bool {
for _, cardFromCards := range cards {
for d, cardFromDraw := range z.Cards {
if cardFromCards.Equals(cardFromDraw) {
break
}
if d == len(z.Cards)-1 && !cardFromCards.Equals(cardFromDraw) {
return false
}
}
}
return true
}