CS 1713, Fall 2003
Project 2 Solution -- Fancy Version
(includes better display and 1 or 11 for Aces)

Here is one possible solution to Project 2, with extra features added beyond the scope of the project, as were shown at the end of the project description. This is just to show one way to do Project 2.


//BlackJackHand.java

//package blackjackpkg2;
import java.util.*;

public class BlackJackHand {
  private static Random myRandom = new Random();
  private String myCardSuit;
  private int myCardNum;
  private int myCardsAltTotal;
  private int myCardsTotal;
  private int myCardsCount;

  public BlackJackHand() {
    myCardsTotal = 0;
    myCardsAltTotal = 0;
    myCardsCount = 0;
  }

  public void dealCard(){
      myCardNum = myRandom.nextInt(13) + 1;
      switch (myRandom.nextInt(4)){
              case 0 : myCardSuit = "Clubs"; break;
              case 1 : myCardSuit = "Diamonds"; break;
              case 2 : myCardSuit = "Hearts"; break;
              case 3 : myCardSuit = "Spades"; break;
      } // end of case to determin suit
    if (myCardNum > 10)  {
      myCardsTotal = myCardsTotal + 10;
      if (myCardsAltTotal > 0)
        myCardsAltTotal = myCardsAltTotal + 10;
    }
    else {
      myCardsTotal = myCardsTotal + myCardNum;
      if (myCardsAltTotal > 0)
        myCardsAltTotal = myCardsAltTotal + myCardNum;
    } //add face value or 10
    // A is 1 or 11.  Don't forget to ignore 2nd A
    if (myCardNum == 1 && myCardsAltTotal == 0) {
      myCardsAltTotal = myCardsTotal + 10;
    }
  } // end of drawCard

  public String toString() {
    return "Total: " + getTotal();
  } // end of toString

  public String getCard(){
    return trans(myCardNum) + " " + myCardSuit;
  } // end of getCard

  private String trans(int cardNum) {
    if (cardNum == 1) return "A";
    else if (cardNum == 11) return "J";
    else if (cardNum == 12) return "Q";
    else if (cardNum == 13) return "K";
    else return cardNum + "";
  }

  public int getTotal() {
    if (myCardsAltTotal <= 21 && myCardsAltTotal != 0)
      return myCardsAltTotal;
    else
      return myCardsTotal;
  } // end of get total

  public void clearHand () {
    myCardsTotal = 0;
    myCardsAltTotal = 0;
  }

  public int compareTo (BlackJackHand p2){
    if (this.getTotal() <= 21 && p2.getTotal() > 21)
      return 1; //calling object won
    else if (this.getTotal() > 21 && p2.getTotal() <= 21)
      return 0; //calling object lost
    else if (this.getTotal() >= p2.getTotal())
      return 1;
    else
      return 0;
  } // end of compareTo


}// end of BlackJackHand

//BlackJackGame.java //package blackjackpkg2; //import cs1.Keyboard; import java.text.NumberFormat; public class BlackJackGame { final int PAYOFF = 2; //payoff amount for winning a game private String myName; private BlackJackHand player; private BlackJackHand dealer; private char takeCard; private double myWager; private double myCash; private NumberFormat money = NumberFormat.getCurrencyInstance(); public BlackJackGame(String name) { myName = name; myCash = 500; player = new BlackJackHand(); dealer = new BlackJackHand(); } public BlackJackGame(String name, double cash) { myName = name; myCash = cash; player = new BlackJackHand(); dealer = new BlackJackHand(); } public void setWager(double wager){ myWager = wager; } public void playGame() { // first card from dealer. Don't deal dealer 2nd card player.clearHand(); dealer.clearHand(); System.out.print("Dealer, \tCard 1: "); dealer.dealCard(); System.out.println(dealer.getCard()); // now the player System.out.print(myName + ", \tCard 1: "); player.dealCard(); System.out.print(player.getCard()); System.out.print(", \tCard 2: "); player.dealCard(); System.out.println(player.getCard()); int count = 2; do { System.out.print("Do you want another card? y/n "); takeCard = Keyboard.readChar(); if (takeCard == 'y') { count++; player.dealCard(); System.out.print(" \tCard " + count + ": "); System.out.println(player.getCard()); } } while (takeCard == 'y' && player.getTotal() <= 21); System.out.println(); count = 1; System.out.print("Dealer,"); do { dealer.dealCard(); count++; if (count == 2) System.out.print(" \t"); else System.out.print(", "); System.out.print("Card " + count + ": "); System.out.print(dealer.getCard()); } while (dealer.getTotal() < 17 && player.getTotal()<=21); System.out.println(); System.out.println(myName + " Total: " + player.getTotal()); System.out.println("Dealer" + " Total: " + dealer.getTotal()); if (player.compareTo(dealer)==1) { System.out.println("You won!!"); myCash += myWager*PAYOFF; } else { System.out.println("You lost!!"); myCash -= myWager; } System.out.println("you now have: " + money.format(myCash)); } }// end of BlackJackGame
//BlackJackTest.java //package blackjackpkg2; //import cs1.Keyboard; public class BlackJackTest { public static void main(String[] args) { char playAgain; double cash = 1000; BlackJackGame game = new BlackJackGame("Robert", cash); System.out.println("Welcome to the Game!"); System.out.println(" Initial cash is: " + cash); do { System.out.println("******************************"); System.out.print("How much do you want to wager? "); game.setWager(Keyboard.readDouble()); game.playGame(); System.out.print("Do you want to play again? y/n "); playAgain = Keyboard.readChar(); } while (playAgain == 'y'); } }// end of BlackJackTest