Solved

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

Posted on 2011-09-09
13
251 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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
 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:

786 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