javacomparisonobject-comparison

Java object comparison: implementing a `compareTo()` method for a playing card game


I am making a method using the String comparison method "compareTo". However I have a few questions pertaining as to how exactly it works.

 public int compareTo(Card player2){    
    int finalRank = this.getRank().compareTo(player2.getRank());


    int turn = this.getRank().compareTo(player2.getRank());

    // Checks for "War" before anything is processed
    if(this.getRank() == player2.getRank())
    {
    return 0; // results in war
    }

    //This logic sets Ace as the highest card
    //after checking for "WAR"
    else if(this.getRank() == Rank.ACE)
    {
    return 1;
    }


    else if(player2.getRank() == Rank.ACE)
    {
    return -1;
    }


    //If it is not an ace, the winner per turn
    //is determined by standard value.
    else if(turn > 0)
    {
        //System.out.println("playerOne wins");
    return 1;
    }

    else 
    {
        //System.out.println("playerTwo wins");
    return -1;  
    }   

        }//end compareTo

This is my portion of code in the compareTo method. **So that i can mention, I had received quite a bit of online help for this segment of code. While it was being explained to me, it made sense. Thus if it makes sense to me, i should be able to rewrite this segment of code in another format. However, i have found myself not being able to do so. Thus i am looking for advice and help.

The function

int turn = this.getRank().compareTo(player2.getRank());

is generally what throws me off. What exactly is the compareTo method doing?

for a players turn, it is comparing the two cards at play -> following the programs logic.

Is it possible for me to compare cards with out using the compareTo method?

Here is how i have my class constructors set up.

Again, to finalize. I am having issues understanding how the compareTo method works. and if anything id like suggestions of another way to compare the players cards at play.

The cards list are coming from 2 seperate classes -> Suit and Rank

private Rank rCard;
  private Suit sCard;

/** ************************ * Initializing an object out of class Card * @param rCard * @param sCard * ************************* */ public Card(Rank rCard, Suit sCard) { this.rCard = rCard; this.sCard = sCard; }


Solution

  • First of all, the purpose of the compareTo() method is to define what makes two objects comparable. Not all objects are comparable. Some objects are made comparable by the Java API. For example, String is comparable. So if you were to use a method such as Collections.sort(someStringArray), this task can be accomplished because the objects are comparable. On the other hand, if you wanted to sort() an array of your Card objects, it can't be accomplished until you make the Card object comparable. You do this by using the compareTo() method. You also want to define your Card class as implements Comparable interface, in which you will have to @Override the compareTo() method. This, will make your Cards comparable by rank, if you decide to compare them by rank in your compareTo().

    What I believe you're trying to is compare the two objects by rank.

    int turn = this.getRank().compareTo(player2.getRank());
    
    is generally what throws me off. What exactly is the compareTo method doing?
    

    You're comparing the rank of this, which I'm assuming is Player1 to the rank of player2. When you use the compareTo() method, you return an int. This int tells you which player's rank is higher... 1 meaning, player1's rank is higher, -1 meaning player2's rank is higher, and 0 meaning the ranks are equal. This int I'm assuming will determine whose turn it is

    Also, I believe that this:

    int turn = this.getRank().compareTo(player2.getRank());
    

    should be placed outside of the compareTo() method and should be change to this:

    int turn = this.compareTo(player2);
    

    You're already comparing the players by their ranks in the compareTo(), so you don't need to use getRank(). I don't know if this will solve your problem completely, because I don't have your complete code, but I hope this gives you a better understanding of how the compareTo() method works.