-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDeck_test.go
116 lines (104 loc) · 2.27 KB
/
Deck_test.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"reflect"
"testing"
)
func TestDeckShouldHaveUniqueCards(t *testing.T) {
deck := InitializeDeck()
result := CheckDups(deck)
if result {
t.Error("There is a duplicate card.")
}
return
}
func TestDeckShouldBeShuffled(t *testing.T) {
deck := InitializeDeck()
if reflect.DeepEqual(deck[:13], Deck{
{1, "Clubs", "A"},
{2, "Clubs", "2"},
{3, "Clubs", "3"},
{4, "Clubs", "4"},
{5, "Clubs", "5"},
{6, "Clubs", "6"},
{7, "Clubs", "7"},
{8, "Clubs", "8"},
{9, "Clubs", "9"},
{10, "Clubs", "10"},
{11, "Clubs", "J"},
{12, "Clubs", "Q"},
{13, "Clubs", "K"},
}) {
t.Error("The deck is not shuffled correctly")
}
deck.Shuffle()
if reflect.DeepEqual(deck, Deck{
{1, "Clubs", "A"},
{2, "Clubs", "2"},
{3, "Clubs", "3"},
{4, "Clubs", "4"},
{5, "Clubs", "5"},
{6, "Clubs", "6"},
{7, "Clubs", "7"},
{8, "Clubs", "8"},
{9, "Clubs", "9"},
{10, "Clubs", "10"},
{11, "Clubs", "J"},
{12, "Clubs", "Q"},
{13, "Clubs", "K"},
}) {
t.Error("The deck is not shuffled correctly")
}
return
}
func TestDrawCards(t *testing.T) {
deck := InitializeDeck()
testHand := &Hand{}
testHand.DrawCard(&deck)
if len(*testHand) == 0 {
t.Error("Failed to draw a card.")
}
testHand.DrawCard(&deck)
if CheckDups(*testHand) {
t.Error("There are duplicates in the hand.")
}
if len(deck) != 50 {
t.Errorf("Deck does not have 50 cards left but %d", len(deck))
}
return
}
func TestDeckDealsTenCardsToPlayers(t *testing.T) {
deck := InitializeDeck()
p1 := &Player{"Michael", Hand{}}
p2 := &Player{"AI", Hand{}}
deck.Deal(p1, p2)
if len(p1.Hand) != 10 {
t.Error("Player one did not draw 10 cards!")
}
if len(p2.Hand) != 10 {
t.Error("Player two did not draw 10 card!")
}
if CheckDups(p1.Hand) || CheckDups(p2.Hand) {
t.Error("There are duplicate cards in the hands!")
}
if len(deck) != 32 {
t.Errorf("Deck does not have 32 cards left but %d", len(deck))
}
return
}
func TestDeckIsEmpty(t *testing.T) {
deck := Deck{}
if !deck.IsEmpty() {
t.Error("Deck should be empty but is not.")
}
}
// Checks for duplicate objects in an array that have Card types.
func CheckDups(arr []Card) bool {
dups := map[Card]bool{}
for _, card := range arr {
if dups[card] == true {
return true
}
dups[card] = true
}
return false
}