Solved

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

Posted on 2011-09-09
13
252 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

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

Suggested Solutions

Title # Comments Views Activity
servlet example issue 6 46
tomcat administrtor 12 61
Configure a Bean in an XML file 4 42
JavaFX TableView not displaying correctly 3 57
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

837 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