Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Random;
- public class Blackjack3
- {
- public static void main( String[] args )
- {
- Random r = new Random();
- Scanner inp = new Scanner(System.in);
- int p1a, p1b, p2a, p2b, bet, win;
- String stayhit, pname;
- // Welcome the player
- System.out.println( "Hello! Welcome to the casino. I'll be your dealer, Gary." );
- System.out.print( "What is your name? " );
- pname = inp.next();
- System.out.println( "Welcome, " + pname + ", let's play some cards." );
- System.out.println();
- // Place bet
- System.out.print( "Ok, " + pname + ", place your bet. The minimum is $5. Bet $" );
- bet = inp.nextInt();
- if ( bet < 5 )
- {
- System.out.print( "Remember, the minimum bet is $5, " + pname + ", please bet again. Bet $" );
- bet = inp.nextInt();
- }
- System.out.println();
- // Player Deal
- p1a = 2 + r.nextInt(11);
- p1b = 2 + r.nextInt(11);
- int sump1 = p1a + p1b;
- System.out.println( "Ok, " + pname + ", you have a " + p1a + " and " + p1b + "." );
- System.out.println( "Your total is " + sump1 + "." );
- System.out.println();
- // Since cards can equal 22 on deal check for over 21
- if ( sump1 > 21 )
- {
- System.out.println( "Sorry! In this game you can draw some crazy cards. You busted, " + pname + ". Sorry you lose." );
- System.exit(0);
- }
- // Dealer Deal
- p2a = 2 + r.nextInt(11);
- p2b = 2 + r.nextInt(11);
- int sump2 = p2a + p2b;
- System.out.println( "I have a " + p2b + " showing over my hole card." );
- System.out.println();
- // Give player option to hit or stand
- System.out.print( "Do you want to \"hit\" or \"stay\"? " );
- stayhit = inp.next();
- System.out.println();
- while ( stayhit.equalsIgnoreCase("hit") )
- {
- int p1c = 2 + r.nextInt(11);
- sump1 = sump1 + p1c;
- if ( sump1 > 21 )
- {
- System.out.println( "You got a " + p1c + " and your total is " + sump1 + ". Sorry, " + pname + " you BUST. The house wins." );
- System.exit(0);
- }
- else
- {
- System.out.println( "You got a " + p1c + " and your total is " + sump1 + "." );
- System.out.print( "Do you want to \"hit\" or \"stay\"? " );
- stayhit = inp.next();
- System.out.println();
- }
- }
- // Determine if the dealer should hit
- System.out.println( "Ok, now it's my turn. I have a " + p2a + " and a " + p2b + " for a total of " + sump2 + "." );
- while ( sump2 <=16 )
- {
- int p2c = 2 + r.nextInt(11);
- sump2 = sump2 + p2c;
- if ( sump2 > 21 )
- {
- System.out.println( "And I drew a " + p2c + " and now have a total of " + sump2 + ". Looks like I bust; you WIN, " + pname + "!." );
- win = bet * 2 ;
- System.out.println("Here are your winnings, $" +win + "." );
- System.exit(0);
- }
- else
- {
- System.out.println( "I drew a " + p2c + " and now have a total of " + sump2 + "." );
- System.out.println();
- }
- }
- // Determine the outcome
- if (sump1 == sump2)
- {
- System.out.println( "It's a PUSH! No winners, no losers, you keep your $" + bet + "." );
- }
- else if (sump1 > sump2)
- // Winner, check for blackjack
- {
- if ( sump1 == 21 )
- {
- win = bet * 3;
- System.out.println( "Well done, " + pname + "! BLACKJACK! YOU WIN!!" );
- System.out.println( "Here you go, $" + win + "." );
- }
- // Winner, no blackjack
- else
- {
- win = bet * 2;
- System.out.println( "Well done, " + pname + "! YOU WIN!!" );
- System.out.println( "Here you go, $" + win + "." );
- }
- }
- else
- {
- System.out.println ( "Sorry " + pname + ", you lose." );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement