Jump to content
  • Advertisement
Sign in to follow this  
kerilynne

How to make a pointer to an STD vector

This topic is 3786 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In my hand class, the m_card_list vector is not somthing I would like to be public in the class.
class Hand
{
public:
	Hand(){}

	std::string & read_cards(void);
	int value(void);
	bool winning_hand(void);

	std::vector<Card> * card_list()
	{
	}

private:
	std::vector<Card> m_card_list;
	std::string m_hand_string;
	std::string m_show_hand;
	bool m_winning_hand;
};
I want to be able to return a pointer to the card list so I can use functions like this:
	void draw_card(std::string player_id, int hand_id, int deck_id, int how_many)
	{
		for(int loop = 0; loop < how_many; loop++)
		{	
			m_rHands.Request_Resource(player_id).px->at(hand_id)->m_card_list.push_back(m_vDeck[deck_id]->m_cards.back());
			m_vDeck[deck_id]->m_cards.pop_back();
		}
	}
And instead of m_rHands.Request_Resource(player_id).px->at(hand_id)->m_card_list.push_back I want to go m_rHands.Request_Resource(player_id).px->at(hand_id)->Card_List()->push_back Can anyone help me with the syntax for setting up a pointer to vector?

Share this post


Link to post
Share on other sites
Advertisement
I'd imagine replacing
std::vector<Card> m_card_list;


with
std::vector<Card> *m_card_list;


in your class def and putting
m_card_list = new std::vector<Card>;


in your constructor and
delete m_card_list;


in the destructor would work... right?
Then just return m_card_list in your card_list() method.

Not very safe, though - and since you need to be able to modify it, you can't make it const. I'd personally write wrapper functions to prevent messing with the vector itself.

Share this post


Link to post
Share on other sites
Don't return it by pointer, that's far too risque, return it by reference, like so:
std::vector<Card> & Hand::card_list()
{
return m_card_list;
}

// Then in draw_card
m_rHands.Request_Resource(player_id).px->at(hand_id)->card_list().push_back(m_vDeck[deck_id]->m_cards.back());

This is anything but object oriented however.
What would be even better is to promote to problem domain into the Hand class with an addCard method:
void Hand::addCard(const Card& card)
{
m_card_list.push_back(card);
}

Share this post


Link to post
Share on other sites
Dmatter you made me realize something today.

I was treating my hand class and my card class and my player classes as pure data objects, or trying to keep that way.

In reality I just think that in some cases we can let the classes do it themselves instead of outsourcing everything to a header file with spaghetti code, like this:


#ifndef GAME_H
#define GAME_H
#include <vector>
#include "Player.h"
#include "Hand.h"
#include "Resource_Manager.h"

class Card_Game
{
public:
void init();
void cleanup();

Card_Game();

virtual void turn_logic(std::string player_id);
virtual void menu_logic();

private:
int calculate_winnings();
void initialize_player(std::string player_id)
{
Player * new_player = new Player(player_id);
m_rPlayers.Manage_Resource( player_id, new_player );
}
// Give player id a new hand
void give_player_hand(std::string player_id)
{
Hand * p_hand = new Hand();
m_rHands.Request_Resource(player_id).px->push_back(p_hand);
}
void shuffle_deck(int deck_id)
{
random_shuffle(m_vDeck[deck_id]->m_cards.begin(), m_vDeck[deck_id]->m_cards.end());
}

// Draw how_many cards to player id's hand_id from deck_idntyd
void draw_card(std::string player_id, int hand_id, int deck_id, int how_many)
{
for(int loop = 0; loop < how_many; loop++)
{
m_rHands.Request_Resource(player_id).px->at(hand_id)->m_card_list.push_back(m_vDeck[deck_id]->m_cards.back());
m_vDeck[deck_id]->m_cards.pop_back();
}
}

void place_bet(std::string player_id, int bet)
{
int * p_bet = &bet;
m_rPlayers.Request_Resource(player_id).px->m_cash -= bet;
m_rBets.Manage_Resource(player_id, &(p_bet));
}
// Split player's active hand
void split_hand(std::string player_id, int hand_id)
{
give_player_hand(player_id);
m_rHands.Request_Resource(player_id).px->back()->m_card_list.push_back(m_rHands.Request_Resource(player_id).px->at(hand_id)->m_card_list.back());
m_rHands.Request_Resource(player_id).px->at(hand_id)->m_card_list.pop_back();
}
// check if hand value > 21, if true player loses
bool validate_hand(std::string player_id, int hand_id)
{
if (m_rHands.Request_Resource(player_id).px->at(hand_id)->value() > 21)
{
return true; // Player loses
}
else
{
return false; // continue game
}
}



int & minimum_bet(); // function to get the minimum bet
Resource_Manager< Player > m_rPlayers;
Resource_Manager< std::vector<Hand*> > m_rHands;
Resource_Manager< int* > m_rBets;
std::vector< Deck* > m_vDeck;

int m_minimum_bet;
int m_result;
bool m_ALL_bets_on_house;
bool m_load_new_players;
bool m_betting_toggle;
};
#endif


I could clean up my engine functions alot by outsourcing some of the logic to the classes themselves, like you suggested with this bit of code:


void add_card(const Card & card)
{
m_card_list.push_back(card);
}


Thanks for the input.

Share this post


Link to post
Share on other sites

Card & Deck::draw_card()
{
return m_cards.back();
m_cards.pop_back();
}


Will this work as expected? After returning the last card it will remove it from the card list?

Share this post


Link to post
Share on other sites
Quote:
Original post by kerilynne

Card & Deck::draw_card()
{
return m_cards.back();
m_cards.pop_back();
}


Will this work as expected? After returning the last card it will remove it from the card list?


Obviously not. Return returns from method. Make a temporary.

Also, turn up your compiler warning levels. This should pop up as warning for unreachable code.

Share this post


Link to post
Share on other sites
-Wall

FTW :)

And BTW, even if this:

Quote:

Card & Deck::draw_card()

{

return m_cards.back();

m_cards.pop_back();

}


Did work, you would be deleting the data which you are making a reference to ( assuming the contents of m_cards were stack allocated ) :/

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
Quote:
Original post by kerilynne

Card & Deck::draw_card()
{
return m_cards.back();
m_cards.pop_back();
}


Will this work as expected? After returning the last card it will remove it from the card list?


Obviously not. Return returns from method. Make a temporary.

Also, turn up your compiler warning levels. This should pop up as warning for unreachable code.



Make a temporary card out of m_cards.back(), m_cards.pop_back, return temp card?

Share this post


Link to post
Share on other sites
Quote:
Original post by kerilynne
Quote:
Original post by Antheus
Quote:
Original post by kerilynne

Card & Deck::draw_card()
{
return m_cards.back();
m_cards.pop_back();
}


Will this work as expected? After returning the last card it will remove it from the card list?


Obviously not. Return returns from method. Make a temporary.

Also, turn up your compiler warning levels. This should pop up as warning for unreachable code.



Make a temporary card out of m_cards.back(), m_cards.pop_back, return temp card?

Yes, and change the return type to return by value, not reference.

Share this post


Link to post
Share on other sites

Card draw_card()
{
Card c(m_cards.back().m_suit, m_cards.back().m_value);
m_cards.pop_back();
return c;
}


Is this what you guys ment?

Now my add_card fuction in the hand looks like this:

void add_card(const Card & card)
{
m_card_list.push_back(card);
}


And I draw cards like this:


void draw_card(std::string player_id, int hand_id, int deck_id, int how_many)
{
for(int loop = 0; loop < how_many; loop++)
{
m_rHands.Request_Resource(player_id).px->at(hand_id)->add_card(m_vDeck[deck_id]->draw_card());
}
}



How's that look guys?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!