Java Blackjack卡总计数

时间:2013-06-01 15:33:58

标签: java counting blackjack

以下是我的Hand类的全部内容:

我的问题是,当我运行程序时,它会给我输出:

  

“欢迎来到二十一点......处理2张卡片:

     

这是你的卡片:[AH,3S]
  10,(h)它或(s)tand?   “

心之王和3个黑桃不应该给我10.我不知道我的代码有什么问题。我的getSoftTotal()方法看起来应该可以正常工作(我还没有对我的getHardTotal()方法感到困扰。

这是另一个输出:

  

“这是你的卡片:[4D,10D]
  25,(h)它或(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) {



}
}

2 个答案:

答案 0 :(得分:1)

该错误发生在您的Hand主要方法中。在你手中显示最初的两张牌之后,你需要额外的牌。

System.out.println("Here are your cards: " + cards.toString());
hand.addCard(deck.deal());

此额外卡的值将包含在下一行中手牌的总值中。我认为如果删除上面的第二行,你的代码(大部分)都会起作用。


附加说明:我认为根据二十一点的规则,你的一些手会被计算错误,因为你的卡片模型中的面部卡值设置不正确。杰克,女王和国王的价值都应该是10。

答案 1 :(得分:0)

比比尔指出的问题更多。您的hasBlackjack()getTotal()函数都是错误的,并且您不会对硬总计和软总计进行任何区分,这对于继续进行是必要的。之后可能会有更多,但一次只有一个错误......而且,永远不要压制警告。他们是有原因的。如果您收到警告,请修复该代码,不要压制它。

这是一个有效的hasBlackjack()。我将保留getTotal()个修复程序,但您的第一个提示是卡片值10,11,12和13应该全部加10。通过课程,value > 9可用于检查10张计数卡。

public static boolean hasBlackjack() {
    if (cards.size() != 2) return false;
    return ((cards.get(0).getValue() == Card.ACE && cards.get(1).getValue() > 9) ||
        (cards.get(1).getValue() == Card.ACE && cards.get(0).getValue() > 9));
}