Skip to content

Instantly share code, notes, and snippets.

Created February 22, 2017 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/352ffc009d17b24452aa8383aee8b5ab to your computer and use it in GitHub Desktop.
Save anonymous/352ffc009d17b24452aa8383aee8b5ab to your computer and use it in GitHub Desktop.
import java.util.Random;
import java.util.Scanner;
public class BlackJackFinal {
private static Scanner in;
private static boolean again;
public static void main(String[] args) throws InterruptedException {
Random r = new Random();
in = new Scanner(System.in);
System.out.print("Welcome to Aaron's Blackjack program v.4\n\n");
int count=0; //used to continue game if user or dealer gets a blackjack
do { //runs entire loop at least once
int[] card = new int[4]; //sets initial four card values
for (int i=0; i<card.length;i++) {
card[i]=2+r.nextInt(10);
}
int sum_player = card[0]+card[1]; //gets initial sum for player
int sum_dealer = card[2]+card[3]; //gets initial sum for dealer
if (card[0]==11&&card[1]==11) { //if players first 2 cards are 11 randomly sets one of the cards to a 1 and sum to 12
int alternate = r.nextInt(2);
card[alternate]=1;
sum_player=12;
}
if (card[2]==11&&card[3]==11) { //if dealers first 2 cards are 11 randomly sets one of the cards to a 1 and sum to 12
card[3]=1;
sum_dealer=12;
}
Thread.sleep(900);
System.out.println("You were dealt a "+card[0]+" and a "+card[1]+"!"); //shows players cards and sum
System.out.println("Your total is "+sum_player+"!\n");
Thread.sleep(900);
System.out.println("Dealer has a "+card[2]+" showing and a hidden card."); //shows one of dealers card
Thread.sleep(900);
if (card[2]==11&&sum_dealer==21&&sum_player!=21) { //if initial dealer total = 21 and player sum isn't 21, dealer wins
System.out.println("\nDealer reveals his hidden card and it is a "+card[3]+".");
System.out.println("Dealer's total is "+sum_dealer+".");
System.out.println("Dealer wins with a Blackjack!");
count++; //adds to count so game will work properly
PlayAgain(); //runs the PlayAgain function
}
else if (card[2]==11&&sum_dealer==21&&sum_player==21) { //tie game if both player and dealer initially get a 21
System.out.println("\nDealer reveals his hidden card and it is a "+card[3]+".");
System.out.println("Dealer's total is "+sum_dealer+".");
System.out.println("Both players have a BlackJack! Push!");
count++;
PlayAgain();
}
while (sum_player < 21 &&sum_dealer != 21) { //if neither have 21 gives option to hit or stay
System.out.println("\nWould you like to 'hit' or 'stay'?");
String hit_stay = in.nextLine();
if (hit_stay.equals("hit")) { //if you hit, adds new value to your sum
int a = 2 + r.nextInt(10);
sum_player = sum_player + a;
System.out.println("You drew a "+a+".");
System.out.println("Your total is "+sum_player+".");
}
else if (hit_stay.equals("stay")) { //breaks while loop if you choose stay
break;
}
else { //forces you to enter hit or stay
System.out.println("Please enter in a valid answer.");
}
}
//below shows so of the following win scenarios
if (sum_player > 21) {
System.out.println("\nBUST! Dealer wins.");
PlayAgain();
}
else if (sum_player==21&&sum_dealer!=21) {
System.out.println("\nWow! A 21?!? You win!");
PlayAgain();
}
if (sum_dealer != 21 && sum_player<21) {
System.out.println("\nDealer reveals his hidden card and it is a "+card[3]+".");
System.out.println("Dealer's total is "+sum_dealer+".");
}
while (sum_player < 21 && sum_dealer <17 ) {
Thread.sleep(900);
int b = 2 + r.nextInt(10);
System.out.println("\nDealer hits and drew a "+b+".");
sum_dealer = sum_dealer + b;
System.out.println("Dealer's total is "+sum_dealer+".");
Thread.sleep(900);
}
if (sum_dealer > 21) {
System.out.println("\nDealer busts, you win!.");
PlayAgain();
}
else if (sum_dealer == 21 && count == 0) {
System.out.println("\nDealer has a 21! Hah never bet against the house!");
count=0;
PlayAgain();
}
if (sum_dealer >= 17 && sum_dealer < 21 && sum_player<21) {
System.out.println("\nDealer stays.");
Thread.sleep(900);
}
while (sum_dealer < 21 && sum_player < 21) {
if (sum_player > sum_dealer) {
System.out.println("\nYou win!");
PlayAgain();
break;
}
else if (sum_player < sum_dealer && sum_dealer != 21) {
System.out.println("\nSorry but the house always wins!");
PlayAgain();
break;
}
else {
System.out.println("\nHouse wins all ties!");
PlayAgain();
break;
}
}
} while(again==true); //loops until you choose not to play again
System.out.println("Thanks for playing! :)");
}
public static boolean PlayAgain() { //gives option to play again and rerun do-while loop
System.out.println("\nPlay again? y/n");
String play = in.nextLine();
switch(play){
case "y": again = true;
break;
case "n": again = false;
break;
default: System.out.println("Invalid answer. Please answer only (y/n)");
PlayAgain();
}
System.out.println();
return again;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment