Sista v3.0.0-alpha.1
Sista is a C++ lightweight OS-agnostic library for terminal animations and videogames
Loading...
Searching...
No Matches
Public Member Functions | List of all members
sista::SwappableField Class Referencefinal

A specialized Field that handles Pawn swaps without conflicts. More...

#include <field.hpp>

Inheritance diagram for sista::SwappableField:
Inheritance graph
[legend]
Collaboration diagram for sista::SwappableField:
Collaboration graph
[legend]

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.
 
- Public Member Functions inherited from sista::Field
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.
 
PawngetPawn (const Coordinates &) const
 Gets the Pawn at specified coordinates.
 
PawngetPawn (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

- Protected Member Functions inherited from sista::Field
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.
 
- Protected Attributes inherited from sista::Field
std::vector< std::vector< std::shared_ptr< Pawn > > > pawns
 2D grid of shared pointers to Pawn objects.
 
Cursor cursor
 
int width
 
int height
 

Detailed Description

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.

See also
Field
Pawn
Coordinates
Path

Constructor & Destructor Documentation

◆ SwappableField()

sista::SwappableField::SwappableField ( int  width,
int  height 
)

Constructor to initialize the SwappableField with specified width and height.

Parameters
widthThe width of the field (number of columns).
heightThe 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.

See also
Field

◆ ~SwappableField()

sista::SwappableField::~SwappableField ( )

Destructor to clean up resources.

Member Function Documentation

◆ addPawn()

void sista::SwappableField::addPawn ( std::shared_ptr< Pawn pawn)
overridevirtual

Adds a Pawn to the field and updates the pawnsCount grid.

Parameters
pawnA 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.

Warning
The Pawn's coordinates must be valid (within the field bounds) as they are not validated here.
Note
The method takes a shared pointer to manage the Pawn's memory automatically.
See also
Field::addPawn
Pawn

Reimplemented from sista::Field.

◆ addPawnToSwap() [1/2]

void sista::SwappableField::addPawnToSwap ( Path path)

Adds a Path to the set of pawnsToSwap.

Parameters
pathA Path representing the Pawn movement to add.

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.

Exceptions
`std::invalid_argument`if the pawn is a nullptr.
`std::out_of_range`if the destination coordinates of the path are out of bounds.
Note
If the start and end coordinates of the path are the same, the path is ignored.
See also
Path
pawnsToSwap

◆ addPawnToSwap() [2/2]

void sista::SwappableField::addPawnToSwap ( Pawn pawn,
const Coordinates destination 
)

Adds a Pawn to the set of pawnsToSwap.

Parameters
pawnA pointer to the Pawn to add.
destinationThe 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.

Exceptions
`std::invalid_argument`if the pawn is a nullptr.
`std::out_of_range`if the destination coordinates are out of bounds.
Note
If the Pawn's current coordinates are the same as the target coordinates, it is ignored.
See also
Pawn
Coordinates
pawnsToSwap

◆ applySwaps()

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.

See also
pawnsToSwap
Path

◆ removePawn()

void sista::SwappableField::removePawn ( Pawn pawn)
overridevirtual

Removes a Pawn from the field and updates the pawnsCount grid.

Parameters
pawnA 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.

Warning
The Pawn's coordinates must be valid (within the field bounds) as they are not validated here.
Note
The method does not delete the Pawn object, it only removes it from the field.
See also
Field::removePawn
Pawn

Reimplemented from sista::Field.

◆ simulateSwaps()

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.

Todo:
Make the function return the paths that were removed as unfeasible.
See also
pawnsToSwap

◆ swapTwoPawns() [1/2]

void sista::SwappableField::swapTwoPawns ( const Coordinates first,
const Coordinates second 
)

Swaps two Pawns at the specified coordinates.

Parameters
firstThe Coordinates of the first Pawn.
secondThe Coordinates of the second Pawn.

This method swaps the positions of the two Pawns located at the given coordinates on the field.

Warning
The coordinates must be valid (within the field bounds) as they are not validated here.
See also
Pawn
Coordinates

◆ swapTwoPawns() [2/2]

void sista::SwappableField::swapTwoPawns ( Pawn first,
Pawn second 
)

Swaps two Pawns.

Parameters
firstA pointer to the first Pawn.
secondA pointer to the second Pawn.

This method swaps the positions of the two specified Pawns on the field.

Warning
The Pawns must have valid coordinates (within the field bounds) as they are not validated here.
See also
Pawn

The documentation for this class was generated from the following files: