Results 1 to 3 of 3

Thread: Blackjack

  1. #1
    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Question Blackjack

    Hello! I'm in an intro to CS class and our teacher is giving us pretty difficult hw. It may be simple to lots of people but as a person that has never seen programming until now, it's pretty difficult. Anyways, I have to program a simplified version of blackjack where I don't need to worry about aces being 1 or 11 (extra credit), splits, doubling down, surrenders, or blackjacks. The only knowledge we are suppose to use include arrays, loops, if statements, and everything less advanced than that. The catch is that we have to program it using 1 deck, 2 decks, and 3 decks. I did the game with 1 deck and it seems to work fine. I thought that the other 2 would be a simple copy paste, then change some variables and also extend the array from 52 to 104, and also while gathering the rank and suits of the cards by using a modulus of 26 rather than 13. Same with the 3 decks but 156, and modulus of 39 instead. Here is the code I did for the game with 1 deck. I've tried doing what I said above but it doesn't work. Am I doing it wrong, or am I forgetting something? I can't seem to find out how to do this so I would greatly appreciate some help! Thanks!

    Here's the code (and yes my teacher makes me comment):

    public static void main(String[] args) {


    // Phase 1: Choose amount of decks
    System.out.println("Welcome to blackjack!");
    System.out.println("How many decks would you like to use? (Maximum 3 decks)");
    Scanner input = new Scanner(System.in);
    int a = input.nextInt();

    if (a == 1) {

    int[] deck1;
    deck1 = new int[52];

    String[] suits = {" of Spades", " of Hearts", " of Clubs", " of Diamonds"};
    String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
    int[] cardValues = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10};

    for (int i = 0; i < deck1.length; i++) {
    deck1[i] = i;
    }
    for (int i = 0; i < deck1.length; i++) {
    int index = (int) (Math.random() * deck1.length);
    int temp = deck1[i];
    deck1[i] = deck1[index];
    deck1[index] = temp;
    }

    int[] playersHand;
    playersHand = new int[12];
    int[] dealersHand;
    dealersHand = new int[12];

    int cardIndex = 0;
    int playersHandIndex = 0;
    int dealersHandIndex = 0;
    int playersCurrentCardValue;
    String playersCurrentSuit;
    String playersCurrentRank;
    int dealersCurrentCardValue;
    String dealersCurrentSuit;
    String dealersCurrentRank;
    int totalPlayerCardValue = 0;
    int totalDealerCardValue = 0;

    // Phase 2: Setup game
    // Player draws 1st card from the deck
    playersHand[playersHandIndex] = cardIndex;
    cardIndex++;
    playersHandIndex++;


    // Get and display the value and suit and rank of the players current card
    playersCurrentSuit = suits[deck1[playersHand[playersHandIndex]] / 13];
    playersCurrentRank = ranks[deck1[playersHand[playersHandIndex]] % 13];
    playersCurrentCardValue = cardValues[deck1[playersHand[playersHandIndex]] % 13];
    System.out.println("Player has a " + playersCurrentRank + playersCurrentSuit);


    // Get the players total card value
    totalPlayerCardValue = totalPlayerCardValue + playersCurrentCardValue;

    // Player draws 2nd card from the deck
    playersHandIndex++;
    playersHand[playersHandIndex] = cardIndex;
    cardIndex++;

    // Get the values for the players current card
    playersCurrentSuit = suits[deck1[playersHand[playersHandIndex]] / 13];
    playersCurrentRank = ranks[deck1[playersHand[playersHandIndex]] % 13];
    playersCurrentCardValue = cardValues[deck1[playersHand[playersHandIndex]] % 13];
    System.out.println("Player has a " + playersCurrentRank + playersCurrentSuit);


    totalPlayerCardValue = totalPlayerCardValue + playersCurrentCardValue;

    System.out.println("Players total card value is " + totalPlayerCardValue);

    // Dealer draws card from the deck
    dealersHand[dealersHandIndex] = cardIndex;

    cardIndex++;


    // Get and display the value and suit and rank of the dealers current card
    dealersCurrentSuit = suits[deck1[dealersHand[dealersHandIndex]] / 13];
    dealersCurrentRank = ranks[deck1[dealersHand[dealersHandIndex]] % 13];
    dealersCurrentCardValue = cardValues[deck1[dealersHand[dealersHandIndex]] % 13];
    System.out.println("The dealer has a " + dealersCurrentRank + dealersCurrentSuit);


    // Get the dealers total card value
    totalDealerCardValue = totalDealerCardValue + dealersCurrentCardValue;

    // Dealer draws his 2nd card
    dealersHandIndex++;
    dealersHand[dealersHandIndex] = cardIndex;
    cardIndex++;

    // Get the values for the dealers current card
    dealersCurrentSuit = suits[deck1[dealersHand[dealersHandIndex]] / 13];
    dealersCurrentRank = ranks[deck1[dealersHand[dealersHandIndex]] % 13];
    dealersCurrentCardValue = cardValues[deck1[dealersHand[dealersHandIndex]] % 13];
    System.out.println("The dealer has a " + dealersCurrentRank + dealersCurrentSuit);
    dealersHandIndex++;

    totalDealerCardValue = totalDealerCardValue + dealersCurrentCardValue;
    System.out.println("The dealer's total card value is " + totalDealerCardValue);

    //Phase 3: Begin actual game
    while (true) {

    System.out.println("Would you like to hit [1] or stand [2]?");
    int hitStand = input.nextInt();
    if (hitStand == 1) {
    playersHand[playersHandIndex] = cardIndex;
    cardIndex++;

    playersCurrentSuit = suits[deck1[playersHand[playersHandIndex]] / 13];
    playersCurrentRank = ranks[deck1[playersHand[playersHandIndex]] % 13];
    playersCurrentCardValue = cardValues[deck1[playersHand[playersHandIndex]] % 13];

    System.out.println("Player has a " + playersCurrentRank + playersCurrentSuit);

    totalPlayerCardValue = totalPlayerCardValue + playersCurrentCardValue;

    System.out.println("Players total card value is " + totalPlayerCardValue);

    if (totalPlayerCardValue > 21) {
    System.out.println("You bust!");
    System.exit(0);
    }
    }


    if (hitStand == 2) {
    System.out.println("Your final card value is: " + totalPlayerCardValue);

    while (true) {
    dealersHand[dealersHandIndex] = cardIndex;
    cardIndex++;

    dealersCurrentSuit = suits[deck1[dealersHand[dealersHandIndex]] / 13];
    dealersCurrentRank = ranks[deck1[dealersHand[dealersHandIndex]] % 13];
    dealersCurrentCardValue = cardValues[deck1[dealersHand[dealersHandIndex]] % 13];
    System.out.println("The dealer has a " + dealersCurrentRank + dealersCurrentSuit);

    totalDealerCardValue = totalDealerCardValue + dealersCurrentCardValue;

    System.out.println("Dealer's total card value is " + totalDealerCardValue);

    if (totalDealerCardValue >= 17) {
    break;
    }

    }
    break;
    }

    }
    if (totalDealerCardValue > 21) {
    System.out.println("The dealer bust! You win!");
    } else if (totalPlayerCardValue > totalDealerCardValue) {
    System.out.println("You win!");
    } else if (totalPlayerCardValue == totalDealerCardValue) {
    System.out.println("It's a push!");
    } else {
    System.out.println("You lose!");
    }


    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,419
    Rep Power
    20

    Default Re: Blackjack

    Quote Originally Posted by jediassassin37 View Post
    Hello! I'm in an intro to CS class and our teacher is giving us pretty difficult hw. It may be simple to lots of people but as a person that has never seen programming until now, it's pretty difficult. Anyways, I have to program a simplified version of blackjack where I don't need to worry about aces being 1 or 11 (extra credit), splits, doubling down, surrenders, or blackjacks. The only knowledge we are suppose to use include arrays, loops, if statements, and everything less advanced than that. The catch is that we have to program it using 1 deck, 2 decks, and 3 decks. I did the game with 1 deck and it seems to work fine. I thought that the other 2 would be a simple copy paste, then change some variables and also extend the array from 52 to 104, and also while gathering the rank and suits of the cards by using a modulus of 26 rather than 13. Same with the 3 decks but 156, and modulus of 39 instead. Here is the code I did for the game with 1 deck. I've tried doing what I said above but it doesn't work. Am I doing it wrong, or am I forgetting something? I can't seem to find out how to do this so I would greatly appreciate some help! Thanks!

    Here's the code (and yes my teacher makes me comment):

    public static void main(String[] args) {


    // Phase 1: Choose amount of decks
    System.out.println("Welcome to blackjack!");
    System.out.println("How many decks would you like to use? (Maximum 3 decks)");
    Scanner input = new Scanner(System.in);
    int a = input.nextInt();

    if (a == 1) {

    int[] deck1;
    deck1 = new int[52];

    String[] suits = {" of Spades", " of Hearts", " of Clubs", " of Diamonds"};
    String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
    int[] cardValues = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10};

    for (int i = 0; i < deck1.length; i++) {
    deck1[i] = i;
    }
    for (int i = 0; i < deck1.length; i++) {
    int index = (int) (Math.random() * deck1.length);
    int temp = deck1[i];
    deck1[i] = deck1[index];
    deck1[index] = temp;
    }

    int[] playersHand;
    playersHand = new int[12];
    int[] dealersHand;
    dealersHand = new int[12];

    int cardIndex = 0;
    int playersHandIndex = 0;
    int dealersHandIndex = 0;
    int playersCurrentCardValue;
    String playersCurrentSuit;
    String playersCurrentRank;
    int dealersCurrentCardValue;
    String dealersCurrentSuit;
    String dealersCurrentRank;
    int totalPlayerCardValue = 0;
    int totalDealerCardValue = 0;

    // Phase 2: Setup game
    // Player draws 1st card from the deck
    playersHand[playersHandIndex] = cardIndex;
    cardIndex++;
    playersHandIndex++;


    // Get and display the value and suit and rank of the players current card
    playersCurrentSuit = suits[deck1[playersHand[playersHandIndex]] / 13];
    playersCurrentRank = ranks[deck1[playersHand[playersHandIndex]] % 13];
    playersCurrentCardValue = cardValues[deck1[playersHand[playersHandIndex]] % 13];
    System.out.println("Player has a " + playersCurrentRank + playersCurrentSuit);


    // Get the players total card value
    totalPlayerCardValue = totalPlayerCardValue + playersCurrentCardValue;

    // Player draws 2nd card from the deck
    playersHandIndex++;
    playersHand[playersHandIndex] = cardIndex;
    cardIndex++;

    // Get the values for the players current card
    playersCurrentSuit = suits[deck1[playersHand[playersHandIndex]] / 13];
    playersCurrentRank = ranks[deck1[playersHand[playersHandIndex]] % 13];
    playersCurrentCardValue = cardValues[deck1[playersHand[playersHandIndex]] % 13];
    System.out.println("Player has a " + playersCurrentRank + playersCurrentSuit);


    totalPlayerCardValue = totalPlayerCardValue + playersCurrentCardValue;

    System.out.println("Players total card value is " + totalPlayerCardValue);

    // Dealer draws card from the deck
    dealersHand[dealersHandIndex] = cardIndex;

    cardIndex++;


    // Get and display the value and suit and rank of the dealers current card
    dealersCurrentSuit = suits[deck1[dealersHand[dealersHandIndex]] / 13];
    dealersCurrentRank = ranks[deck1[dealersHand[dealersHandIndex]] % 13];
    dealersCurrentCardValue = cardValues[deck1[dealersHand[dealersHandIndex]] % 13];
    System.out.println("The dealer has a " + dealersCurrentRank + dealersCurrentSuit);


    // Get the dealers total card value
    totalDealerCardValue = totalDealerCardValue + dealersCurrentCardValue;

    // Dealer draws his 2nd card
    dealersHandIndex++;
    dealersHand[dealersHandIndex] = cardIndex;
    cardIndex++;

    // Get the values for the dealers current card
    dealersCurrentSuit = suits[deck1[dealersHand[dealersHandIndex]] / 13];
    dealersCurrentRank = ranks[deck1[dealersHand[dealersHandIndex]] % 13];
    dealersCurrentCardValue = cardValues[deck1[dealersHand[dealersHandIndex]] % 13];
    System.out.println("The dealer has a " + dealersCurrentRank + dealersCurrentSuit);
    dealersHandIndex++;

    totalDealerCardValue = totalDealerCardValue + dealersCurrentCardValue;
    System.out.println("The dealer's total card value is " + totalDealerCardValue);

    //Phase 3: Begin actual game
    while (true) {

    System.out.println("Would you like to hit [1] or stand [2]?");
    int hitStand = input.nextInt();
    if (hitStand == 1) {
    playersHand[playersHandIndex] = cardIndex;
    cardIndex++;

    playersCurrentSuit = suits[deck1[playersHand[playersHandIndex]] / 13];
    playersCurrentRank = ranks[deck1[playersHand[playersHandIndex]] % 13];
    playersCurrentCardValue = cardValues[deck1[playersHand[playersHandIndex]] % 13];

    System.out.println("Player has a " + playersCurrentRank + playersCurrentSuit);

    totalPlayerCardValue = totalPlayerCardValue + playersCurrentCardValue;

    System.out.println("Players total card value is " + totalPlayerCardValue);

    if (totalPlayerCardValue > 21) {
    System.out.println("You bust!");
    System.exit(0);
    }
    }


    if (hitStand == 2) {
    System.out.println("Your final card value is: " + totalPlayerCardValue);

    while (true) {
    dealersHand[dealersHandIndex] = cardIndex;
    cardIndex++;

    dealersCurrentSuit = suits[deck1[dealersHand[dealersHandIndex]] / 13];
    dealersCurrentRank = ranks[deck1[dealersHand[dealersHandIndex]] % 13];
    dealersCurrentCardValue = cardValues[deck1[dealersHand[dealersHandIndex]] % 13];
    System.out.println("The dealer has a " + dealersCurrentRank + dealersCurrentSuit);

    totalDealerCardValue = totalDealerCardValue + dealersCurrentCardValue;

    System.out.println("Dealer's total card value is " + totalDealerCardValue);

    if (totalDealerCardValue >= 17) {
    break;
    }

    }
    break;
    }

    }
    if (totalDealerCardValue > 21) {
    System.out.println("The dealer bust! You win!");
    } else if (totalPlayerCardValue > totalDealerCardValue) {
    System.out.println("You win!");
    } else if (totalPlayerCardValue == totalDealerCardValue) {
    System.out.println("It's a push!");
    } else {
    System.out.println("You lose!");
    }


    }
    Guide For New Members
    BB Code List - Java Programming Forum

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Blackjack

    Please ask specific questions. I don't see that you posted any errors, and I don't know what doesn't work. Also, please read the links Darryl posted. :)

Similar Threads

  1. Need help with BlackJack Program
    By SilverSoul in forum New To Java
    Replies: 3
    Last Post: 04-25-2012, 01:52 PM
  2. Designing a blackjack game
    By sonofJack3 in forum New To Java
    Replies: 1
    Last Post: 01-15-2012, 07:32 PM
  3. Need help...blackjack not working.
    By g2mediagroup in forum New To Java
    Replies: 5
    Last Post: 05-03-2011, 06:40 AM
  4. Help With BlackJack Game !
    By TangoMango in forum New To Java
    Replies: 1
    Last Post: 01-24-2011, 05:40 PM
  5. blackjack help
    By jordaejava in forum New To Java
    Replies: 1
    Last Post: 12-11-2009, 05:44 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •