Solved

How do I sum all numbers in a BlackJack game?

Posted on 2011-09-10
3
171 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 59

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 59

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

895 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

17 Experts available now in Live!

Get 1:1 Help Now