import hsa.*; public class MyBlackJackProject { public static void main(String[] args) { Console con = new Console(); //Declare Variables int again = 0; do { int money = 1000; double range = 26; int randomGenNum; int yourBet; int userHandValue = 0, userDrawnValue; int dealerHandValue = 0, dealerDrawnValue; String playAgain; String hit = ""; String strBetAmount; String userCard, dealerCard; int[] arrayCardValues = {0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10}; String[] arrayCardSuites = { "","","Ace/Clubs","Ace/Diamonds","Ace/Hearts","Ace/Spades", "2/Clubs","2/Diamonds","2/Hearts","2/Spades","3/Clubs","3/Diamonds","3/Hearts","3/Spades", "4/Clubs","4/Diamonds","4/Hearts","4/Spades","5/Clubs","5/Diamonds","5/Hearts","5/Spades", "6/Clubs","6/Diamonds","6/Hearts","6/Spades","7/Clubs","7/Diamonds","7/Hearts","7/Spades", "8/Clubs","8/Diamonds","8/Hearts","8/Spades","9/Clubs","9/Diamonds","9/Hearts","9/Spades", "10/Clubs","10/Diamonds","10/Hearts","10/Spades","Jack/Clubs","Jack/Diamonds","Jack/Hearts","Jack/Spades" ,"Queen/Clubs","Queen/Diamonds","Queen/Hearts","Queen/Spades","King/Clubs","King/Diamonds","King/Hearts","King/Spades" }; //ask the user how much they want to bet con.println("You currently have $" + money + " to play with"); con.println("How much do you want to bet?"); strBetAmount = con.readLine(); yourBet = Integer.parseInt(strBetAmount); //dealers first card randomGenNum = (int)((range * Math.random()) + 1)*2; //assigning the dealer hand value & card, pulling index from array dealerHandValue = arrayCardValues[randomGenNum]; //Check if the dealer got an ace if ((randomGenNum >= 2) && (randomGenNum <= 5)) { dealerHandValue = dealerHandValue + 10; } int tempDealerHandValue = dealerHandValue; dealerCard = arrayCardSuites[randomGenNum]; //displaying values to the screen con.println("The dealer is showing a " + dealerCard); //dealers second card randomGenNum = (int)((range * Math.random()) + 1)*2; //assigning the dealer hand value & card, pulling index from array dealerHandValue = arrayCardValues[randomGenNum]; //Check if the dealer got an ace if ((dealerHandValue != 11) && (randomGenNum >= 2) && (randomGenNum <= 5)) { dealerHandValue = dealerHandValue + 10; } dealerCard = arrayCardSuites[randomGenNum]; //displaying values to the screen con.println("The dealers second card is " + dealerCard); dealerHandValue = tempDealerHandValue + dealerHandValue; con.println("The dealers total hand value is: " + (dealerHandValue)); con.println(""); //users first card randomGenNum = (int)((range * Math.random()) + 1)*2; //assigning the user hand value & card, pulling index from array userHandValue = arrayCardValues[randomGenNum]; //Check if the user got an ace if ((randomGenNum >= 2) && (randomGenNum <= 5)) { userHandValue = userHandValue + 10; } int tempUserHandValue = userHandValue; userCard = arrayCardSuites[randomGenNum]; //displaying values to the screen con.println("Your first card is " + userCard); //users second card randomGenNum = (int)((range * Math.random()) + 1)*2; //assigning the user hand value & card, pulling index from array userHandValue = arrayCardValues[randomGenNum]; //Check if the user has an ace if ((userHandValue != 11) && (randomGenNum >= 2) && (randomGenNum <= 5)) { userHandValue = userHandValue + 10; } userCard = arrayCardSuites[randomGenNum]; //displaying values to the screen con.println("Your second card is " + userCard); userHandValue=tempUserHandValue+userHandValue; con.println("Your total hand value is: " + (userHandValue)); con.println(""); //check for the either player having a blackjack(hand value = 21) if ((dealerHandValue == 21) && (userHandValue != 21)) { money=money-yourBet; con.println("Dealer has 21. You lose! You have $"+money+" left."); } if ((dealerHandValue != 21) && (userHandValue == 21)) { money=money+yourBet; con.println("Winner, Winner chicken dinner. You have 21. ! You have $"+money+" left."); } if ((dealerHandValue == 21) && (userHandValue == 21)) { con.println("You tied with the dealer. Its a push. You have $"+money+" left."); } if ((dealerHandValue != 21) && (userHandValue != 21)) { do { con.println("Would you like to hit? (Y/N)"); hit = con.readLine(); hit = hit.toUpperCase(); if (hit.compareTo("N") == 0 ) break; if (hit.compareTo("Y") == 0 ) { again = 0; //placeholder for giving user another card. randomGenNum = (int)((range * Math.random()) + 1)*2; //assigning the user hand value & card, pulling index from array userDrawnValue = arrayCardValues[randomGenNum]; userHandValue = userDrawnValue + userHandValue; userCard = arrayCardSuites[randomGenNum]; con.println("Your hit card is " + userCard); con.println("Your hand value is "+userHandValue+"."); } } while ((userHandValue < 21) && (again == 0)); con.println(""); //If the user exceeds 21 if (userHandValue > 21) { money=money-yourBet; con.println("You busted! You have $"+ money +" left."); } else again = 1; } //start the dealers loop do { if ((dealerHandValue <= 16) && (again != 0)) { again = 1; //placeholder for giving dealer another card. randomGenNum = (int)((range * Math.random()) + 1)*2; //assigning the dealer hand value & card, pulling index from array dealerDrawnValue = arrayCardValues[randomGenNum]; dealerHandValue = dealerDrawnValue+dealerHandValue; dealerCard = arrayCardSuites[randomGenNum]; con.println("The dealers hit card is " + dealerCard); con.println("The dealers hand value is " + dealerHandValue + "."); } } while (dealerHandValue <= 16); //If the dealer exceeds 21 if (dealerHandValue > 21) { money=money+yourBet; con.println("Dealer busted! You have $"+money+" left."); } //print final hand values con.println(""); con.println("Your final hand value is: " + userHandValue); con.println("The dealers final hand value is: " + dealerHandValue); //if noone breaks or has 21 compare the dealer hand to the user hand to see who wins. //If the dealer wins the hand. if ((dealerHandValue < 22) && (dealerHandValue > userHandValue)) { money=money-yourBet; con.println("The dealer beat you! You have $"+money+" left."); } //If the player wins the hand. if ((userHandValue < 22) && (dealerHandValue < userHandValue)) { money=money+yourBet; con.println("You beat the dealer, Congratulations!!! You have $"+money+" left."); } //If there is a tie. if (dealerHandValue == userHandValue) { con.println("You tied with the dealer. Its a push. You have $"+money+" left."); } //See if the user wants to play again. con.println("Would you like to play again? (Y/N)"); playAgain = con.readLine(); playAgain = playAgain.toUpperCase(); if (playAgain.compareTo("Y") == 0 ) again = 1; else again = 0; con.println(""); } while (again == 1); } }