I've written a Deck class and now have to shuffle it, and then print 开发者_高级运维out a few hands to check how it works. However, it doesn't seem to shuffle anything, but rather give the exact same deck.
public void makeHands () {
Deck deck = new Deck ();
Deck shuffled = shuffleDeck (deck);
printDeck (subdeck (shuffled, 0, 4));
printDeck (subdeck (shuffled, 5, 9));
printDeck (subdeck (shuffled, 10, 14));
printDeck (subdeck (shuffled, 15, 19));
}
public static int randomInt (int length, int i) {
double x = Math.random () * length;
int g = (int) x;
return g;
}
public Deck shuffleDeck (Deck deck) {
for (int i=0; i<deck.cards.length; i++) {
int g = randomInt (deck.cards.length, i);
swapCards (i, g);
}
return deck;
}
}
public void swapCards (int first, int swap) {
Card temp = cards[first];
cards[first] = cards[swap];
cards[swap] = temp;
}
The code looks incomplete.
It looks like you need to pass the deck object to the swapCards method.
swapCards() is referring to a cards array. It is n't clear where this array is declared. Instead you should pass deck to this method and swap deck.cards[first] and deck.cards[swap].
精彩评论