-4

So I'm building a poker program and working on a way to sort cards Jack through Ace. I'm currently converting the face cards into numerical values and using Arrays.sort() but it's giving me a few glitches later in the code. Is there a way to build a key to sort an array against? Thanks for the help!

Anubian Noob
  • 13,426
  • 6
  • 53
  • 75

2 Answers2

1

A Card has a Rank and a Suit. Java enums can be given values beyond just their ordinal value. Maybe you can leverage that? This is not so difficult when your abstractions affect only a single game, but trying to make a truly abstract Card and Deck, especially regarding Ranking, gets kind-of tricky. Consider Black Jack; Is that Ace worth 10 or 1? It depends. In any event, note what is going on with Collections.sort in the main below. Come to think of it, Cards and Card Games makes for a good exercise in Object design. There is more than one way to model this.

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Card {
    public static enum Rank {
        One( 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 ), Ace( 14 );

        public final int value;

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

    public static enum Suit {
        Spade, Heart, Diamond, Club
    }

    public final Rank rank;
    public final Suit suit;

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

    public String toString() {
        StringBuilder sb = new StringBuilder( this.getClass().getSimpleName() );
        sb.append( "( rank: " + rank );
        sb.append( ", suit: " + suit );
        sb.append( ")" );
        return sb.toString();
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ( ( rank == null ) ? 0 : rank.hashCode() );
        result = prime * result + ( ( suit == null ) ? 0 : suit.hashCode() );
        return result;
    }

    @Override
    public boolean equals( Object obj ) {
        if ( this == obj ) {
            return true;
        }
        if ( obj == null ) {
            return false;
        }
        if ( getClass() != obj.getClass() ) {
            return false;
        }
        Card other = (Card) obj;
        if ( rank != other.rank ) {
            return false;
        }
        if ( suit != other.suit ) {
            return false;
        }
        return true;
    }

    public static void main( String[] args ) {
        Card aceOfSpades = new Card( Rank.Ace, Suit.Spade );
        System.out.println( "aceOfSpades: " + aceOfSpades );
        System.out.println( "aceOfSpades.rank.ordinal: " + aceOfSpades.rank.ordinal() );
        System.out.println( "aceOfSpades.rank.value: " + aceOfSpades.rank.value );

        List<Card> deck = new ArrayList<Card>();
        for ( Suit suit : Suit.values() ) {
            for ( Rank rank : Rank.values() ) {
                deck.add( new Card( rank, suit ) );
            }
        }

        System.out.println( "deck: " + deck );

        Collections.sort( deck, new Comparator<Card>() {
            public int compare( Card c1, Card c2 ) {
                int result = c1.rank.value - c2.rank.value;
                return result;
            }
        } );

        System.out.println( "deck: " + deck );
    }

}
Bob Kuhar
  • 10,838
  • 11
  • 62
  • 115
  • AFAIK usage of ordinals in that way is strongly discouraged. The developer should provide enum with the custom value instead (see http://goo.gl/nMwYQW for more information). – tkroman May 07 '14 at 22:31
  • Fair enough. I'll make an edit. – Bob Kuhar May 07 '14 at 22:38
0

Can you use a Map<String, String>?

loli
  • 1,058
  • 8
  • 14
  • Map interface sort behaviour is up to its implementing class. A HashMap is not sorted, where as TreeMap is. I don't think your answer is related to the question, however, the question is unclear. – Cahit Gungor May 07 '14 at 21:35
  • yeah you're right, Treemap is what I should have said – loli May 07 '14 at 21:39