質問

I am currently stuck in my project. The assignment is to create a BlackJack game, using println/outprints to visualize the actual game.

Currently i have defined the card values and types, in two Enums as following:

public enum Rank {    
    TWO("2"),    
    THREE("3"),    
    FOUR("4"),    
    FIVE("5"),    
    SIX("6"),    
    SEVEN("7"),    
    EIGHT("8"),    
    NINE("9"),    
    TEN("10"),    
    JACK("J"),    
    QUEEN("Q"),    
    KING("K"),    
    ACE("A");       

    public String symbol;        

    private Rank(String symbol)    
    {    
        this.symbol = symbol;    
    }

    public String getSymbol()    
    {    
        return symbol;    
    }    
}


public enum Suit
{
    CLUB,DIAMOND,HEART,SPADES;
}

These are used in my Deck Class as following:

public class Deck    
{

   public Suit suit;
   public Rank rank;      

   public Deck(Suit suit, Rank rank)    
   {    
       this.suit = suit;    
       this.rank = rank;    
   } 

   public Suit getSuit()    
   {    
       return this.suit;    
   }

   public Rank getRank()    
   {
       return this.rank;    
   }         
}

I will use the deck class for my classes, Dealerhand, and PlayerHand.

My question is as following. i would like to be able to draw a random card from an array of cards with the values of an actual card game.

How would I go about making the actual array so that it functions with my Enums, and would I be better off using an arraylist?

役に立ちましたか?

解決

How about an Object-oriented solution?

What is a card? A card is an object that possesses (i.e. instance fields) both Rank and Suit. You currently have rank and suit as properties of your Deck class, but I would not do that. A Deck is a collection of PlayingCards which I would define like:

public class PlayingCard {
    private Rank r;
    private Suit s;
    :
    :
}

Then, you can initialize an array with PlayingCard objects.

You could shuffle a Deck (that contained an array of PlayingCards) with a custom method that you'd write like:

myDeck.shuffle();
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top