Project #3 Blackjack
*No fancy or special things added so I can move on to the next program for now.
Code
/// Name: Koosha Kimelman
/// Period: 7
/// Program Name: Blackjack
/// File Name: Blackjack.java
/// Date Completed: 12/18/15
import java.util.Scanner;
import java.util.Random;
public class Blackjack {
public static void main(String[] args) throws Exception {
Scanner k = new Scanner(System.in);
Random r = new Random();
int a, b, c, d, hit, t1, t2;
String re = "N/A"; // It needed a value to begin with for one of the while loops.
a = (2+r.nextInt(10));
b = (2+r.nextInt(10));
c = (2+r.nextInt(10));
d = (2+r.nextInt(10));
while (a + b > 21 || c + d > 21) { // Since the values only go from 2 to 11, this makes sure no
a = (2+r.nextInt(10)); // one gets two 11's before the game even starts and busts.
b = (2+r.nextInt(10));
c = (2+r.nextInt(10));
d = (2+r.nextInt(10));
}
t1 = (a + b);
t2 = (c + d);
System.out.println("Time to play Blackjack.");
Thread.sleep(2000);
System.out.print("\nYou have drawn a(n) ");
Thread.sleep(750);
System.out.print(a);
Thread.sleep(750);
System.out.print(" and a(n) ");
Thread.sleep(750);
System.out.print(b + ".");
Thread.sleep(750);
System.out.println("\nYour total is " + t1 + ".");
Thread.sleep(1000);
System.out.print("\nThe dealer has drawn a(n) ");
Thread.sleep(750);
System.out.print(c);
Thread.sleep(750);
System.out.print(" and ");
Thread.sleep(750);
System.out.print("you do not know the other card.");
Thread.sleep(750);
System.out.println("\nYou also do not know the dealer's total.\n");
Thread.sleep(1000); // This shows the cards to begin with.
while (t1 <= 21 && !re.equals("stay")) {
System.out.print("\nWould you like to \"hit\" or \"stay\"? ");
re = k.next();
if (re.equals("hit")) {
hit = (2+r.nextInt(10));
t1 += hit;
System.out.print("\nYou drew a(n) ");
Thread.sleep(750);
System.out.print(hit + ".");
Thread.sleep(750);
System.out.println("\nYour total is " + t1 + ".");
}
else if (re.equals("stay"))
System.out.println("You stay.");
else {
System.out.println("You didn't put in one of the two answers. I'll assume you wanted to stay.");
re = "stay"; // It's their fault if they type one of them wrong.
}
}
if (t1 > 21) {
System.out.println("\nYou've busted.");
t1 = -1; // Because no one can get a negative number as their total.
Thread.sleep(1000);
}
else { // Just like the next loop, this will
System.out.println("\nDealer's turn."); // not show if the player busts.
System.out.print("Their hidden card was a(n)");
Thread.sleep(750);
System.out.print(d + ".");
Thread.sleep(750);
System.out.println("\nTheir total is " + t2 + ".");
}
while (t2 <= 16 && t2 <= 21 && t1 != -1) { // The last requirement for this loop is so this
hit = (2+r.nextInt(10)); // loop does not happen if the player busts.
t2 += hit;
System.out.println("\nDealer chooses to hit.");
System.out.print("They draw a(n) ");
Thread.sleep(750);
System.out.print(hit + ".");
Thread.sleep(750);
System.out.println("\nTheir total is " + t2 + ".");
}
if (t2 > 21) {
System.out.println("\nDealer has busted.");
t2 = -1; // Because no one can get a negative number as their total.
Thread.sleep(1000);
}
if (t1 == -1)
System.out.println("\nDealer wins."); // These two declare a winner without
else if (t2 == -1) // showing totals if someone busts.
System.out.println("\nYou win.");
else {
System.out.println("\nDealer stays.");
Thread.sleep(1000);
System.out.println("\nDealer total is " + t2);
Thread.sleep(750);
System.out.println("Your total is " + t1);
System.out.println();
Thread.sleep(1000);
if (t1 > t2)
System.out.println("You win.");
else if (t1 == t2)
System.out.println("You tie. Dealer wins."); // Dealer wins ties.
else
System.out.println("Dealer wins.");
}
Thread.sleep(500);
}
}
Picture of the output