Vector problem

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Asfodel
Posts: 5
Joined: May 15th, 2017, 8:32 am

Vector problem

Post by Asfodel » May 15th, 2017, 8:57 am

Vector of objects, each element in vector contains:
position.x, position.y, color, type... bla bla

after matching some conditions i need to change position.x and position.y in one of the objects in vector. How to change that?

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Vector problem

Post by chili » May 15th, 2017, 9:12 am

Not sure what the problem is. Maybe show us some code.

It sounds like you should just use the [] operator on the vector to access and change the object.
Chili

Asfodel
Posts: 5
Joined: May 15th, 2017, 8:32 am

Re: Vector problem

Post by Asfodel » May 15th, 2017, 9:32 am

Im trying to make chess game:

Code: Select all

**Piece.h**
enum PIECE_TYPE
{
	ROOK,KNIGHT, BISHOP, QUEEN, KING, PAWN
};

enum PIECE_COLOR
{
	BLACK, WHITE
};

struct PieceCoordinatesOnBoard
{
	int x;
	int y;
};

class Piece
{
public:
	Piece();
	~Piece();

	void initGame();

	PIECE_TYPE type;
	PIECE_COLOR color;
	PieceCoordinatesOnBoard position;
	PieceCoordinatesOnBoard destination;

	std::vector<Piece*> figurice;

Code: Select all

from -> **Game.cpp**

void Game::moveSelectedPiece(Piece &piece)
{

	piece.destination.x = piece.getRightClickX();
	piece.destination.y = piece.getRightClickY();

	for (size_t i = 0; i < piece.figurice.size(); i++)
	{
		int posX = piece.figurice[i]->position.x;
		int posY = piece.figurice[i]->position.y;

		if (posX == piece.getLeftClickX() && posY == piece.getLeftClickY())
		{
			if (isPieceMoveValid(piece.figurice[i], piece.destination) == true)
			{
				posX = piece.destination.x;
				posY = piece.destination.y;

				/*piece.figurice.push_back(piece.figurice[i]);*/
					
			}
		}
	}
when i debug its all ok till last part when it need to change coordinates of selected element in vector. It always stays on starting coordinates.
I have all 32 pieces on board stored in vector "figurice".

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: Vector problem

Post by LuisR14 » May 15th, 2017, 10:00 am

push_back just adds *new* elements to the vector, so instead just do:

Code: Select all

piece.figurice[i]->position.x = piece.destination.x;
piece.figurice[i]->position.y = piece.destination.y;
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

Asfodel
Posts: 5
Joined: May 15th, 2017, 8:32 am

Re: Vector problem

Post by Asfodel » May 15th, 2017, 10:36 am

thanks

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Vector problem

Post by chili » May 15th, 2017, 12:06 pm

Why does Piece contain a vector of pointers to piece?
Chili

Asfodel
Posts: 5
Joined: May 15th, 2017, 8:32 am

Re: Vector problem

Post by Asfodel » May 15th, 2017, 8:05 pm

oh... didn't think about that, it should be outside?

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: Vector problem

Post by LuisR14 » May 16th, 2017, 1:03 am

yea, what chili said after noticing him hehe (had only overlooked before)
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

Asfodel
Posts: 5
Joined: May 15th, 2017, 8:32 am

Re: Vector problem

Post by Asfodel » May 16th, 2017, 9:54 am

ok, thanks! Hope i'll finish this game in day or two and post code here, if someone have any tip, suggestion, advice how to improve, i'll appreciate that.

Post Reply