[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How do I sum all numbers in a BlackJack game?

Posted on 2011-09-10
3
Medium Priority
?
179 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

656 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