Advertisement
ArmanS17

Blackjack in Java

Jul 24th, 2014
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.31 KB | None | 0 0
  1. // Blackjack
  2. // Created by Arman Shah
  3.  
  4. import java.util.*;
  5. import javax.swing.JOptionPane;
  6. public class blackjack {
  7.  
  8.     public static void main(String[] args) {
  9.  
  10.  
  11.         //Declarations
  12.         Random r = new Random();
  13.  
  14.  
  15.         //Generate Cards
  16.         int card = r.nextInt(9) +1;
  17.  
  18.         // check for ace
  19.  
  20.         if (card == 1) {
  21.             String ace= JOptionPane.showInputDialog ("You drew an Ace for your first card! Would you like a 1 or an 11? (Type 'one' or 'eleven')");
  22.             if (ace.equals("one")){
  23.                 card = 1;
  24.             } else if (ace.equals("eleven")){
  25.                 card = 11;
  26.             } else {
  27.                 JOptionPane.showMessageDialog(null,  "I don't play with cheaters.");
  28.                 System.exit(0);
  29.             }
  30.  
  31.         }
  32.  
  33.         int card2 = r.nextInt(9) +1;
  34.         // check for ace
  35.  
  36.         if (card2 == 1) {
  37.             String ace1= JOptionPane.showInputDialog ("You drew an Ace for your second card! Would you like a 1 or an 11? (Type 'one' or 'eleven')");
  38.             if (ace1.equals("one")){
  39.                 card2 = 1;
  40.             } else if (ace1.equals("eleven")){
  41.                 card2= 11;
  42.             } else {
  43.                 JOptionPane.showMessageDialog(null,  "I don't play with cheaters.");
  44.                 System.exit(0);
  45.             }
  46.  
  47.         }
  48.         int dealerscore = r.nextInt(18) +3;
  49.  
  50.  
  51.         // get starting total
  52.         int stotal = card + card2;
  53.  
  54.  
  55.         //Draw Two Cards
  56.         String op1 = JOptionPane.showInputDialog ("Your cards:\n[" +card+ "]" +  " " +"[" +card2+ "]" + "\nTotal: " + stotal + "\nHit or stay?");
  57.  
  58.         // ask if user wants to hit or stay
  59.         // enter while loop (while (user choice is hit))
  60.         //      generate a new card
  61.         //      check if ace
  62.         //      add to total
  63.         //      check total
  64.         //          if total > 21, bust
  65.         //      ask if user wants to hit or stay
  66.  
  67.  
  68.         while (op1.equals("hit")){
  69.  
  70.             int newcard = r.nextInt(9) +1;
  71.  
  72.             if (newcard == 1){
  73.                 String ace3= JOptionPane.showInputDialog ("You drew an Ace! Would you like a 1 or an 11? (Type 'one' or 'eleven')" + "\nTotal with 1: " +(stotal+1)+ "\nTotal with 11: " +(stotal+11));
  74.                 if (ace3.equals("one")){
  75.                     newcard = 1;
  76.                 } else if (ace3.equals("eleven")){
  77.                     newcard = 11;
  78.                 } else {
  79.                     JOptionPane.showMessageDialog(null,  "I don't play with cheaters.");
  80.                     System.exit(0);
  81.                 }
  82.             }
  83.  
  84.             stotal = stotal + newcard;
  85.             if (stotal == 21) {
  86.                 JOptionPane.showMessageDialog(null,  "You won with a perfect score of 21!");
  87.                 System.exit(0);
  88.             }
  89.  
  90.             if (stotal > 21) {
  91.                 JOptionPane.showMessageDialog(null,  "You busted with a score of " +stotal+ "!"+ "\nThe Dealer won with a score of " +dealerscore);
  92.                 System.exit(0);
  93.  
  94.                 break;
  95.             }
  96.  
  97.             op1 = JOptionPane.showInputDialog ("Your Total: " + stotal + "\nHit or stay?");
  98.         }
  99.  
  100.  
  101.         //dealerscore
  102.  
  103.  
  104.         if ((dealerscore <= 21) && (dealerscore > stotal)) {
  105.             JOptionPane.showMessageDialog(null,  "The dealer won with a score of " +dealerscore+ "."+ "\nYou lost with a score of " +stotal);
  106.         }else if ((dealerscore <=21) && (dealerscore == stotal)) {
  107.             JOptionPane.showMessageDialog(null,  "You tied with the dealer with a score of " +stotal+ "!");
  108.         }
  109.  
  110.         //win result
  111.         if ((dealerscore <=21) && (stotal>dealerscore)) {
  112.             JOptionPane.showMessageDialog(null,  "You won with a score of " +stotal+ "!"+ "\nThe Dealer lost with a score of " +dealerscore);
  113.         }
  114.         if (stotal>21) {
  115.             JOptionPane.showMessageDialog(null,  "You busted with a score of " +stotal+ "!"+ "\nThe Dealer won with a score of " +dealerscore);
  116.             System.exit(0);
  117.  
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement