SHARE
TWEET

Blackjack

a guest Dec 16th, 2018 103 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2.  
  3. public class Blackjack {
  4.  
  5.     public static void main(String[] args) {
  6.     Scanner keyboard = new Scanner(System.in);
  7.         System.out.println("WELCOME TO BLACKJACK");
  8.         System.out.println();
  9.         System.out.println("Blackjack is a card game that is played against a dealer.");
  10.         System.out.println("Each card is assigned a value and the goal to is get as close to 21 without going over.");
  11.         System.out.println("You start with 2 cards facing up and the dealer has 1 card facing down and 1 up");
  12.         System.out.println("You have two options, hit or stay");
  13.         System.out.println("If you hit, you get another card which adds to your value.");
  14.         System.out.println("You can keep hitting until you bust or decide to stay.");
  15.         System.out.println("If you stay, you end you turn with your current value and wait for the dealer to go.");
  16.         System.out.println("If you have a higher value than the dealer without going over, you win.");
  17.         System.out.println("If you go over a value of 21 or if your cards have less value than the dealer's, you lose.");
  18.         System.out.println();
  19.         System.out.println();
  20.         Deck playingDeck=new Deck();
  21.         playingDeck.createFullDeck();
  22.         playingDeck.shuffle();
  23.        
  24.         Deck playerDeck = new Deck();
  25.         Deck dealerDeck = new Deck();
  26.        
  27.         char game='Y';
  28.         double totalDeposit=0;
  29.         double deposit=0;
  30.         double playerMoney=0;
  31.         double profit=0;
  32.                        
  33.         while (game=='y' || game=='Y') {
  34.            
  35.             System.out.println("You have a balance of $" + playerMoney + ". Enter how much you would like to deposit.");
  36.             deposit=keyboard.nextInt();
  37.             playerMoney=playerMoney+deposit;
  38.             totalDeposit=totalDeposit+deposit;
  39.            
  40.             if(playerMoney>0) {
  41.             System.out.println("How much would you like to bet? You currently have $" + playerMoney + ". ");
  42.             double playerBet = keyboard.nextDouble();
  43.            
  44.             boolean endRound=false;
  45.             playerDeck.draw(playingDeck);
  46.             playerDeck.draw(playingDeck);
  47.            
  48.             dealerDeck.draw(playingDeck);
  49.             dealerDeck.draw(playingDeck);
  50.            
  51.             while(true) {
  52.                 System.out.println();
  53.                 System.out.println("Your hand:" + playerDeck.toString());
  54.                 System.out.println("You hand currently has a value of: " + playerDeck.cardsValue());
  55.                 System.out.println();
  56.                 System.out.println("Dealer's hand: " + dealerDeck.getCard(0).toString() + " and 1 card faced down");
  57.                 System.out.println();
  58.                 System.out.println("Type 1 if you want to hit and 2 if you want to stay.");
  59.                 int response = keyboard.nextInt();
  60.                
  61.                 if(response ==1) {
  62.                     playerDeck.draw(playingDeck);
  63.                     System.out.println();
  64.                     System.out.println("You draw a: " + playerDeck.getCard(playerDeck.deckSize()-1).toString());
  65.                     System.out.println();
  66.                     if(playerDeck.cardsValue()>21) {
  67.                         System.out.println("Your hand:" + playerDeck.toString());
  68.                         System.out.println("Bust! Your cards are valued at: " + playerDeck.cardsValue());
  69.                         playerMoney-=playerBet;
  70.                         System.out.println();
  71.                         System.out.println("You lost your bet of $" + playerBet + " and now have a total of $" + playerMoney);
  72.                         System.out.println();
  73.                         endRound=true;
  74.                         break;
  75.                     }
  76.                 }
  77.                 if(response==2) {
  78.                     break;
  79.                 }
  80.             }
  81.             System.out.println("Dealer's cards: " + dealerDeck.toString());
  82.             if((dealerDeck.cardsValue()> playerDeck.cardsValue())&& endRound ==false) {
  83.                 System.out.println("Dealer wins!");
  84.                 playerMoney-= playerBet;
  85.                 System.out.println();
  86.                 System.out.println("You lost your bet of $" + playerBet + " and now have a total of $" + playerMoney);
  87.                 System.out.println();
  88.                 endRound=true;
  89.             }
  90.             while(dealerDeck.cardsValue()<17 && endRound==false) {
  91.                 dealerDeck.draw(playingDeck);
  92.                 System.out.println("Dealer draws: " + dealerDeck.getCard(dealerDeck.deckSize()-1).toString());
  93.             }
  94.             System.out.println("The dealer's hand is valued at: " + dealerDeck.cardsValue());
  95.             if(dealerDeck.cardsValue()>21&& endRound ==false) {
  96.                 System.out.println("Dealer busts! You win!");
  97.                 playerMoney+=playerBet;
  98.                 System.out.println();
  99.                 System.out.println("You won $" + playerBet + " and now have a total of $" + playerMoney);
  100.                 System.out.println();
  101.                 endRound=true;
  102.             }
  103.             if(playerDeck.cardsValue()==dealerDeck.cardsValue()&& endRound ==false) {
  104.                 System.out.println("Push");
  105.                 endRound=true;
  106.             }
  107.            
  108.            
  109.             if(playerDeck.cardsValue()> dealerDeck.cardsValue() && endRound == false) {
  110.                 System.out.println("You win!");
  111.                 playerMoney+=playerBet;
  112.                 System.out.println();
  113.                 System.out.println("You won $" + playerBet + " and now have a total of $" + playerMoney);
  114.                 System.out.println();
  115.                 endRound=true;
  116.             }
  117.             else if(endRound==false) {
  118.                 System.out.println("You lose!");
  119.                 playerMoney-=playerBet;
  120.                 System.out.println();
  121.                 System.out.println("You lost your bet of $" + playerBet + " and now have a total of $" + playerMoney);
  122.                 System.out.println();
  123.                 endRound=true;
  124.             }
  125.            
  126.             playerDeck.moveToDeck(playingDeck);
  127.             dealerDeck.moveToDeck(playingDeck);
  128.             System.out.println("End of Game");
  129.             System.out.println();
  130.             System.out.println("Would you like to continue playing? Y/N");
  131.             game=keyboard.next().charAt(0);
  132.         }
  133.         if(playerMoney<=0) {
  134.         System.out.println("You are out of money. Would you like to deposit more money and continue playing? Y/N");
  135.         game=keyboard.next().charAt(0);
  136.         }
  137.     }
  138.        
  139.     System.out.println("Thank you for playing Blackjack. You deposited a total of $" + totalDeposit + " and now have a total of $" + playerMoney);
  140.     profit=playerMoney-totalDeposit;
  141.    
  142.     if (profit>0) {
  143.         System.out.println("You have made a profit of $" + profit);
  144.     }
  145.    
  146.     else {
  147.         System.out.println("You have lost a total of " + profit + " dollars.");
  148.     }
  149. }
  150. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top