-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
144 lines (115 loc) · 3.26 KB
/
main.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import "fmt"
import "math/rand"
import "time"
import "github.com/fatih/color"
import "os"
type Card struct {
color string
value int
}
type CardCollection struct {
cards []Card
}
type Player struct {
hand CardCollection
board CardCollection
}
func (p *Player) Play(i int) {
p.board.cards = append(p.board.cards, p.hand.cards[i])
p.hand.cards = append(p.hand.cards[:i], p.hand.cards[i+1:]...)
}
func (p *Player) Draw(deck *CardCollection) {
p.hand.cards = append(p.hand.cards, deck.cards[:1]...)
deck.cards = deck.cards[1:]
}
var Colors = []string{"White", "Red", "Blue", "Green", "Yellow"}
func main() {
println("Hello! Welcome to GoExplore.\n\n");
rand.Seed(time.Now().UTC().UnixNano())
var deck CardCollection
deck = InitializeDeck(deck)
deck = Shuffle(deck)
var p1 Player
var p2 Player
p1.hand = CardCollection{cards: deck.cards[:8]}
deck.cards = deck.cards[8:]
p2.hand = CardCollection{cards: deck.cards[:8]}
deck.cards = deck.cards[8:]
fmt.Printf("\nP1: ")
PrintCards(p1.hand)
fmt.Printf("\nP2: ")
PrintCards(p2.hand)
for {
//Simulate P1's first turn by playing a card at random
//Slice a card off of P1 and add it to the Board
fmt.Printf("\nPlayer 1 plays the 3rd card:")
p1.Play(3)
p1.Draw(&deck)
fmt.Printf("\nP1: ")
PrintCards(p1.hand)
PrintCards(p1.board)
//Ask for Player Input (P2) to play a card from P2's hand
fmt.Printf("\nPlayer 2's turn, enter a card index: ")
var cardIndex int
fmt.Scanf("%d", &cardIndex)
p2.Play(cardIndex)
p2.Draw(&deck)
fmt.Printf("\nP2: ")
PrintCards(p2.hand)
PrintCards(p2.board)
if len(deck.cards) == 0 {
fmt.Printf("Game over!")
os.Exit(2)
}
}
//TODO: Add game loop: do until...len(deck.cards) = 0
}
func InitializeDeck(deck CardCollection) (CardCollection) {
for _,color := range Colors {
for i := 1; i <= 10; i++ {
deck.cards = append(deck.cards, Card{color: color, value: i})
}
//Add betting cards as cards with a value of zero for now.
deck.cards = append(deck.cards, Card{color: color, value: 0})
deck.cards = append(deck.cards, Card{color: color, value: 0})
deck.cards = append(deck.cards, Card{color: color, value: 0})
}
return deck
}
func Shuffle(cardCol CardCollection) (CardCollection) {
for i := range cardCol.cards {
j := rand.Intn(i+1)
cardCol.cards[i], cardCol.cards[j] = cardCol.cards[j], cardCol.cards[i]
}
return cardCol
}
func PrintCards(deck CardCollection) {
//Print each card in the passed in collection.
for _,card := range deck.cards {
switch card.color {
case "Yellow":
color.Set(color.FgYellow, color.Bold)
case "Green":
color.Set(color.FgGreen, color.Bold)
case "Blue":
color.Set(color.FgBlue, color.Bold)
case "Red":
color.Set(color.FgRed, color.Bold)
case "White":
color.Set(color.FgWhite, color.Bold)
}
fmt.Printf("%d ", card.value)
color.Unset()
}
//Print the count of cards in the Deck
fmt.Printf("Card count: %d\n\n",len(deck.cards))
}
func TestStructs() {
// Write each color in the Colors collection.
for _,element := range Colors {
println (element);
}
newCard := Card{ color: "white", value: 1 }
println(newCard.color)
}