[英]Shuffling Deck of Cards Using Card object
一直在嘗試創建一個名為 CardDeck 的 class 來洗牌在不同的 class 中創建的卡片對象(稱為 DeckOfCard,抱歉造成混淆,名稱不好),但已經沒有如何實現這一點的想法。
這是我想出的,我也包含了原始的 class DeckOfCards,歡迎和感謝任何幫助/建議!
//CODE FOR CARD OBJECT//
public final static int ace = 1, two= 2,three=3, four=4, five=5, six=6, seven =7, eight=8, nine=9, ten=10, jack= 11, queen=12, king=13;
public final static int diamonds= 1, clubs= 2, spades= 3, hearts=4;
private final static int numberOfFaces = 13;
private final static int numberOfSuits = 4;
private int face, suit;
private String faceValue, suitName;
//create a random card
public DeckOfCards()
{
face= (int)(Math.random() * numberOfFaces);
setFaceValue();
suit= (int) (Math.random() * numberOfSuits);
setSuitName();
}
//sets the string representation of each face value to its coorspdoing numeric value
private void setFaceValue()
{
switch(face)
{
case ace:
faceValue= "Ace";
break;
case two:
faceValue= "Two";
break;
case three:
faceValue= "Three";
break;
case four:
faceValue= "Four";
break;
case five:
faceValue = "Five";
break;
case six:
faceValue = "Six";
break;
case seven:
faceValue= "Seven";
break;
case eight:
faceValue= "Eight";
break;
case nine:
faceValue= "Nine";
break;
case ten:
faceValue= "Ten";
break;
case jack:
faceValue= "Jack";
break;
case queen:
faceValue= "Queen";
break;
case king:
faceValue= "King";
break;
}
}
//set the string representation of each suit
private void setSuitName()
{
switch(suit)
{
case diamonds:
suitName = "Diamonds";
break;
case clubs:
suitName= "Clubs";
break;
case spades:
suitName = "Spades";
break;
case hearts:
suitName = "Hearts";
break;
}
}
public String getFaceValue()
{
return faceValue;
}
public String getSuitName()
{
return suitName;
}
public String toString()
{
return faceValue+ " of " +suitName;
}
}
這是我當前的代碼......雖然不多,但這是迄今為止我所能得到的最接近的:
import java.util.Random;
public class CardDeck
{
private DeckOfCards[] cards;
//create new deck of cards
public CardDeck()
{
cards = new DeckOfCards[52];
int index= 0;
int[] cardTypes = {DeckOfCards.ace, DeckOfCards.diamonds, DeckOfCards.spades, DeckOfCards.hearts};
for(int cardType : cardTypes)
{
for(int i = 1; i<=13; i++)
{
DeckOfCards card = new DeckOfCards();
cards[index++]= card;
}
}
}
//create shuffle method, use loop to generate random suit and random faceValue
public void shuffle()
{
System.out.println("Suffuling cards");
int loopCount = 53;
while (loopCount > 0) {
double index1 = Math.random();
double index2 = Math.random();
DeckOfCards temp = cards[index1];
cards[index1] = cards[index2];
cards[index2] = temp;
loopCount--;
}
}
}
定義等級和西裝的enum
類型。 這提供了類型安全性,這樣您就不會意外傳遞花色參數的等級,反之亦然。 枚舉的值還可以用作地圖中用於定義不同游戲等的評分系統的好鍵。 另請注意,枚舉可以具有屬性和方法,因此您可以通過這種方式為值添加用戶友好的名稱。
然后創建具有等級和花色的Card
類型。
迭代等級,並且對於每個等級,迭代套裝。 為每個等級和花色組合創建一張新Card
,並將其添加到List
; 這是你的甲板。 當你的牌組建好后,你可以使用Collections
中的便捷方法對其進行洗牌。
public final class Card {
public enum Rank {
ACE, DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING
}
public enum Suit {
SPADES, HEARTS, DIAMOND, CLUBS
}
private final Rank rank;
private final Suit suit;
public Card(Rank rank, Suit suit) {
this.rank = Objects.requireNonNull(rank);
this.suit = Objects.requireNonNull(suit);
}
public Rank getRank() {
return rank;
}
public Suit getSuit() {
return suit;
}
@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (!(obj instanceof Card)) return false;
Card that = (Card) obj;
return (getRank() == that.getRank()) && (getSuit() == that.getSuit());
}
@Override
public int hashCode() {
return getRank().hashCode() * 31 + getSuit().hashCode();
}
@Override
public String toString() {
return getRank() + " of " + getSuit();
}
}
public class Deck {
private final List<? extends Card> cards;
public Deck(Collection<? extends Card> cards) {
this.cards = new ArrayList<>(cards);
}
public void shuffle(Random random) {
if (random == null) random = ThreadLocalRandom.current();
Collections.shuffle(cards, random);
}
public static Deck newStandardDeck() {
List<Card> cards = new ArrayList<>();
for (Card.Rank rank : Card.Rank.values()) {
for (Card.Suit suit : Card.Suit.values()) {
cards.add(new Card(rank, suit));
}
}
return new Deck(cards);
}
}
你的問題在這里:
double index1 = Math.random();
double index2 = Math.random();
Math.random() 返回一個介於 0 和 1 之間的雙浮點數。試試這個:
Random r = new Random();
while(loopCount > 0) {
int index1 = r.nextInt(cards.length);
int index2 = r.nextInt(cards.length);
因此,您的代碼結構存在一些大問題,但我挽救了這一點。 最簡單的解決方案是創建一個排序的卡片列表,每個套件和值一個,然后將每張卡片設置為隨機索引。 這將在字段“卡片”上執行該操作
// create shuffle method, use loop to generate random suit and random faceValue
public void shuffle() {
System.out.println("Suffuling cards");
int loopCount = 0;
ArrayList<DeckOfCards> inOrder = new ArrayList<DeckOfCards>();
for(int i = 0; i < 54; i++)
{
inOrder.add(cards[i]);
}
DeckOfCards[] shuffled = new DeckOfCards[54];
for(int i = 0; i < 54; i++)
{
//Math.random()*size of inOrder will give you a double between 0 and size of in order.
//Round this down and convert to an int and you have a random index!
int randCardIndex = (int) (Math.floor(Math.random()*inOrder.size()));
//Grab the card and add it to the shuffled deck in the current position
shuffled[i] = inOrder.get(randCardIndex);
//Remove this card so it can no longer be grabbed
inOrder.remove(randCardIndex);
}
cards = shuffled;
}
請將名稱更改為Card
而不是DeckOfCards
。 Card
對象列表變成一副卡片,因此您不想將 object 本身稱為卡片組。
隨機 function 應遵循與 DeckOfCards 的構造函數類似的方法,其中您的乘數應應用於隨機數之上並轉換為 int。 之后,您的代碼應該可以正常工作。 卡片大小 52 應存儲為全局私有變量。 切勿使用“幻數”而不將它們存儲在變量中並調用該變量。 index1 和 index2 都應如下所示:
double index1 = Math.random()*NUM_CARDS
在您的私有數組下方有以下變量: private final int NUM_CARDS = 52
否則,您將走在正確的軌道上,並且改組機制很好。 只是隨機索引生成得到一個介於 0 到 1 之間的十進制數。
當您必須調試時,請使用打印變量技術。 在這種情況下,打印出 index1 和 index2 的每個值,並檢查這些值是否按預期生成。 您還可以嘗試一種對初學者不太友好的方法,稱為調試模式(蟑螂圖標)並雙擊行號以設置您希望暫停執行的斷點,您可以查看存儲在 memory 中的所有變量代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.