Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Purpose: Simple "player-dealer" blackjack program to showcase the use of loops.
- */
- import java.util.*;
- import java.util.Scanner;
- public class BlackjackGame
- {
- private static void isWinner(int results)
- {
- switch (results) {
- case 21:
- System.out.println("Blackjack!");
- break;
- default:
- if (results >= 22) {
- System.out.println("BUST! You lose.");
- } else if (results <= 20) {
- System.out.println("UNDER! You lose.");
- } // end else if
- } // end switch
- } // end isWinner()
- private static int randomizer()
- {
- final int[] deck = new int[] {
- 1,1,1,1,
- 2,2,2,2,
- 3,3,3,3,
- 4,4,4,4,
- 5,5,5,5,
- 6,6,6,6,
- 7,7,7,7,
- 8,8,8,8,
- 9,9,9,9,
- 10,10,10,10,
- 10,10,10,10,
- 10,10,10,10,
- 10,10,10,10,
- 11,11,11,11};
- Random random = new Random();
- return deck[random.nextInt(deck.length)];
- } // end randomizer()
- private static int getSum(int a, int b)
- {
- return a + b;
- } // end getSum()
- private static void welcome()
- {
- System.out.println();
- System.out.println("**************************");
- System.out.println("Let's play Blackjack!");
- } // end welcome()
- private static String hitOrStand()
- {
- Scanner s = new Scanner(System.in);
- System.out.print("Do you want another card? ('y' to hit / 'n' to stand) ");
- String choice = s.nextLine();
- while (!(choice.equalsIgnoreCase("y") || choice.equalsIgnoreCase("n"))) {
- System.out.print("Invalid input. Please choose either Y or N only.");
- choice = s.nextLine();
- }
- return choice;
- }
- public static void main(String args[])
- {
- Scanner scan = new Scanner(System.in);
- char playAgain = 'y';
- final int NATURAL, BLACKJACK = 21;
- String choice = "";
- do {
- int firstCard = randomizer();
- int secondCard = randomizer();
- int newerCard = randomizer();
- int dealerOne = randomizer();
- int dealerTwo = randomizer();
- welcome();
- System.out.printf("\nDealer's cards: %d, %d\n",
- dealerOne, dealerTwo);
- int dealerHand = getSum(dealerOne,dealerTwo);
- switch(dealerHand)
- {
- case 21:
- System.out.println("Dealer wins!");
- continue;
- /* The dealer will continue to take cards until they have
- a total of 17 or higher.
- */
- default:
- if (dealerHand <= 16) {
- System.out.println("Dealer must take another card.");
- int newDealerCard = randomizer();
- System.out.printf("Dealer's third card: %d\n",newDealerCard);
- System.out.printf("Total: %d\n",dealerHand += newDealerCard);
- for (int i = 0; dealerHand < 17; i++) {
- System.out.printf("Another card: %d\n", newDealerCard);
- System.out.printf("Total so far: %d\n",
- dealerHand += newDealerCard);
- }
- if (dealerHand >= 17 && dealerHand < 21 || dealerHand > 21) {
- System.out.println("Dealer stands.");
- } // end inner if
- } else if (dealerHand >= 17 && dealerHand != 21 || dealerHand > 21) {
- System.out.printf("Dealer total: %d. Dealer stands..\n", dealerHand);
- } else if (dealerHand == 21) {
- System.out.printf("Dealer total: %d. Dealer wins.\n", dealerHand);
- }
- break;
- } // end switch
- System.out.printf("\nHere are your cards: %d, %d\n", firstCard,secondCard);
- int playerHand = getSum(firstCard,secondCard);
- System.out.printf("Total: %d\n", playerHand);
- switch (hitOrStand())
- {
- case "y":
- System.out.printf("Here's your third card: %d\n", newerCard);
- playerHand += newerCard;
- System.out.printf("Total: %d\n", playerHand);
- if (playerHand >= BLACKJACK) {
- isWinner(playerHand); // skips while loop below
- break;
- }
- while (hitOrStand().equalsIgnoreCase("y")) {
- int anotherPlayerCard = randomizer();
- System.out.printf("Here's another card: %d\n", anotherPlayerCard);
- playerHand += anotherPlayerCard;
- System.out.printf("Total: %d\n", playerHand);
- if (playerHand >= BLACKJACK)
- break;
- } // end while
- isWinner(playerHand);
- break;
- case "n":
- isWinner(playerHand);
- break;
- default:
- System.out.println("Invalid choice.");
- break;
- } // end switch
- System.out.println();
- System.out.print("Do you want to play again? (y/n): ");
- playAgain = scan.next().charAt(0);
- while (!(playAgain == 'y' || playAgain == 'Y' ||
- playAgain == 'n' || playAgain == 'N')) {
- System.out.print("Do you want to play again? (y/n): ");
- playAgain = scan.next().charAt(0);
- } // end while, input validation
- } while(!(playAgain == 'n'));
- } // end main
- } // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement