Advertisement
jitteryjim

76 Project Blackjack

Oct 10th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.82 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Random;
  3.  
  4. public class Blackjack3
  5. {
  6.   public static void main( String[] args )
  7.   {
  8.     Random r = new Random();
  9.     Scanner inp = new Scanner(System.in);
  10.  
  11.     int p1a, p1b, p2a, p2b, bet, win;
  12.     String stayhit, pname;
  13.  
  14.     // Welcome the player
  15.  
  16.     System.out.println( "Hello! Welcome to the casino. I'll be your dealer, Gary." );
  17.     System.out.print( "What is your name? " );
  18.     pname = inp.next();
  19.     System.out.println( "Welcome, " + pname + ", let's play some cards." );
  20.     System.out.println();
  21.  
  22.     // Place bet
  23.  
  24.     System.out.print( "Ok, " + pname + ", place your bet. The minimum is $5. Bet $" );
  25.     bet = inp.nextInt();
  26.     if ( bet < 5 )
  27.     {
  28.       System.out.print( "Remember, the minimum bet is $5, " + pname + ", please bet again. Bet $" );
  29.       bet = inp.nextInt();
  30.     }
  31.     System.out.println();
  32.  
  33.     // Player Deal
  34.  
  35.     p1a = 2 + r.nextInt(11);
  36.     p1b = 2 + r.nextInt(11);
  37.     int sump1 = p1a + p1b;
  38.     System.out.println( "Ok, " + pname + ", you have a " + p1a + " and " + p1b + "." );
  39.     System.out.println( "Your total is " + sump1 + "." );
  40.     System.out.println();
  41.  
  42.     // Since cards can equal 22 on deal check for over 21
  43.  
  44.     if ( sump1 > 21 )
  45.     {
  46.       System.out.println( "Sorry! In this game you can draw some crazy cards. You busted, " + pname + ". Sorry you lose." );
  47.       System.exit(0);
  48.     }
  49.  
  50.     // Dealer Deal
  51.  
  52.     p2a = 2 + r.nextInt(11);
  53.     p2b = 2 + r.nextInt(11);
  54.     int sump2 = p2a + p2b;
  55.     System.out.println( "I have a " + p2b + " showing over my hole card." );
  56.     System.out.println();
  57.  
  58.     // Give player option to hit or stand
  59.  
  60.     System.out.print( "Do you want to \"hit\" or \"stay\"? " );
  61.     stayhit = inp.next();
  62.     System.out.println();
  63.  
  64.     while ( stayhit.equalsIgnoreCase("hit") )
  65.     {
  66.       int p1c = 2 + r.nextInt(11);
  67.       sump1 = sump1 + p1c;
  68.       if ( sump1 > 21 )
  69.       {
  70.         System.out.println( "You got a " + p1c + " and your total is " + sump1 + ". Sorry, " + pname + " you BUST. The house wins." );
  71.         System.exit(0);
  72.       }
  73.       else
  74.       {
  75.         System.out.println( "You got a " + p1c + " and your total is " + sump1 + "." );
  76.         System.out.print( "Do you want to \"hit\" or \"stay\"? " );
  77.         stayhit = inp.next();
  78.         System.out.println();
  79.       }
  80.     }
  81.  
  82.     // Determine if the dealer should hit
  83.  
  84.     System.out.println( "Ok, now it's my turn. I have a " + p2a + " and a " + p2b + " for a total of " + sump2 + "." );
  85.  
  86.     while ( sump2 <=16 )
  87.     {
  88.       int p2c = 2 + r.nextInt(11);
  89.       sump2 = sump2 + p2c;
  90.       if ( sump2 > 21 )
  91.       {
  92.         System.out.println( "And I drew a " + p2c + " and now have a total of " + sump2 + ". Looks like I bust; you WIN, " + pname + "!." );
  93.         win = bet * 2 ;
  94.         System.out.println("Here are your winnings, $" +win + "." );
  95.         System.exit(0);
  96.       }
  97.       else
  98.       {
  99.         System.out.println( "I drew a " + p2c + " and now have a total of " + sump2 + "." );
  100.         System.out.println();
  101.       }
  102.     }
  103.  
  104.     // Determine the outcome
  105.  
  106.     if (sump1 == sump2)
  107.     {
  108.       System.out.println( "It's a PUSH! No winners, no losers, you keep your $" + bet + "." );
  109.     }
  110.     else if (sump1 > sump2)
  111.  
  112.     // Winner, check for blackjack
  113.  
  114.     {
  115.       if ( sump1 == 21 )
  116.       {
  117.         win = bet * 3;
  118.         System.out.println( "Well done, " + pname + "! BLACKJACK! YOU WIN!!" );
  119.         System.out.println( "Here you go, $" + win + "." );
  120.       }
  121.  
  122.       // Winner, no blackjack
  123.  
  124.         else
  125.         {
  126.           win = bet * 2;
  127.           System.out.println( "Well done, " + pname + "! YOU WIN!!" );
  128.           System.out.println( "Here you go, $" + win + "." );
  129.       }
  130.     }
  131.     else
  132.     {
  133.       System.out.println ( "Sorry " + pname + ", you lose." );
  134.     }
  135.   }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement