Project 3 BlackJack.java
Code
/// Name: Boris Kazantsev
/// Period: 6
/// Program Name: BlackJack
/// File Name: BlackJack.java
/// Date Finished: 3/28/2016
import java.util.Random;
import java.util.Scanner;
public class BlackJack
{
static final byte PLAYER = 0;
static final byte DEALER = 1;
static Random r = new Random();
static Scanner keyboard = new Scanner(System.in);
static int playerTotal = 0;
static int dealerTotal = 0;
static String read = "";
public static void main(String[] args)
{
setup();
do
{
playerTurn();
}
while (!read.equals("stay") && playerTotal < 21);
if (playerTotal < 22)
{
dealerTurn();
}
decideWhoWins();
}
static void setup()
{
System.out.println("Let's play Real Blackjack!!!");
System.out.println("You get a " + newCard(PLAYER) + " and " + newCard(PLAYER) + ".");
System.out.println("Your Total is "+playerTotal + ".");
System.out.println();
System.out.println("The dealer has a " + newCard(DEALER) + " and a hidden card.");
System.out.println("His total is hidden.\n");
}
static int newCard(byte who)
{
int card = r.nextInt(10)+2;
if (who==PLAYER) playerTotal += card;
else dealerTotal += card;
return(card);
}
static void playerTurn()
{
System.out.print("Would you like to \"hit\" or to \"stay\"? ");
read = keyboard.nextLine();
if (!read.equals("stay"))
{
if (read.equals("hit"))
{
System.out.println("You drew a "+newCard(PLAYER)+".");
System.out.println("Your total is "+playerTotal + ".\n");
}
}
}
static void dealerTurn()
{
System.out.println("\nOkay, dealer's turn.\n");
System.out.println("Dealer's hidden card was a " + newCard(DEALER)+".");
System.out.println("His total is " + dealerTotal + ".\n");
while(dealerTotal<=16)
{
System.out.println("Dealer decides to hit.");
System.out.println("Dealer draws a "+newCard(DEALER)+".");
System.out.println("His total is "+dealerTotal + ".\n");
}
System.out.println("Dealer decides to stay.\n");
}
static void decideWhoWins()
{
if (playerTotal>21) System.out.println("You BUST, so you LOSE!!!");
else if (dealerTotal>21) System.out.println("Dealer BUSTS, so you WIN!!!");
else
{
System.out.println("Your total is " + playerTotal + ".");
System.out.println("Dealer's total is " + dealerTotal + ".\n");
if (playerTotal > dealerTotal) System.out.println("YOU WIN!!!");
else if (playerTotal < dealerTotal) System.out.println("YOU LOSE!!!");
else System.out.println("IT'S A TIE!!!");
}
}
}
Picture of the output