Solved

How do I get a card value in a two player black jack game?

Posted on 2011-09-09
13
250 Views
Last Modified: 2012-05-12
I need help  writing a public int getCardValue(Card c). I have to use getRank() from class card. If rank is Jack, Queen, King return 10, if Ace return 1. I also need to convert the Strings two-ten to a integer value.

Card class:

public class Card
{
   
    private String rank;//card rank: a number between 2 and 10, or Jack, Queen, King or Ace
    private char suit;//card suit: S,C,H,or D
    private static String[] Ranks={"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"};
    private static char[] Suites={'C','H','D','S'};
     
    /**
     * Constructor for objects of class Card
     */
    public Card()
    {
        rank="2";
        suit='H';
       
    }
   
    public Card(String rank, char suit)
    {
        this.rank=rank;
        this.suit=suit;
    }
     public String getRank()
    {
        return rank;
    }
   
    public char getSuit()
    {
        return suit;
    }
   
    public void setRank(String rank)
    {
        this.rank=rank;
    }
   
    public void setSuit(char suit)
    {
        this.suit=suit;
    }
    //Returns an array of possible card ranks
    public static String[] getPossibleRanks()
    {
        return Ranks;
    }
    //Returns an array of possible card suits
    public static char[] getPossibleSuits()
 {
        return Suites;
    }
   
    public String toString()
    {String suitName = "";
        switch (suit){
        case 'H':
         suitName = rank + " of Hearts";
            break;
         case 'S':
            suitName = rank + " of Spades";
             break;
              case 'D':
                suitName = rank + " of Diamonds";
                  break;
                  case 'C':
                   suitName = rank + " of Clubs";


    }
return suitName;
}
}

DeckOfCards class:

import java.util.Random;
import java.util.ArrayList;
public class DeckOfCards
{
    // instance variables - replace the example below with your own
    private ArrayList<Card> deck;
    Random getRandom = new Random();
    /**
     * Constructor for objects of class DeckOfCards
     */
    public DeckOfCards()
    {
        deck = new ArrayList<Card>();
        resetDeckOfCards();
    }

    public void resetDeckOfCards()
    {
       String[] ranks = Card.getPossibleRanks();
       char[] suites = Card.getPossibleSuits();

  for (int i = 0; i < getPossibleRanks().length ; i++)
       {
           for (int j = 0; j < getPossibleSuits().length; j++)
           {
              deck.add(new Card(ranks, suites[j]));//
           }
       }
    }


    public Card getCard()//remove random card of arraylist of cards and return value from method
    {
       if (deck.isEmpty())
            resetDeckOfCards();
       return deck.remove(getRandom.nextInt(deck.size()));
    }

    public ArrayList<Card> getHand(int size)
    {
        ArrayList<Card> hand = new ArrayList<Card>();
        for (int i = 0; i < size(); i++)
        {
            hand.add(this.getCard());
        }
        return hand;
    }

}

TwoPlayerBlackJack so far:

public class TwoPlayerBlackJack
{
    // instance variables - replace the example below with your own
    private String rank;

    /**
     * Constructor for objects of class TwoPlayerBlackJack
     */
    public TwoPlayerBlackJack()
    {
       TwoPlayerBlackJack game=new TwoPlayerBlackJack();
    }

    /**
     * An example of a method - replace this comment with your own
     *
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y
     */
    public int getCardValue(Card c)
    {
        String card = Card.getRank();
        if(suit.Card.getRank()="Jack,Queen,King")
        {
            return 10;
        if(rank.Card.getRank()="Ace")
        {
            return 1;
        }
    }
    int i= Integer.parseInt("two", "three", "four", "five", "six", "seven", "eight", "nine", "ten");
    System.out.println("i ": + i);
    return i;
    }
   
     
}

0
Comment
Question by:ryanbecker24
  • 9
  • 4
13 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36513302

 private static int[] values={2,3,4,5,6,7,8,9,10,10,10,10,11};


HashMap<String, Integer> rankValues = new rankValues<String,Integer>();

for(int i=0; i<ranks.length; i++){
rankValues.put(Ranks[i],values[i]);

} 


and then int vlaue = rankVlaues.get(rank);

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36513310
I made Ace 11, but you can easily change it to 1
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36513321
you can of couse make a longer method like:

if(rank.equals("2")) return 2;
else if rank.equals("3")) return 3;
else if ...

and so on

just a little bit of typing but strightforawrd andthere is also a value in tha either
0
 

Author Comment

by:ryanbecker24
ID: 36513332
What part are you talking about? And what is that for? I also want help on the TwoPlayerBlackJackGame
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36513348
Well, let's do things stepwise

with HashMap

you add the theird array right next to Rank and Sutites arrays

then you can declare static HashMap in class Card
and in constructor you camn populate HashMap


and then in your method getVlaue()

you'll use this Hashmap
0
 

Author Comment

by:ryanbecker24
ID: 36513367
Whats the loop for? Also, is there anyway I could use getRank() from the Card class to write this method?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 47

Expert Comment

by:for_yan
ID: 36513407
I guess this way it should work (only Card class is modifyed):

(of cousre in the getCardVlaue() method
you can use getRank() isnated of simpley rank
but does not make much semnse

import java.util.HashMap;




public class Card
{

    private String rank;//card rank: a number between 2 and 10, or Jack, Queen, King or Ace
    private char suit;//card suit: S,C,H,or D
    private static String[] Ranks={"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"};
    private static char[] Suites={'C','H','D','S'};
    private static int[] values={2,3,4,5,6,7,8,9,10,10,10,10,11};
    private static HashMap<String, Integer> rankValues;


    static  {
        for(int i=0; i<Ranks.length; i++){
        rankValues.put(Ranks[i],values[i]);

        }


    }
    /**
     * Constructor for objects of class Card
     */
    public Card()
    {
        rank="2";
        suit='H';

    }

    public Card(String rank, char suit)
    {
        this.rank=rank;
        this.suit=suit;
        
    }
     public String getRank()
    {
        return rank;
    }

    public char getSuit()
    {
        return suit;
    }

    public void setRank(String rank)
    {
        this.rank=rank;
    }

    public void setSuit(char suit)
    {
        this.suit=suit;
    }
    //Returns an array of possible card ranks
    public static String[] getPossibleRanks()
    {
        return Ranks;
    }
    //Returns an array of possible card suits
    public static char[] getPossibleSuits()
 {
        return Suites;
    }

    public int getCardValue(){
        return rankValues.get(rank);
    }

    public String toString()
    {String suitName = "";
        switch (suit){
        case 'H':
         suitName = rank + " of Hearts";
            break;
         case 'S':
            suitName = rank + " of Spades";
             break;
              case 'D':
                suitName = rank + " of Diamonds";
                  break;
                  case 'C':
                   suitName = rank + " of Clubs";


    }
return suitName;
}
}

Open in new window

0
 

Author Comment

by:ryanbecker24
ID: 36513414
The problem is that I need to use the method getRank() from Class Card and I need to use a method to convert two-ten to integer values as 2,3,4,5,6,7,8,9,10.
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36513444


Ok, there you go:
public int getCardValue(Card c){
String rank = c.getRank();

int value = -1;
try {
value = new Integer(rank);
}catch(Exception ex){
if(rank.equals("Ace"))value = 1;
else value = 10;

}
return value;
}

Open in new window








}
0
 

Author Comment

by:ryanbecker24
ID: 36513473
method such as Integer(String s)
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36513481
look at the code above - it uses costructor Integer(String)
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36513483
equivalent method is Integer.parseInt(String)  - this will also work in place of new Integer(String)
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36513690

this of course cannot make sense:
   int i= Integer.parseInt("two", "three", "four", "five", "six", "seven", "eight", "nine", "ten");

one thing is that

int i = Integer.parseInt("2"); will work

but

int i = Integer.parseInt("two"); will not - luckily you don';t need to do anything like that


besides Integer.parseInt("...") takes one String as an argument not multiple strings


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 79
jar file executable 12 43
windows explorer path to command prompt 5 33
MySQL  on Tomcat 8 30
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now