bool Player::Discard(int _index, Card& _discard)
{
/* TODO Lab2:
Implement this method.
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
returntrue;
}
using vector container is easy for that , using array it is a little tricky , but basically you would have to move all card to front (like a vector do) and then destroy the card (since it is not a const reference). Or Simply swap and pop (swap the card with the last card (with vector container) and pop_back() ) . You can try that with you array but as I said it is tricky.
can you help me first with the first part of the function?
i need to know if in the _index the input is empty or if it already has a card stored.
if there is a card it should return true and if its empty it should return false.
You could just post lonk to follow-up problem in your previous thread instead of sending PM. That way people who read it could follow through and help too.
Question: bool Player::Discard(int _index, Card& _discard) What purpose of the arguments? If you are going to delete based on index, why pass card? If you need to delete specific card, why pass index?
Second question: are you ok with making a simple standard library call which solves problem of shifting, or you want to do it manually?
// 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"
i forgot to add this^ at the top of the declaration
umm, for the second question i guess Ill have to do it manually, but i will like to know the other method as well for future projects.
@MecNasty as I said simply replace your array by a vector container and do the swap/pop_back technique. Because the problem you are facing is that the size of your array wont change , but the number of your deck(m_hand) will at runtime . So either you keep updating that size value in an attribute called m_handSize , or create an invalidCard object . All that is to prevent you to read at deallocated index , etc.
Ok. First of all you need to check if your request is valid. It is easy: index should be inside your array, so you need to check if it is less than number of cards in array.
Then you need to store discarded card in _discard referenced object (we won't have another chance)
Then we need to shift items backward. It is simple: loop from index one higher than passd index and up to number of elements. On each iteration assign element from current array index to previous one.
1 2
for(int i = _index + 1; i < m_numCards; ++i)
m_hand[i-1] = m_hand[i];
Then you will need to correct m_numCards: actual number of elements in array changed, so we need to reflect that. Decrement m_numCards.
You can substutute loop to call to std::copy or std::rotate if you wish.
if (m_hand[_index] != 0 && m_numCards != 0 && m_numCards <= m_maxCards)
{
_discard = m_hand[_index];
for (int i = _index + 1; i < m_numCards; ++i)
{
m_hand[i - 1] = m_hand[i];
}
m_numCards -= 1;
returntrue;
}
elsereturnfalse;
but still when I run the tests
Attempting to discard an out of bounds index
e:\full sail\pg2\pg2 project\card game\lab2_test.cpp(492): error: Failure in PG2_Lab2_Player_Discard: Expected 0 but was 1
Discarding last card
e:\full sail\pg2\pg2 project\card game\lab2_test.cpp(508): error: Failure in PG2_Lab2_Player_Discard: Expected 4 but was 3
Discarding first card
Discarding second (middle) card
e:\full sail\pg2\pg2 project\card game\lab2_test.cpp(529): error: Failure in PG2_Lab2_Player_Discard: Expected 4 but was 12
Attempting to discard an out of bounds index
e:\full sail\pg2\pg2 project\card game\lab2_test.cpp(535): error: Failure in PG2_Lab2_Player_Discard: Expected 0 but was 1