Here is the entirety of my Hand class below:
The question that I have is that when I run the program, it gives me output like:
"Welcome to blackjack...dealing 2 cards:
Here are your cards: [AH, 3S]
10 , (h)it or (s)tand? "
An Ace of Hearts and a 3 of spades should not give me 10. I do not know what's wrong with my code. My getSoftTotal()
method looks like it should work fine (I haven't bothered with my getHardTotal()
method yet.
Here's another output:
"Here are your cards: [4D, 10D]
25 , (h)it or (s)tand? "
import java.util.ArrayList;
import java.util.Scanner;
public class Hand extends Deck {
private static ArrayList<Card> cards;
public Hand() {
cards = new ArrayList<Card>();
}
public void addCard(Card other) {
cards.add(other);
}
public static boolean hasBlackjack() {
boolean ace = true;
boolean ten = true;
for (int i = 0; i < cards.size(); i++) {
if (!(cards.get(i).getValue() == Card.ACE))
ace = false;
if (!(cards.get(i).getValue() == 10))
ten = false;
}
return (ace && ten);
}
public static boolean isBusted() {
return getTotal() > 21;
}
public static int getSoftTotal() {
int total = 0;
for (Card card : cards) {
total += card.getValue();
}
return total;
}
public static int getTotal() {
return getSoftTotal(); // soft
}
@SuppressWarnings("static-access")
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Deck deck; // A deck of cards. A new deck for each game.
String hit = "";
Hand hand = new Hand();
deck = new Deck();
deck.shuffle();
System.out.println("Welcome to blackjack...dealing 2 cards:");
System.out.println("");
hand.addCard(deck.deal());
hand.addCard(deck.deal());
do {
System.out.println("Here are your cards: " + cards.toString());
hand.addCard(deck.deal());
System.out.println(hand.getTotal() + " , (h)it or (s)tand? ");
System.out.println("");
hit = input.nextLine();
} while (hit.equals("h") && isBusted() == false
&& hasBlackjack() == false);
if (isBusted() == true) {
System.out.println("BUSTED! YOU LOSE!");
}
if (hasBlackjack() == true) {
System.out.println("BLACKJACK! YOU WIN!");
}
if (hit.equals("s") && isBusted() == false) {
System.out.println("You ended with " + getTotal());
}
}
}
Card.java
public class Card {
public final static int CLUBS = 0;
public final static int HEARTS = 1;
public final static int SPADES = 2;
public final static int DIAMONDS = 3;
// Numbers for the values
public final static int ACE = 1;
public final static int JACK = 11;
public final static int QUEEN = 12;
public final static int KING = 13;
/**
* This card's suit, one of the constants SPADES, HEARTS, DIAMONDS, CLUBS,
* or JOKER. The suit cannot be changed after the card is constructed.
*/
private final int suit;
/**
* The card's value. For a normal cards, this is one of the values 1 through
* 13, with 1 representing ACE. For a JOKER, the value can be anything. The
* value cannot be changed after the card is constructed.
*/
private final int value;
/**
* Creates a card with a specified suit and value.
*
* @param theValue
* the value of the new card. For a regular card (non-joker), the
* value must be in the range 1 through 13, with 1 representing
* an Ace. You can use the constants Card.ACE, Card.JACK,
* Card.QUEEN, and Card.KING. For a Joker, the value can be
* anything.
* @param theSuit
* the suit of the new card. This must be one of the values
* Card.SPADES, Card.HEARTS, Card.DIAMONDS, Card.CLUBS, or
* Card.JOKER.
* @throws IllegalArgumentException
* if the parameter values are not in the Permissible ranges
*/
public Card(int cardValue, int cardSuit) {
if (cardSuit > 3 || cardSuit < 0)
throw new IllegalArgumentException("Illegal card suit:" + cardSuit);
if (cardValue > 13 || cardValue < 1)
throw new IllegalArgumentException("Illegal card value:"
+ cardValue);
value = cardValue;
suit = cardSuit;
}
public int getSuit() {
return suit;
}
public int getValue() {
return value;
}
public String getSuitString() {
switch (suit) {
case CLUBS:
return "C";
case HEARTS:
return "H";
case SPADES:
return "S";
case DIAMONDS:
return "D";
default:
return "";
}
}
/**
* Returns a String representation of the card's value.
*
* @return for a regular card, one of the strings "Ace", "2", "3", ...,
* "10", "Jack", "Queen", or "King". For a Joker, the string is
* always numerical.
*/
public String getValueString() {
switch (value) {
case 1:
return "A";
case 2:
return "2";
case 3:
return "3";
case 4:
return "4";
case 5:
return "5";
case 6:
return "6";
case 7:
return "7";
case 8:
return "8";
case 9:
return "9";
case 10:
return "10";
case 11:
return "J";
case 12:
return "Q";
case 13:
return "K"; // Default will return King if it's not any of those up
default:
return "";
}
}
public String toString() {
return getValueString() + "" + getSuitString();
}
public boolean equals(Card other) {
if (this.value == other.value && this.suit == other.suit){
return true;}
else{
return false;
}
}
public static void main(String[] args) {
Card a = new Card(10, 3);
System.out.println(a.toString());
// Card b = new Card(14, 3); //ThrowsIllegalArgumentException
// System.out.println(b.toString());
Card c = new Card(1, 2);
System.out.println(c.toString());
Card equals1Yes = new Card(2, 3);
Card equals2Yes = new Card(2, 3);
Card notEquals = new Card(3, 2);
System.out.println(equals1Yes.equals(equals2Yes));
System.out.println(notEquals.equals(equals1Yes));
System.out.println(a.getSuit());
System.out.println(a.getValue());
System.out.println(a.getValueString());
System.out.println(a.getSuitString());
}
} // end class Card
Deck.java
import java.util.*;
public class Deck {
private ArrayList<Card> cards;
public Deck() {
cards = new ArrayList<Card>();
for (int a = 1; a <= 13; a++) {
for (int b = 0; b <= 3; b++) {
cards.add(new Card(a, b));
}
}
}
public Card deal() {
if (cards.size() != 0) {
return cards.remove(0);
} else {
return null;
}
}
public String toString() {
String return1 = "";
for (int i = 0; i <= cards.size(); i++) {
return1 = +cards.indexOf(i) + " ";
}
return return1;
}
public int size() {
return cards.size();
}
public void shuffle() {
Collections.shuffle(cards);
}
public boolean isEmpty() {
if (cards.size() == 0) {
return true;
} else {
return false;
}
}
public void reset() {
}
public static void main(String[] args) {
}
}