001package algs12;
002import stdlib.*;
003
004public class XCard implements Comparable<XCard> {
005        public static enum Rank {
006                TWO("2"),
007                THREE("3"),
008                FOUR("4"),
009                FIVE("5"),
010                SIX("6"),
011                SEVEN("7"),
012                EIGHT("8"),
013                NINE("9"),
014                TEN("10"),
015                JACK("J"),
016                QUEEN("Q"),
017                KING("K"),
018                ACE("A");
019
020                private final String name;
021                private Rank (String name) { this.name = name; }
022                public String toString () { return name; }
023        }
024        public static enum Suit {
025                CLUBS("C"),
026                DIAMONDS("D"),
027                HEARTS("H"),
028                SPADES("S");
029
030                private final String name;
031                private Suit (String name) { this.name = name; }
032                public String toString () { return name; }
033        }
034
035        public Rank rank;
036        public Suit suit;
037        private XCard (Rank r, Suit s) { this.rank = r; this.suit = s; }
038        public String toString () { return rank.toString () + suit.toString (); }
039        public int compareTo (XCard that) {
040                if (this.suit.compareTo (that.suit) < 0) return -1;
041                if (this.suit.compareTo (that.suit) > 0) return +1;
042                if (this.rank.compareTo (that.rank) < 0) return -1;
043                if (this.rank.compareTo (that.rank) > 0) return +1;
044                return 0;
045        }
046        /* 
047         * NOTE: We don't need to override Object.equals 
048         *  because there is only one possible instance of each card.
049         *  But if you did do it, it would look like this:
050         */
051        //public boolean equals (Object that) {
052        //      if (that == null)
053        //              return false;
054        //      if (this.getClass() != that.getClass())
055        //              return false;
056        //      XCard thatCard = (XCard) that;
057        //      return (this.rank == thatCard.rank) && (this.suit == thatCard.suit);
058        //}
059
060        private static XCard[] protoDeck = new XCard[52];
061        static { // This is how you run a loop to initialize a static variable.
062                int i = 0;
063                for (Suit s : Suit.values ())
064                        for (Rank r : Rank.values ()) {
065                                protoDeck[i] = new XCard (r, s);
066                                i++;
067                        }
068        }
069        public static XCard[] newDeck () {
070                XCard[] deck = new XCard[protoDeck.length];
071                for (int i = 0; i<protoDeck.length; i++)
072                        deck[i] = protoDeck[i];
073                return deck;
074        }
075
076        public static void main (String[] args) {
077                XCard[] d1 = XCard.newDeck ();  final XCard c1 = d1[51];
078                XCard[] d2 = XCard.newDeck ();  final XCard c2 = d2[50];
079                StdOut.println (c1 + (c1.equals(c2) ? " equals " : " does not equal ") + c2);
080        }
081}