我需要帮助画一手牌

时间:2012-05-08 08:59:12

标签: java

我正在尝试画一手牌(在Hand类中),但是它已经洗牌了。我的问题是如果我在initialDeal()方法中执行shuffleDeck()(我需要绘制一个Hand of Cards但是洗牌)它会给我一个ArrayIndexOutOfBounds异常。

我画了两个俱乐部的narfs ...注意,narf基本上是0,只是一个占位符。它不会被使用。

class Card {    
int suit, rank;   
public Card () {    
    this.suit = 0; this.rank = 0;   
}       
public Card (int suit, int rank) { 
    this.suit = suit; this.rank = rank;   
}      
public int getSuit() {   
    return suit;   
}       
public int getRank() {  
    return rank; 
}       
public void printCard () { 
    String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" };    
    String[] ranks = { "narf", "Ace", "2", "3", "4", "5", "6",  
    "7", "8", "9", "10", "Jack", "Queen", "King" }; 
    System.out.println (ranks[rank] + " of " + suits[suit]); 
} //end printCard
} //end class 

卡类(这基本上是做基本的东西)^

import java.util.Random;
class Hand {
static Card[] deck = new Card[52];
Card[] hand = new Card[10];
static int index;

public static void createDeck () {
    int m = 0;
    for (int j = 0; j < 4; j++) {
        for (int k = 1; k < 14; k++) {
            deck[m] = new Card(j,k);
            m++;
        }
    }
    index = 0;
} // end createDeck

public static Card deal () {
    Hand.index++;
    return deck[index-1];
} // end deal

public static int compareCard (Card c1, Card c2) {
    if (c1.getSuit() > c2.getSuit()) return 1;
    if (c1.getSuit() < c2.getSuit()) return -1;
    if (c1.getRank() > c2.getRank()) return 1;
    if (c1.getRank() < c2.getRank()) return -1;
    return 0;
} // end compareCard

public void printDeck (int size) {
    int j = 0;
    while (j < size) {
        deck[j].printCard();
        j++;
    }
}// end printDeck

public static void shuffleDeck () {
    Card tempCard;
    Random rd = new Random();
    for (index = 0; index < deck.length; index++) {
        int r = rd.nextInt(deck.length);
        tempCard = deck[index];
        deck[index] = deck[r];
        deck[r] = tempCard;
    }
} // end shuffleDeck

public void initialDeal() {
    hand[0] = null;
    hand[1] = null;

    hand[0] = deal();
    hand[1] = deal();
}


public void printHand() {
    initialDeal();
    index = 0;
    for(Card outputCard = new Card();  hand[index] != null;  index++) {
        outputCard.printCard();
    }
}    
}

手类^(这是我需要从洗牌的牌组中抽出两张牌)

class Dealer {    
Hand dealer = new Hand();    
public Dealer () {      
    dealer.createDeck();  
    dealer.shuffleDeck();

    System.out.println("Dealer's Hand");
    System.out.println("");
    initDeal();
}

public void initDeal() {
    dealer.initialDeal();
    dealer.printHand();
}
} //end class       

经销商类。^调用Hand的方法

class Driver {     
     public static void main (String[] args) {  
         Dealer dealer = new Dealer(); 
     } //end main method 
} //end class  

^基本上运行一切

1 个答案:

答案 0 :(得分:0)

您正在修改index中的shuffleDeck,因此在shuffleDeck完成后,indexshuffleDeck.length

shuffleDeck中,执行for (int index = 0; index < deck.length; index++)

(注意int index - &gt;声明局部变量,而不是使用静态变量。)

或者您可以(在initialDeal中)将索引设置为0。