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
// _rid一个引用来存储该值
// // Return: True if there was a card actually stored at that index.
// // Return:如果有一张卡实际存储在该索引处,则为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_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. **/
尽管它看起来像有两个9♥,我们不会被显示给用户,并将成为第一个被覆盖如果添加另一张牌的手。* * /
// Just here for compilation
//就在这里编译
in the header i have this member variables.
在header中,我有这个成员变量。
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;
4 个解决方案
#1
1
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:
那么你总有7张牌。当然你可以添加一个“哨兵”牌值或其他东西,但实际上总有7张牌。不能删除或附加原始数组。另一方面,如果你用动态容器代替:
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;
}
#2
0
There are 52 cards in a standard deck and an unsigned char can represent more than 52 distinct values.
在标准的卡片组中有52张卡片,无符号字符可以代表超过52个不同的值。
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.
将0指定为“无卡”值,并使用值1 - 52表示卡片。不要从数组中删除卡片,然后重新排列它们,只需用0覆盖要擦除的值。
When adding a card to the array, iterate until you find an index where the value is zero.
当向数组添加card时,迭代直到找到一个值为0的索引。
#3
0
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;
}
#4
-1
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:
尽管这可能是家庭作业,但您应该在对象中使用内置的方法,比如std::vector或std::list。如果你真的想使用数组,你可以通过将所有元素移动到你想删除的元素的左边来删除一个元素。大概是这样的:
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
}
}
#1
1
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:
那么你总有7张牌。当然你可以添加一个“哨兵”牌值或其他东西,但实际上总有7张牌。不能删除或附加原始数组。另一方面,如果你用动态容器代替:
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;
}
#2
0
There are 52 cards in a standard deck and an unsigned char can represent more than 52 distinct values.
在标准的卡片组中有52张卡片,无符号字符可以代表超过52个不同的值。
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.
将0指定为“无卡”值,并使用值1 - 52表示卡片。不要从数组中删除卡片,然后重新排列它们,只需用0覆盖要擦除的值。
When adding a card to the array, iterate until you find an index where the value is zero.
当向数组添加card时,迭代直到找到一个值为0的索引。
#3
0
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;
}
#4
-1
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:
尽管这可能是家庭作业,但您应该在对象中使用内置的方法,比如std::vector或std::list。如果你真的想使用数组,你可以通过将所有元素移动到你想删除的元素的左边来删除一个元素。大概是这样的:
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
}
}