Face - Shepphard/Rubics GitHub Wiki
Face.h and Face.cpp The class Face gives you the possibility to line up 9 _Tile_s to form one Face of a Rubics Cube. It also contains methods to rotate the Faces and a getter and setter to manipulate the single Tiles.
Face.h
class Face
{
private:
Tile* f_tiles;
public:
//Constructors and Destructor
Face(int color = -1);
Face(const Face& f);
~Face();
//Face Rotation
void rotateRight();
void rotateLeft();
//Get Tile and Set Tile Color at specific row and col
int getTileColor(int index) const;
void setTileColor(int index, int color);
};
Face.cpp
// Create a uniform Face with same color.
Face::Face(int color)
{
f_tiles = new Tile[9];
for(int i = 0; i < 9; i++)
{
f_tiles[i].setColor(color);
}
}
The above Method is the standard constructor. It creates an array of 9 Tiles in f_tiles. It then sets the color of each tile given by the argument in the constructors header.
//Copy Constructor
Face::Face(const Face& f)
{
f_tiles = new Tile[9];
for(int i = 0; i < 9; i++)
{
f_tiles[i].setColor(f.getTileColor(i));
}
}
This copy Constructor creates a deep copy of one object, so tat the same object isn't referenced.
// Destructor
Face::~Face()
{
delete [] f_tiles;
}
The destructor cleans up the memory taken by f_tiles.
// Rotate whole face by -90°
void Face::rotateRight()
{
//Make temporary copy of the face
Face* temp = new Face(*this);
//Perform the changes
this->setTileColor(0, temp->getTileColor(6));
this->setTileColor(1, temp->getTileColor(3));
this->setTileColor(2, temp->getTileColor(0));
this->setTileColor(3, temp->getTileColor(7));
this->setTileColor(4, temp->getTileColor(4));
this->setTileColor(5, temp->getTileColor(1));
this->setTileColor(6, temp->getTileColor(8));
this->setTileColor(7, temp->getTileColor(5));
this->setTileColor(8, temp->getTileColor(2));
delete temp;
}
//Rotate face by 90°
void Face::rotateLeft()
{
//Make temporary copy of the Face
Face* temp = new Face(*this);
//Perform the changes
this->setTileColor(0, temp->getTileColor(2));
this->setTileColor(1, temp->getTileColor(5));
this->setTileColor(2, temp->getTileColor(8));
this->setTileColor(3, temp->getTileColor(1));
this->setTileColor(4, temp->getTileColor(4));
this->setTileColor(5, temp->getTileColor(7));
this->setTileColor(6, temp->getTileColor(0));
this->setTileColor(7, temp->getTileColor(3));
this->setTileColor(8, temp->getTileColor(6));
delete temp;
}
int Face::getTileColor(int index) const
{
return f_tiles[index].getColor();
}
void Face::setTileColor(int index, int color)
{
f_tiles[index].setColor(color);
}