Solved

How do I sum all numbers in a BlackJack game?

Posted on 2011-09-10
3
175 Views
Last Modified: 2012-05-12
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;
}
}

Open in new window


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

} 

Open in new window


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;

} 

Open in new window

0
Comment
Question by:ryanbecker24
  • 2
3 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 36518563
Here are some thoughts to help:

Line 26: DeckOfCards.java
deck.add(new Card(ranks, suites[j]));//
should be:
deck.add(new Card(ranks[j], suites[j]));//

Line 26: TwoPlayerBlackJack.java
 blackJack.add(new Card(ranks, suites[j]));//
should be:
 blackJack.add(new Card(ranks[j], suites[j]));//

That should help you with some of the compilation issues. If you are having logical or run-time problems beyond that, please be specific and we will help guide you through getting this going.

Kevin
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 36518582
Okay, this is also not correct:
public computeHandValue(ArrayList <Card> hand)
{
    String rank = hand.getCardValue();
    for(rank.equals("Ace") >=1)&&(getCardValue()<12)){
        n = n+10;
        
    }
    return hand;

} 

Open in new window


Some issues:
- no return type for the method; you are currently returning hand which is ArrayList<Card>, but doubt that is your true intention.
- n is not defined, i.e., you don't declare int n anywhere in code.
- I would not use a for loop to do a simple if condition check
- enum might come in handy, but will take this a little at a time.
- you defined getCardValue(Card c), but are calling getCardValue() < 12 here.
- String rank = hand.getCardValue(); < incorrect, also. Card has a getRank() function, hand is an ArrayList<Card> and not a Card.
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
V.simple question about Android packages and libraries 1 38
jboss 7.1 start up error 1 64
How to configure empty element in XML Document parser? 15 45
jsp error 6 49
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
This video teaches viewers about errors in exception handling.

730 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