| Fou-Lu |
04-27-2012 06:08 PM |
It never will. Object is a base type that contains no properties, and your Card doesn't represent what it is.
You'll need to represent a card as an object, and a deck of Card. I'd represent the suit and value as an enum string pair:
PHP Code:
public class Card implements Comparable<Card> { public static enum Suit { HEART ("Hearts"), SPADE ("Spades"), CLUB ("Clubs"), DIAMOND ("Diamonds"); private final String sSuit; Suit(String s) { this.sSuit = s; } public String getSuit() { return this.sSuit; } } public static enum FaceValue { ACE ("A"), TWO ("2"), THREE ("3"), FOUR ("4"), FIVE ("5"), SIX ("6"), SEVEN ("7"), EIGHT ("8"), NINE ("9"), TEN ("10"), JACK ("J"), QUEEN ("Q"), KING ("K"); private final String sValue; FaceValue(String s) { this.sValue = s; } public String getValue() { return this.sValue; } }
private Suit suit; private FaceValue value; public Card(Suit suit, FaceValue value) { this.suit = suit; this.value = value; } public Card(String suit, String value) { this.suit = Suit.valueOf(suit); this.value = FaceValue.valueOf(value); }
public Suit getSuit() { return this.suit; }
public FaceValue getValue() { return this.value; } public int compareTo(Card c) { int iResult = this.suit.compareTo(c.suit); if (iResult == 0) { iResult = this.value.compareTo(c.value); } return iResult; } public String toString() { return this.value.getValue() + "-" + this.suit.getSuit(); } }
And the deck as an array list. I like the list since you can shuffle it, but without an override it will accept duplicate values.
PHP Code:
import java.util.Collections; import java.util.ArrayList;
public class Deck extends ArrayList<Card> { private static final long serialVersionUID = 7465934980544707488L;
public static void main(String... argv) { Deck d = new Deck(); // Populate the deck with one of each suit and face value. for (Card.Suit s : Card.Suit.values()) { for (Card.FaceValue v : Card.FaceValue.values()) { d.add(new Card(s, v)); } } Collections.shuffle(d); System.out.println("shuffled:"); for (Card c : d) { System.out.println(c); } Collections.sort(d); System.out.println("sorted:"); for (Card c : d) { System.out.println(c); } } }
I like the enums since you can do a lot with them. It forces specific allowed entries and lets you run a switch on them unlike a string.
Edit:
Oh, btw I should mention simplicity as well. If you don't really care about a Deck or a Card, you can represent it just using the String array you have. That can be thrown into a list and Shuffled as well and print just as a string:
PHP Code:
public static void main(String[] argv) { String[] allCards = { "A-Hearts", "2-Hearts", "3-Hearts", "4-Hearts", "5-Hearts", "6-Hearts", "7-Hearts", "8-Hearts", "9-Hearts", "10-Hearts", "J-Hearts", "Q-Hearts", "K-Hearts", "A-Dimonds", "2-Dimonds", "3-Dimonds", "4-Dimonds", "5-Dimonds", "6-Dimonds", "7-Dimonds", "8-Dimonds", "9-Dimonds", "10-Dimonds", "J-Dimonds", "Q-Dimonds", "K-Dimonds", "A-Clubs", "2-Clubs", "3-Clubs", "4-Clubs", "5-Clubs", "6-Clubs", "7-Clubs", "8-Clubs", "9-Clubs", "10-Clubs", "J-Clubs", "Q-Clubs", "K-Clubs", "A-Spades", "2-Spades", "3-Spades", "4-Spades", "5-Spades", "6-Spades", "7-Spades", "8-Spades", "9-Spades", "10-Spades", "J-Spades", "Q-Spades", "K-Spades" }; java.util.List<String> cards = Arrays.asList(allCards); java.util.Collections.shuffle(cards); for (String s : cards) { System.out.println(s); } }
Depending on exactly what you want to do, sometimes simplicity is for the better.
|