Project #3 and Blackjack
Code
/// Name: Ali Kurland
/// Period: 6
/// Program Name: Blackjack
/// File Name: Blackjack.java
/// Date Completed: 11/12/2015
import java.util.Scanner;
import java.util.Random;
public class Blackjack
{
public static void main( String[] args )
{
Random r = new Random();
Random x = new Random();
Scanner keyboard = new Scanner(System.in);
String action = "", again = "", playerCardOne = "", playerCardTwo = "", playerCardThree;
String dealerCardOne, dealerCardTwo = "", dealerCardThree;
String playerSuitOne, playerSuitTwo, playerSuitThree, dealerSuitOne, dealerSuitTwo = "", dealerSuitThree;
int tokens = 10, wager, playerCard1, playerCard2, playerCard3, playerTotal = 0;
int dealerCard1, dealerCard2 = 0, dealerCard3 = 0, dealerTotal = 0;
int playerSuit1, playerSuit2, playerSuit3, dealerSuit1, dealerSuit2, dealerSuit3;
System.out.println( "Welcome to Ali's blackjack program!" );
do
{
/// Allows the player to wager tokens on the outcome of the game.
System.out.print( "\nYou have " + tokens + " tokens. How many would you like to wager? (The dealer will match your wager.) " );
wager = keyboard.nextInt();
/// Ensures that the player's wager does not exceed the number of tokens they have.
while ( wager > tokens || wager <= 0 )
{
System.out.print( "You must wager between 1 & " + tokens + "! Try again: how many would you like to wager? " );
wager = keyboard.nextInt();
}
tokens = tokens - wager; /// Takes the player's wager from their store of tokens.
System.out.println( "The dealer also wagers " + wager + " tokens." );
System.out.println( "There are " + ( wager * 2 ) + " tokens in the pot." );
/// Determines the player's first card (value and suit).
playerCard1 = 1 + r.nextInt(13);
if ( playerCard1 == 1 )
playerCardOne = "n Ace";
else if ( playerCard1 == 2 )
playerCardOne = " 2";
else if ( playerCard1 == 3 )
playerCardOne = " 3";
else if ( playerCard1 == 4 )
playerCardOne = " 4";
else if ( playerCard1 == 5 )
playerCardOne = " 5";
else if ( playerCard1 == 6 )
playerCardOne = " 6";
else if ( playerCard1 == 7 )
playerCardOne = " 7";
else if ( playerCard1 == 8 )
playerCardOne = " 8";
else if ( playerCard1 == 9 )
playerCardOne = " 9";
else if ( playerCard1 == 10 )
playerCardOne = " 10";
else if ( playerCard1 == 11 )
playerCardOne = " Jack";
else if ( playerCard1 == 12 )
playerCardOne = " Queen";
else
playerCardOne = " King";
playerSuit1 = 1 + x.nextInt(4);
if ( playerSuit1 == 1 )
playerSuitOne = "hearts" ;
else if ( playerSuit1 == 2 )
playerSuitOne = "spades" ;
else if ( playerSuit1 == 3 )
playerSuitOne = "diamonds" ;
else
playerSuitOne = "clubs" ;
/// Determines the player's second card (value and suit).
playerCard2 = 1 + r.nextInt(13);
if ( playerCard2 == 1 )
playerCardTwo = "n Ace";
else if ( playerCard2 == 2 )
playerCardTwo = " 2";
else if ( playerCard2 == 3 )
playerCardTwo = " 3";
else if ( playerCard2 == 4 )
playerCardTwo = " 4";
else if ( playerCard2 == 5 )
playerCardTwo = " 5";
else if ( playerCard2 == 6 )
playerCardTwo = " 6";
else if ( playerCard2 == 7 )
playerCardTwo = " 7";
else if ( playerCard2 == 8 )
playerCardTwo = " 8";
else if ( playerCard2 == 9 )
playerCardTwo = " 9";
else if ( playerCard2 == 10 )
playerCardTwo = " 10";
else if ( playerCard2 == 11 )
playerCardTwo = " Jack";
else if ( playerCard2 == 12 )
playerCardTwo = " Queen";
else
playerCardTwo = " King";
playerSuit2 = 1 + x.nextInt(4);
if ( playerSuit2 == 1 )
playerSuitTwo = "hearts" ;
else if ( playerSuit2 == 2 )
playerSuitTwo = "spades" ;
else if ( playerSuit2 == 3 )
playerSuitTwo = "diamonds" ;
else
playerSuitTwo = "clubs" ;
System.out.println( "\nThe cards are dealt.\nYou get a" + playerCardOne + " of " + playerSuitOne + " and a" + playerCardTwo + " of " + playerSuitTwo + "." );
playerTotal = playerTotal + playerCard1 + playerCard2;
System.out.println( "Your total is " + playerTotal + ".\n" );
if ( playerTotal <= 21 ) /// Skips to the end of the game if the player busts.
{
/// Determines the dealer's first card (value and suit).
dealerCard1 = 1 + r.nextInt(13);
if ( dealerCard1 == 1 )
dealerCardOne = "n Ace";
else if ( dealerCard1 == 2 )
dealerCardOne = " 2";
else if ( dealerCard1 == 3 )
dealerCardOne = " 3";
else if ( dealerCard1 == 4 )
dealerCardOne = " 4";
else if ( dealerCard1 == 5 )
dealerCardOne = " 5";
else if ( dealerCard1 == 6 )
dealerCardOne = " 6";
else if ( dealerCard1 == 7 )
dealerCardOne = " 7";
else if ( dealerCard1 == 8 )
dealerCardOne = " 8";
else if ( dealerCard1 == 9 )
dealerCardOne = " 9";
else if ( dealerCard1 == 10 )
dealerCardOne = " 10";
else if ( dealerCard1 == 11 )
dealerCardOne = " Jack";
else if ( dealerCard1 == 12 )
dealerCardOne = " Queen";
else
dealerCardOne = " King";
dealerSuit1 = 1 + x.nextInt(4);
if ( dealerSuit1 == 1 )
dealerSuitOne = "hearts" ;
else if ( dealerSuit1 == 2 )
dealerSuitOne = "spades" ;
else if ( dealerSuit1 == 3 )
dealerSuitOne = "diamonds" ;
else
dealerSuitOne = "clubs" ;
/// Determines the dealer's second card (value and suit).
dealerCard2 = 1 + r.nextInt(13);
if ( dealerCard2 == 1 )
dealerCardTwo = "n Ace";
else if ( dealerCard2 == 2 )
dealerCardTwo = " 2";
else if ( dealerCard2 == 3 )
dealerCardTwo = " 3";
else if ( dealerCard2 == 4 )
dealerCardTwo = " 4";
else if ( dealerCard2 == 5 )
dealerCardTwo = " 5";
else if ( dealerCard2 == 6 )
dealerCardTwo = " 6";
else if ( dealerCard2 == 7 )
dealerCardTwo = " 7";
else if ( dealerCard2 == 8 )
dealerCardTwo = " 8";
else if ( dealerCard2 == 9 )
dealerCardTwo = " 9";
else if ( dealerCard2 == 10 )
dealerCardTwo = " 10";
else if ( dealerCard2 == 11 )
dealerCardTwo = " Jack";
else if ( dealerCard2 == 12 )
dealerCardTwo = " Queen";
else
dealerCardTwo = " King";
dealerSuit2 = 1 + x.nextInt(4);
if ( dealerSuit2 == 1 )
dealerSuitTwo = "hearts" ;
else if ( dealerSuit2 == 2 )
dealerSuitTwo = "spades" ;
else if ( dealerSuit2 == 3 )
dealerSuitTwo = "diamonds" ;
else
dealerSuitTwo = "clubs" ;
System.out.println( "The dealer has a" + dealerCardOne + " of " +dealerSuitOne + " and a hidden card." );
dealerTotal = dealerTotal + dealerCard1 + dealerCard2;
System.out.println( "The dealer's total is hidden, too.\n" );
System.out.print( "Would you like to hit or stay? " );
action = keyboard.next();
}
/// Gives the player another turn if they choose to hit.
while ( action.equals( "hit" ) && ( playerTotal <= 21 ) )
{
/// Determines the player's third card (value and suit).
playerCard3 = 1 + r.nextInt(13);
if ( playerCard3 == 1 )
playerCardThree = "n Ace";
else if ( playerCard3 == 2 )
playerCardThree = " 2";
else if ( playerCard3 == 3 )
playerCardThree = " 3";
else if ( playerCard3 == 4 )
playerCardThree = " 4";
else if ( playerCard3 == 5 )
playerCardThree = " 5";
else if ( playerCard3 == 6 )
playerCardThree = " 6";
else if ( playerCard3 == 7 )
playerCardThree = " 7";
else if ( playerCard3 == 8 )
playerCardThree = " 8";
else if ( playerCard3 == 9 )
playerCardThree = " 9";
else if ( playerCard3 == 10 )
playerCardThree = " 10";
else if ( playerCard3 == 11 )
playerCardThree = " Jack";
else if ( playerCard3 == 12 )
playerCardThree = " Queen";
else
playerCardThree = " King";
playerSuit3 = 1 + x.nextInt(4);
if ( playerSuit3 == 1 )
playerSuitThree = "hearts" ;
else if ( playerSuit3 == 2 )
playerSuitThree = "spades" ;
else if ( playerSuit3 == 3 )
playerSuitThree = "diamonds" ;
else
playerSuitThree = "clubs" ;
System.out.println( "You drew a" + playerCardThree + " of " + playerSuitThree + "." );
playerTotal = playerTotal + playerCard3;
System.out.println( "Your total is " + playerTotal + ".\n" );
if ( playerTotal <= 21 ) /// Allows the player another turn if they haven't busted.
{
System.out.print( "Would you like to hit or stay? " );
action = keyboard.next();
}
}
/// Gives the dealer a turn if the player stays and hasn't busted.
if ( playerTotal <= 21 )
{
System.out.println( "\nOkay, dealer's turn." );
System.out.println( "His hidden card was a" + dealerCardTwo + " of " + dealerSuitTwo + "." );
System.out.println( "His total was " + dealerTotal + ".\n" );
/// Allows the dealer to have another turn if their card total is under 16.
while ( ( dealerTotal <= 16 ) && ( playerTotal <= 21 ) )
{
System.out.println( "Dealer chooses to hit." );
/// Determines the dealer's third card (value and suit).
dealerCard3 = 1 + r.nextInt(13);
if ( dealerCard3 == 1 )
dealerCardThree = "n Ace";
else if ( dealerCard3 == 2 )
dealerCardThree = " 2";
else if ( dealerCard3 == 3 )
dealerCardThree = " 3";
else if ( dealerCard3 == 4 )
dealerCardThree = " 4";
else if ( dealerCard3 == 5 )
dealerCardThree = " 5";
else if ( dealerCard3 == 6 )
dealerCardThree = " 6";
else if ( dealerCard3 == 7 )
dealerCardThree = " 7";
else if ( dealerCard3 == 8 )
dealerCardThree = " 8";
else if ( dealerCard3 == 9 )
dealerCardThree = " 9";
else if ( dealerCard3 == 10 )
dealerCardThree = " 10";
else if ( dealerCard3 == 11 )
dealerCardThree = " Jack";
else if ( dealerCard3 == 12 )
dealerCardThree = " Queen";
else
dealerCardThree = " King";
dealerSuit3 = 1 + x.nextInt(4);
if ( dealerSuit3 == 1 )
dealerSuitThree = "hearts" ;
else if ( dealerSuit3 == 2 )
dealerSuitThree = "spades" ;
else if ( dealerSuit3 == 3 )
dealerSuitThree = "diamonds" ;
else
dealerSuitThree = "clubs" ;
System.out.println( "He draws a" + dealerCardThree + " of " + dealerSuitThree + "." );
dealerTotal = dealerTotal + dealerCard3;
System.out.println( "His total is " + dealerTotal + ".\n" );
}
if ( dealerTotal <= 21 ) /// Ends the dealer's turn if they haven't busted.
{
System.out.println( "Dealer stays.\n" );
System.out.println( "Dealer total is " + dealerTotal + "." );
System.out.println( "Your total is " + playerTotal + ".\n" );
}
}
/// Displays the outcome of the game and the player's new token balance.
if ( playerTotal > 21 )
System.out.println( "You bust. Dealer wins." );
else if ( ( playerTotal <= dealerTotal ) && ( dealerTotal <= 21 ) )
System.out.println( "Dealer wins." );
else if ( dealerTotal > 21 )
{
System.out.println( "Dealer busts. You win!" );
tokens = tokens + ( wager * 2 );
}
else
{
System.out.println( "YOU WIN!" );
tokens = tokens + ( wager * 2 );
}
/// Allows the player to play again if they still have tokens and choose to do so.
if ( tokens != 0 )
{
System.out.print( "You have " + tokens + " tokens remaining. Would you like to play again? " );
again = keyboard.next();
playerTotal = 0;
dealerTotal = 0;
}
else
{
System.out.println( "You have no tokens remaining. Thanks for playing!" );
again = "no";
}
} while ( again.equals( "yes" ) );
}
}
Picture of the output