#Confused on Logic Implementation

11 messages · Page 1 of 1 (latest)

split fractal
#

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();
        }       
}
rain bearBOT
#

This post has been reserved for your question.

Hey @split fractal! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

split fractal
#

Ok so lets say I use the evaluate method in the driver code, I the hand have these cards [6♠ 4♣ 2♣ 9♦ 9♠ ]
if card played is:
Q♠

I should play 6 spades.

I'm struggling with the logic of the code.

here is something else I'm trying:

 int switc = 0;
        Card highCard = null; // dummy card
        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;
                    if(switc > 0) {
                        
                    }
                    switc++;
               }
            }  
        }

essentially we first compare if the card is a high card and find the highest card of the suit. If there are multiple highcards of the suit then we choose the lowest high card. I'm not sure how I can format the code to make this logic work

sharp citrus
#

First find what winning card to play.
If you have one, return its index. If you don't have one, search a card for a different outcome

#

So basically you need a variable that holds the winning card you've picked so far from the cards in hand.
If you have no winning card so far, when a card is a winning card, make it the current winning card.
If you have a a winning card so far and you see another one, make it so the lowest one is your winning card.

#

Apply similar logic for the rest of the task.

rain bearBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.

split fractal
# sharp citrus So basically you need a variable that holds the winning card you've picked so fa...

I tried implementing your logic in code but it doesn't seem to work:

Card winningCard = null;

        for (int i = 0; i < setofcards.size(); i++) {
            Card card = setofcards.get(i);

            if (card.getSuit() == playedCard.getSuit()) {
                if (card.getFace().getValue() > playedCard.getFace().getValue()) {
                    // If it's a winning card and no winning card so far, or if it's the lowest winning card
                    if (winningCard == null || card.getFace().getValue() < winningCard.getFace().getValue()) {
                        winningCard = card;
                    }
                } else if (card.getFace().getValue() < playedCard.getFace().getValue()) {
                    // If it's the lowest winning card of the same suit
                    if (winningCard == null || card.getFace().getValue() < winningCard.getFace().getValue()) {
                        winningCard = card;
                    }
                }
            }
        }

        if (winningCard != null) {
            // If there is a winning card, play it
            setofcards.remove(winningCard);
            return winningCard.getFace().getValue();
        } else {
            // If no winning card, play the lowest card in the hand
            Card lowestCard = setofcards.get(0);
            for (int i = 1; i < setofcards.size(); i++) {
                Card card = setofcards.get(i);
                if (card.getFace().getValue() < lowestCard.getFace().getValue()) {
                    lowestCard = card;
                }
            }
            setofcards.remove(lowestCard);
            // return lowestCard.getFace().getValue();
            return 0;
        }
#

Examples of times it doesnt work:
Initial Hand: [4♦ A♦ 8♣ 8♠ K♠ ]
Played Card: Q♠
Evaluated Value: 8

Should've played K

Initial Hand: [8♥ K♥ 3♣ 4♠ 10♣ ]
Played Card: J♠
Evaluated Value: 4

Should've played king here (13)

Initial Hand: [A♦ K♣ 5♦ 10♣ 8♣ ]
Played Card: 6♦
Evaluated Value: 5

Should've played ace here (14)

#

I think the issue is it always plays the lowest card of the suit