Am trying to implement this method and am stuck. This is for a card game. Here is my code for this part:
public int evaluate(Card cards) {
// Find a higher card of the suit played by the opponent
int switc = 0;
Card highCard = null;
Card lowCard = null;
for (Card card : setofcards) {
if (card.equals(cards) && card.getFace().getValue() > cards.getFace().getValue()) { // if they equal same face and check if value of hand card > value of person card
if (highCard == null || card.getFace().getValue() > highCard.getFace().getValue()) { // highCard = null for first loop, then compare highCard value
highCard = card;
switc = 1;
}
}
}
// Find the smallest lower card of the same suit
if (switc == 0) {
for (Card card : setofcards) {
if (card.equals(cards) && card.getFace().getValue() < cards.getFace().getValue()) {
if (lowCard == null || card.getFace().getValue() < lowCard.getFace().getValue()) {
lowCard = card;
}
}
}
}
if(lowCard != null) {
setofcards.remove(lowCard);
return lowCard.getFace().getValue();
}
else if(highCard != null) {
setofcards.remove(highCard);
return highCard.getFace().getValue();
}
else {
// Play the lowest card in the hand
Card lowestCard = setofcards.get(0);
for (int i = 1; i < setofcards.size(); i++) {
Card set = setofcards.get(i);
if (set.getFace().getValue() < lowestCard.getFace().getValue()) {
lowestCard = set;
}
}
setofcards.remove(lowestCard);
return lowestCard.getFace().getValue();
}
}