-1

(Hand クラスで) Hand of Cards を引こうとしていますが、シャッフルされています。私の問題は、initialDeal() メソッドで shuffleDeck() を実行すると (ハンド オブ カードを引く必要があるがシャッフルされる)、ArrayIndexOutOfBounds例外が発生することです。

そして、私は 2 つの narf OF Clubs を描きます... 注、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();
    }
}    
}

ハンド クラス ^ (シャッフルされたデッキから 2 枚のカードを引く必要がある場所)

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  

^ 基本的にすべてを実行する

4

1 に答える 1

0

indexinを変更しているshuffleDeckので、shuffleDeck終了後、indexis shuffleDeck.length.

shuffleDeck、するfor (int index = 0; index < deck.length; index++)

( int index-> ローカル変数を宣言し、静的変数を使用しないことに注意してください。)

initialDealまたは、( で) インデックスを 0 に設定できます。

于 2012-05-08T09:04:19.530 に答える