简体   繁体   中英

How can I check if a index in an array is empty and how to shift an array

I have this function for a Go Fish Card game.

// Remove a Card from the player's hand

// In: _index The index of the card to remove from the array

// _discard A reference to store that value in

// // Return: True if there was a card actually stored at that index.

// False if the index was "empty"

bool Player::Discard(int _index, Card& _discard)
{
        return true;
}

Should store the requested card into the reference being passed in. After that, "shift" the array back to show this card has been removed.

Example: [ 7♥ 3♣ 5♦ 9♥ ] m_numCards: 4

  • discarding index 1 (the 3♣) *

[ 7♥ 5♦ 9♥ 9♥ ] m_numCards: 3

  • Even though it looks like there are two 9♥, we won't ever be displaying that to the user, and it will be the first one that gets overwritten if another card is added to the hand. **/

    // Just here for compilation

in the header i have this member variables.

char m_name[32];    
Card m_hand[7];
int m_numCards; 
int m_maxCards; 
int m_score;    

I had this inside to do the first part But im pretty sure im missing something.

      if (m_hand[_index] != FALSE)
{
    _discard = m_hand[_index];
    return true;
}
else 
    return false;

If you define your "hand" as an array:

Card m_hand[7];

Then you always have 7 cards. Sure you could add a "sentinel" Card value or something, but really there's always 7 cards. You can't remove or append to a raw array. On the other hand, if you used a dynamic container instead:

std::vector<Card> m_hand;

Now, you can have a variable-sized hand and add and remove cards as you see fit. And your discard function becomes easy:

bool Player::Discard(int _index, Card& _discard)
{
    if (m_hand.size() > _index) {
        _discard = m_hand[_index];
        m_hand.erase(m_hand.begin() + _index);
        return true;
    }
    else {
        return false;
    }
}

Although with Go Fish, it probably makes more sense to discard a card by value:

bool Player::Discard(Card const& card)
{
    size_t cur = m_hand.size();
    m_hand.erase(std::remove(m_hand.begin(), m_hand.end(), card), m_hand.end());
    return m_hand.size() != cur;
}

There are 52 cards in a standard deck and an unsigned char can represent more than 52 distinct values.

Assign zero as a 'no card' value and use values 1 - 52 to represent the cards. Instead of removing cards from the array and then rearranging it, simply overwrite the value to be erased with zero.

When adding a card to the array, iterate until you find an index where the value is zero.

ok so I figured it out

bool Player::Discard(int _index, Card& _discard)
{
   if (_index < m_numCards)
{
    _discard = m_hand[_index];

    for (int i = _index + 1; i < m_numCards; ++i)
    {
        m_hand[i - 1] = m_hand[i];
    }
    m_numCards -= 1;
    return true;
      }
       else
    return false;

   }

Even though this is probably homework assignment, you should really use built-in methods in objects such as std::vector or std::list . If you really want to use arrays, you can delete an element by moving all elements to the left that are after the element you want to delete. It would look something like this:

for (int i = 0; i < cardsLength; i ++) {
    if (cards[i] == theCardYouWantToDelete) {
        for (int j = i; j < cardsLength - 1; j ++) {
            cards[j] = cards[j + 1];
        }
        break; // to break out of outer for loop
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM