Solved

How do I add sum of all card values?

Posted on 2011-09-10
15
314 Views
Last Modified: 2013-11-23
I need to count the value of the hand, with each Ace value being 1, count the number of aces, if the number of aces is greater than or equal to 1 and the current hand value is less than 12 then increment the hand value by 10 and of course return it.

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 < Card.getPossibleRanks().length ; i++)
       {
           for (int j = 0; j < Card.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 class so far:

import java.util.ArrayList;
/**
 * Write a description of class TwoPlayerBlackJack here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class TwoPlayerBlackJack
{
    // instance variables - replace the example below with your own
    private String rank;
    private ArrayList<Card> blackJack;
    private char suit;
    /**
     * Constructor for objects of class TwoPlayerBlackJack
     */
    public TwoPlayerBlackJack()
    {
       String[] ranks = Card.getPossibleRanks();
       char[] suites = Card.getPossibleSuits();
       blackJack = new ArrayList<Card>();
  for (int i = 0; i < Card.getPossibleRanks().length ; i++)
       {
           for (int j = 0; j < Card.getPossibleSuits().length; j++)
           {
              blackJack.add(new Card(ranks, suites[j]));//
           }
       }
    }

    /**
     * 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 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;
}
}
public computeHandValue(ArrayList <Card> hand)
{
    String rank = hand.getCardValue();
    for(rank.equals("Ace") >=1)&&(getCardValue()<12)){
        n = n+10;
       
    }
    return hand;

}
0
Comment
Question by:ryanbecker24
  • 11
  • 2
  • 2
15 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36518584

this is how yo should computer:
public int computeHandValue(ArrayList <Card> hand)
{
  
int sum = 0;
int numAces = 0;

for(Card c: hand){
  int value = c.getCardValue();
   sum += value;
 if(c.getRank().equals("Ace"))numAces++;

}
 if(sum<12 && numAces >0)sum += 10;  
    }
    return sum;;

}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36518588

removed one extra parenthesiis

public int computeHandValue(ArrayList <Card> hand)
{
  
int sum = 0;
int numAces = 0;

for(Card c: hand){
  int value = c.getCardValue();
   sum += value;
 if(c.getRank().equals("Ace"))numAces++;

}
 if(sum<12 && numAces >0)sum += 10;  
   
    return sum;;

}

Open in new window

0
 

Author Comment

by:ryanbecker24
ID: 36519226
Thanks, but for some reason my compiler says it can't find getCardValue(). The rest of the code is fine.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36519230
Post your code for Card class exactly as you use it.
(when posting the code plese use the code tags - it is easier to cut and paste such code)
 
0
 

Author Comment

by:ryanbecker24
ID: 36519261
What do you mean by post your code for Card class exactly as you use it? That is my Card class and DeckOfCards class. Do you want me to the computeHand method in the Card class?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36519304
Sure post the code as it reaports you the error - I'll try to compile it myslef -
the code you posted above was different as you cahnged the computeHand  method
and may have distuvbed something in the meantime - maybe I made a mistajke in typing also - but I cannot check unless I have the full code
with the cwhole lasses
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36519308
In order to see what eeror you exeprience I need to have your code exactly as the one where compiler generates error for you
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 47

Expert Comment

by:for_yan
ID: 36519315
The best thing is to post all public classes separately like that:

public class Card {


//contentsof class  ghere 

}

Open in new window



public class DeckOfCrads {
//contetsn of class here

}

Open in new window


this is the simples way for me to curt and paste them into my IDE
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36519319
if compiler does not find soime method this is probably because you have wrong declaration format or
something wrong with the number of braces - I can't detect this error unless I have your full code and unless I can
paste it into .java files into my IDE
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36519406

try this variant:
public int computeHandValue(ArrayList <Card> hand)
{
  
int sum = 0;
int numAces = 0;

for(Card c: hand){
  int value = getCardValue(c);
   sum += value;
 if(c.getRank().equals("Ace"))numAces++;

}
 if(sum<12 && numAces >0)sum += 10;  
   
    return sum;;

}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36519440
In general, it is more logical to have the method getCardValue() (or maybe getValue()) as the method of the Card class - as it is the property of the
instance of the Card class which you are retrieving - as you have getRank() , getSuite() method - next to them you'll have
getValue()  method, as you may want to use this method not only in the class TwoPlayerBlackJack but maybe in some other class also.
As this is a property of the Card instance it is better to have such method in the Card class from the point of view of the design of the application.

0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36539316
Ryan, your only signature for getCardValue is
public int getCardValue(Card c)

Open in new window


However your messages to getCardValue are
String rank = hand.getCardValue();
    for(rank.equals("Ace") >=1)&&(getCardValue()<12)

Open in new window


The compiler can't find a signature of getCardValue that doesn't have any parameters.  Thus the error.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36539333
@hmccurdy,
This was already corrected in the code ID:36519406
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36540381
@for_yan, yes it is.  However, in my experience as a tutor, I see this problem frequently.  It is the problem I looked for first.  I find that when I'm explicit about the problem with the student that the student almost never makes the same mistake again (at least for the remainder of the term).

The reason for the post is that I want the author to learn.   (My desire to help others learn is why I volunteer to be an unpaid tutor.)
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36540399
I agree with you and that's why I mentioned to the authtor that it is makes sense to make method getValue() as the method of the card class, rather than
to have getVlaue(Card c)  in the general class TwoPlayerBlackJack  which more represents the game rather than the Card (ID:36519440).
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

707 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

12 Experts available now in Live!

Get 1:1 Help Now