Advertisement
binibiningtinamoran

BlackjackGame

Jul 12th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.08 KB | None | 0 0
  1. /*
  2.  Purpose: Simple "player-dealer" blackjack program to showcase the use of loops.
  3. */
  4.  
  5. import java.util.*;
  6. import java.util.Scanner;
  7.  
  8. public class BlackjackGame
  9. {
  10.  
  11.     private static void isWinner(int results)
  12.     {
  13.         switch (results) {
  14.             case 21:
  15.                 System.out.println("Blackjack!");
  16.                 break;
  17.             default:
  18.                 if (results >= 22) {
  19.                     System.out.println("BUST! You lose.");
  20.                 } else if (results <= 20) {
  21.                     System.out.println("UNDER! You lose.");
  22.                 } // end else if
  23.         } // end switch
  24.     } // end isWinner()
  25.  
  26.     private static int randomizer()
  27.     {
  28.         final int[] deck = new int[] {
  29.                 1,1,1,1,
  30.                 2,2,2,2,
  31.                 3,3,3,3,
  32.                 4,4,4,4,
  33.                 5,5,5,5,
  34.                 6,6,6,6,
  35.                 7,7,7,7,
  36.                 8,8,8,8,
  37.                 9,9,9,9,
  38.                 10,10,10,10,
  39.                 10,10,10,10,
  40.                 10,10,10,10,
  41.                 10,10,10,10,
  42.                 11,11,11,11};
  43.  
  44.         Random random = new Random();
  45.         return deck[random.nextInt(deck.length)];
  46.     } // end randomizer()
  47.  
  48.     private static int getSum(int a, int b)
  49.     {
  50.         return a + b;
  51.     } // end getSum()
  52.  
  53.     private static void welcome()
  54.     {
  55.         System.out.println();
  56.         System.out.println("**************************");
  57.         System.out.println("Let's play Blackjack!");
  58.     } // end welcome()
  59.  
  60.     private static String hitOrStand()
  61.     {
  62.         Scanner s = new Scanner(System.in);
  63.         System.out.print("Do you want another card? ('y' to hit / 'n' to stand) ");
  64.         String choice = s.nextLine();
  65.         while (!(choice.equalsIgnoreCase("y") || choice.equalsIgnoreCase("n"))) {
  66.             System.out.print("Invalid input. Please choose either Y or N only.");
  67.             choice = s.nextLine();
  68.         }
  69.         return choice;
  70.     }
  71.  
  72.     public static void main(String args[])
  73.     {
  74.         Scanner scan = new Scanner(System.in);
  75.         char playAgain = 'y';
  76.         final int NATURAL, BLACKJACK = 21;
  77.         String choice = "";
  78.  
  79.         do {
  80.             int firstCard = randomizer();
  81.             int secondCard = randomizer();
  82.             int newerCard = randomizer();
  83.             int dealerOne = randomizer();
  84.             int dealerTwo = randomizer();
  85.  
  86.             welcome();
  87.             System.out.printf("\nDealer's cards: %d, %d\n",
  88.                     dealerOne, dealerTwo);
  89.             int dealerHand = getSum(dealerOne,dealerTwo);
  90.  
  91.             switch(dealerHand)
  92.             {
  93.                 case 21:
  94.                     System.out.println("Dealer wins!");
  95.                     continue;
  96.  
  97.                 /* The dealer will continue to take cards until they have
  98.                    a total of 17 or higher.
  99.                 */
  100.                 default:
  101.                     if (dealerHand <= 16) {
  102.                         System.out.println("Dealer must take another card.");
  103.                         int newDealerCard = randomizer();
  104.                         System.out.printf("Dealer's third card: %d\n",newDealerCard);
  105.                         System.out.printf("Total: %d\n",dealerHand += newDealerCard);
  106.  
  107.                         for (int i = 0; dealerHand < 17; i++) {
  108.                             System.out.printf("Another card: %d\n", newDealerCard);
  109.                             System.out.printf("Total so far: %d\n",
  110.                                     dealerHand += newDealerCard);
  111.                         }
  112.                         if (dealerHand >= 17 && dealerHand < 21 || dealerHand > 21) {
  113.                             System.out.println("Dealer stands.");
  114.                         } // end inner if
  115.                     } else if (dealerHand >= 17 && dealerHand != 21 || dealerHand > 21) {
  116.                         System.out.printf("Dealer total: %d. Dealer stands..\n", dealerHand);
  117.                     } else if (dealerHand == 21) {
  118.                         System.out.printf("Dealer total: %d. Dealer wins.\n", dealerHand);
  119.                     }
  120.                     break;
  121.             } // end switch
  122.  
  123.             System.out.printf("\nHere are your cards: %d, %d\n", firstCard,secondCard);
  124.             int playerHand = getSum(firstCard,secondCard);
  125.             System.out.printf("Total: %d\n", playerHand);
  126.             switch (hitOrStand())
  127.             {
  128.                 case "y":
  129.                     System.out.printf("Here's your third card: %d\n", newerCard);
  130.                     playerHand += newerCard;
  131.                     System.out.printf("Total: %d\n", playerHand);
  132.                     if (playerHand >= BLACKJACK) {
  133.                         isWinner(playerHand); // skips while loop below
  134.                         break;
  135.                     }
  136.  
  137.                     while (hitOrStand().equalsIgnoreCase("y")) {
  138.                         int anotherPlayerCard = randomizer();
  139.                         System.out.printf("Here's another card: %d\n", anotherPlayerCard);
  140.                         playerHand += anotherPlayerCard;
  141.                         System.out.printf("Total: %d\n", playerHand);
  142.                         if (playerHand >= BLACKJACK)
  143.                             break;
  144.                     } // end while
  145.  
  146.                     isWinner(playerHand);
  147.                     break;
  148.                 case "n":
  149.                     isWinner(playerHand);
  150.                     break;
  151.                 default:
  152.                     System.out.println("Invalid choice.");
  153.                     break;
  154.             } // end switch
  155.             System.out.println();
  156.             System.out.print("Do you want to play again? (y/n): ");
  157.             playAgain = scan.next().charAt(0);
  158.             while (!(playAgain == 'y' || playAgain == 'Y' ||
  159.                     playAgain == 'n' || playAgain == 'N')) {
  160.                 System.out.print("Do you want to play again? (y/n): ");
  161.                 playAgain = scan.next().charAt(0);
  162.             } // end while, input validation
  163.         } while(!(playAgain == 'n'));
  164.     } // end main
  165. } // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement