Solved

how to put cards in order by using Comparable?

Posted on 2004-08-06
14
260 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

737 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