/* Lesley Hogan * June 30th 2006 * Blackjack.java * Play a modified version of blackjack * CS 101 HW J4: * http://www.cs.virginia.edu/~asb/teaching/cs101-spring06/hws/hwj4/index.html */ package net.bunnie.uva.cs101; import java.util.*; public class Blackjack { public static void main (String[] args) { System.out.println("Welcome to the UVA Casino Blackjack table."); //declarations Vector deck = new Vector(); int i; int dealerPoints; int playerPoints; String displayCard; boolean keepDealing; boolean keepPlaying; YesNoExtractor e = new YesNoExtractor(); do { //create and shuffle deck System.out.println(); dealerPoints = 0; playerPoints = 0; keepDealing = true; for(i = 0; i < 52; i++) { deck.add(new Card(i + 1)); } Collections.shuffle(deck); playerPoints = drawCard(deck, playerPoints, "You", false); //System.out.println(playerPoints); Card hiddenCard = deck.remove(0); dealerPoints = hiddenCard.getBlackjackValue(); System.out.println("Dealt to Dealer: hidden card"); playerPoints = drawCard(deck, playerPoints, "You", false); dealerPoints = drawCard(deck, dealerPoints, "Dealer", false); System.out.println("\nYou have " + playerPoints + " right now."); while ((playerPoints < 21) && (keepDealing == true)) { //System.out.println("Loop: " + playerPoints + ", " + keepDealing); if (e.askUser("Would you like another card?")) { playerPoints = drawCard(deck, playerPoints, "You", true); } else { keepDealing = false; } } System.out.println("No more cards for you! Your total: " + playerPoints); //dealer automatically wins on bust, so this is before dealer loop if (playerPoints > 21) { System.out.println("\nYou busted. The dealer wins."); } else { //dealer loop displayCard = hiddenCard.toString(); System.out.println("\nDealer's hidden card: " + displayCard); while (dealerPoints < 17) { dealerPoints = drawCard(deck, dealerPoints, "Dealer", true); } System.out.println("Dealer is done with getting more cards at " + dealerPoints + ".\n"); //game result if (dealerPoints > 21) { System.out.println("Dealer busted. You win!"); } else if (dealerPoints == playerPoints) { if (dealerPoints == 21) { System.out.print("You both got 21! "); } System.out.println("You tied."); } else if (dealerPoints > playerPoints) { if (dealerPoints == 21) { System.out.print("Dealer got a 21. "); } System.out.println("Dealer wins."); } else { if (playerPoints == 21) { System.out.print("You got 21! "); } System.out.println("You win!!"); } } } while (e.askUser("Would you like to play again?")); } static int drawCard(Vector deck, int points, String who, boolean display) { Card cardDrawn = deck.remove(0); points += cardDrawn.getBlackjackValue(); String displayCard = cardDrawn.toString(); System.out.println("Dealt to " + who + ": " + displayCard); if (display == true) { System.out.println("Total for " + who + ": " + points); } return points; } }