-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.go
85 lines (69 loc) · 1.52 KB
/
game.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
package main
import (
"fmt"
)
type Game struct {
deck Deck
player Player
dealer Player
Playing bool
}
func NewGame() Game {
return Game{
deck: NewDeck(),
player: Player{},
dealer: Player{},
Playing: false,
}
}
func (g *Game) Start() {
fmt.Println("Welcome to Go 21!")
dealerCard := g.dealer.DrawCard(g.deck)
fmt.Printf("Dealer draws a %s\n", dealerCard)
playerCardFirst := g.player.DrawCard(g.deck)
playerCardSecond := g.player.DrawCard(g.deck)
fmt.Printf("You draw a %s and a %s\n", playerCardFirst, playerCardSecond)
g.Playing = true
}
func (g *Game) Hit() {
card := g.player.DrawCard(g.deck)
fmt.Printf("You draw a %s\n", card)
if g.player.IsBust() {
fmt.Println("You BUST!")
g.Playing = false
}
}
func (g *Game) Stand() {
fmt.Println("You stand!")
g.dealerTurn()
g.Playing = false
}
func (g *Game) Hand() {
fmt.Println("Your hand:")
for _, card := range g.player.Hand {
fmt.Printf("- %s\n", card)
}
}
func (g *Game) Score() {
fmt.Printf("Your score: %d\n", g.player.GetScore())
}
func (g *Game) Exit() {
fmt.Println("Goodbye!")
g.Playing = false
}
func (g *Game) dealerTurn() {
fmt.Println("Dealer's turn!")
target := g.player.GetScore()
for g.dealer.GetScore() < target {
card := g.dealer.DrawCard(g.deck)
fmt.Printf("Dealer hits and gets a %s\n", card)
}
score := g.dealer.GetScore()
if score > 21 {
fmt.Printf("Dealer BUST! Score: %v\n", score)
fmt.Println("You win!")
} else {
fmt.Printf("Dealer stands with score: %v\n", score)
fmt.Println("You lose!")
}
}