2013-06-01 334 views
-1

這裏是低於我的手類的全部:Java的酒杯卡總計數

,我有是,當我運行程序時,它給了我喜歡輸出的問題:

「歡迎酒杯...打交道2卡:

這是你的卡:[AH,3S]
10(H),或(S)t和 「

一顆心和一個黑桃3不應該給我10.我不知道我的代碼有什麼問題。我getSoftTotal()方法看起來像它應該工作正常(我還沒有與我getHardTotal()方法困擾尚未

下面是另一個輸出:

「這是你的卡:[4D,10D]
25, (H),或(S)t和?「

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) { 



} 
} 
+0

我們需要看看你的卡類。 –

+0

我已經添加了卡類。 – user2443505

+0

對不起,看起來我們也需要看'Deck'類,因爲這顯然是你爲卡分配值的地方。 –

回答

1

bug是你Hand主要方法。在你手中顯示最初的兩張牌之後,你正在處理一張額外的牌。

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

這張額外卡的價值已包含在下一行的手牌總值中。我認爲你的代碼將(主要)工作,如果你刪除上面的第二行。


附加說明:我覺得有些你的雙手將根據二十一點規則,因爲你必須在你的顯卡型號設置不正確的面對卡值計算錯誤。傑克,女王和國王應該都是10的值。

+0

謝謝你的迴應! (對不起,你必須閱讀所有的代碼才能看到這樣的簡單錯誤。) 此外,對於我的程序,我們使用不同的值(但感謝警告)。 – user2443505

+0

@ user2443505當然,沒問題。 –

0

您比Bill指出的問題還多。您的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)); 
}