-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (78 loc) · 1.77 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
package main
import (
"bufio"
"fmt"
"math/rand"
"os"
"strings"
"time"
)
var playerAmount int = 0
var dealerAmount int = 0
var playerDrawCount int = 0
var pick int = 0
var dealerPick int = 0
var finalCount = []int{playerAmount, dealerAmount}
var j int = 10
var q int = 10
var k int = 10
var cards = [12]int{2, 3, 4, 5, 6, 7, 8, 9, 10, j, q, k}
func main() {
dealer()
for {
if playerAmount < 21 {
reader := bufio.NewReader(os.Stdin)
fmt.Println("Hit or stand")
fmt.Print("-> ")
text, _ := reader.ReadString('\n')
text = strings.Replace(text, "\n", "", -1)
if strings.Compare("hit", text) == 0 {
randCard(0)
fmt.Println(playerAmount)
} else if strings.Compare("stand", text) == 0 {
if 21 >= finalCount[0] {
x := 21 - finalCount[0]
y := 21 - finalCount[1]
if x < y {
fmt.Println("You win")
os.Exit(0)
} else if y < x {
fmt.Println("The dealer wins")
os.Exit(0)
} else if y == x {
fmt.Println("Push")
os.Exit(0)
}
}
} else if playerDrawCount == 5 {
fmt.Println("You got a five card charlie! You Win!")
os.Exit(0)
}
} else if playerAmount > 21 {
fmt.Println("Bust!")
break
} else if playerAmount == 21 {
fmt.Println("Blackjack!")
break
}
}
}
func randCard(n int) int {
rand.Seed(time.Now().UnixNano())
in := cards
randomIndex := rand.Intn(len(in))
pick = in[randomIndex]
playerAmount = playerAmount + pick
finalCount[0] = playerAmount
playerDrawCount++
return 0
}
func randCardDealer(n int) int {
rand.Seed(time.Now().UnixNano())
in := cards
randomIndex := rand.Intn(len(in))
dealerPick = in[randomIndex]
dealerAmount = dealerAmount + dealerPick
finalCount[1] = dealerAmount
return 0
}