0

am trying to find four of a kind in a five-card poker hand. but it's not working and couldn't figure out why.

public boolean hasFourOfaKind(String hand) {
        int counter = 0;
        char x = 0;

        for (int i = 0; i < hand.length(); i++) 
        {
            if (i == 0) {
                x = hand.charAt(0);
            } else if (x == hand.charAt(i)) {
                counter++;

            }
        }
        if (counter >= 4) {
            return true;
        } else {
            return false;
        }
    }

the same problem here am trying to check whether the given four-card hand is a badugi

    public boolean hasFourCardBadugi(String hand) {
        int diffcounter = 0;
        char badugi = 0;

        for (int i = 0; i < hand.length(); i++) {
            if (i == 0) {
                badugi = hand.charAt(0);
            } else if (badugi != hand.charAt(i)) {
                diffcounter++;
            }
        }
        if (diffcounter >= 10) {
            return true;
        } else {
            return false;
        }
    }
Heniam
  • 68
  • 16
  • So what exactly is the problem? Can you give an example of an input you're giving and getting the wrong output for? – Mureinik Oct 08 '16 at 15:58
  • 2
    Representing data as chars in a `String` is not good, `java` is an object oriented language, which means you have concepts like classes and objects, that do the job much better. – Tobb Oct 08 '16 at 15:59
  • am using a junit that's given to test it. – Heniam Oct 08 '16 at 15:59
  • @tobb so what should i use then? – Heniam Oct 08 '16 at 16:00
  • Your four of a kind might only work if the first card matches. E.g. if it's kaaaa hand. Your method won't work. Maybe have a map for each char, and increment the counter on the map then see if one of the counters is greater than 4 – Ash Oct 08 '16 at 16:00
  • An issue is that `x` never updates. If you have "32222", then each "2" will be compared with the 3, and never with each other. – Tobb Oct 08 '16 at 16:02
  • "Object-orienting" this would be a class `Card`, and a class `Hand` that has 5 cards, for instance. – Tobb Oct 08 '16 at 16:02
  • Please edit your title to actually describe your issue – Basil Bourque Oct 08 '16 at 16:16

1 Answers1

0

Let's look at your for loop.

    for (int i = 0; i < hand.length(); i++) 
    {
        if (i == 0) {
            x = hand.charAt(0);
        } else if (x == hand.charAt(i)) {
            counter++;
        }
    }

In this part

        if (i == 0) {
            x = hand.charAt(0);
        }

You set x to the first card. But you never count that card. You need to add:

        if (i == 0) {
            x = hand.charAt(0);
            counter++;
        }

Of course this still has the issue that it will not detect hands where the four of a kind does not match the first card (ace two two two two), but you should be able to work that out now that the basic bug is fixed. One way to do it would just involve a second loop.

nhouser9
  • 6,730
  • 3
  • 21
  • 42
  • Thanks is there the same issue with the badugi too. – Heniam Oct 08 '16 at 16:11
  • @Heniammm no... in that one you set diffcounter to 10 and then check if diffcoutner >= 10, which will always be true. Please upvote and accept this if it helped =] – nhouser9 Oct 08 '16 at 16:24