Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help with blackjack game

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with blackjack game

    My program uses a switch for the values of my cards
    I need help making Jack, Queen, and King all equal to 10
    and NOT 11, 12, 13 as in my program

    Also, how do i make the player choose if they want Ace to equal to 1 or 11?

     /*
    An object of class card represents one of the 52 cards in a
    standard deck of playing cards. Each card has a suit and
    a value.
    */
     
     
    public class Card {
     
    public final static int SPADES = 0, // Codes for the 4 suits.
    HEARTS = 1,
    DIAMONDS = 2,
    CLUBS = 3;
     
    public final static int ACE = 1, // Codes for the non-numeric cards.
    JACK = 11, // Cards 2 through 10 have their 
    QUEEN = 12, // numerical values for their codes.
    KING = 13;
     
    private final int suit; // The suit of this card, one of the constants
    // SPADES, HEARTS, DIAMONDS, CLUBS.
     
    private final int value; // The value of this card, from 1 to 11.
     
     
    public Card(int theValue, int theSuit) {
    // Construct a card with the specified value and suit.
    // Value must be between 1 and 13. Suit must be between
    // 0 and 3. If the parameters are outside these ranges,
    // the constructed card object will be invalid.
    value = theValue;
    suit = theSuit;
    }
     
    public int getSuit() {
    // Return the int that codes for this card's suit.
    return suit;
    }
     
    public int getValue() {
    // Return the int that codes for this card's value.
    return value;
    }
     
    public String getSuitAsString() {
    // Return a String representing the card's suit.
    // (If the card's suit is invalid, "??" is returned.)
    switch ( suit ) {
    case SPADES: return "Spades";
    case HEARTS: return "Hearts";
    case DIAMONDS: return "Diamonds";
    case CLUBS: return "Clubs";
    default: return "??";
    }
    }
     
    public String getValueAsString() {
    // Return a String representing the card's value.
    // If the card's value is invalid, "??" is returned.
    switch ( value ) {
    case 1: return "Ace";
    case 2: return "2";
    case 3: return "3";
    case 4: return "4";
    case 5: return "5";
    case 6: return "6";
    case 7: return "7";
    case 8: return "8";
    case 9: return "9";
    case 10: return "10";
    case 11: return "Jack";
    case 12: return "Queen";
    case 13: return "King";
    default: return "??";
    }
    }
     
    public String toString() {
    // Return a String representation of this card, such as
    // "10 of Hearts" or "Queen of Spades".
    return getValueAsString() + " of " + getSuitAsString();
    }
     
     
    } // end class Card

    /* 
    An object of type Deck represents an ordinary deck of 52 playing cards.
    The deck can be shuffled, and cards can be dealt from the deck.
    */
     
    public class Deck {
     
    private Card[] deck; // 52 Cards are created in array that will be used for the deck
    private int cardsUsed; // How many cards have been dealt from the deck.
     
    public Deck() {
    // Create an unshuffled deck of cards.
    deck = new Card[52];
    int cardCt = 0; // How many cards have been created so far.
    for ( int suit = 0; suit <= 3; suit++ ) {
    for ( int value = 1; value <= 13; value++ ) {
    deck[cardCt] = new Card(value,suit);
    cardCt++;
    }
    }
    cardsUsed = 0;
    }
     
    public void shuffle() {
    // Put all the used cards back into the deck, and shuffle it into
    // a random order.
    for ( int i = 51; i > 0; i-- ) {
    int rand = (int)(Math.random()*(i+1));
    Card temp = deck[i];
    deck[i] = deck[rand];
    deck[rand] = temp;
    }
    cardsUsed = 0;
    }
     
    public int cardsLeft() {
    // As cards are dealt from the deck, the number of cards left
    // decreases. This function returns the number of cards that
    // are still left in the deck.
    return 52 - cardsUsed;
    }
     
    public Card dealCard() {
    // Deals one card from the deck and returns it.
    if (cardsUsed == 52)
    shuffle();
    cardsUsed++;
    return deck[cardsUsed - 1];
    }
     
    } // end class Deck

    import java.util.Scanner;
     
    public class G21
    {
        static Deck deck; 
        static int playerHand;
        static int compHand;
        static Scanner scanner;
     
        public static void main(String[] args)
        {
            scanner = new Scanner(System.in);
            deck = new Deck();
            deck.shuffle();
            boolean deal = true;
            do {
                playerHand = 0;
                compHand = 0;
                start();
                if(playersTurn())
                    dealersTurn();
                checkScores();
                System.out.println("Play again? y/n");
                if(scanner.nextLine().charAt(0) != 'y')
                    deal = false;
            } while(deal);
        }
     
        private static void start() {
            for(int j = 0; j < 2; j++) {
                Card card = deck.dealCard();
                String s = (j == 0) ? "1st" : "2nd";
                System.out.println("Your " + s + " card: " + card);
                playerHand += card.getValue();
                // check for blackjack
            }
            for(int j = 0; j < 2; j++) {
                Card card = deck.dealCard();
                String s = (j == 0) ? "1st" : "2nd";
                System.out.println("Dealer's " + s + " card: " + card);
                compHand += card.getValue();
                // check for blackjack
            }
        }
     
        private static boolean playersTurn() {
            boolean takeHit = true;
            while(takeHit) {
                System.out.println("Do you want a hit? y/n");
                if(scanner.nextLine().charAt(0) == 'y') {
                    Card card = deck.dealCard();
                    System.out.println("Your next card: " + card);
                    playerHand += card.getValue();
                    if(playerHand > 21)
                        takeHit = false;
                } else {
                    takeHit = false;
                }
            }
            return playerHand <= 21;
        }
     
        private static void dealersTurn() {
            while(compHand < 16) {
                Card card = deck.dealCard();
                System.out.println("Dealer's next card: " + card);
                compHand += card.getValue();
            }
        }
     
        private static void checkScores() {
            if(playerHand > 21)
                System.out.println("You lose with " + playerHand);
            else if(compHand > 21)
                System.out.println("Dealer loses with " + compHand);
            else if(compHand == 21)
                System.out.println("Dealer wins with " + compHand);
            else if (playerHand == 21)
                System.out.println("You win with " + playerHand);
            else {
                String result;
                if(playerHand > compHand)
                    result = "You win: " + playerHand + " to " + compHand;
                else
                    result = "Dealer wins with " + compHand + " to " + playerHand;
                System.out.println(result);
            }
        }
    }


  2. #2
    Junior Member
    Join Date
    Nov 2010
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Help with blackjack game

    for the value thing...you could just add another parameter to cardss for value. So you would have like a name a suit and a value, that would be pretty simple im thinking. and then as for the ace thing: deciding what you want it to be is a very trivial problem, if the player has an ace total the value as if its 11, then if he busts turn it into a 1 and continue. Thats atleast how i see it, because its not like, hmmm do i want this to be 1 or 11, its like well i guess it has to be a one now.

  3. The Following User Says Thank You to crazed8s For This Useful Post:

    javapenguin (December 11th, 2010)

  4. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with blackjack game

    how would i assign and call my value with the paramter without the suit? and im still confused about the Ace thing

Similar Threads

  1. Blackjack program runs excrutiatingly slow.
    By sina12345 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 10th, 2010, 04:55 AM
  2. A little help with my Blackjack code
    By Neophyte in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 3rd, 2010, 01:13 PM
  3. Creating program Blackjack - Dr.Java
    By TheUntameable in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2010, 12:54 PM
  4. Game 3x3
    By Koren3 in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 20th, 2009, 08:43 PM
  5. Job offers to program Hobo Wars
    By MooncakeZ in forum Paid Java Projects
    Replies: 7
    Last Post: September 17th, 2009, 09:41 PM

Tags for this Thread