Directory:Derek Elder/Programs/Blackjack

MyWikiBiz, Author Your Legacy — Tuesday November 19, 2024
Jump to navigationJump to search
import java.util.ArrayList;
import java.util.Random;
import chn.util.*;

//Coded by Shane Peterson, updates by Derek Elder

public class Blackjack
{
  Deck deck;
  Hand playerHand;
  Hand dealerHand;
  ConsoleIO console = new ConsoleIO();

  public void startGame()
  {
    System.out.println("Welcome to Blackjack!\nPress Enter to begin...");
    String blank = console.readLine();

    shuffle();
    deal();
    playerTurn();
  }

  public void playerTurn()
  {
    System.out.println("Dealer shows: " + dealerHand.card(1));
    System.out.println("You have: " + playerHand.card(0) + ", " + playerHand.card(1));
    query();
  }

  public void query()
  {
    System.out.print("Current score: " + playerHand.score() + " Hit (h) or stay (s)?: ");
    String response = console.readLine();
    if (response.equals("h"))
      playerHit();
    else
      dealerTurn();
  }
    
  public void playerHit()
  {
    playerHand.add(deck.draw());
    System.out.println("You draw: " + playerHand.card(playerHand.myHand.size()-1));
    if (playerHand.score() > 21)
      System.out.print("Current score: " + playerHand.score() + " Bust! You lose...");
    else
      query();
  }

  public void dealerTurn()
  {
    System.out.println("Dealer has: " + dealerHand.card(0) + ", " + dealerHand.card(1));
    while (dealerHand.score() < 17)
    {
      dealerHand.add(deck.draw());
      System.out.println("Dealer draws: " + dealerHand.card(dealerHand.myHand.size()-1));
    }
    System.out.print("Dealer's score: " + dealerHand.score());
    if (dealerHand.score() > 21)
      System.out.println(" Dealer busts! You win!");
    else if (dealerHand.score() < playerHand.score())
      System.out.println(" You win!");
    else
      System.out.println(" You lose...");
  }

  public void shuffle()
  {
    deck = new Deck();
    System.out.println("Deck shuffled...");
  }

  public void deal()
  {
    playerHand = new Hand();
    dealerHand = new Hand();
    playerHand.add(deck.draw());
    dealerHand.add(deck.draw());
    playerHand.add(deck.draw());
    dealerHand.add(deck.draw());
    System.out.println("Cards dealt...");
  }

  public static void main(String[] args)
  {
    Blackjack blackjack = new Blackjack();
    blackjack.startGame();
  }
}

class Hand
{
  ArrayList myHand = new ArrayList();
  private int handScore = 0;
  private int aceCount = 0;

  public void add(Card card)
  {
    myHand.add(card);
    if (card.value() == 11 || card.value() == 12 || card.value() == 13)
      handScore += 10;
    else if (card.value() == 1)
    {
      handScore += 11;
      aceCount++;
    }
    else
      handScore += card.value();
    if (handScore > 21 && aceCount > 0)
    {
      handScore -= 10;
      aceCount--;
    }
  }

  public Card card(int card)
  {
    return (Card)myHand.get(card);
  }

  public int score()
  {
    return handScore;
  }
}

class Deck
{
  ArrayList theDeck = new ArrayList();
  Random random = new Random();

  public Deck()
  {
    for (int suit = 1; suit <= 4; suit++)
      for (int value = 1; value <= 13; value++)
	theDeck.add(new Card(value, suit));
  }

  public Card draw()
  {
    return (Card)theDeck.remove(random.nextInt(theDeck.size()));
  }
}

class Card
{
  private int cardValue;
  private int cardSuit;
  public String cardName = "";

  public Card(int value, int suit)
  {
    cardValue = value; cardSuit = suit;
    nameCard();
  }

  private void nameCard()
  {
    String value = "", suit = "";
    switch (cardValue)
    {
      case 1: value = "ace"; break;
      case 2: value = "two"; break;
      case 3: value = "three"; break;
      case 4: value = "four"; break;
      case 5: value = "five"; break;
      case 6: value = "six"; break;
      case 7: value = "seven"; break;
      case 8: value = "eight"; break;
      case 9: value = "nine"; break;
      case 10: value = "ten"; break;
      case 11: value = "jack"; break;
      case 12: value = "queen"; break;
      case 13: value = "king"; break;
    }
    switch (cardSuit)
    {
      case 1: suit = "diamonds"; break;
      case 2: suit = "clubs"; break;
      case 3: suit = "hearts"; break;
      case 4: suit = "spades"; break;
    }
    cardName = value + " of " + suit;
  }

  public int value()
  {
    return cardValue;
  }

  public int suit()
  {
    return cardSuit;
  }

  public String toString()
  {
    return cardName;
  }
}