Project #3
Code
/*
jack newsom
period 5
program Blackjack
filename Blackjack.java
date completed 10/27/15
*/
import java.util.Scanner;
import java.util.Random;
public class Blackjack
{
public static void main( String[] args )
{
Random r = new Random();
Scanner bot = new Scanner(System.in);
System.out.println("Welcome to Jack's blackjack program!");
int playerSum = 0;
int dealerSum = 0;
int playerTurn = 0;
int dealerTurn = 0;
int playerStay = 0;
int dealerStay = 0;
int stayTotal = 0;
int player1 = 2 + r.nextInt(9);
int player2 = 2 + r.nextInt(9);
playerSum = player1 + player2;
int dealer1 = 2 + r.nextInt(9);
int dealer2 = 2 + r.nextInt(9);
dealerSum = dealer1 + dealer2;
System.out.println("You draw a " + player1 + " and a " + player2 + "." );
System.out.println("Your total is " + playerSum + "." );
System.out.println("");
System.out.println("The dealer draws a " + dealer1 + " and a card that is face down." );
System.out.println("The dealer's total is unknown.");
//this next line says while neither person's sum is less than 21, the player can continue to play
while ( playerSum <= 21 && dealerSum <= 21 && stayTotal != 2 )
{
while ( playerSum <= 21 && dealerSum <= 21 && playerTurn == 0 && stayTotal != 2 )
{
playerStay = 0;
System.out.println();
System.out.println("Your turn!");
System.out.println("Your total is " + playerSum + ".");
System.out.print("Would you like to \"hit\" or \"stay\"?");
System.out.println("");
String playerChoice = bot.next();
boolean bPlayerChoice = playerChoice.equals("hit");
if ( bPlayerChoice == true )
{
// this is if the player hits
int playerNew = 2 + r.nextInt(9);
playerSum = playerSum + playerNew;
System.out.println("You drew a " + playerNew + ", bringing your total to " + playerSum + "." );
}
else
{
// this is if the player stays
System.out.println("You decide to stay.");
playerStay = 1;
}
playerTurn = 1;
dealerTurn = 0;
System.out.println();
}
//dealer's turn
while ( playerSum <= 21 && dealerSum <= 21 && dealerTurn == 0 && stayTotal != 2 )
{
dealerStay = 0;
System.out.println("Okay, dealer's turn.");
if ( dealerSum <= 16 )
{
int dealerNew = 2 + r.nextInt(9);
dealerSum = dealerSum + dealerNew;
System.out.println("The dealer's hidden card was a " + dealer1 + ".");
System.out.println("The dealer drew a " + dealerNew + ", bringing his total to " + dealerSum + ".");
}
else
{
System.out.println("The dealer decides to stay.");
dealerStay = 1;
}
dealerTurn = 1;
playerTurn = 0;
System.out.println();
}
//this is to test if both players stayed
stayTotal = dealerStay + playerStay;
}
//this section tests both totals to see who wins
if ( playerSum >= 21 && dealerSum < 21 )
{
if ( playerSum == 21 )
System.out.println("You win! Congrats.");
else if ( playerSum > 21 )
System.out.println("Whoops. You bust.");
}
else if ( playerSum < 21 && dealerSum >= 21 )
{
if ( dealerSum == 21 )
System.out.println("The dealer wins. Aww.");
else if ( dealerSum > 21 )
System.out.println("The dealer busts. Yay.");
}
else if ( playerSum > 21 && dealerSum > 21 )
{
System.out.println("You both bust. The dealer wins.");
}
else if ( playerSum < 21 && dealerSum < 21 )
{
if ( playerSum > dealerSum )
System.out.println("You win! Congrats.");
else if ( playerSum <= dealerSum )
System.out.println("The dealer wins. Aww.");
}
}
}
Picture of the output