简体   繁体   English

如何检查数组中的索引是否为空以及如何移动数组

[英]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. 我具有Go Fish Card游戏的此功能。

// Remove a Card from the player's hand //从玩家手中取出卡

// In: _index The index of the card to remove from the array // In:_index要从阵列中删除的卡的索引

// _discard A reference to store that value in // _discard将该值存储在的引用

// // Return: True if there was a card actually stored at that index. // //返回:如果在该索引处实际存储有卡,则为True。

// False if the index was "empty" //如果索引为“空”,则为False

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 例如:[7♥3♣5♦9♥] m_numCards:4

  • discarding index 1 (the 3♣) * 丢弃索引1(3♣)*

[ 7♥ 5♦ 9♥ 9♥ ] m_numCards: 3 [7♥5♦9♥9♥] m_num卡数: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. 即使看起来有两个9♥,我们也不会向用户显示该数字。如果将另一张卡添加到手上,它将是第一个被覆盖的9♥。 **/ ** /

    // 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. 那么您总是有7张卡。 Sure you could add a "sentinel" Card value or something, but really there's always 7 cards. 当然,您可以添加“前哨” Card值,但实际上总有7张卡。 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: 尽管使用Go Fish,按值丢弃卡片可能更有意义:

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. 一个标准牌组中有52张卡片,一个未签名的char可以代表52个以上的不同值。

Assign zero as a 'no card' value and use values 1 - 52 to represent the cards. 将零指定为“无牌”值,并使用值1-52表示牌。 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 . 即使这可能是家庭作业,您也应该在诸如std::vectorstd::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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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