#Confused on implementing this method

1 messages · Page 1 of 1 (latest)

icy violet
#

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();
        }       
}
delicate cryptBOT
# icy violet Am trying to implement this method and am stuck. This is for a card game. Here i...

Detected code, here are some useful tools:

Formatted code
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();
  }
}
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

It seems like you are trying to implement a method called evaluate for a card game. However, there are a few issues with your code.

  1. Method Signature: The method signature should indicate that the method takes in multiple cards, not just one card. You can change the parameter type from Card to List<Card> or Card[] to represent multiple cards.

  2. Return Type: Since you are evaluating the cards, it would be more appropriate to return a value that represents the evaluation result. The return type of the method should be changed from int to something else, depending on what you want to evaluate (e.g., boolean, String, or a custom class).

  3. Implementation: Without knowing the specific rules of your card game and what you want to evaluate, it is difficult to provide specific guidance on how to implement this method. You need to define the logic for evaluating the cards based on your game's rules.

Here is an example of how you can modify your code:

public boolean evaluate(List<Card> cards) {
    // Implement your logic for evaluating the cards here
    // Return true or false based on the evaluation result
}

To help you further, please provide more information about your card game and what exactly you want to evaluate.

meager lily
#

How are you stuck?

icy violet
# meager lily How are you stuck?

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

delicate cryptBOT
meager lily
#

And how does the result differ from what you get?

#

you made something, what does it put out?

icy violet
delicate cryptBOT
icy violet
#

it seems to be skipping the first and secon if statement

#

thats interesting

meager lily
#

what is the purpose of the dummy cards?

icy violet
#

these two right
Card highCard = null; // dummy card
Card lowCard = null;

its to set an initial value for them

meager lily
#

and what are you comparing to in the if condition=

meager lily
#

you dont need them. you can just apply any logic to the cards you take out of the arry directly

icy violet
#

hmmm I'm confused

icy violet
meager lily
#

well, you declare a value to be null, but never overwrite that.

#

thus you get 0

icy violet
#
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;  // OVER HERE - highCard = card
                   
                    switc++;
               }
            }  
        }

highCard = card; // highCard = card

delicate cryptBOT
meager lily
#

you can do something like this arraylist.getItem().applyMethod();

icy violet
#

does that not overwrite the null

meager lily
#

not sure, but I think null is not greater than anything. its just undefined.

#

but there is no need to use any null variable in the first place

#

you are using a for each loop already

#

you can contain all the logic in there.

icy violet
#
               if (highCard == null || card.getFace().getValue() > highCard.getFace().getValue()) { // highCard = null for first loop, then compare highCard value

so how would I go about this then

meager lily
#

i would change it to a for loop first. so you can pick an index i and compare it to i + 1. so you can call the card next in line. make sure to catch the indexOutOfBoundsException

#

and break the loop when it appears

#

and then use finally to display what you want to display or set a Card highestCard; outside of the method and write the value of your until then found lowest card to it

#

then you can work with that

#

there is no need to set a value to you variables that is = null. null is their default state.

icy violet
#

I've never used finally before. what is it about?

#

oh seems like its used after try catch

#

concluding statement 🤷‍♂️

meager lily
#

finally is a block of code that is executed after you do a try catch block. no matter what happens during the try catch, finally is executed in succession

icy violet
#

👍

icy violet
#

thank you

meager lily
#

sure, I will answer if I can 🙂

icy violet
# meager lily sure, I will answer if I can 🙂

I tried adapting your logic to my code, I need some assistance bug catching. Not sure how to get around without initializing Card highCard = null...:

    Card winningCard = null;

    try {
        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;
                    }
                }
            }
        }
    } catch (IndexOutOfBoundsException e) {
        System.out.println("IndexOutOfBoundsException caught. Hand size: " + setofcards.size());
    } finally {
        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();
        }
    }
delicate cryptBOT
icy violet
#

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)

#

It seems to only play the lowest card of the suit

meager lily
#

you are not catching the exception, how I described. when the exception happens, you have reached the end of the array. meaning, the card you now hold as the highest card as far, is the highest card.

icy violet
#

So is the try catch inside the for loop

mint igloo
tribal zodiac
#

yeah. a OOB exception is always a bug in the code

#

don't provoke it, create an if-else instead

#

its not that bad though. like, it would work. it's just not a habit u should get used to at all

meager lily
# mint igloo Why are you suggesting exception handling as a control flow? This is beyond bad.

he is checking with the next index, if this index out of bounds the code breaks, thats pretty much it. when he is at the last entry, he cannot check the next. otherwise he would need to check each entries index if it is the last of the list, which would increase computation time and defeat almost the entire point of the for loop.
But now that you mention it, he could also just sort the list and then take the highest entry. 🤷‍♂️
I suggested what I think works best.

craggy summit
icy violet
#

I’ve figured it out. Just sorted the list to get the highest card

#

Thank you all!

#

.close

delicate cryptBOT
# icy violet .close

Looks like you attempted to use a command? Please note that we only use slash-commands on this server 🙂

Try starting your message with a forward-slash / and Discord should open a popup showing you all available commands.
A command might then look like /foo 👍