Sista v3.0.0-alpha.1
Sista is a C++ lightweight OS-agnostic library for terminal animations and videogames
|
A specialized Field that handles Pawn swaps without conflicts. More...
#include <field.hpp>
Public Member Functions | |
SwappableField (int, int) | |
Constructor to initialize the SwappableField with specified width and height. | |
~SwappableField () | |
Destructor to clean up resources. | |
void | addPawn (std::shared_ptr< Pawn >) override |
Adds a Pawn to the field and updates the pawnsCount grid. | |
void | removePawn (Pawn *) override |
Removes a Pawn from the field and updates the pawnsCount grid. | |
void | addPawnToSwap (Pawn *, const Coordinates &) |
Adds a Pawn to the set of pawnsToSwap. | |
void | addPawnToSwap (Path &) |
Adds a Path to the set of pawnsToSwap. | |
void | simulateSwaps () |
Simulates the swaps of the pawnsToSwap and removes unfeasible paths. | |
void | applySwaps () |
Applies the swaps of the pawnsToSwap to the field. | |
void | swapTwoPawns (const Coordinates &, const Coordinates &) |
Swaps two Pawns at the specified coordinates. | |
void | swapTwoPawns (Pawn *, Pawn *) |
Swaps two Pawns. | |
![]() | |
void | clear () |
Clears the field by removing all Pawns and resetting the grid. | |
Field (int, int) | |
Constructor to initialize the field with specified width and height. | |
~Field () | |
Destructor to clean up resources. | |
void | print () const |
Prints the entire field to the terminal. | |
void | print (char) const |
Prints the entire field to the terminal. | |
void | print (Border &) const |
Prints the entire field to the terminal. | |
virtual void | addPawn (std::shared_ptr< Pawn >) |
Adds a Pawn to the field at its specified coordinates. | |
virtual void | removePawn (Pawn *) |
Removes a Pawn from the field. | |
virtual void | removePawn (const Coordinates &) |
Removes a Pawn from the field at specified coordinates. | |
virtual void | erasePawn (Pawn *) |
Removes a Pawn from the field at specified coordinates. | |
virtual void | erasePawn (const Coordinates &) |
Removes a Pawn from the field at specified coordinates. | |
void | addPrintPawn (std::shared_ptr< Pawn >) |
Adds a Pawn to the field at its specified coordinates and prints it. | |
void | rePrintPawn (Pawn *) |
Reprints a Pawn at its current coordinates. | |
void | movePawn (Pawn *, const Coordinates &) |
Moves a Pawn to new coordinates. | |
void | movePawn (Pawn *, unsigned short, unsigned short) |
Moves a Pawn to new coordinates. | |
void | movePawnBy (Pawn *, const Coordinates &) |
Moves a Pawn by a relative offset. | |
void | movePawnBy (Pawn *, unsigned short, unsigned short) |
Moves a Pawn by a relative offset. | |
void | movePawnBy (Pawn *, const Coordinates &, Effect) |
Moves a Pawn by a relative offset with specified effect on out-of-bounds coordinates. | |
void | movePawnBy (Pawn *, short int, short int, Effect) |
Moves a Pawn by a relative offset with specified effect on out-of-bounds coordinates. | |
void | movePawnFromTo (const Coordinates &, const Coordinates &) |
Moves a Pawn from one set of coordinates to another. | |
void | movePawnFromTo (unsigned short, unsigned short, unsigned short, unsigned short) |
Moves a Pawn from one set of coordinates to another. | |
Coordinates | movingByCoordinates (Pawn *, short int, short int) const |
Calculates the new coordinates of a Pawn after moving by a relative offset. | |
Coordinates | movingByCoordinates (Pawn *, short int, short int, Effect) const |
Calculates the new coordinates of a Pawn after moving by a relative offset with specified effect on out-of-bounds coordinates. | |
Pawn * | getPawn (const Coordinates &) const |
Gets the Pawn at specified coordinates. | |
Pawn * | getPawn (unsigned short, unsigned short) const |
Gets the Pawn at specified coordinates. | |
bool | isOccupied (const Coordinates &) const |
Checks if specified coordinates are occupied by a Pawn. | |
bool | isOccupied (unsigned short, unsigned short) const |
Checks if specified coordinates are occupied by a Pawn. | |
bool | isOccupied (short int, short int) const |
Checks if specified coordinates are occupied by a Pawn. | |
bool | isOutOfBounds (const Coordinates &) const |
Checks if specified coordinates are out of bounds. | |
bool | isOutOfBounds (unsigned short, unsigned short) const |
Checks if specified coordinates are out of bounds. | |
bool | isOutOfBounds (short int, short int) const |
Checks if specified coordinates are out of bounds. | |
bool | isFree (const Coordinates &) const |
Checks if specified coordinates are free (not occupied by a Pawn). | |
bool | isFree (unsigned short, unsigned short) const |
Checks if specified coordinates are free (not occupied by a Pawn). | |
bool | isFree (short int, short int) const |
Checks if specified coordinates are free (not occupied by a Pawn). | |
void | validateCoordinates (const Coordinates &) const |
Validates that the given coordinates are within bounds and not occupied. | |
void | validateCoordinates (unsigned short, unsigned short) const |
Validates that the given coordinates are within bounds and not occupied. | |
Additional Inherited Members | |
![]() | |
void | cleanCoordinates (const Coordinates &) const |
Cleans the coordinates on screen by printing spaces. | |
void | cleanCoordinates (unsigned short, unsigned short) const |
Cleans the coordinates on screen by printing spaces. | |
![]() | |
std::vector< std::vector< std::shared_ptr< Pawn > > > | pawns |
2D grid of shared pointers to Pawn objects. | |
Cursor | cursor |
int | width |
int | height |
A specialized Field that handles Pawn swaps without conflicts.
The SwappableField class extends the Field class to manage scenarios where multiple Pawns may need to swap positions. It maintains a count of Pawns at each position and a set of paths representing Pawns that need to be moved. The class provides methods to add and remove Pawns, manage the swap paths, and simulate or apply the swaps. This class is designed to ensure that Pawn movements are handled correctly, even when multiple Pawns are attempting to move to the same position.
sista::SwappableField::SwappableField | ( | int | width, |
int | height | ||
) |
Constructor to initialize the SwappableField with specified width and height.
width | The width of the field (number of columns). |
height | The height of the field (number of rows). |
This constructor initializes a SwappableField object with the given dimensions. It sets up a 2D grid to track the number of Pawns at each position and initializes the base Field class.
sista::SwappableField::~SwappableField | ( | ) |
Destructor to clean up resources.
|
overridevirtual |
Adds a Pawn to the field and updates the pawnsCount grid.
pawn | A shared pointer to the Pawn to add. |
This method places the given Pawn on the field at its current coordinates and increments the count of Pawns at that position in the pawnsCount grid. If the coordinates are already occupied, the existing Pawn will be replaced.
Reimplemented from sista::Field.
void sista::SwappableField::addPawnToSwap | ( | Path & | path | ) |
Adds a Path to the set of pawnsToSwap.
This method adds the specified Path to the set of pawnsToSwap, indicating that the Pawn associated with the path needs to be moved. The method ensures that no duplicate paths are added and handles conflicts where multiple Pawns may want to move to the same position.
`std::invalid_argument` | if the pawn is a nullptr. |
`std::out_of_range` | if the destination coordinates of the path are out of bounds. |
void sista::SwappableField::addPawnToSwap | ( | Pawn * | pawn, |
const Coordinates & | destination | ||
) |
Adds a Pawn to the set of pawnsToSwap.
pawn | A pointer to the Pawn to add. |
destination | The Coordinates representing the destination of the Pawn. |
This method adds the specified Pawn and its target coordinates to the set of pawnsToSwap, indicating that the Pawn needs to be moved. The method ensures that no duplicate entries are added and handles conflicts where multiple Pawns may want to move to the same position.
`std::invalid_argument` | if the pawn is a nullptr. |
`std::out_of_range` | if the destination coordinates are out of bounds. |
void sista::SwappableField::applySwaps | ( | ) |
Applies the swaps of the pawnsToSwap to the field.
This method executes the movements of the Pawns as specified in the pawnsToSwap set. It updates the positions of the Pawns on the field and clears the pawnsToSwap set afterward.
|
overridevirtual |
Removes a Pawn from the field and updates the pawnsCount grid.
pawn | A pointer to the Pawn to remove. |
This method removes the specified Pawn from the field based on its coordinates and decrements the count of Pawns at that position in the pawnsCount grid. If the Pawn is not found at its coordinates, no action is taken.
Reimplemented from sista::Field.
void sista::SwappableField::simulateSwaps | ( | ) |
Simulates the swaps of the pawnsToSwap and removes unfeasible paths.
This method processes the set of pawnsToSwap to simulate the movements of the Pawns. It identifies and removes any paths that cannot be executed due to conflicts or cycles, ensuring that only feasible swaps remain in the set.
void sista::SwappableField::swapTwoPawns | ( | const Coordinates & | first, |
const Coordinates & | second | ||
) |
Swaps two Pawns at the specified coordinates.
first | The Coordinates of the first Pawn. |
second | The Coordinates of the second Pawn. |
This method swaps the positions of the two Pawns located at the given coordinates on the field.