-
Notifications
You must be signed in to change notification settings - Fork 0
/
blackjack.go
149 lines (134 loc) · 3.87 KB
/
blackjack.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
package main
import (
"fmt"
"strings"
"math/rand"
"strconv"
"time"
)
const (
dealerThreshold = 16 //Determines when the dealer will stick
bustThreshold = 21 //Determines when the player will bust
randomThreshold = 9 //Determines max values for random number generation
)
//Global Variable Declarations
var playerName string
var anotherHand string
var playerScore int
var dealerScore int
var userWonHands int
var dealerWonHands int
//Random number generator
func newCardGenerator() (int){
randomSeed := rand.NewSource(time.Now().UnixNano())
seededRandomGen := rand.New(randomSeed)
return (seededRandomGen.Intn(randomThreshold) + 1)
}
//Game Methods
func determineHandWinner(computerScore int, userScore int) (bool){
//returns true if the player wins
if (computerScore < userScore){
if (userScore < 22){
return true
} else if (computerScore == userScore){
//tie dealer wins
return false
} else {
//user was bust
return false
}
} else if (computerScore > userScore){
if (dealerScore < 22){
return false
} else {
return true
}
}
//If there is an error, dealer wins
return false
}
func dealerTurn() {
for dealerRequestCard := true; dealerRequestCard; dealerRequestCard = (dealerScore < dealerThreshold) {
currentCard := newCardGenerator()
dealerScore += currentCard
fmt.Print("The dealer has "+strconv.Itoa(dealerScore)+"\n")
}
}
func playerTurn(){
var twist = "t"
for userRequestCard := true; userRequestCard; userRequestCard = (!strings.EqualFold(twist, "s")) {
currentCard := newCardGenerator()
playerScore += currentCard
if (playerScore > 21){
fmt.Print(playerName+" you've gone bust with "+ strconv.Itoa(playerScore)+"\n")
twist = "s"
}else{
fmt.Print("You have "+strconv.Itoa(playerScore)+" would you like to (s)tick or (t)wist\n")
fmt.Scan(&twist)
}
}
}
func playGame() {
playerTurn()
dealerTurn()
if(determineHandWinner(dealerScore, playerScore)){
userWonHands += 1
fmt.Print("Congratulations "+playerName+" you won the hand with "+strconv.Itoa(playerScore)+" vs the dealer's "+strconv.Itoa(dealerScore)+"\n")
} else {
dealerWonHands +=1
fmt.Print("Unlucky "+playerName+" the dealer won the hand with "+strconv.Itoa(dealerScore)+" you had "+strconv.Itoa(playerScore)+"\n")
}
fmt.Print("The scores are: \n"+playerName+" "+strconv.Itoa(userWonHands)+" wins\n"+" Dealer "+strconv.Itoa(dealerWonHands)+" wins\n")
continueGame()
}
func exitGame() {
var playerQuit string
fmt.Print("Are you sure you would like to exit? (Y/N)\n")
fmt.Scan(&playerQuit)
if strings.EqualFold(playerQuit,"Y") {
fmt.Print("Bye, thanks for playing\n")
}else if strings.EqualFold(playerQuit,"N"){
playBlackjack()
} else {
fmt.Print("Sorry, we didn't recognise that, please enter 'Y' or 'N'\n")
exitGame()
}
}
func continueGame() {
fmt.Print("Hello "+playerName+" are you ready to play Blackjack? (Y/N):\n ")
fmt.Scan(&anotherHand)
if strings.EqualFold(anotherHand,"Y") {
playerScore = 0
dealerScore = 0
playGame()
} else if strings.EqualFold(anotherHand,"N"){
exitGame()
} else {
fmt.Print("Sorry, we didn't recognise that, please enter 'Y' or 'N'\n")
continueGame()
}
}
func showRules(){
var userRuleChoice string
fmt.Print(playerName + " Would you like to view the rules? (Y/N)\n")
fmt.Scan(&userRuleChoice)
if strings.EqualFold(userRuleChoice,"Y"){
fmt.Printf("LMGTFY\n")
showRules()
}else if strings.EqualFold(userRuleChoice,"N"){
fmt.Print("Ok, let's begin "+playerName+"\n")
continueGame()
}else{
fmt.Print("Sorry, we didn't recognise that, please enter 'Y' or 'N'\n")
showRules()
}
}
func playBlackjack() {
showRules()
}
//Main Execution
func main() {
fmt.Printf("Please enter your name:\n")
fmt.Scan(&playerName)
playBlackjack()
}