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.