Goal: Create a game of blackjack. Card faces, aces being 1 or 11, and bettings are optional inclusions.
NOTE: This project uses two classes and involves object-oriented programming and generics.
Blackjack.java
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Blackjack
/// File Name: Blackjack.java
/// Date Finished: Mar 24, 2016
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Blackjack {
public static void main(String[] args) throws InterruptedException {
Scanner in = new Scanner(System.in);
List<Card> playerCards = new ArrayList<>();
int playerTotal = 0, playerAces = 0, dealerTotal = 0, dealerAces = 0;
boolean playerStay = false, dealerStay = false, playerBusts = false, dealerBusts = false;
System.out.println("Welcome to Cornelius's Blackjack program!");
System.out.println("The rules are simple:");
System.out.println(" * There's you and the dealer. You both initially draw 2 cards, with you going first.");
System.out.println(" * You initially cannot see one of the dealer's cards.");
System.out.println(" * Numbered cards are worth their printed value. Faces are worth 10. Aces can be worth 1 or 11.");
System.out.println(" * You can ask to be \"hit\" as many times as you want, but your total card value must never go above 21.");
System.out.println(" * When you ask to \"stay\", the dealer's hidden card is revealed, and he'll hit as many times as needed.");
System.out.println(" * A \"bust\" happens when someone goes above 21, which results in an automatic game over.");
System.out.println(" * Assuming no one busts, the player with the highest total wins, with the dealer winning a tie.");
System.out.println();
Card pc1 = Card.getRandomCard();
Card pc2 = Card.getRandomCard();
playerCards.add(pc1);
playerCards.add(pc2);
if (pc1.isAce()) {
playerAces++;
} else {
playerTotal += pc1.getValue();
}
if (pc2.isAce()) {
playerAces++;
} else {
playerTotal += pc2.getValue();
}
Card dc1 = Card.getRandomCard();
Card dc2 = Card.getRandomCard();
System.out.println("You drew a " + pc1 + " and a " + pc2 + ".");
System.out.println("The dealer draw a " + dc1 + " and a hidden card.");
while (!playerStay && !playerBusts) {
System.out.println("Would you like to (1) hit, (2) stay, or (3) view your cards?");
System.out.print("> ");
int choice = in.nextInt();
if (choice < 1 || choice > 3) {
System.out.println("ERROR: Invalid choice: " + choice + ". Going with default choice of (3).");
choice = 3;
}
if (choice == 1) {
Card c = Card.getRandomCard();
playerCards.add(c);
if (c.isAce()) {
playerAces++;
} else {
playerTotal += c.getValue();
}
System.out.println("You drew a " + c + ".");
} else if (choice == 2) {
for (Card card : playerCards) {
if (card.isAce()) {
System.out.println("You have an ace. Should it be worth 1 or 11? (Current total: " + playerTotal + ")");
System.out.print("> ");
int aceValue = in.nextInt();
if (aceValue != 1 && aceValue != 11) {
System.out.println("ERROR: Invalid value: " + aceValue + ". Using default of 1.");
aceValue = 1;
}
playerTotal += aceValue;
}
}
if (playerTotal > 21) {
playerBusts = true;
} else {
playerStay = true;
}
} else {
System.out.println("Your cards:");
for (Card card : playerCards) {
System.out.println(" " + card);
}
System.out.println("Dealer's exposed card: " + dc1);
}
}
System.out.println("You chose to stay. You have a total of " + playerTotal + ".");
if (playerBusts) {
System.out.println("Sorry, looks like you bust! The dealer wins!");
return;
}
System.out.println("Now it's the dealer's turn.");
System.out.println("His hidden card was a " + dc2 + ".");
if (dc1.isAce()) {
dealerAces++;
} else {
dealerTotal += dc1.getValue();
}
if (dc2.isAce()) {
dealerAces++;
} else {
dealerTotal += dc2.getValue();
}
while (!dealerStay && !dealerBusts) {
// goes through all combinations of treating aces as ones and elevens
for (int i = 1; i < dealerAces + 1; i++) {
int combo = dealerTotal + i * 11 - (i - dealerAces);
// if less than or equal to 21 or greater than or equal to 16, keep
if (combo <= 21 && combo >= 16) {
dealerStay = true;
dealerTotal = combo;
System.out.println("The dealer is having " + i + " of his aces worth 11, and the rest worth 1.");
break;
}
}
if (!dealerStay) {
Card c = Card.getRandomCard();
//Card.getDeck()[0];
System.out.println("The dealer draws a " + c.toString() + ".");
if (c.isAce()) {
dealerAces++;
} else {
dealerTotal += c.getValue();
}
if (dealerTotal + dealerAces >= 16) {
if (dealerTotal + dealerAces > 21) {
dealerTotal += dealerAces;
if (dealerAces > 0) {
System.out.println("The dealer is having all of his aces worth 1.");
}
dealerBusts = true;
} else {
dealerStay = true;
}
}
}
}
System.out.println("The dealer chooses to stay. He has a total of " + dealerTotal + ".");
if (dealerBusts) {
System.out.println("The dealer busts. Looks like you win!");
} else {
if (dealerTotal >= playerTotal) {
System.out.println("The dealer has a higher or equal value. You lose!");
} else {
System.out.println("You have a higher value. You win!");
}
}
}
}
Card.java
/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: PROGRAM_NAME
/// File Name: Card.java
/// Date Finished: Mar 24, 2016
import java.util.Random;
public class Card {
public static String[]
SUITS = new String[] {"Hearts", "Spades", "Diamonds", "Clubs"},
NAMES = new String[] {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
private int value;
private String suit, name;
private boolean ace = false;
public Card(int value, String suit, String name) {
this.value = value;
this.suit = suit;
this.name = name;
}
public boolean isAce() {
return ace;
}
public String getName() {
return name;
}
public String getSuit() {
return suit;
}
public int getValue() {
return value;
}
@Override
public String toString() {
return name + " of " + suit;
}
private static Card[] DECK = new Card[52];
private static final Random RANDOM = new Random();
static {
for (int i = 0; i < SUITS.length; i++) {
for (int j = 0; j < NAMES.length; j++) {
int value = j + 1;
if (value > 10) {
value = 10;
}
Card c = new Card(value, SUITS[i], NAMES[j]);
if (NAMES[j].equals("Ace")) {
c.ace = true;
}
DECK[j + i * NAMES.length] = c;
}
}
}
public static Card[] getDeck() {
return DECK.clone();
}
public static Card getRandomCard() {
return DECK[RANDOM.nextInt(52)];
}
}