diff --git a/cmd/gojackcli/card.go b/cmd/gojackcli/card.go index c9af8e5..8e458b3 100644 --- a/cmd/gojackcli/card.go +++ b/cmd/gojackcli/card.go @@ -35,4 +35,4 @@ func CardValue(c Card) int { func IsAceCard(c Card) bool { return c.Type == "Ace" -} \ No newline at end of file +} diff --git a/cmd/gojackcli/deck.go b/cmd/gojackcli/deck.go index efa567a..ecb39f9 100644 --- a/cmd/gojackcli/deck.go +++ b/cmd/gojackcli/deck.go @@ -52,4 +52,4 @@ func Deal(d Deck, n int) ([]Card, Deck) { deck = RemoveCard(d, i, n) } return cards, deck -} \ No newline at end of file +} diff --git a/cmd/gojackcli/hand.go b/cmd/gojackcli/hand.go index 7e86e19..57fc375 100644 --- a/cmd/gojackcli/hand.go +++ b/cmd/gojackcli/hand.go @@ -13,4 +13,4 @@ func GetHands(cards []Card) ([]Card, []Card) { } } return dealerHand, playerHand -} \ No newline at end of file +} diff --git a/cmd/gojackcli/helpers.go b/cmd/gojackcli/helpers.go index 4647cd6..9270fb6 100644 --- a/cmd/gojackcli/helpers.go +++ b/cmd/gojackcli/helpers.go @@ -67,4 +67,4 @@ func ShowWelcome() { fmt.Println("####### \u2665 \u2666 Gojack \u2663 \u2660 #######") fmt.Println(strings.Repeat("#", 30) + "\n") // fmt.Println("Welcome!\n") -} \ No newline at end of file +} diff --git a/cmd/gojackcli/main.go b/cmd/gojackcli/main.go index 31380e4..9b4f126 100644 --- a/cmd/gojackcli/main.go +++ b/cmd/gojackcli/main.go @@ -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 { @@ -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) - } diff --git a/cmd/gojackcli/main_test.go b/cmd/gojackcli/main_test.go index f3c61f4..125960c 100644 --- a/cmd/gojackcli/main_test.go +++ b/cmd/gojackcli/main_test.go @@ -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) + } }