Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Blackjack
- // Created by Arman Shah
- import java.util.*;
- import javax.swing.JOptionPane;
- public class blackjack {
- public static void main(String[] args) {
- //Declarations
- Random r = new Random();
- //Generate Cards
- int card = r.nextInt(9) +1;
- // check for ace
- if (card == 1) {
- String ace= JOptionPane.showInputDialog ("You drew an Ace for your first card! Would you like a 1 or an 11? (Type 'one' or 'eleven')");
- if (ace.equals("one")){
- card = 1;
- } else if (ace.equals("eleven")){
- card = 11;
- } else {
- JOptionPane.showMessageDialog(null, "I don't play with cheaters.");
- System.exit(0);
- }
- }
- int card2 = r.nextInt(9) +1;
- // check for ace
- if (card2 == 1) {
- String ace1= JOptionPane.showInputDialog ("You drew an Ace for your second card! Would you like a 1 or an 11? (Type 'one' or 'eleven')");
- if (ace1.equals("one")){
- card2 = 1;
- } else if (ace1.equals("eleven")){
- card2= 11;
- } else {
- JOptionPane.showMessageDialog(null, "I don't play with cheaters.");
- System.exit(0);
- }
- }
- int dealerscore = r.nextInt(18) +3;
- // get starting total
- int stotal = card + card2;
- //Draw Two Cards
- String op1 = JOptionPane.showInputDialog ("Your cards:\n[" +card+ "]" + " " +"[" +card2+ "]" + "\nTotal: " + stotal + "\nHit or stay?");
- // ask if user wants to hit or stay
- // enter while loop (while (user choice is hit))
- // generate a new card
- // check if ace
- // add to total
- // check total
- // if total > 21, bust
- // ask if user wants to hit or stay
- while (op1.equals("hit")){
- int newcard = r.nextInt(9) +1;
- if (newcard == 1){
- 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));
- if (ace3.equals("one")){
- newcard = 1;
- } else if (ace3.equals("eleven")){
- newcard = 11;
- } else {
- JOptionPane.showMessageDialog(null, "I don't play with cheaters.");
- System.exit(0);
- }
- }
- stotal = stotal + newcard;
- if (stotal == 21) {
- JOptionPane.showMessageDialog(null, "You won with a perfect score of 21!");
- System.exit(0);
- }
- if (stotal > 21) {
- JOptionPane.showMessageDialog(null, "You busted with a score of " +stotal+ "!"+ "\nThe Dealer won with a score of " +dealerscore);
- System.exit(0);
- break;
- }
- op1 = JOptionPane.showInputDialog ("Your Total: " + stotal + "\nHit or stay?");
- }
- //dealerscore
- if ((dealerscore <= 21) && (dealerscore > stotal)) {
- JOptionPane.showMessageDialog(null, "The dealer won with a score of " +dealerscore+ "."+ "\nYou lost with a score of " +stotal);
- }else if ((dealerscore <=21) && (dealerscore == stotal)) {
- JOptionPane.showMessageDialog(null, "You tied with the dealer with a score of " +stotal+ "!");
- }
- //win result
- if ((dealerscore <=21) && (stotal>dealerscore)) {
- JOptionPane.showMessageDialog(null, "You won with a score of " +stotal+ "!"+ "\nThe Dealer lost with a score of " +dealerscore);
- }
- if (stotal>21) {
- JOptionPane.showMessageDialog(null, "You busted with a score of " +stotal+ "!"+ "\nThe Dealer won with a score of " +dealerscore);
- System.exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement