0

I am trying to write code for a poker game to delete cards at certain indices using an array.

The code i have so far is the following and is not working.

ncard is the number of cards currently in the hand. any help would be appreciated.

/** 
 * discard the indexed cards from the Hand.
 * @param indices the indices of cards to delete.
 * @return true if all Cards deleted, false if not.
 */

public boolean discard(int[] indices){

    int i = 0;

    while (i < indices.length){

        if (indices[i] < 0 || indices[i] >= ncard)
        {
            return false;
        }

        for (int in = indices[i]; in < ncard; in++){
            cards[in] = null;
            ncard--;


        }
        i++; 

    }
    return true;
}
  • Define "not working". – Marvin Apr 28 '17 at 10:22
  • it will discard too many cards at times. – James Dayman Apr 28 '17 at 10:22
  • It looks like you have the cards in a static array, and are setting the discarded indices to null. If you discard n cards and then try to discard a card at index 52-n, the metdod will return false and not discard any more cards. Also, you are not discarding cards at the indices, you are discarding every card between each given index and ```ncard``` (which doesn't represent the index of the last card since the nulls are at arbitrary locations) – Haem Apr 28 '17 at 10:23

2 Answers2

0

It looks like you have the cards in a static array, and are setting the discarded indices to null. If you discard n cards and then try to discard a card at index 52-n, the method will return false and not discard any more cards.

You should probably use some dynamic data structure, such as a stack or a list, to store the cards. If you must use an array, here's how to go about fixing your problem:

You are not discarding cards at the indices, you are discarding every card between each given index and ncard (which doesn't represent the index of the last card since the nulls are at arbitrary locations). That for-loop should be replaced with

cards[indices[i]] = null
deletedCards++;//You should initialize this before the loop

After the loop, you should put this code:

Arrays.sort(cards, new Comparator<Card>(){
  public int compare(Card a, Card b){
    return Boolean.compare(a==null,b==null);
  }
});

It sorts the nulls to the back of the array, and keeps the order otherwise same. Then you decrement ncards by deletedCards.

Haem
  • 929
  • 6
  • 15
  • 31
0

I highly reccomend for-loops in this case, since all this i++ stuff makes the code hard to understand

public boolean discard(int[] indices) {
    // check if all indices consumed are valid
    for(int index : indices) {
        // i just use your validation, don't know, if this is correct though
        if(index < 0 || index >= ncard)
            return false;
    }
    // remove cards
    for(int index : indices) {
        cards[index] = null;
        ncard--;
    }
    return true;
}

My guess is that the problem comes from the validation, but I would need more information to really solve that problem...
Maybe this would be a better validation sorting out indexOutOfBounds (negative and positive) and the attempt to discard more cards than ncard:

if(index < 0 || index > (cards.length -1) || indices.length > ncard)
    return false;

EDIT: I assume that cards is an array that has the size of your card deck and all cards that the player does not have are null values?

goerlibe
  • 120
  • 1
  • 12