Three of a Kind Algorithm help PLEASE

This is part of a much larger project. This algorithm is currently throwing an indexoutofbounds exception, and I don't know why. Someone please help.

Thanks!


public boolean isThreeOfKind( ArrayList h ){
int countSame = 1;
for(int i=0; i<h.size(); i++){
Card c1 = (Card) h.get(i);
for(int k=i+1; k<h.size(); k++){
if(k == h.size())
break;
Card c2 = (Card) h.get(k);
if( c1.equals(c2) ){
countSame++;
_cardsInvolved.add(c2);
h.set( h.indexOf(c2), null ); //index // out of bounds exception here
if(countSame == 3){
_cardsInvolved.add(c1);
for(int z=0; z<h.size(); z++){
if( h.get(z) != null)
_cardsNotInvolved.add(h.get(z));
if( h.get(z) == null)
_hand.remove(z);
}
return true;
}
}
}
}
return false;
}

Comments

  • : This is part of a much larger project. This algorithm is currently throwing an indexoutofbounds exception, and I don't know why. Someone please help.
    :
    : Thanks!
    :
    : [code]
    : public boolean isThreeOfKind( ArrayList h ){
    : int countSame = 1;
    : for(int i=0; i<h.size(); i++){
    : Card c1 = (Card) h.get(i);
    : for(int k=i+1; k<h.size(); k++){
    : if(k == h.size())
    : break;
    : Card c2 = (Card) h.get(k);
    : if( c1.equals(c2) ){
    : countSame++;
    : _cardsInvolved.add(c2);
    : h.set( h.indexOf(c2), null ); //index // out of bounds exception here
    : if(countSame == 3){
    : _cardsInvolved.add(c1);
    : for(int z=0; z<h.size(); z++){
    : if( h.get(z) != null)
    : _cardsNotInvolved.add(h.get(z));
    : if( h.get(z) == null)
    : _hand.remove(z);
    : }
    : return true;
    : }
    : }
    : }
    : }
    : return false;
    : }
    : [/code]

    Why do you set h.indexOf(c2) as null? Don't you want to use h.remove(c2) instead?
    There is also no need for h.indexOf(c2), since c2 is the object at position k in h. This means that h.indexOf(c2) == k.

    Homero C. de Almeida

    [italic]There's no dishonour in failure. For we aren't allowed to know wheter we'll achieve success or not. There is only one final shame, the cowardice of not trying.[/italic]

  • If ArrayList contains 2 pairs, your method will flag it as isThreeOfKind==TRUE.
    [code]
    8 8 K K

    index 0 and index 1 match, so index 1 is set to null and countSame is increased (=2)

    8 . K K

    index 2 and index 3 match, so index 3 is set to null and countSame is increased (=3!)

    8 . K .
    [/code]
    If there are no threee cards which match, you don't insert the removed cards into the ArrayList again, or do you?

    A simpler way of finding three of a kind is to sort the ArrayList before you test. In that way you know that any three cards that match will come in succession.
    eg (not tested)
    [code]
    boolean isThreeOfKind(ArrayList h) {
    // h is sorted according to value before entering the method
    Card c[] = new Card[3];
    c[0] = (Card)h.get(0);
    c[1] = (Card)h.get(1);
    c[2] = (Card)h.get(2);
    int index = 0, count = 3;
    while(true) {
    if(c[0].equals(c[1]) && c[1].equals(c[2])) {
    //Match found between cards at indices count-3, count-2, and count-1
    return true;
    }
    if(count<h.size()) {
    c[index] = (Card)h.get(count++);
    index = (index+1)%3;
    }
    else
    return false;
    };
    return false;
    }[/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories