Heniam
Heniam

Reputation: 66

Finding four of a kind in a 5 card game

I am trying to find four of kind in a 5 poker hand. I don't know where I went wrong, but the JUnit is reporting it's wrong.

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

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

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

Upvotes: 0

Views: 2515

Answers (3)

coolstoner
coolstoner

Reputation: 799

a real quickie on this since you wanted to test your hand assuming its a string of characters and below code is rewritten in javascript since i see the JS tag for the question too.....

function hasFourOfaKind(hand) {
    var counter = 0;
    var set_of_four = [];

    for (i = 0; i < hand.length; i++) {
        if (i == 0) {
            x = hand.charAt(0);
            set_of_four.push(x);
            counter++;
        } else {
            x = hand.charAt(i);
            if(set_of_four.indexOf(x) != '-1'){
                counter++;
            } else {
                set_of_four = set_of_four.splice(-1,1);
                set_of_four.push(x);
                counter = 0;
            }
        }
    }
    return (counter >= 3);
}

var poker_hand = 'BAAAAA';
var result = hasFourOfaKind(poker_hand);
console.log(result);

@Dmitry: thanks for the correction earlier...i was in too much haste then......

Upvotes: 0

cody123
cody123

Reputation: 2080

Your loop logic is wrong. It is incrementing counter for the same card again. That's why it is failing. In below mentioned code I am considering card only once.

public static boolean hasFourOfaKind(String hand) {
    int counter = 0;
    for (int i = 0; i < hand.length(); i++) {
        counter = 0;
        for (int j = 0; j < hand.length(); j++) {
            if (hand.charAt(j) == hand.charAt(i)) {
                counter++;
            }
        }
        if (counter >= 4) {
            return true;
        }
    }
    return false;
}

Upvotes: 1

Dmitry Smorzhok
Dmitry Smorzhok

Reputation: 635

I assume your hand is represented as something like "ABCDE". Your code is wrong. Let's take a look at the inner loop body:

for (int i = 0; i < hand.length(); i++) {
    for (int j = 0; j < hand.length(); j++) {
        if (i == 0) {  // take a look at this line
            x = hand.charAt(0);
            counter++;
        } else if (x == hand.charAt(i)) {
            counter++;

        }
    }
}

I've commented the line you should look at. The i will be always be 0 at first outer loop iteration so you'll increase your counter hand.length() times (that's how many times inner loop will execute while i == 0). If your hand length is 4 or more your method will always return true. Moreover even if you'll fix this part it won't help as you're always comparing chars to the first char of the string.

As a suggestion you can get a char array from your string, sort it and look how many identical chars are going one by one:

private boolean hasFourOfaKind(String hand) {
    if (hand == null || hand.length() == 0) {
        return false;
    }
    char[] chars = hand.toCharArray();
    Arrays.sort(chars);
    char current = chars[0];
    int count = 1;
    for (int i = 1; i < chars.length; i++) {
        char next = chars[i];
        if (current != next) {
            current = next;
            count = 1;
        } else {
            count++;
            if (count == 4) {
                return true;
            }
        }
    }
    return false;
}

Upvotes: 0

Related Questions