Sign in to follow this  
Lothia

Java, giving Cards values

Recommended Posts

Lothia    102
Hay, I am trying to just make a basic game like blackjack and currently I created just 2 classes, one for cards/deck and the second which will run the game. Then I will make a GUI later. My issue comes with I am trying to make it run "fast" (I have to change arraylists to Nodes eventually but not yet) My biggest issue is I am using Enums for the rank and suit of the card and was wondering what the fastest way to set values (int values) of each card. Would it be best to just add it into the Card class and each card also has a specific value with it? How would be the best way to go about adding the values into this class (the For loop I use to create the deck).
public class Card {
  public enum Rank { TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, 
                     JACK, QUEEN, KING, ACE};
  public enum Suit { SPADES, DIAMOND, CLUB, HEART};
  
  private Suit my_suit;
  private Rank my_rank;
  private static final List<Card> deck = new ArrayList<Card>();
  
  public Card() {
  }
  
  public Card(Rank rank, Suit suit) {
    my_suit = suit;
    my_rank = rank;
  }
  
  public List<Card> create() {
    for(Suit my_suit : Suit.values()) {
      for(Rank my_rank : Rank.values()) {
        deck.add(new Card(my_rank, my_suit));
        //System.out.println(my_rank + " of " + my_suit);
      }
    }
    return deck;
  }
  
  public void shuffle() {
    Random rand = new Random();
    
    for(int i=0; i < deck.size(); i++) {     
      int number = rand.nextInt(52);
      Card temp = deck.get(i); //temp is i. 
      deck.set(i, deck.get(number)); //replace i with whatever is in number
      deck.set(number, temp);   
    }
  }
  
  public Card[] deal(int num_cards) {
    Card[] hand = new Card[num_cards];
    for(int i=0; i < num_cards; i++) {
      hand[i] = deck.get(i);
      deck.remove(i);
    }
    return hand;
  }
  
  public Card dealOne() {
    Card card = deck.get(0);  
    deck.remove(0);
    return card;
  }
}
Thanks for the help.

Share this post


Link to post
Share on other sites
INsanityDesign    138
Hi,

if you ask for the fastest way you'll probably get many different answers. As this is ought to be a very small way, my opinion to do it clearly and modular would be to add it to the enumeration like this:


public enum Rank {
TWO(2),
THREE(3),
FOUR(4),
FIVE(5),
SIX(6),
...;

public value;

private Rank(int value) {
this.value = value;
}

};


That would be a good way, easy to change, clearly to use. If you traverse the cards with defined Rank you would just have to get the current value for your calculations like Rank.value.

Share this post


Link to post
Share on other sites
INsanityDesign    138
First of all since Java5 enums are classes. Thats the reason why they can be instanced privately. Everything can stay as it is, you do not have to "class" the enums. This will change nothing special as the compiler will nevertheless even without the constructor deploy the inner classes into extra .class files, probably Card$Rank.class no matter what.

Personally I do not really get what should be the problem as you probably will .jar them anyway. If the same text is in the same or another class file won't make it a "big" game. We're talking about kilobytes here.

So to answer directly: No, the Rank does not have to be a special class as the enum already is. You can use the code just as I posted here.

Share this post


Link to post
Share on other sites
Lothia    102
I did one of those, I'm stupid mistakes. After listing my enums I forgot the ; LOL, so I was getting huge amounts of errors. Yeah it works fine thank you that will help a lot.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this