Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I write a method to win in blackjack?

Posted on 2011-09-13
9
Medium Priority
?
567 Views
Last Modified: 2012-08-14
Here are my Card, DeckOfCards, and TwoPlayerBlackJack classes:

TwoPlayerBlackJack class:

import java.util.ArrayList;
/**
 * Write a description of class TwoPlayerBlackJack here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class TwoPlayerBlackJack
{
   
    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]));
           }
       }
    }

    /**
     * The method takes an object of class Card and return an integer that represents the value of that card.
     * The values of the cards are determines as follows:
     * two is 2, three is 3, ...., nine is 9, ten is 10,  jacks, queens and kings are all 10.
     * Ace could be either 1 or 11 but for this method we will assume that the value Ace is always 1
     */
    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;
}

/**
 * Given a hand cards (i.e. ArrayList of Cards) return an integer value that corresponds to the hand value
 * (i.e. sum of all card values).
 */
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;

}

/**
 * Method display hand value display the value of each card followed by the hand value
 */
public void printHandValue(ArrayList <Card> hand)
{
    System.out.println(computeHandValue(hand));
}

/**
 * Method that takes two Array Lists of Cards (i.e. two card hands) and determines the winner
 * of the BlackJack game.
 * 1 – the first hand won the game
 * 0 – nobody won, it is a push
 * -1 – the first hand lost the game
 */
public int isWinner(ArrayList <Card> hand01, ArrayList <Card> hand02)
{

if(computeHandValue(hand01)==computeHandValue(hand02)){
return 0;

if(computeHandValue(hand01)>computeHandValue(hand02)&&(computeHandValue(hand01)<=21))
return 1;

if(computeHandValue(hand01)<computeHandValue(hand02)&&(computeHandValue(hand02)<=21))
return 1;

if((computeHandValue(hand01)&&computeHandValue(hand02))>21)
return 0;

if((computeHandValue(hand01) <= 21) && (computeHandValue(hand02) >= 21))
return -1;

if((computeHandValue(hand02) <= 21) && (computeHandValue(hand01) >= 21))
return -1;
}
}

DeckOfCards:

import java.util.Random;
import java.util.ArrayList;
public class DeckOfCards
{
    private ArrayList<Card> deck;
    Random getRandom = new Random();
   
    /**
     * Default constructor creates an Array List of Cards and populates it with 52 different cards
     */
    public DeckOfCards()
    {
        deck = new ArrayList<Card>();
        resetDeckOfCards();
    }

    /**
     * Remove all leftover cards from the ArrayList of Cards called deck
     * Populate an empty deck with 52 new cards...
     */
    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]));
           }
       }
    }

    /**
     * Remove random card of arraylist of cards and return value from method
     */
    public Card getCard()
    {
       if (deck.isEmpty())
            resetDeckOfCards();
       return deck.remove(getRandom.nextInt(deck.size()));
    }

    /**
     * Return an ArrayList of specified size (i.e. returned cards are being removed from the deck).
     */
    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;
       
    }

}

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'};
     
    /**
     * Default constructor: sets card rank to 2 and card suit to H
     */
    public Card()
    {
        rank="2";
        suit='H';
       
    }
   
    /**
     * Class constructor: sets card rank and suit to provided values.
     */
    public Card(String rank, char suit)
    {
        this.rank=rank;
        this.suit=suit;
    }
   
    /**
     * Gets the rank
     */
    public String getRank()
    {
        return rank;
    }
   
    /**
     * Gets the suit
     */
    public char getSuit()
    {
        return suit;
    }
   
    /**
     * Sets the rank
     */
    public void setRank(String rank)
    {
        this.rank=rank;
    }
   
    /**
     * Sets the suit
     */
    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;
    }
   
    /**
     * Returns string representation of the card that looks as follows:
     * 2 of Hearts, 3 of Spades, Jack of Diamonds, etc.
     */
    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;
}
}
0
Comment
Question by:ryanbecker24
  • 5
  • 3
9 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36533392
So waht do you want to get in response to this question ?
0
 

Author Comment

by:ryanbecker24
ID: 36533408
How to write a isWinner of my TwoPlayerBlackJack class using the ArrayList parameters and return the winner, loser, and tie (1,-1,0, respectively)
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36533410
But you have such method there?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:ryanbecker24
ID: 36533418
I don't think this is right so thats why I want help

/**
 * Method that takes two Array Lists of Cards (i.e. two card hands) and determines the winner
 * of the BlackJack game.
 * 1 – the first hand won the game
 * 0 – nobody won, it is a push
 * -1 – the first hand lost the game
 */
public int isWinner(ArrayList <Card> hand01, ArrayList <Card> hand02)
{

if(computeHandValue(hand01)==computeHandValue(hand02)){
return 0;

if(computeHandValue(hand01)>computeHandValue(hand02)&&(computeHandValue(hand01)<=21))
return 1;

if(computeHandValue(hand01)<computeHandValue(hand02)&&(computeHandValue(hand02)<=21))
return 1;

if((computeHandValue(hand01)&&computeHandValue(hand02))>21)
return 0;

if((computeHandValue(hand01) <= 21) && (computeHandValue(hand02) >= 21))
return -1;

if((computeHandValue(hand02) <= 21) && (computeHandValue(hand01) >= 21))
return -1;
}
}

0
 
LVL 24

Expert Comment

by:yo_bee
ID: 36533426
Why is no one using the code window?
0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 36533429

I don't think your method is right eirther

Assuming that compueHadnValue is right
(it is tricky to treat Ace, - I'm not sure this part is correct, but anyway);
I think this code should be better (maybe not - I'm not specialits in Blacvkjack):
public int isWinner(ArrayList <Card> hand01, ArrayList <Card> hand02)
{

if(computeHandValue(hand01) > 21) return -1;

if(computeHandValue(hand02)  > 21) return 1;

if(computeHandValue(hand01) > computeHandValue(hand02)) return 1;
else  if(computeHandValue(hand01) == computeHandValue(hand02)) return 0;
else return -1;
} 

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36533435
Actually in real game there can never be the situation when both > 21
in this method it is possible - so it is not defined who will be the winner in this case.
The above method  assumes that hand01 will lose in this case
0
 

Author Closing Comment

by:ryanbecker24
ID: 36533453
Thank you!
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36533455
Actually if hand01 is the player and hand02 is the dealer then I guess, as soon as hand01 becomes > 21
then player loses no matter what the dealer will get - so in this sense the above method is right
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
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.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

926 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