-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
193 lines (169 loc) · 3.59 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"fmt"
"strings"
"github.com/masahiroyoshida/blackjack/deck"
)
type Hand []deck.Card
func (h Hand) String() string {
strs := make([]string, len(h))
for i := range h {
strs[i] = h[i].String()
}
return strings.Join(strs, ", ")
}
func (h Hand) DealerString() string {
return h[0].String() + ", **HIDDEN**"
}
func (h Hand) Score() int {
minScore := h.MinScore()
if minScore > 11 {
return minScore
}
for _, c := range h {
if c.Rank == deck.Ace {
return minScore + 10
}
}
return minScore
}
func (h Hand) MinScore() int {
score := 0
for _, c := range h {
score += min(int(c.Rank), 10)
}
return score
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func Shuffle(gs GameState) GameState {
ret := clone(gs)
ret.Deck = deck.New(deck.Deck(3), deck.Shuffle)
return ret
}
func Deal(gs GameState) GameState {
ret := clone(gs)
ret.Player = make(Hand, 0, 5)
ret.Dealer = make(Hand, 0, 5)
var card deck.Card
for i := 0; i < 2; i++ {
card, ret.Deck = draw(ret.Deck)
ret.Player = append(ret.Player, card)
card, ret.Deck = draw(ret.Deck)
ret.Dealer = append(ret.Dealer, card)
}
ret.State = StatePlayerTurn
return ret
}
func Hit(gs GameState) GameState {
ret := clone(gs)
hand := ret.CuurentPlayer()
var card deck.Card
card, ret.Deck = draw(ret.Deck)
*hand = append(*hand, card)
if hand.Score() > 21 {
return Stand(ret)
}
return ret
}
func Stand(gs GameState) GameState {
ret := clone(gs)
ret.State++
return ret
}
func EndHand(gs GameState) GameState {
ret := clone(gs)
pScore, dScore := gs.Player.Score(), gs.Dealer.Score()
fmt.Println("==FINAL HANDS==")
fmt.Println("Player:", gs.Player, "\nScore:", pScore)
fmt.Println("Dealer:", gs.Dealer, "\nScore:", dScore)
switch {
case pScore > 21:
fmt.Println("You busted")
case dScore > 21:
fmt.Println("Dealer busted")
case pScore > dScore:
fmt.Println("You win")
case dScore > pScore:
fmt.Println("You lose")
case pScore == dScore:
fmt.Println("Draw")
}
fmt.Println()
ret.Player = nil
ret.Dealer = nil
return ret
}
func main() {
var gs GameState
gs = Shuffle(gs)
for i := 0; i < 5; i++ {
gs = Deal(gs)
var input string
for gs.State == StatePlayerTurn {
fmt.Println("Player:", gs.Player, "\nScore:", gs.Player.Score())
fmt.Println("Dealer:", gs.Dealer.DealerString(), "\nScore:", gs.Dealer.Score())
fmt.Print("What will you do?(h)it, (s)tand: ")
fmt.Scanf("%s\n", &input)
switch input {
case "h":
gs = Hit(gs)
case "s":
gs = Stand(gs)
default:
fmt.Println("Invalid input. either \"h\" or \"s\"")
}
}
// If dealer score <= 16, we hit
// If dealer has a soft 17, then we hit
for gs.State == StateDealerTurn {
if gs.Dealer.Score() <= 16 || (gs.Dealer.Score() == 17 && gs.Dealer.MinScore() != 17) {
gs = Hit(gs)
} else {
gs = Stand(gs)
}
}
gs = EndHand(gs)
}
}
func draw(cards []deck.Card) (deck.Card, []deck.Card) {
return cards[0], cards[1:]
}
type State uint8
const (
StatePlayerTurn State = iota
StateDealerTurn
StateHandOver
)
type GameState struct {
Deck []deck.Card
State State
Player Hand
Dealer Hand
}
func (gs *GameState) CuurentPlayer() *Hand {
switch gs.State {
case StatePlayerTurn:
return &gs.Player
case StateDealerTurn:
return &gs.Dealer
default:
panic("it isn't currently any player's turn")
}
}
func clone(gs GameState) GameState {
ret := GameState{
Deck: make([]deck.Card, len(gs.Deck)),
State: gs.State,
Player: make(Hand, len(gs.Player)),
Dealer: make(Hand, len(gs.Dealer)),
}
copy(ret.Deck, gs.Deck)
copy(ret.Player, gs.Player)
copy(ret.Dealer, gs.Dealer)
return ret
}