Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By JosAH

Thread: Designing a blackjack game

  1. #1
    sonofJack3 is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Designing a blackjack game

    Hey all, I'm designing a game of blackjack in Java. The way I have to have it set up is with a Card class and a DeckOfCards class. I have designed both of these. The problem I'm having is displaying each card in the deck. I want it set up so that the toString method of the Card class displays a card in the form "value - suit". Value is an int and suit is a string, and are parameters entered in a new Card's constructor. The DeckOfCards class contains a method, create, which makes an array of 52 cards, and uses a for loop nested with if-else statements to create each card with its respective value and suit. Here is the code for the for loop:

    for (int count = 0; count < 52; count++)
    {
    if (count < 13)
    {
    if (count == 0)
    deck[count] = new Card(A, "Hearts");
    else if (count > 0 && count < 9)
    deck[count] = new Card(count + 1, "Hearts");
    else
    deck[count] = new Card(10, "Hearts");

    }
    else if (count < 26)
    {
    if (count == 13)
    deck[count] = new Card(A, "Diamonds");
    else if (count > 13 && count < 22)
    deck[count] = new Card(count % 12, "Diamonds");
    else
    deck[count] = new Card(10, "Diamonds");
    }
    else if (count < 39)
    {
    if (count == 26)
    deck[count] = new Card(A, "Spades");
    else if (count > 26 && count < 35)
    deck[count] = new Card(count % 25, "Spades");
    else
    deck[count] = new Card(10, "Spades");
    }
    else if (count < 52)
    {
    if (count == 39)
    deck[count] = new Card(A, "Clubs");
    else if (count > 39 && count < 48)
    deck[count] = new Card(count % 38, "Clubs");
    else
    deck[count] = new Card(10, "Clubs");
    }
    }

    The problem is, Jack, Queen and King all have a value of 10, but they need to be displayed as different cards (I have the above loop set up so that there are simply 3 cards with a value of 10 created (excluding the numerical 10)). I can't tell the toString method of the Card class to check whether the card's value is 10 and display J, Q, or K for this reason. I also can't reference the deck in the Card class and reference these problem cards by their array indices. Any ideas?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: Designing a blackjack game

    This is an ideal problem for enums; google for "Java enum" and you'll find a fine tutorial that explains enums and gives a card desk example using enums.

    kind regards,

    Jos
    Fubarable likes this.
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Need help...blackjack not working.
    By g2mediagroup in forum New To Java
    Replies: 5
    Last Post: 05-03-2011, 06:40 AM
  2. Creating a game of blackjack
    By adjit in forum New To Java
    Replies: 3
    Last Post: 04-29-2011, 02:14 PM
  3. Help With BlackJack Game !
    By TangoMango in forum New To Java
    Replies: 1
    Last Post: 01-24-2011, 06:40 PM
  4. blackjack help
    By jordaejava in forum New To Java
    Replies: 1
    Last Post: 12-11-2009, 06:44 AM
  5. Need ideas and help for a simple BlackJack Game
    By Dannii in forum New To Java
    Replies: 5
    Last Post: 04-27-2009, 10:53 AM

Posting Permissions

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