Solved

how to put cards in order by using Comparable?

Posted on 2004-08-06
14
258 Views
Last Modified: 2010-03-31
Hello
can anybody help on how to code  my Card.java class that implements Comparable?

my code prints out fours hands with 13 random card but they r unordered, how can I ordered them by using Comparable in Card.java class ?

any help please

public class Card{

  private int value, suit;

  public Card(int val, int s){
    value = val;
    suit = s;
  }
  public int getValue(){
    return value;
  }

  public int getSuit(){
    return suit;
  }

  public void setValue(int v){
    value = v;
  }

  public void setSuit(int s){
    suit = s;
  }
 
  public String toString() {
      return "Suit = " + suit + " Value = " + value;
  }
}


/*
 * CardGame.java
 *
*/

import java.util.*;



public class CardGame {
    List theDeck = new ArrayList(52);
    Random r = new Random();
     
    public CardGame() {
        // Fill the deck
        for (int j = 1; j <= 4; j++)
            for (int i = 1;i <= 13;i++)
               theDeck.add( new Card(i,j) );
    }

    public List deal(int number) {
        List hand = new ArrayList();
        while (hand.size()<number) {
            int index = r.nextInt( theDeck.size() );
            hand.add( theDeck.get(index) );
            theDeck.remove(index);
        }
        return hand;
    }
   
    public void listHand(List hand) {
        for (int i=0; i<hand.size(); i++)
            System.out.println( ((Card) hand.get(i)) );
    }

    public static void main(String args[]) {

         CardGame cg = new CardGame();
         List hand1 = cg.deal(13);
         System.out.println("Hand 1 contains:");
         cg.listHand(hand1);
         List hand2 = cg.deal(13);
         System.out.println("Hand 2 contains:");
         cg.listHand(hand2);
         List hand3 = cg.deal(13);
         System.out.println("Hand 3 contains:");
         cg.listHand(hand3);
         List hand4 = cg.deal(13);
         System.out.println("Hand 4 contains:");
         cg.listHand(hand4);
    }
}
0
Comment
Question by:anosTekbot
  • 7
  • 3
  • 2
  • +2
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11738992
I'm not totally sure about the ordering rules for cards, but i think this is right:

public int compareTo(Object other) {
      Card otherCard = (Card)other;
      int returnValue = value - otherCard.getValue();
      if (returnValue == 0) {
            return suit - other.getSuit();
      }
      else {
            return returnValue;
      }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11739001
That of course should be made a method of Card and

>>public class Card{

should be

public class Card implements Comparable {
0
 

Author Comment

by:anosTekbot
ID: 11739093
the order rule is like that for suit ( Clubs, Diamonds Hearts Spades)
for values ( highest to lowest) --> ( ace king queen .....  2 )
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 86

Expert Comment

by:CEHJ
ID: 11739516
Yes, i know, but what i meant is which takes priority - the suit or the rank? (My code assumes rank then suit)
0
 

Author Comment

by:anosTekbot
ID: 11739557
I wrote this code for compareTo method
but how I can order suits (Clubs, Diamonds Hearts Spades) and values ( highest to lowest) in my cardgame class?

so i want to get an output such as :
player1: Clubs 14 7 3 2

public class Card implements Comparable {
  private int suit;  // Clubs, Diamonds, Hearts, Spades.
  private int val;   // Card number: 2-14.
 
  public int compareTo (Object rhs) {
    Card card = (Card)rhs;
    if (this.suit < card.suit) return –1;
    if (this.suit > card.suit) return  1;
    if (this.val  < card.val ) return –1;
    if (this.val  > card.val ) return  1;
    return 0;
  }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11739595
Your code would rank the 2 of spades above the king of clubs, which is not right AFAIK
0
 

Author Comment

by:anosTekbot
ID: 11739624
any suggestion?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11739643
Yes - change it to mine ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11739653
You must also use the accessor method of the other class:

>>if (this.val  < card.val ) return –1;

should be

if (this.val  < card.getValue() ) return –1;
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11739833
In my code,

>>return suit - other.getSuit();

should be

return suit - otherCard.getSuit();
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11740143
Just a remark. I read

>> private int val;   // Card number: 2-14.

But your card values are 1 - 13 :

public CardGame() {
        // Fill the deck
        for (int j = 1; j <= 4; j++)
            for (int i = 1;i <= 13;i++)    // <<<<<<<<<<<<<< I changed that in antoher Q since that's more like real life, isn't it?
               theDeck.add( new Card(i,j) );
}


0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 11740504
try this:

public class Card impleemnts Comparable {

  private int value, suit;

  public Card(int val, int s){
    value = val;
    suit = s;
  }
  public int getValue(){
    return value;
  }

  public int getSuit(){
    return suit;
  }

  public void setValue(int v){
    value = v;
  }

  public void setSuit(int s){
    suit = s;
  }

  public int compareTo (Object rhs) {
    Card card = (Card)rhs;
    return (value==card.value ? suit - card.suit : value - card.value);
  }

  public String toString() {
      return "Suit = " + suit + " Value = " + value;
  }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 11741105
Also instead of using Random to select a Card from deck, you could just shuffle the deck initially making your code simpler:

    public CardGame() {
        // Fill the deck
        for (int j = 1; j <= 4; j++)
            for (int i = 1;i <= 13;i++)
               theDeck.add( new Card(i,j) );
        Collections.shuffle(theDeck);
    }

    public List deal(int number) {
        List hand = new ArrayList();
        while (hand.size()<number) {
            hand.add( theDeck.remove(0) );
        }
        return hand;
    }
0
 
LVL 1

Expert Comment

by:wolfc
ID: 11742529
I like objects implementation of CEHJ's idea.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question