Project #3: Blackjack

Code

    /// Name: Kelsey Lieberman
    /// Period 5
    /// Program Name: Blackjack
    /// File Name: Blackjack.java
    /// Date Finished: 2/21/2016
    
import java.util.Scanner;
import java.util.Collections;
import java.util.Arrays;
import java.util.List;

public class Blackjack
{
    static Scanner keyboard = new Scanner(System.in);
    static int index = 0; /// integer variable that can be used to call upon a number within the array defined below.
    
    public static void main(String[] args) throws Exception
    {
        Integer[] cards = new Integer[52]; /// creates an array of 52 cards (assigns values in next for-loop). Avoids repeats of cards.
        
        String playerName, playAgain;
        double playerMoney = 500;
        
        System.out.println("\nWelcome to Kdawg's casino!!! Lets play blackjack!");
        System.out.print("What is your name? ");
        playerName = keyboard.nextLine();
        
        do /* do-while loop so player can play multiple times */
        {
            cards = new Integer[52];

            for (int i = 0; i < cards.length; i++)  ///assigns values to cards introduced above
                cards[i] = i + 2;
            
            Collections.shuffle(Arrays.asList(cards)); ///shuffles the deck; randomizes the order of the array
            
            int playerCard1 = nextCard(cards), playerCard2 = nextCard(cards); 
            int dealerCard1 = nextCard(cards), dealerCard2 = nextCard(cards);  
            int playerTotal = cardValue(playerCard1) + cardValue(playerCard2); 
            int dealerTotal = cardValue(dealerCard1) + cardValue(dealerCard2);
            int newCard;
            double bet;
            boolean blackjack1 = false, blackjack2 = false, betIsGood;
            String ans;
            /// these are the dealer and player's cards of values 1-11, their totals at the time the cards are dealt, and the player's bet.
            /// most other variables for the program are defined here.
            if (playerTotal == 22)
                playerTotal = 12;
            do{       
            System.out.print("You have $" + playerMoney + ", " + playerName + ". How much would you like to bet?  $");
            bet = keyboard.nextDouble();
                if (bet > playerMoney)
                {
                    System.out.println("You don't have that much money. Bet less.");
                    betIsGood = false;
                }
                else if (bet < 0)
                {
                    System.out.println("You can't bet a negative amount of money, try again");
                    betIsGood = false;
                }
                else
                {
                    betIsGood = true;
                    playerMoney = playerMoney - bet;
                }
                    
            }while (betIsGood == false);
            
            
            
            for (int x = 1; x < 4; x++)
            {
                System.out.print("\rThe dealer is dealing    ");
                Thread.sleep(250);
                System.out.print("\rThe dealer is dealing.   ");
                Thread.sleep(250);
                System.out.print("\rThe dealer is dealing..  ");
                Thread.sleep(250);
                System.out.print("\rThe dealer is dealing... ");
                Thread.sleep(250); /* adds suspense to the dealing of the cards and a pausing animation */
            } /// runs short animation 3 times ///
            
            System.out.println("\r                        ");
            System.out.println("You were dealt a " + cardTitle(playerCard1) + " and a " + cardTitle(playerCard2) + ".");
            System.out.println("[" + cardAbbreviation(playerCard1) + "] [" + cardAbbreviation(playerCard2) + "]");
            System.out.println("Your total is " + playerTotal);
            if (playerTotal == 21)
            {
                System.out.println("\nYou got blackjack! You win $" + (1.5*bet) + "!");
                playerMoney = playerMoney + bet + (1.5*bet);
            }
            else
            {
                System.out.println("The dealer has a " + cardTitle(dealerCard1) + " showing.");
                System.out.println("[[" + cardAbbreviation(dealerCard1) + "]");
                if (dealerTotal == 21)
                {
                    System.out.println("[" + cardAbbreviation(dealerCard2) + " [" + cardAbbreviation(dealerCard1) + "]");
                    System.out.println("\nThe dealer got blackjack! You lose!");
                }
                else
                {
                    System.out.println("His total is hidden.\n");

                    if (cardName(playerCard1) == cardName(playerCard2) && bet + bet <= playerMoney)
                    {
                        System.out.print ("Would you like to double down (yes/no)? ");
                        if ((keyboard.next()).equals("yes"))
                        {
                            int value = cardValue(playerCard1), playerTotal1, playerTotal2;
                            playerMoney = playerMoney - bet;

                            System.out.println("You split your cards... both of them are" + cardName(playerCard1) + "s.");

                            newCard = nextCard(cards);
                            playerTotal1 = value + cardValue(newCard);
                            System.out.print("For your first " + cardName(playerCard1) + ", you are also dealt a " + cardTitle(newCard) + ", for a total of " + playerTotal1 + ".");
                            if (playerTotal1 == 21)
                            {
                                System.out.println("\nYou got blackjack! You win $" + (1.5*bet) + "! dayumm!");
                                playerMoney = playerMoney + bet + (1.5*bet);
                                blackjack1 = true;
                            }
                            else
                            {
                                System.out.println("Would you like to \"hit\" or \"stay\"? ");
                                ans = keyboard.next();
                                Thread.sleep(500);

                                if (ans.equals("hit"))
                                {
                                    newCard = nextCard(cards);
                                    playerTotal1 = playerTotal1 + cardValue(newCard);
                                    System.out.println("You were dealt a " + cardTitle(newCard) + ".");
                                    System.out.println();
                                }

                                while (ans.equals("hit") && playerTotal1 < 22)
                                {
                                    System.out.print("Your total is " + playerTotal1 + ". Would you like to \"hit\" or \"stay\"? ");
                                    ans = keyboard.next();
                                    Thread.sleep(500);

                                    if (ans.equals("hit"))
                                    {
                                        newCard = nextCard(cards);
                                        playerTotal1 = cardValue(nextCard(cards)) + value;
                                        System.out.println("You were dealt a " + cardTitle(newCard) + ".");
                                        System.out.println();
                                    }
                                }

                                if (playerTotal1 > 21)
                                    System.out.println("\nAw man, your total is " + playerTotal1 + ", so you bust! Better luck with your other card.");
                                else if (blackjack1 == false)
                                    System.out.println("\nYour toal is " + playerTotal1 + ". Now for the next card.");
                            }
                            newCard = nextCard(cards);
                            playerTotal2 = value + cardValue(newCard);
                            System.out.println("For your second " + cardName(playerCard2) + ", you are also dealt a " + cardTitle(newCard) + ", for a total of " + playerTotal1 + ".");
                            if (playerTotal2 == 21)
                            {
                                System.out.println("\nYou got blackjack! You win $" + (1.5*bet) + "! dayumm!");
                                playerMoney = playerMoney + bet + (1.5*bet);
                                blackjack2 = true;
                            }
                            else
                            {
                                System.out.println("Would you like to \"hit\" or \"stay\"? ");
                                ans = keyboard.next();
                                Thread.sleep(500);

                                if (ans.equals("hit"))
                                {
                                    newCard = nextCard(cards);
                                    playerTotal2 = cardValue(nextCard(cards)) + value;
                                    System.out.println("You were dealt a " + cardTitle(newCard) + ".");
                                    System.out.println();
                                }

                                while (ans.equals("hit") && playerTotal2 < 22)
                                {
                                    System.out.print("Your total is " + playerTotal2 + ". Would you like to \"hit\" or \"stay\"? ");
                                    ans = keyboard.next();
                                    Thread.sleep(500);

                                    if (ans.equals("hit"))
                                    {
                                        newCard = nextCard(cards);
                                        playerTotal2 = playerTotal2 + cardValue(newCard);
                                        System.out.println("You were dealt a " + cardTitle(newCard) + ".");
                                        System.out.println();
                                    }
                                }

                                if (playerTotal2 > 21)
                                    System.out.println("\nAw man, your total is " + playerTotal2 + ", so you bust! Better luck with your other card.");
                                else if (blackjack2 == false)
                                    System.out.println("\nYour toal is " + playerTotal2 + ".");
                            } 
                            /// removed lines here.
                            if (playerTotal1 > 21 && playerTotal2 > 21)
                                System.out.println("\nYou busted on both your cards! You lose!");
                                
                            else if (blackjack1 == false || blackjack2 == false && (playerTotal1 < 21 || playerTotal2 < 21))
                            {
                                System.out.println("Now it is the dealer's turn.");

                                System.out.println("The dealer flips over his other card. He now has a " + cardTitle(dealerCard1) + " and a " + cardTitle(dealerCard2) + " showing.");

                                while (dealerTotal < 17 && (dealerTotal < playerTotal1 || dealerTotal < playerTotal2))
                                {
                                    newCard = nextCard(cards);
                                    dealerTotal = dealerTotal + cardValue(newCard);
                                    System.out.print("The dealer hits. ");
                                    Thread.sleep(500);
                                    System.out.println("He is dealt a " + cardTitle(newCard) + ".");
                                    System.out.println("The dealer's total is now " + dealerTotal + ".");
                                }
                                System.out.println("Your card totals are " + playerTotal2 + " and " + playerTotal2 + ", and the dealer's card total is " + dealerTotal + ".");
                                if (dealerTotal > 21)
                                {
                                    System.out.println("\nThe dealer busts! You win!");
                                    
                                    if (blackjack1 == false && blackjack2 == false)
                                        playerMoney = playerMoney + bet + bet + bet + bet;
                                    else if ((blackjack1 == false && blackjack2 == true) || (blackjack2 == false && blackjack1 == true))
                                        playerMoney = playerMoney + bet + bet;
                                }
                                else if (dealerTotal == playerTotal1 && dealerTotal == playerTotal2)
                                {
                                    System.out.println("\nYou and the dealer have the same card totals, so it is a push... nobody wins!");
                                    playerMoney = playerMoney + bet + bet;
                                }
                                else if ((dealerTotal == playerTotal1 && dealerTotal > playerTotal2) || (dealerTotal == playerTotal2 && dealerTotal > playerTotal1))
                                {
                                    System.out.println("\nYou and the dealer tie, or push on one hand. The dealer wins the other hand. Better luck next time!");
                                    playerMoney = playerMoney + bet;
                                }
                                else if ((dealerTotal == playerTotal1 && dealerTotal < playerTotal2) || (dealerTotal == playerTotal2 && dealerTotal < playerTotal1))
                                {
                                    System.out.println("\nYou and the dealer tie, or push on one hand. You win the other hand. Not bad!");
                                    playerMoney = playerMoney + bet + bet + bet;
                                }
                                else
                                {
                                    System.out.println("\nYou won both hands! What luck!");
                                    playerMoney = playerMoney + bet + bet + bet + bet;
                                }
                            }

                        }
                        else
                        {
                            System.out.println("Ok, then your total remains the same");
                            
                            do{
                                System.out.print("Your total is " + playerTotal + ". Would you like to \"hit\" or \"stay\"? ");
                                ans = keyboard.next();
                                Thread.sleep(500);
                                if (ans.equals("hit"))
                                {
                                    newCard = nextCard(cards);
                                    playerTotal = playerTotal + cardValue(newCard);
                                    System.out.println("You were dealt a " + cardTitle(newCard) + ".");
                                    System.out.println();
                                }
                            }while (ans.equals("hit") && playerTotal < 22);
                            
                            System.out.println("\nYour total is " + playerTotal);
                            if (playerTotal > 21)
                                System.out.println("\nSince your total is over 21, you bust! Better luck next time, loser.");
                            else 
                            {
                                System.out.println("Now it is the dealer's turn.\n");
                                System.out.println("The dealer flips over his other card. He now has a " + cardTitle(dealerCard1) + " and a " + cardTitle(dealerCard2) + " showing.");

                                while (dealerTotal < 17 && dealerTotal < playerTotal)
                                {
                                    newCard = nextCard(cards);
                                    dealerTotal = dealerTotal + cardValue(newCard);
                                    System.out.print("The dealer hits. ");
                                    Thread.sleep(500);
                                    System.out.println("He is dealt a " + cardTitle(newCard) + ".");
                                    System.out.println("The dealer's total is now " + dealerTotal + ".");
                                }
                                System.out.println("Your card total is " + playerTotal + ", and the dealer's card total is " + dealerTotal + ".");
                                if (dealerTotal > 21)
                                {
                                    System.out.println("\nThe dealer busts! You win!");
                                    playerMoney = playerMoney + bet + bet;
                                }
                                else if (playerTotal > dealerTotal)
                                {
                                    System.out.println("\nYou done won the game. Good job!");
                                    playerMoney = playerMoney + bet + bet;
                                }
                                else if (playerTotal == dealerTotal)
                                {
                                    System.out.println("\nYou and the dealer have the same card total, so you push. Nobody wins!");
                                    playerMoney = playerMoney + bet;
                                }
                                else if (playerTotal < dealerTotal)
                                {
                                    System.out.println("\nOooh you suck man, the dealer won this game. Better luck next time!");
                                }
                            }
                            
                        }
                    }
                    else
                    {
                        if (cardName(playerCard1) == cardName(playerCard2) && bet + bet > playerMoney)
                            System.out.println("I'd ask if you want to double down, but you don't have enough money for that :( .");
                        do{
                            System.out.print("Your total is " + playerTotal + ". Would you like to \"hit\" or \"stay\"? ");
                            ans = keyboard.next();
                            Thread.sleep(500);
                            if (ans.equals("hit"))
                            {
                                newCard = nextCard(cards);
                                playerTotal = playerTotal + cardValue(newCard);
                                System.out.println("You were dealt a " + cardTitle(newCard) + ".");
                                System.out.println();
                            }
                        }while (ans.equals("hit") && playerTotal < 22);
                            
                        System.out.println("Your total is " + playerTotal);
                        if (playerTotal > 21)
                            System.out.println("Since your total is over 21, you bust! Better luck next time, loser.");
                        else 
                        {
                            System.out.println("Now it is the dealer's turn.\n");
                            System.out.println("The dealer flips over his other card. He now has a " + cardTitle(dealerCard1) + " and a " + cardTitle(dealerCard2) + " showing.");

                            while (dealerTotal < 17 && dealerTotal < playerTotal)
                            {
                                newCard = nextCard(cards);
                                dealerTotal = dealerTotal + cardValue(newCard);
                                System.out.print("The dealer hits. ");
                                Thread.sleep(500);
                                System.out.println("He is dealt a " + cardTitle(newCard) + ".");
                                System.out.println("The dealer's total is now " + dealerTotal + ".");
                            }
                            System.out.println("Your card total is " + playerTotal + ", and the dealer's card total is " + dealerTotal + ".");
                            if (dealerTotal > 21)
                            {
                                System.out.println("\nThe dealer busts! You win!");
                                playerMoney = playerMoney + bet + bet;
                            }
                            else if (playerTotal > dealerTotal)
                            {
                                System.out.println("\nYou done won the game. Good job!");
                                playerMoney = playerMoney + bet + bet;
                            }
                            else if (playerTotal == dealerTotal)
                            {
                                System.out.println("\nYou and the dealer have the same card total, so you push. Nobody wins!");
                                playerMoney = playerMoney + bet;
                            }
                            else if (playerTotal < dealerTotal)
                            {
                                System.out.println("\nOooh you suck man, the dealer won this game. Better luck next time!");
                            }
                        }
                    }
                }
            }
            if (playerMoney == 0)
            {
                System.out.println("You can't play again because you have no money. bummer.");
                playAgain = "no";
            }
            else
            {
                System.out.print("\nDo you want to play again? ");
                playAgain = keyboard.next();
                System.out.println();
            }
            
        }while (playAgain.equalsIgnoreCase("yes"));
                                     
        System.out.println("\nThanks for playing, come again!");
    }
    
    public static String cardTitle(int card) /// returns the full name and suit of the card
    {
        String name = cardName(card);
        String suit = cardSuit(card);
        
        return (name + " of " + suit);
    }
    
    public static String cardAbbreviation(int card) /// abbreviates the card name; ie. "king" = "K"
    {
        String name = cardName(card), abbv;
        
        if (name.equals("Ace"))
            abbv = "A";
        else if (name.equals("King"))
            abbv = "K";
        else if (name.equals("Queen"))
            abbv = "Q";
        else if (name.equals("Jack"))
            abbv = "J";
        else
            abbv = cardName(card);
        
        return abbv;
    }
    
    public static String cardName(int card) /// returns the name of the card
    {
        String name, suit = cardSuit(card);
        
        if (suit.equals("hearts"))
        {
            if (card <= 8)
                name = String.valueOf(card + 2);
            else if (card == 9 )
                name = "Jack";
            else if (card == 10 )
                name = "Queen";
            else if (card == 11 )
                name = "King";
            else 
                name = "Ace";
        }
        else if (suit.equals("spades"))
        {
            card = card - 13;
            
            if (card <= 8)
                name = String.valueOf(card + 2);
            else if (card == 9 )
                name = "Jack";
            else if (card == 10 )
                name = "Queen";
            else if (card == 11 )
                name = "King";
            else 
                name = "Ace";
        }
        else if (suit.equals("diamonds"))
        {
            card = card - 26;
            
            if (card <= 8)
                name = String.valueOf(card + 2);
            else if (card == 9 )
                name = "Jack";
            else if (card == 10 )
                name = "Queen";
            else if (card == 11 )
                name = "King";
            else 
                name = "Ace";
        }
        else
        {
            card = card - 39;
            
            if (card <= 8)
                name = String.valueOf(card + 2);
            else if (card == 9 )
                name = "Jack";
            else if (card == 10 )
                name = "Queen";
            else if (card == 11 )
                name = "King";
            else 
                name = "Ace";
        }
        
        return name;
    }
    
    public static int cardValue(int card) /// determines the value of a card (1-11)
    {
        int value;
        String suit = cardSuit(card);
        
        if (suit.equals("hearts"))
        {
            if (card <= 8)
                value = card + 2;
            else if (card < 12)
                value = 10;
            else 
                value = 11;
        }
        else if (suit.equals("spades"))
        {
            card = card - 13;
            
            if (card <= 8)
                value = card + 2;
            else if (card < 12)
                value = 10;
            else 
                value = 11;
        }
        else if (suit.equals("diamonds"))
        {
            card = card - 26;
            
            if (card <= 8)
                value = card + 2;
            else if (card < 12)
                value = 10;
            else 
                value = 11;
        }
        else
        {
            card = card - 39;
            
            if (card <= 8)
                value = card + 2;
            else if (card < 12)
                value = 10;
            else 
                value = 11;
        }
        
        return value;
    }
    
    public static String cardSuit(int card) /// determines what suit a card is
    {
        if (card <= 12)
            return "hearts";
        else if (card <= 25)
            return "spades";
        else if (card <= 38)
            return "diamonds";
        else
            return "clubs";
    }
    
    public static int nextCard(Integer[] cards)
    {
        if (index == 51)
            index = 0;
        return  cards[index++];
    }

}
    

Picture of the output

Project 3 Project 3 Project 3