Assignemnt Project #3 and Blackjack
Code
///Name: Daniel Tiffany-Appleton
///Period: 7
///Program Name: Blackjack
///File Name: Blackjack
///Date Finished: 2/22/16
import java.util.Scanner;
import java.util.Random;
public class Blackjack
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
int cardOne, cardTwo, playerTotal, dealerTotal, hiddenCard, drawCard;
String hitOrStay, four;
boolean hit, stay;
System.out.println("WELCONE TO THE APPLETON BLACKJACK TABLE");
cardOne = r.nextInt((11 - 2) + 1) + 2;
//equation ((max - min) + 1) + min
cardTwo = r.nextInt((11 - 2) + 1) + 2;
playerTotal = cardOne + cardTwo;
System.out.println(" ");
System.out.println("You got a " + cardOne + " and a " + cardTwo + ".");
System.out.println("Your total is " + playerTotal + ".");
//used to redefine both card draws
cardOne = r.nextInt((11 - 2) + 1) + 2;
cardTwo = r.nextInt((11 - 2) + 1) + 2;
hiddenCard = cardTwo;
//To keep the variable of the hidden card
dealerTotal = cardOne + cardTwo;
System.out.println(" ");
System.out.println("The dealer drew a " + cardOne + " and a hidden card.");
System.out.println("His total is hidden too.");
//player's turn
do
{
System.out.println(" ");
System.out.print("Would you like to hit or stay? ");
hitOrStay = keyboard.next();
if(hitOrStay.equals("hit"))
{
drawCard = r.nextInt((11 - 2) + 1) + 2;
playerTotal = playerTotal + drawCard;
System.out.println("You drew a " + drawCard + ".");
System.out.println("Your total is " + playerTotal + ".");
}
}while(playerTotal < 21 && ! hitOrStay.equals("stay"));
//if player busts
if(playerTotal >= 21)
{
System.out.println(" ");
System.out.println("You busted. Dealer wins!" );
return;
}
//dealer's turn
System.out.println(" ");
System.out.println("Dealer's turn");
System.out.println("His hidden card is a " + hiddenCard + ".");
System.out.println("His total is " + dealerTotal + ".");
while(dealerTotal < 16 || dealerTotal < 21)
{
drawCard = r.nextInt((11 - 2) + 1) + 2;
dealerTotal = dealerTotal + drawCard;
System.out.println(" ");
System.out.println("Dealer decides to hit.");
System.out.println("Dealer drew a " + drawCard + ".");
System.out.println("Dealer's total is " + dealerTotal + ".");
}
//if dealer busts
if( dealerTotal >= 21 )
{
System.out.println(" ");
System.out.println("Dealer busts. Player wins!");
return;
}
//if dealer doesn't bust
else
{
System.out.println(" ");
System.out.println("Dealer stays.");
}
//if no one busts
System.out.println(" ");
System.out.println("Dealer's total is " + dealerTotal + ".");
System.out.println("Your total is " + playerTotal + ".");
}
}
Picture of the output