Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.96 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class Game {
  5.     public static void main(String[] args) throws InterruptedException {
  6.  
  7.         Scanner scanner = new Scanner(System.in);
  8.         int result = 0, rounds = 0, wins = 0, losses = 0, ties = 0;
  9.         String play = "";
  10.  
  11.         while (true) {
  12.             Thread.sleep(2000);
  13.             System.out.println("\n==================Blackjack===================");
  14.             System.out.print("[P]lay round or [Q]uit? ");
  15.             play = scanner.next().toUpperCase();
  16.             System.out.println("==============================================");
  17.  
  18.             if (play.equals("P")) {
  19.                 rounds += 1;
  20.                 result = playGame();    //runs the playGame method which returns a result
  21.                 if (result == 1) wins += 1;
  22.                 if (result == 2) losses += 1;
  23.                 if (result == 3) ties += 1;
  24.             }
  25.             else if (play.equals("Q")) break;
  26.             else System.out.println("\nInvalid input");
  27.         }
  28.  
  29.         System.out.println("\nYou played " + rounds + " rounds");
  30.         System.out.print("Wins: " + wins + "\nTies: " + ties + "\nDealer wins: " + losses + "\n\n");
  31.  
  32.         scanner.close();
  33.     }
  34.  
  35.     public static int playGame() throws InterruptedException {
  36.         Scanner scanner = new Scanner(System.in);
  37.         Deck deck = new Deck();
  38.         deck.shuffle();
  39.  
  40.         BlackjackHand player = new BlackjackHand();
  41.         BlackjackHand dealer = new BlackjackHand();
  42.  
  43.         String choice = "";
  44.        
  45.         //start of player turn
  46.         System.out.println("\n-------------Players Turn------------");
  47.         Thread.sleep(2000);
  48.         player.add(deck.deal());
  49.         System.out.print(player.toString() + "\nTotal: " + player.value() + "\n\n");
  50.         player.add(deck.deal());
  51.         Thread.sleep(2000);    //deals the first two cards for the user
  52.  
  53.         //loop to allow the user to deal cards or stand (checks for turn ending conditions)
  54.         while(!player.isBust() && !player.isNatural() && player.value() != 21) {
  55.             System.out.print(player.toString() + "\nTotal: " + player.value() + "\n\n");  
  56.             System.out.printf("[H]it or [S]tand? ");
  57.             choice = scanner.next().toUpperCase();
  58.             System.out.print("\n");
  59.             if (choice.equals("H")) player.add(deck.deal());
  60.             else if (choice.equals("S")) {
  61.                 break;
  62.             }
  63.             else System.out.println("Invalid input\n");
  64.         }
  65.  
  66.         System.out.print(player.toString() + "\nTotal: " + player.value() + "\n");
  67.  
  68.         if (player.isBust()) {
  69.             Thread.sleep(2000);
  70.             System.out.print("\nBust.\n");
  71.             System.out.println("-------------------------------------\n");
  72.             Thread.sleep(2000);
  73.             System.out.println("Dealer wins.");
  74.             return 2;   // dealer wins if player goes bust
  75.         }        
  76.         else if (player.isNatural()) {
  77.             Thread.sleep(2000);
  78.             System.out.println("\nNatural.");
  79.         }
  80.         else if (player.value() == 21) {
  81.             Thread.sleep(2000);
  82.             System.out.println("\nBlackjack.");
  83.         }
  84.         System.out.println("-------------------------------------");
  85.         //end of player turn
  86.  
  87.         Thread.sleep(2000);
  88.  
  89.         //start of dealers turn
  90.         System.out.print("\n-------------Dealers Turn------------");
  91.         while (!dealer.isBust() && dealer.value() < 17 && !dealer.isNatural() && dealer.value() <= player.value()) {
  92.             Thread.sleep(2000);
  93.             dealer.add(deck.deal());
  94.             System.out.print("\n" + dealer.toString() + "\nTotal: " + dealer.value() + "\n");
  95.         }        
  96.         Thread.sleep(2000);      
  97.         if (dealer.isNatural()) System.out.println("\nNatural.");
  98.         else if (dealer.value() == 21) System.out.println("\nBlackjack.");
  99.         else if (dealer.isBust()) System.out.println("\nBust.");
  100.         System.out.println("-------------------------------------\n");
  101.         //end of dealers turn
  102.  
  103.         Thread.sleep(2000);
  104.         if (player.isNatural()) {
  105.             if (dealer.isNatural()) {
  106.                 System.out.println("Tie.");
  107.                 return 3;   //both natural: tie
  108.             }
  109.         }
  110.  
  111.         if (dealer.isBust()) {
  112.             System.out.println("Player wins.");
  113.             return 1;    //dealer bust: win
  114.         }
  115.         else {
  116.             if (player.value() > dealer.value()) {
  117.                 System.out.println("Player wins.");
  118.                 return 1;   //player score higher than dealer score: win
  119.             }
  120.             if (player.value() < dealer.value()) {
  121.                 System.out.println("Dealer wins.");
  122.                 return 2;    //player score lower than dealer score: loss
  123.             }
  124.             else {
  125.                 System.out.println("Tie.");
  126.                 return 3;    //scores are the same: tie
  127.             }
  128.         }
  129.        
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement