All pastes #470159 Raw Edit

Untitled

public text v1 · immutable
·published 2007-05-04 03:13 UTC
rendered paste body
import java.text.*;
import java.util.*;
// import blackjack.*;

/**
 * This is the main class for the Blackjack Game.
 *
 * @author Bill Kukowski
 */
public class BlackjackMain
{
	/**
	 * Main method. Controls program I/O
	 *
	 * @param args
	 */
		Account account;
		boolean userDumb, sameHand;
		Deck deck;
		DecimalFormat df;
		Rules rules;
		Hand playerHand, dealerHand;
	
	public static void main(String[] args)
	{
		BlackjackMain bjm = new BlackjackMain();
	}
	
	BlackjackMain()
	{
		sameHand = true;
		
		//create deck
		df = new DecimalFormat("0.00"); //used for cost display
		rules = new Rules();
			
		deck = new Deck();
		deck.shuffle();
		account = new Account();
			
			while (sameHand && account.getBalance() > 0) //hand loop
			{
				round();
			}
	}

	public void round() {
		playerHand = new Hand();
		dealerHand = new Hand();
		userDumb = true;

		playerHand.addCardToHand(deck.drawCard());
		playerHand.addCardToHand(deck.drawCard());

		dealerHand.addCardToHand(deck.drawCard());
		dealerHand.addCardToHand(deck.drawCard());


		Scanner scanner = new Scanner(System.in);

		double amount = 0;
		
		//getbet
		while (userDumb) 
		{
			System.out.println("You have $" + df.format(account.getBalance()) + " in your account.");

			System.out.print("How much do you want to bet? ");
			amount = 0;

			amount = scanner.nextDouble();
	
			if (amount <= account.getBalance()) 
			{
				account.withdraw(amount);
				account.addToBet(amount);
				userDumb = false;
			}
			else {System.out.println("You are stupid. Bet less money!" );
			}
	
		}

		System.out.println("You have these cards:");
		playerHand.printCards();

		System.out.println("The dealer has a " + deck.getCardName(dealerHand.getCard(1)) + " showing.");

		if (rules.blackjack(playerHand)) //check blackjack!
		{
			System.out.println("You have Blackjack!");
			account.deposit(amount*2);			
		}
		
		if (rules.doubleDownEnable(playerHand))//check double down
		{
			System.out.println("Press 1 to double down.  Press 0 if you don't want to.");
			if (scanner.nextInt() == 1) 
			{
				rules.doubleDown(account, amount, playerHand, deck);
				System.out.println("You now have these cards.");
				playerHand.printCards();
			}
		}
		
		while (playerHand.getHandTotal() < 21) //enable hit while player has less than 21
		{
			System.out.println("Press 1 to hit, 2 to stand.");
			if (scanner.nextInt() == 1) 
			{
				playerHand.addCardToHand(deck.drawCard());
				System.out.println("You now have these cards.");
				playerHand.printCards();
			}
			if (playerHand.getHandTotal() > 21) {
				System.out.println("bust!");
				sameHand = false;
			}
		}
	}
}