Page 1 of 2 12 LastLast
Results 1 to 20 of 35
  1. #1
    Kirstielol is offline Member
    Join Date
    Mar 2012
    Posts
    28
    Rep Power
    0

    Question Help setting values to cards in an array

    I'm making a Java program that plays blackjack. I'm having trouble setting values to the cards. I have my logic worked out, like how to determine if ace should be 1 or 11, I'm just completely stuck on how to get values onto them. This is what I have so far:

    Java Code:
    public class DeckOfCards {
    
        String faces[] = {"Ace", "Deuce", "Three", "Four", "Five", "Six",
            "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
        String suits[] = {"Hearts", "Diamonds", "Clubs", "Spades"};
        private Card deck[]; // array of Card objects
        private int currentCard; // the index of next Card to be dealt
        private final int NUMBER_OF_CARDS = 52; // constant number of cards
        private Random randomNumbers; // random number generator
        private int value; //value of the card
    
        // constructor fills deck of cards
        public DeckOfCards() {
            deck = new Card[NUMBER_OF_CARDS]; // create array of Card objects
            currentCard = 0; // initialize currentCard 
            randomNumbers = new Random(); // create random number generator
    
            // populate deck with Card objects
            for (int count = 0; count < deck.length; count++) {
                deck[ count] =
                        new Card(faces[ count % 13], suits[ count / 13]);
            }
        } // end DeckOfCards constructor
    
        // shuffle deck of cards with one-pass algorithm
        public void shuffle() {
            currentCard = 0; // reinitialize currentCard
    
            // for each card, pick another random card and swap them
            for (int first = 0; first < deck.length; first++) {
                int second = randomNumbers.nextInt(NUMBER_OF_CARDS);
                Card temp = deck[ first];
                deck[ first] = deck[ second];
                deck[ second] = temp;
            } // end for
        } // end method shuffle
    
        // deal one card
        public Card dealCard() {
            // determine whether cards remain to be dealt
            if (currentCard < deck.length) {
                return deck[ currentCard++]; // return current Card in array
            } else {
                return null; // return null to indicate that all cards were dealt
            }
        } // end method dealCard
    
        public int getHandValue(Card hand[]) {
            int HandTotal;
            boolean ace;
    
            HandTotal = 0;
            ace = false;
    
            for (int i = 0; i < hand.length; i++) {
                int cardValue;
                cardValue = faces[];
                if (cardValue > 10) {
                    cardValue = 10; //sets Jack, Queen and King to 10
                }
                if (cardValue == 1) {
                    ace = true;
                }
                HandTotal = HandTotal + cardValue;
            }
            if (ace == true && HandTotal + 10 <= 21) {
                HandTotal = HandTotal + 10;
            }
            return HandTotal;
        }
    }
    I'm stuck in my getHandValue() method. The part that is coming up with an error is:

    Java Code:
                cardValue = faces[];
    Honestly, I just guessed at this part. I have tried a few things but I can't get it working. Any suggestions or help is appreciated, thanks in advance :)
    Last edited by Kirstielol; 03-19-2012 at 08:47 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

    Default Re: Help setting values to cards in an array

    Can you explain what statement at line 57 is supposed to do?
    The variable on the left is an int. What is the value on the right supposed to be? If you want an element from an array you need an index value inside the []s

    Could the Card class hold a card's value?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Kirstielol is offline Member
    Join Date
    Mar 2012
    Posts
    28
    Rep Power
    0

    Default Re: Help setting values to cards in an array

    Quote Originally Posted by Norm View Post
    Can you explain what statement at line 57 is supposed to do?
    The variable on the left is an int. What is the value on the right supposed to be? If you want an element from an array you need an index value inside the []s
    I wanted to assign cardValue to the position of the face[] array (plus 1 because the array starts at 0). So Ace would be 1, Deuces would be 2, and so on. So then I could add up the total of the players cards for blackjack. I realize what I wrote is completely wrong, I'm just really stuck at this one part.

    Could the Card class hold a card's value?
    No, this is a homework assignment. In the instructions it says to edit the DeckOfCards class and the Main class. It doesn't say to touch the Card class, which was given to us.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

    Default Re: Help setting values to cards in an array

    What info is available in the Card class that will help you get the value of the card?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Kirstielol is offline Member
    Join Date
    Mar 2012
    Posts
    28
    Rep Power
    0

    Default Re: Help setting values to cards in an array

    Nothing as far as I can tell, this is the Card class.

    Java Code:
    public class Card 
    {
       private String face; // face of card
       private String suit; // suit of card
    
       // two-argument constructor initializes card's face and suit
       public Card( String cardFace, String cardSuit )
       {
          face = cardFace; // initialize face of card
          suit = cardSuit; // initialize suit of card
       } // end two-argument Card constructor
    
       // return card face
       public String getFace()
       {
          return face;
       } // end method getFace
    
       // return card suit
       public String getSuit()
       {
          return suit;
       } // end method getSuit
    
       // return String representation of Card
       public String toString() 
       { 
          return face + " of " + suit;
       } // end method toString
    } // end class Card

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

    Default Re: Help setting values to cards in an array

    There are several ways to get a value for a String. One is a loop to lookup in the array of names and use the index to compute the value. Another would be a hashtable with key = name and value = the cards value.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Kirstielol is offline Member
    Join Date
    Mar 2012
    Posts
    28
    Rep Power
    0

    Default Re: Help setting values to cards in an array

    Quote Originally Posted by Norm View Post
    There are several ways to get a value for a String. One is a loop to lookup in the array of names and use the index to compute the value. Another would be a hashtable with key = name and value = the cards value.
    Should I create another method that returns the value of the card? I think that would be easier than putting it all in this one method. Also, could you show me what the loop would look like? I can't figure out how to set it up.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

    Default Re: Help setting values to cards in an array

    create another method that returns the value of the card
    Yes that sounds good.

    a for loop that indexes through the faces array and compares the element in the array: faces[ix] with the Card's face value.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Kirstielol is offline Member
    Join Date
    Mar 2012
    Posts
    28
    Rep Power
    0

    Default Re: Help setting values to cards in an array

    Okay, so here's what I got so far:

    Java Code:
    public int getCardValue(Card hand[]) {
            int val;
            val = 0;
            for (int i = 0; i > faces.length; i++) {
                if (faces[i] == Card.hand[]) {
                    val = i;
                    break;
                }
            }
            value = val;
            return value;
        }
        
        
        
        public void getHandValue(Card hand[]) {
            int HandTotal;
            boolean ace;
    
            HandTotal = 0;
            ace = false;
    
            for (int i = 0; i < hand.length; i++) {
                int cardValue;
                cardValue = value;
                
                if (cardValue > 10) {
                    cardValue = 10; //sets Jack, Queen and King to 10
                }
                if (cardValue == 1) {
                    ace = true;
                }
                HandTotal = HandTotal + cardValue;
            }
            if (ace == true && HandTotal + 10 <= 21) {
                HandTotal = HandTotal + 10;
            }
            System.out.printf("Hand total: %d", HandTotal);
        }
    } // end class DeckOfCards
    Line 5 has an error, I'm not sure how to exactly write in how to check what face the hand card is. How would I do this? Also, am I doing this right?

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

    Default Re: Help setting values to cards in an array

    Please post the full text of any error messages.

    When comparing Strings you should use the equals() method, not the == operator.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    Kirstielol is offline Member
    Join Date
    Mar 2012
    Posts
    28
    Rep Power
    0

    Default Re: Help setting values to cards in an array

    Quote Originally Posted by Norm View Post
    Please post the full text of any error messages.

    When comparing Strings you should use the equals() method, not the == operator.
    Okay, now I have:

    Java Code:
    public int getCardValue(Card hand[]) {
            int val;
            val = 0;
            for (int i = 0; i > faces.length; i++) {
                if (faces[i].equals(hand)) {
                    val = i;
                    break;
                }
            }
            value = val + 1; //adds 1 since the array starts at 0
            return value;
        }
    The error code is on line 5 again, but now it says ".equals() on incompatible types. .equals called on array".

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

    Default Re: Help setting values to cards in an array

    What is the argument passed to the getCardValue() method? I would think it would be an instance of a Card object, not an array???
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    Kirstielol is offline Member
    Join Date
    Mar 2012
    Posts
    28
    Rep Power
    0

    Default Re: Help setting values to cards in an array

    Quote Originally Posted by Norm View Post
    What is the argument passed to the getCardValue() method? I would think it would be an instance of a Card object, not an array???
    Okay now I'm totally lost. Do I create a card object in getHandValue()? How do I assign each card in the hand to the objects?

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

    Default Re: Help setting values to cards in an array

    Look at line 20 in your code in post#1. That is where you define the Card objects.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    Kirstielol is offline Member
    Join Date
    Mar 2012
    Posts
    28
    Rep Power
    0

    Default Re: Help setting values to cards in an array

    Okay, I tried this, but it still has an error over the .equals() method. The error says "error on incompatible types". When I run the program, it tells me that the total is 0.

    Java Code:
    public int getCardValue(Card card) {
            int val;
            val = 0;
            for (int i = 0; i > faces.length; i++) {
                if (faces[i].equals(card)) {
                    val = i;
                    break;
                }
            }
            value = val + 1; //adds 1 since the array starts at 0
            return value;
        }
        
        
        
        public void getHandValue(Card hand[]) {
            int HandTotal;
            boolean ace;
    
            HandTotal = 0;
            ace = false;
    
            for (int i = 0; i < hand.length; i++) {
                Card card;
                card = hand[i];
                int cardValue;
                cardValue = value;
                
                if (cardValue > 10) {
                    cardValue = 10; //sets Jack, Queen and King to 10
                }
                if (cardValue == 1) {
                    ace = true;
                }
                HandTotal = HandTotal + cardValue;
            }
            if (ace == true && HandTotal + 10 <= 21) {
                HandTotal = HandTotal + 10;
            }
            System.out.printf("Hand total: %d", HandTotal);
        }

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

    Default Re: Help setting values to cards in an array

    What do you want to compare to the contents of the face[] array? What member in the Card class? How do you get a reference to that member?
    Look at the definition of the Card class and find the method to call that gets what you want to compare to the contents of faces.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    Kirstielol is offline Member
    Join Date
    Mar 2012
    Posts
    28
    Rep Power
    0

    Default Re: Help setting values to cards in an array

    Quote Originally Posted by Norm View Post
    What do you want to compare to the contents of the face[] array? What member in the Card class? How do you get a reference to that member?
    Look at the definition of the Card class and find the method to call that gets what you want to compare to the contents of faces.
    I want to compare it to the face of the card. I'm not sure how to do that though. I tried:

    Java Code:
                Card card;
                card = hand[Card.face];
    But it said "face has a private access in Card. Then I tried:

    Java Code:
                Card card;
                card = hand[Card.getFace()];
    But that said "non-static method getFace() cannot be referenced from a static context". What do you put in there?

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

    Default Re: Help setting values to cards in an array

    What public methods does the Card class have? Which one do you want to use?
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    Kirstielol is offline Member
    Join Date
    Mar 2012
    Posts
    28
    Rep Power
    0

    Default Re: Help setting values to cards in an array

    Quote Originally Posted by Norm View Post
    What public methods does the Card class have? Which one do you want to use?
    It has getFace(), which returns the face of the card, but when I tried that one it said I couldn't reference a non-static method.

  20. #20
    Kirstielol is offline Member
    Join Date
    Mar 2012
    Posts
    28
    Rep Power
    0

    Default Re: Help setting values to cards in an array

    Oh! I have this now, which isn't showing any errors, but when I run it, it still comes out as 0. What's wrong with it? :/

    Java Code:
    public int getCardValue(Card card) {
            int val;
            val = 0;
            for (int i = 0; i > faces.length; i++) {
                if (faces[i].equals(card.getFace())) {
                    val = i;
                    break;
                }
            }
            value = val + 1; //adds 1 since the array starts at 0
            return value;
        }
        
        
        
        public void getHandValue(Card hand[]) {
            int HandTotal;
            boolean ace;
    
            HandTotal = 0;
            ace = false;
    
            for (int i = 0; i < hand.length; i++) {
                Card card;
                card = hand[i];
                int cardValue;
                cardValue = value;
                
                if (cardValue > 10) {
                    cardValue = 10; //sets Jack, Queen and King to 10
                }
                if (cardValue == 1) {
                    ace = true;
                }
                HandTotal = HandTotal + cardValue;
            }
            if (ace == true && HandTotal + 10 <= 21) {
                HandTotal = HandTotal + 10;
            }
            System.out.printf("Hand total: %d", HandTotal);
        }

Page 1 of 2 12 LastLast

Similar Threads

  1. Issue with setting values in an array
    By pyat77 in forum New To Java
    Replies: 4
    Last Post: 11-04-2011, 02:01 AM
  2. Replies: 14
    Last Post: 01-22-2011, 02:36 PM
  3. Setting up a String Array
    By AJArmstron@aol.com in forum New To Java
    Replies: 12
    Last Post: 04-16-2010, 12:42 PM
  4. setting and getting array
    By jgonzalez14 in forum New To Java
    Replies: 6
    Last Post: 11-26-2008, 06:54 AM
  5. setting format for FLoat values
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-16-2007, 01:22 PM

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
  •