Skip to content

Commit

Permalink
Added ability to play another game without restarting the app. Format…
Browse files Browse the repository at this point in the history
…ting stuff.
  • Loading branch information
Chris Christensen committed Jan 24, 2024
1 parent 47a1f84 commit eded1c9
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 114 deletions.
2 changes: 1 addition & 1 deletion cmd/gojackcli/card.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ func CardValue(c Card) int {

func IsAceCard(c Card) bool {
return c.Type == "Ace"
}
}
2 changes: 1 addition & 1 deletion cmd/gojackcli/deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ func Deal(d Deck, n int) ([]Card, Deck) {
deck = RemoveCard(d, i, n)
}
return cards, deck
}
}
2 changes: 1 addition & 1 deletion cmd/gojackcli/hand.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ func GetHands(cards []Card) ([]Card, []Card) {
}
}
return dealerHand, playerHand
}
}
2 changes: 1 addition & 1 deletion cmd/gojackcli/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ func ShowWelcome() {
fmt.Println("####### \u2665 \u2666 Gojack \u2663 \u2660 #######")
fmt.Println(strings.Repeat("#", 30) + "\n")
// fmt.Println("Welcome!\n")
}
}
121 changes: 70 additions & 51 deletions cmd/gojackcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,75 @@ import (
)

func main() {
deck := New()
Debug(deck)
Shuffle(deck)
Debug(deck)
cards, newDeck := Deal(deck, 4)
dealerHand, playerHand := GetHands(cards)
playerHandSlice := playerHand
dealerHandSlice := dealerHand
newDeckSlice := newDeck

ShowWelcome()
ShowWelcome()

fmt.Printf("Dealer Hand: %v\n", dealerHand[0])
fmt.Printf("Your Hand: %v (%v)\n", playerHand, GetTotal(playerHand))
play := true
game := true
for game {
deck := New()
Debug(deck)
Shuffle(deck)
Debug(deck)
cards, newDeck := Deal(deck, 4)
dealerHand, playerHand := GetHands(cards)
playerHandSlice := playerHand
dealerHandSlice := dealerHand
newDeckSlice := newDeck

if GetTotal(playerHand) == 21 && GetTotal(dealerHand) == 21 {
fmt.Println("BlackJack Push!")
return
} else if GetTotal(playerHand) == 21 {
fmt.Println("BlackJack! You win!")
return
} else if GetTotal(dealerHand) == 21 {
fmt.Println("Dealer BlackJack! You lose!")
return
}
fmt.Printf("Dealer Hand: %v\n", dealerHand[0])
fmt.Printf("Your Hand: %v (%v)\n", playerHand, GetTotal(playerHand))
play := true

for play {
fmt.Print("Do you want to hit or stay?\n")
if GetTotal(playerHand) == 21 && GetTotal(dealerHand) == 21 {
fmt.Println("BlackJack Push!")
return
} else if GetTotal(playerHand) == 21 {
fmt.Println("BlackJack! You win!")
return
} else if GetTotal(dealerHand) == 21 {
fmt.Println("Dealer BlackJack! You lose!")
return
}

for play {
fmt.Print("Do you want to hit or stay?\n")
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
fmt.Println("An error occurred while reading input. Please try again", err)
return
}
input = strings.TrimSuffix(input, "\n")

if strings.ToLower(input) == "hit" {
playerHandSlice, newDeckSlice = Hit(newDeckSlice, playerHandSlice)
fmt.Printf("Your Hand: %v (%v)\n", playerHandSlice, GetTotal(playerHandSlice))
if GetTotal(playerHandSlice) > 21 {
fmt.Printf("Dealer Hand: %v (%v)\n", dealerHandSlice, GetTotal(dealerHandSlice))
fmt.Println("Busted")
return
}
// fmt.Printf("Dealer Total: %d\nYour Total: %d\n", GetTotal(dealerHandSlice), GetTotal(playerHandSlice))
} else {
play = false
fmt.Printf("Dealer Hand: %v\n", dealerHandSlice)
}
}

dealerTotal := GetTotal(dealerHandSlice)
for dealerTotal < 17 {
fmt.Println("Dealer Hits!")
dealerHandSlice, newDeckSlice = Hit(newDeckSlice, dealerHandSlice)
fmt.Printf("Dealer Hand: %v (%v)\n", dealerHandSlice, GetTotal(dealerHandSlice))
dealerTotal = GetTotal(dealerHandSlice)
}

dealerTotal = GetTotal(dealerHandSlice)
playerTotal := GetTotal(playerHandSlice)
fmt.Printf("Dealer Total: %d\nYour Total: %d\n", dealerTotal, playerTotal)
result := GetWinner(dealerTotal, playerTotal)
fmt.Println(result)

fmt.Print("Play again? ")
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
Expand All @@ -45,33 +85,12 @@ func main() {
}
input = strings.TrimSuffix(input, "\n")

if strings.ToLower(input) == "hit" {
playerHandSlice, newDeckSlice = Hit(newDeckSlice, playerHandSlice)
fmt.Printf("Your Hand: %v (%v)\n", playerHandSlice, GetTotal(playerHandSlice))
if GetTotal(playerHandSlice) > 21 {
fmt.Printf("Dealer Hand: %v (%v)\n", dealerHandSlice, GetTotal(dealerHandSlice))
fmt.Println("Busted")
return
}
// fmt.Printf("Dealer Total: %d\nYour Total: %d\n", GetTotal(dealerHandSlice), GetTotal(playerHandSlice))
} else {
play = false
fmt.Printf("Dealer Hand: %v\n", dealerHandSlice)
if strings.ToLower(input) == "no" || strings.ToLower(input) == "n" {
game = false
fmt.Println("Thanks for playing!")
return
}
}

dealerTotal := GetTotal(dealerHandSlice)
for dealerTotal < 17 {
fmt.Println("Dealer Hits!")
dealerHandSlice, newDeckSlice = Hit(newDeckSlice, dealerHandSlice)
fmt.Printf("Dealer Hand: %v (%v)\n", dealerHandSlice, GetTotal(dealerHandSlice))
dealerTotal = GetTotal(dealerHandSlice)
}

dealerTotal = GetTotal(dealerHandSlice)
playerTotal := GetTotal(playerHandSlice)
fmt.Printf("Dealer Total: %d\nYour Total: %d\n", dealerTotal, playerTotal)
result := GetWinner(dealerTotal, playerTotal)
fmt.Println(result)

}
118 changes: 59 additions & 59 deletions cmd/gojackcli/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,75 @@ package main
import "testing"

func TestCardValue(t *testing.T) {
testCases := []struct {
card Card
expected int
}{
{Card{"Two", "Spades"}, 2},
{Card{"Ten", "Hearts"}, 10},
{Card{"Ace", "Clubs"}, 11},
}
testCases := []struct {
card Card
expected int
}{
{Card{"Two", "Spades"}, 2},
{Card{"Ten", "Hearts"}, 10},
{Card{"Ace", "Clubs"}, 11},
}

for _, tc := range testCases {
actual := CardValue(tc.card)
if actual != tc.expected {
t.Errorf("CardValue(%v) expected %d, actual %d", tc.card, tc.expected, actual)
}
}
for _, tc := range testCases {
actual := CardValue(tc.card)
if actual != tc.expected {
t.Errorf("CardValue(%v) expected %d, actual %d", tc.card, tc.expected, actual)
}
}
}

func TestGetTotal(t *testing.T) {
hand := Hand{
Card{"Ace", "Spades"},
Card{"King", "Hearts"},
}
expected := 21
actual := GetTotal(hand)
if actual != expected {
t.Errorf("GetTotal(%v) expected %d, actual %d", hand, expected, actual)
}
hand := Hand{
Card{"Ace", "Spades"},
Card{"King", "Hearts"},
}

expected := 21
actual := GetTotal(hand)

if actual != expected {
t.Errorf("GetTotal(%v) expected %d, actual %d", hand, expected, actual)
}
}

func TestGetHands(t *testing.T) {
cards := []Card{
{"Ace", "Spades"},
{"King", "Hearts"},
{"Queen", "Clubs"},
{"Jack", "Diamonds"},
}
expectedDealer := []Card{{"Ace", "Spades"}, {"Queen", "Clubs"}}
expectedPlayer := []Card{{"King", "Hearts"}, {"Jack", "Diamonds"}}
dealer, player := GetHands(cards)
if len(dealer) != len(expectedDealer) || len(player) != len(expectedPlayer) {
t.Errorf("GetHands(%v) returned incorrect hand sizes", cards)
}
for i := range dealer {
if dealer[i] != expectedDealer[i] {
t.Errorf("GetHands(%v) returned incorrect dealer hand", cards)
}
}
for i := range player {
if player[i] != expectedPlayer[i] {
t.Errorf("GetHands(%v) returned incorrect player hand", cards)
}
}
cards := []Card{
{"Ace", "Spades"},
{"King", "Hearts"},
{"Queen", "Clubs"},
{"Jack", "Diamonds"},
}

expectedDealer := []Card{{"Ace", "Spades"}, {"Queen", "Clubs"}}
expectedPlayer := []Card{{"King", "Hearts"}, {"Jack", "Diamonds"}}

dealer, player := GetHands(cards)

if len(dealer) != len(expectedDealer) || len(player) != len(expectedPlayer) {
t.Errorf("GetHands(%v) returned incorrect hand sizes", cards)
}

for i := range dealer {
if dealer[i] != expectedDealer[i] {
t.Errorf("GetHands(%v) returned incorrect dealer hand", cards)
}
}

for i := range player {
if player[i] != expectedPlayer[i] {
t.Errorf("GetHands(%v) returned incorrect player hand", cards)
}
}
}

func TestGetWinner(t *testing.T) {
dealerTotal := 20
playerTotal := 21
dealerTotal := 20
playerTotal := 21

expected := "You win!"
actual := GetWinner(dealerTotal, playerTotal)
expected := "You win!"
actual := GetWinner(dealerTotal, playerTotal)

if actual != expected {
t.Errorf("GetWinner(%d, %d) expected %s, actual %s", dealerTotal, playerTotal, expected, actual)
}
if actual != expected {
t.Errorf("GetWinner(%d, %d) expected %s, actual %s", dealerTotal, playerTotal, expected, actual)
}
}

0 comments on commit eded1c9

Please sign in to comment.