Solved

How do I sum all numbers in a BlackJack game?

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

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

Title # Comments Views Activity
Exchange and Third Party application. 8 60
hash value 2 39
Eclipse Neon and jdk 1.8.0 11 72
Java DateChooser? 3 16
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…

744 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

13 Experts available now in Live!

Get 1:1 Help Now