package oving5.card;
import java.util.Comparator;
public class HandComparator implements Comparator<Hand>{
public int compare (Hand hand1, Hand hand2){
int hand1Pair = hasPair(hand1);
int hand2Pair = hasPair(hand2);
CardComparator com = new CardComparator();
int highestCard = com.compare(hand1.getHighestCard(), hand2.getHighestCard());
if(hand1Pair < 0 && hand2Pair < 0){
return highestCard;
}
else if (hand2Pair < 0){ //hvis hand2 er mindre enn 0
return 1;
}
else if (hand1Pair < 0){ //hvis hand1 er mindre enn 0
return -1;
}
else if (hand1Pair != hand2Pair){ //hvis hand1 ikke er det samme som hand2
return hand1Pair - hand2Pair; //return det høyeste kortet
}
return highestCard;
}
public int hasPair(Hand hand) {
hand.sort();
int face = -1; //setter face lik verdien -1
for (int i = hand.getCardCount() - 1; i > 0; i--){ //lager en forløkke for å sjekke om kortet foran i er likt
if (hand.getCard(i).getFace() == hand.getCard(i - 1).getFace()){
face = hand.getCard(i).getFace();
}
}return face;
}
}