Solved

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

Posted on 2011-09-09
13
249 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
Comment Utility

 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
Comment Utility
I made Ace 11, but you can easily change it to 1
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Whats the loop for? Also, is there anyway I could use getRank() from the Card class to write this method?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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
Comment Utility
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
Comment Utility


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
Comment Utility
method such as Integer(String s)
0
 
LVL 47

Expert Comment

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

Expert Comment

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

Expert Comment

by:for_yan
Comment Utility

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

743 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

17 Experts available now in Live!

Get 1:1 Help Now