Java纸牌列表无序排列



我正在开发的这个程序需要一些帮助。基本上是一个抽卡程序,它创建一副牌(牌和牌组类(,对创建的牌组进行洗牌,并将洗牌后的牌组的第一张牌添加到玩家的手上。(CardPlayer类(。

package cards;
public class Card {
private String[]cardSuit = {"Hearts", "Diamonds", "Spades", "Clubs"};
private String[]cardFaceValue = {"Ace", "King", "Queen", "Jack", "10", "9", "8", "7", "6", "5", "4", "3", "2"};

private int suit;
private int faceValue;

public Card(int newSuit, int newValue) {
suit = newSuit;
faceValue = newValue;
}

public String toString() {
String card = cardFaceValue[faceValue] + " of " + cardSuit[suit];

return card;
}
}
package cards;
import java.util.*;
public class Deck {

ArrayList<Card> deck = new ArrayList<>();

public Deck() {
for(int suit = 0; suit <= 3; suit++) {
for(int value = 0; value <= 12; value++) {
deck.add(new Card(suit,value));
}
}
}
public void Shuffle() {
Collections.shuffle(deck);
}
public Card Deal() {
Card card = deck.get(0);
deck.remove(0);
return card;
}
}
package cards;
import java.util.*;
public class CardPlayer {
Deck deck = new Deck();

ArrayList<Card> hand = new ArrayList<>();
public void getCard(){
hand.add(deck.Deal());
}
public ArrayList<Card> showCards(){

return hand;
}
}
package cards;
public class Demonstration {

public static void main(String[] args) {
Deck deck;
deck = new Deck();

CardPlayer cardPlayer;
cardPlayer = new CardPlayer();

System.out.println("The deck contains the cards: " + deck.deck);

deck.Shuffle();
System.out.println("New order of cards: " + deck.deck);

cardPlayer.getCard();
cardPlayer.getCard();
cardPlayer.getCard();
cardPlayer.getCard();
cardPlayer.getCard();
System.out.println("The card player's hand is now: " + cardPlayer.showCards());

}
}

我的问题是,每当我运行演示程序时,牌组和洗牌都会起作用,但当我试图展示玩家手中的牌时,它总是按照最初创建牌组时创建的顺序排列的,没有洗牌。我该如何着手解决这个问题?

您有两个Deck类的实例,一个在主方法中,另一个在CardPlayer类中。CardPlayer类中的那个在getCard中使用,但该牌组没有被打乱。

CardPlayer类中创建的牌组进行洗牌。或者将main中创建的shuffled deck传递给CardPlayer类的构造函数。

相关内容

  • 没有找到相关文章

最新更新