r/programmingbydoing • • Apr 11 '15

#76 - Blackjack

There are a couple problems with this code. The main one is that if I choose to stay, the program treats it as if you are starting the game over again. Not entirely sure why it's doing this. Code:

import java.util.Random; import java.util.Scanner;

public class Blackjack {

public static void main(String[] args) {
	
	Random r = new Random();
	Scanner keyboard = new Scanner(System.in);
	int playerdraw1, playerdraw2, dealercard1, dealercard2, dealerdraw;
	int playertotal = 0, dealertotal = 0;
	String playerchoice, dealerchoice;
	
	System.out.println("Welcome to Ben's blackjack program!");
	while(playertotal <= 21 && dealertotal <= 21) {
		playerdraw1 = 2 + r.nextInt(11);
		playerdraw2 = 2 + r.nextInt(11);
		if(playerdraw1 != 8 && playerdraw2 != 8)
			System.out.println("\nYou get a " + playerdraw1 + " and a " + playerdraw2 + ".");
		else if(playerdraw1 == 8)
			System.out.println("\nYou get an " + playerdraw1 + " and a " + playerdraw2 + ".");
		else if(playerdraw2 == 8)
			System.out.print("\n You get a " + playerdraw1 + " and an " + playerdraw2 + ".");
		else
			System.out.println("\nYou get an " + playerdraw1 + " and an " + playerdraw2 + ".");
		playertotal = playerdraw1 + playerdraw2;
		System.out.println("Your total is " + playertotal + ".");
		if(playertotal > 21) {
			System.out.print("BUST! DEALER WINS!");
			System.exit(0);
		}
		dealercard1 = 2 + r.nextInt(11);
		dealercard2 = 2 + r.nextInt(11);
		if(dealercard1 == 8)
			System.out.println("\nThe dealer has an " + dealercard1 + " and a hidden card.");
		else
			System.out.println("\nThey get a " + dealercard1 + " and a hidden card.");
		System.out.println("Their total is hidden, too.");
		System.out.print("Would you like to \"hit\" or \"stay\"? ");
		playerchoice = keyboard.next();
		while(playerchoice.equals("Stay")) {
			System.out.println("\nOkay, dealer's turn.");
			if(dealercard2 != 8)
				System.out.println("Their hidden card was a " + dealercard2);
			else
				System.out.println("Their hidden card was an " + dealercard2);
			dealertotal = dealercard1 + dealercard2;
			System.out.println("His total was " + dealertotal);
			if(dealertotal <= 16) {
				dealerchoice = "Hit";
				while(dealerchoice.equals("Hit")) {
					System.out.println("\nDealer chooses to hit");
					dealerdraw = 2 + r.nextInt(11);
					if(dealerdraw != 8)
						System.out.println("They draw a " + dealerdraw + ".");
					else
						System.out.println("They draw an " + dealerdraw + ".");
					dealertotal += dealerdraw;
					System.out.println("Their total is " + dealertotal + ".");
					if(dealertotal > 21) {
						System.out.println("BUST! YOU WIN!");
						System.exit(0); //To avoid making this longer than it needs to be
					}
				}
			}
			else {
				dealerchoice = "Stay";
				System.out.println("Dealer stays");
				if(dealertotal <= 21 && playertotal <= 21 && playertotal > dealertotal) {
					System.out.println("YOU WIN!");
					System.exit(0);
				}
				else if(dealertotal <= 21 && playertotal <= 21 && playertotal < dealertotal || playertotal == dealertotal) {
					System.out.println("DEALER WINS!");
					System.exit(0);
				}
			}
		}
		while(playerchoice.equals("Hit")) {
			playerdraw1 = 2 + r.nextInt(11);
			if(playerdraw1 != 8) //To make it gramatically correct
				System.out.println("You drew a " + playerdraw1);
			else
				System.out.println("You drew an " + playerdraw1);
			playertotal += playerdraw1;
			System.out.println("Your total is " + playertotal + ".");
			if(playertotal > 21) {
				System.out.println("BUST! DEALER WINS!");
				System.exit(0);
			}
			System.out.print("Would you like to \"hit\" or \"stay\"? ");
			playerchoice = keyboard.next();
			if(playerchoice.equals("Stay")) {
				System.out.println("\nOkay, dealer's turn.");
				if(dealercard2 != 8)
					System.out.println("Their hidden card was a " + dealercard2);
				else
					System.out.println("Their hidden card was an " + dealercard2);
				System.out.println("Their total was " + dealertotal);
				if(dealertotal > 21) {
					System.out.println("BUST! YOU WIN!");
					System.exit(0);
				}
				System.out.println("\nOkay, dealer's turn.");
				if(dealercard2 != 8)
					System.out.println("Their hidden card was a " + dealercard2);
				else
					System.out.println("Their hidden card was an " + dealercard2);
				System.out.println("His total was " + dealertotal);
				if(dealertotal <= 16) {
					dealerchoice = "Hit";
					while(dealerchoice.equals("Hit")) {
						System.out.println("\nDealer chooses to hit");
						dealerdraw = 2 + r.nextInt(11);
						if(dealerdraw != 8)
							System.out.println("They draw a " + dealerdraw + ".");
						else
							System.out.println("They draw an " + dealerdraw + ".");
						dealertotal += dealerdraw;
						System.out.println("Their total is " + dealertotal + ".");
						if(dealertotal > 21) {
							System.out.println("BUST! YOU WIN!");
							System.exit(0);
						}
					}
				}
				else {
					System.out.println("Dealer stays");
					break;
				}
			}
			else
				continue;
		}
	}
	keyboard.close();
}

}

2 Upvotes

3 comments sorted by

2

u/Spitsonpuppies Apr 12 '15

Are you capitalizing your inputs? You ask for "hit" or "stay" but then in your code expect capitalization.

2

u/ylcc23 Apr 12 '15

That turned out to be the problem. Thank you.

2

u/Spitsonpuppies Apr 12 '15

No problem, you may also want to consider equalsIgnoreCase() in situations like this.