我目前正在制作Line3类型的游戏,我在尝试交换数组中的两个对象时遇到问题。我的游戏有两个类:Gem类,你必须排队获得点和Board类,它包含一个Gem数组[6] [6]。
问题在于我的交换方法。在方法内部,我有一些cout来帮助我看到并且交换实际上已完成,但我认为我在副本上执行此操作后,当我刷新我的板时,Gems不会互换。这是我的代码,随时提出任何建议:
Board.h
#pragma once
#include "Gem.h"
class Board
{
public:
Board();
~Board();
void gameLoop();
void refreshGameGrid();
void refillGameGrid();
void checkIfStacks();
void swapGems(Gem& p_firstGem, Gem& p_secondGem);
Gem getGem(int line, int column);
private:
Gem m_gameGrid[6][6];
};
Board.CPP
// Swap the position of two Gems on the Board.
void Board::swapGems(Gem& p_firstGem, Gem& p_secondGem)
{
cout <<"Avant : " << p_firstGem.getRepresentation() << " | " << p_secondGem.getRepresentation() << endl;
p_firstGem.switchWith(p_secondGem);
cout << "Après : " << p_firstGem.getRepresentation() << " | " << p_secondGem.getRepresentation() << endl;
}
Gem.h
#pragma once
#include "gemMania.h"
class Gem
{
public:
Gem();
~Gem();
// Getters
const string& getRepresentation();
const string& getColor();
const string& getPosition();
int getNumberOfGems();
// Setters
void setColorAndRepr(const string&, const string&);
//Methods
void switchWith(Gem&);
void destroyGem();
void renewGem();
private:
string m_representation;
string m_color;
string m_position;
static int numberOfGems;
};
Gem.cpp
// Switch the attrbutes of two gems, hence swaping the two of them.
void Gem::switchWith(Gem& p_otherGem)
{
swap(this->m_color, p_otherGem.m_color);
swap(this->m_representation, p_otherGem.m_representation);
}
Picture of what's happening in console
非常感谢所有愿意花时间帮助我的人!