Solved

how to put cards in order by using Comparable?

Posted on 2004-08-06
14
256 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now