手中的所有牌都一样

时间:2014-11-26 23:45:28

标签: java

我现在正在中学学习java并且我的代码有问题。当使用字符串创建一个手时,列表中的所有Card对象具有相同的等级和套装(在这种情况下; 32)这当然会导致问题,因为我然后尝试按等级排序(比较排名然后适合的常规比较) 。然后尝试输出列表。我完全不知道为什么这些卡片都是以32的等级和套装创建的。如果我发布太多的代码或非常大的描述,第一次在网上询问,我提前抱歉。

卡类:

public class Card implements Comparable<Card>

// 1 - Ace, 2 to 9 , 10 - Ten, 11 - Jack, 12 - Queen, 13 - King
private int rank;
// 1 - Clubs, 2 - Diamonds, 3 - Hearts, 4 - Spades
private int suit;
private boolean isFaceUp;

private static final String SUITS = " CDHS";
private static final String RANKS = " A23456789TJQK";
public static final Comparator<Card> SUIT_ORDER = new SuitOrder();

public Card(int rank, int suit)
{
    this.rank = rank;
    this.suit = suit;
    if (Character.isUpperCase(suit))
        this.isFaceUp = true;

    this.isFaceUp = false;
}

public Card(String givenCard)
{
    // charAt(1) always a letter (suit)
    if (Character.isUpperCase(givenCard.charAt(1)))
    {
        this.isFaceUp = true;
    }
    this.isFaceUp = false;
    // Set both characters to uppercase to match static suits/ranks list
    if (Character.isLetter(givenCard.charAt(0)))
        Character.toUpperCase(givenCard.charAt(0));
    Character.toUpperCase(givenCard.charAt(1));
    // now set rank/suit of this card object
    // dont call other card constructor, use this one
    //this.rank = 22;
    //this.suit = 3;
    this.rank = RANKS.indexOf(givenCard.charAt(0));
    this.suit = SUITS.indexOf(givenCard.charAt(1));
}

public void flip()
{
    this.isFaceUp = true;
}

public boolean isFaceUp()
{
    return this.isFaceUp;
}

public int compareTo(Card otherCard)
{
    if (this.rank != otherCard.rank)
        return this.rank - otherCard.rank;

    return otherCard.suit - this.suit;
}

public static class SuitOrder implements Comparator<Card>
{
    public int compare(Card first, Card second)
    {
        if (first.suit != second.suit)
            return second.suit - first.suit;

        return second.rank - first.rank;
    }
}

public int getValue()
{
    // 1-10, face cards value 11/12/13
    return this.rank;
}

public boolean isAce()
{
    if (this.rank == 1)
        return true;
    else
        return false;
}

public String toString()
{
    return String.format("%c%c", RANKS.charAt(rank), SUITS.charAt(suit));
}

手类:

public class Hand

private ArrayList<Card> hand;

public Hand()
{
    // empty hand
    hand = new ArrayList<Card>();
}

public Hand(String str)
{
    Scanner handStr = new Scanner(str);
    // Initialize array list
    hand = new ArrayList<Card>(str.length() / 3 + 1);
    while (handStr.hasNext())
    {
        hand.add(new Card(handStr.next()));
    }
    handStr.close();
}

public int getValue()
{
    int value = 0;
    for (int count = 0; count < hand.size(); count++)
    {
        value += (hand.get(count)).getValue();
        // if ace add 10
    }
    // if over 21, keep track of aces, change to 1
    return value;
}

public boolean isBlackJack()
{
    return (hand.size() == 2 && getValue() == 21);
}

public void addCard(Card card)
{
    hand.add(card);
}

public void sortByRank()
{
    Collections.sort(hand);
}

public void sortBySuit()
{
    Collections.sort(hand, Card.SUIT_ORDER);
}

public void clear()
{
    hand.clear();
}

public int cardsLeft()
{
    return hand.size();
}

public String toString()
{
    return hand.toString();
}

甲板课程:

public class Deck

private Card[] deck;
int topCard;

public Deck(int noOfDecks)
{
    topCard = 0;
    deck = new Card[52 * noOfDecks];
    for (int decks = 0; decks < noOfDecks; decks++)
    {
        for (int suit = 1; suit < 5; suit++)
        {
            for (int rank = 1; rank < 14; rank++)
            {
                deck[topCard] = (new Card(rank, suit));
                topCard++;
            }
        }
    }
}

public Deck()
{
    // calls other deck constructor but with only one deck
    this(1);
}

public void shuffle()
{
    // double check
    // zero based arrays
    for (int i = 0; i < deck.length - 1; i++)
    {
        int random = (int) (Math.random() * (deck.length - 1));
        Card temp = deck[i];
        deck[i] = deck[random];
        deck[random] = temp;
    }
    // check to make sure all cards are face down after shuffle
    for (int cards = 0; cards < deck.length - 1; cards++)
    {
        if (deck[cards].isFaceUp() == true)
            deck[cards].flip();
    }
    // re-set topCard to deck length
    topCard = deck.length - 1;
}

public Card dealCard()
{
    // no cards left in deck
    if (topCard == 0)
        return null;

    topCard--;
    return deck[topCard];
}

public int cardsLeft()
{
    return topCard;
}

public String toString()
{
    // show only non-dealt cards
    StringBuilder deckDisplay = new StringBuilder((deck.length - 1) * 2);
    for (int index = 0; index < topCard; index++)
    {
        deckDisplay.append(deck[index].toString());
        deckDisplay.append(" ");
    }
    return deckDisplay.toString();
}

最后但并非最不重要的是打破这一切的测试代码:

// Make a second Hand from the Cards in a String
    Hand secondHand = new Hand("9H 9C 6D KH JS 5C AD 7S 8S JH 6C 8H TD " +
                               "3C 3D 6H AH QD 2S 9S AC 8D TC 5D 7D QH " +
                               "4D QC QS 3H 6S KD KS TS 7H 7C 4C 9D JC " +
                               "AS 8C KC 2C 4S 2H TH 5S 2D 3S 5H 4H JD");
    secondHand.sortByRank();
    System.out.println(secondHand);

3 个答案:

答案 0 :(得分:1)

Card构造函数使用

进行硬编码
this.rank = 22;
this.suit = 3;

所以你总是得到同一张卡片。您需要根据传入的givenCard设置等级和套装。此外,Character不会进行适当修改。我想你想要像

这样的东西
this.rank = Character.toUpperCase(givenCard.charAt(0));
this.suit = Character.toUpperCase(givenCard.charAt(1));

答案 1 :(得分:0)

你的等级为32的原因是,在你的卡片构造函数中,你将套装设置为3.你还将等级设置为22,并拉出第一个字符,所以你将其读作2.如果你删除这些行,它应该自行纠正。

答案 2 :(得分:0)

今天和老师谈过,结果我修改了我的代码,但从未清理过我的项目,因此我的.class文件从未使用新代码更新,这就是为什么我看不到任何更改。但我确实从之前的地方修复了它,我认为它是卡构造函数中添加的indexOf。