I dont Know how to define this function

I have this function and i have tried various things but they dont work, How can I define it?

this is in the Header of the clas. This are the member variables;
char m_name[32];
Card m_hand[7];
int m_numCards;
int m_maxCards;
int m_score;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  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
	return true;
}

can you provide what the compiler shows as errors and warnings ?
i didnt get any errors with what I wrote in the body, but it wasnt doing what it was supposed to do so I deleted what i wrote
so actually you want to remove a Card from your deck called m_hand ?
m_hand is an array of Card.

i have to remove a card from the array and shift the array back to show that the card is removed
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.
i dont think i have touch on that subject yet
then look at this thread , was it helpful for the shifting ?
http://www.cplusplus.com/forum/general/16477/
Last edited on
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.

would this be alright?

if(m_hand[_index] !=0)
{
return true;
}
else
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.
Last edited on
@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.
I got this.

1
2
3
4
5
6
7
8
9
10
11
12
13
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;
		return true;
	}
	else
		return false;



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

FAILURE: 1 out of 1 tests failed (4 failures).
m_hand[_index] != 0
What are you trying to achieve? Why do you care about your card value?

m_numCards != 0
Does not quite acieves what you need.

m_numCards <= m_maxCards
This is your class invariant. You do not need to check it, as if it false , then your class itself is broken.

YOu need just a single condition. I repeat:
index should be inside your array, so you need to check if it is less than number of cards in array.
Hint: index is _index and number of cards in array is m_numCards
ahh I was doing it
if(m_hand[_index] < m_numCards)

instead of if (_index < m_numCards)

so i started messing around with it

alright it is fixed thanks,
Topic archived. No new replies allowed.