?
Solved

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

Posted on 2011-09-09
13
Medium Priority
?
256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month8 days, 7 hours left to enroll

765 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