Sista v3.0.0-alpha.1
Sista is a C++ lightweight OS-agnostic library for terminal animations and videogames
|
Represents a 2D grid where Pawns can be placed, moved, and managed. More...
#include <field.hpp>
Public Member Functions | |
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. | |
Protected Member Functions | |
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 | |
std::vector< std::vector< std::shared_ptr< Pawn > > > | pawns |
2D grid of shared pointers to Pawn objects. | |
Cursor | cursor |
int | width |
int | height |
Represents a 2D grid where Pawns can be placed, moved, and managed.
The Field class encapsulates a 2D grid structure using a vector of vectors to hold shared pointers to Pawn objects. It provides methods to add, remove, move, and print Pawns on the field. The class also includes functionality to check if specific coordinates are occupied, free, or out of bounds, and to validate coordinates.
The Field class is designed to be extended for more specialized behavior, such as in the SwappableField subclass, which handles scenarios where multiple Pawns may need to swap positions without conflicts.
sista::Field::Field | ( | int | width_, |
int | height_ | ||
) |
Constructor to initialize the field 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 Field object with the given dimensions. It sets up a 2D grid (vector of vectors) to hold shared pointers to Pawn objects, and initializes the Cursor for terminal operations.
sista::Field::~Field | ( | ) |
Destructor to clean up resources.
|
virtual |
Adds a Pawn to the field at its specified coordinates.
pawn | A shared pointer to the Pawn to add. |
This method places the given Pawn on the field at its current coordinates. If the coordinates are already occupied, the existing Pawn will be replaced.
Reimplemented in sista::SwappableField.
void sista::Field::addPrintPawn | ( | std::shared_ptr< Pawn > | pawn | ) |
Adds a Pawn to the field at its specified coordinates and prints it.
pawn | A shared pointer to the Pawn to add and print. |
This method places the given Pawn on the field at its current coordinates and prints it to the terminal. If the coordinates are already occupied, the existing Pawn will be replaced.
|
protected |
Cleans the coordinates on screen by printing spaces.
Height of the matrix
coordinates | The Coordinates to clean. |
|
protected |
Cleans the coordinates on screen by printing spaces.
y | The y coordinate (row). |
x | The x coordinate (column). |
void sista::Field::clear | ( | ) |
Clears the field by removing all Pawns and resetting the grid.
|
virtual |
Removes a Pawn from the field at specified coordinates.
coordinates | The Coordinates of the Pawn to remove. |
This method removes the Pawn located at the given coordinates from the field. Unlike removePawn, this method also cleans the cell on the terminal by printing a space.
|
virtual |
Removes a Pawn from the field at specified coordinates.
pawn | A pointer to the Pawn to remove. |
This method removes the given Pawn from the field. Unlike removePawn, this method also cleans the cell on the terminal by printing a space.
Pawn * sista::Field::getPawn | ( | const Coordinates & | coordinates | ) | const |
Gets the Pawn at specified coordinates.
coordinates | The Coordinates to get the Pawn from. |
This method retrieves the Pawn located at the specified coordinates on the field. If no Pawn is found at those coordinates, the method returns nullptr.
Pawn * sista::Field::getPawn | ( | unsigned short | y, |
unsigned short | x | ||
) | const |
Gets the Pawn at specified coordinates.
y | The y coordinate (row) to get the Pawn from. |
x | The x coordinate (column) to get the Pawn from. |
This method retrieves the Pawn located at the specified coordinates on the field. If no Pawn is found at those coordinates, the method returns nullptr.
bool sista::Field::isFree | ( | const Coordinates & | coordinates | ) | const |
Checks if specified coordinates are free (not occupied by a Pawn).
coordinates | The Coordinates to check. |
true
if the coordinates are free, false
otherwise.This method checks if there is no Pawn located at the specified coordinates on the field.
bool sista::Field::isFree | ( | short int | y, |
short int | x | ||
) | const |
Checks if specified coordinates are free (not occupied by a Pawn).
y | The y coordinate (row) to check. |
x | The x coordinate (column) to check. |
true
if the coordinates are free, false
otherwise.This method checks if there is no Pawn located at the specified coordinates on the field.
bool sista::Field::isFree | ( | unsigned short | y, |
unsigned short | x | ||
) | const |
Checks if specified coordinates are free (not occupied by a Pawn).
y | The y coordinate (row) to check. |
x | The x coordinate (column) to check. |
true
if the coordinates are free, false
otherwise.This method checks if there is no Pawn located at the specified coordinates on the field.
bool sista::Field::isOccupied | ( | const Coordinates & | coordinates | ) | const |
Checks if specified coordinates are occupied by a Pawn.
coordinates | The Coordinates to check. |
true
if the coordinates are occupied, false
otherwise.This method checks if there is a Pawn located at the specified coordinates on the field.
bool sista::Field::isOccupied | ( | short int | y, |
short int | x | ||
) | const |
Checks if specified coordinates are occupied by a Pawn.
y | The y coordinate (row) to check. |
x | The x coordinate (column) to check. |
true
if the coordinates are occupied, false
otherwise.This method checks if there is a Pawn located at the specified coordinates on the field.
bool sista::Field::isOccupied | ( | unsigned short | y, |
unsigned short | x | ||
) | const |
Checks if specified coordinates are occupied by a Pawn.
y | The y coordinate (row) to check. |
x | The x coordinate (column) to check. |
true
if the coordinates are occupied, false
otherwise.This method checks if there is a Pawn located at the specified coordinates on the field.
bool sista::Field::isOutOfBounds | ( | const Coordinates & | coordinates | ) | const |
Checks if specified coordinates are out of bounds.
coordinates | The Coordinates to check. |
true
if the coordinates are out of bounds, false
otherwise.This method checks if the given coordinates are outside the valid range of the field.
bool sista::Field::isOutOfBounds | ( | short int | y, |
short int | x | ||
) | const |
Checks if specified coordinates are out of bounds.
y | The y coordinate (row) to check. |
x | The x coordinate (column) to check. |
true
if the coordinates are out of bounds, false
otherwise.This method checks if the given coordinates are outside the valid range of the field.
bool sista::Field::isOutOfBounds | ( | unsigned short | y, |
unsigned short | x | ||
) | const |
Checks if specified coordinates are out of bounds.
y | The y coordinate (row) to check. |
x | The x coordinate (column) to check. |
true
if the coordinates are out of bounds, false
otherwise.This method checks if the given coordinates are outside the valid range of the field.
void sista::Field::movePawn | ( | Pawn * | pawn, |
const Coordinates & | coordinates | ||
) |
Moves a Pawn to new coordinates.
pawn | A pointer to the Pawn to move. |
coordinates | The new Coordinates to move the Pawn to. |
This method moves the specified Pawn to the given coordinates on the field. If the target coordinates are already occupied, the existing Pawn will be replaced.
`std::invalid_argument` | if the coordinates are occupied by another Pawn or out of bounds. |
void sista::Field::movePawn | ( | Pawn * | pawn, |
unsigned short | y, | ||
unsigned short | x | ||
) |
Moves a Pawn to new coordinates.
pawn | A pointer to the Pawn to move. |
y | The new y coordinate (row) to move the Pawn to. |
x | The new x coordinate (column) to move the Pawn to. |
This method moves the specified Pawn to the given coordinates on the field. If the target coordinates are already occupied, the existing Pawn will be replaced.
`std::invalid_argument` | if the coordinates are occupied by another Pawn or out of bounds. |
void sista::Field::movePawnBy | ( | Pawn * | pawn, |
const Coordinates & | coordinates | ||
) |
Moves a Pawn by a relative offset.
pawn | A pointer to the Pawn to move. |
coordinates | The relative Coordinates to move the Pawn by. |
This method moves the specified Pawn by the given relative offset.
`std::invalid_argument` | if the resulting coordinates are occupied by another Pawn or out of bounds. |
void sista::Field::movePawnBy | ( | Pawn * | pawn, |
const Coordinates & | coordinates, | ||
Effect | effect | ||
) |
Moves a Pawn by a relative offset with specified effect on out-of-bounds coordinates.
pawn | A pointer to the Pawn to move. |
coordinates | The relative Coordinates to move the Pawn by. |
effect | The Effect to apply when the resulting coordinates are out of bounds. |
This method moves the specified Pawn by the given relative offset, applying the specified effect if the resulting coordinates go out of bounds.
`std::invalid_argument` | if the resulting coordinates are occupied by another Pawn after applying the effect. |
`std::out_of_range` | if the effect is MATRIX and the resulting coordinates are out of bounds. |
Moves a Pawn by a relative offset with specified effect on out-of-bounds coordinates.
pawn | A pointer to the Pawn to move. |
y | The relative y offset (rows) to move the Pawn by. |
x | The relative x offset (columns) to move the Pawn by. |
effect | The Effect to apply when the resulting coordinates are out of bounds. |
This method moves the specified Pawn by the given relative offset, applying the specified effect if the resulting coordinates go out of bounds.
`std::invalid_argument` | if the resulting coordinates are occupied by another Pawn after applying the effect. |
`std::out_of_range` | if the effect is MATRIX and the resulting coordinates are out of bounds. |
void sista::Field::movePawnBy | ( | Pawn * | pawn, |
unsigned short | y, | ||
unsigned short | x | ||
) |
Moves a Pawn by a relative offset.
pawn | A pointer to the Pawn to move. |
y | The relative y offset (rows) to move the Pawn by. |
x | The relative x offset (columns) to move the Pawn by. |
This method moves the specified Pawn by the given relative offset.
`std::invalid_argument` | if the resulting coordinates are occupied by another Pawn or out of bounds. |
void sista::Field::movePawnFromTo | ( | const Coordinates & | coordinates, |
const Coordinates & | newCoordinates | ||
) |
Moves a Pawn from one set of coordinates to another.
coordinates | The current Coordinates of the Pawn. |
newCoordinates | The new Coordinates to move the Pawn to. |
This method finds the Pawn at the specified current coordinates and moves it to the new coordinates.
`std::invalid_argument` | if there is no Pawn at the current coordinates or if the new coordinates are occupied by another Pawn or out of bounds. |
void sista::Field::movePawnFromTo | ( | unsigned short | y, |
unsigned short | x, | ||
unsigned short | newY, | ||
unsigned short | newX | ||
) |
Moves a Pawn from one set of coordinates to another.
y | The current y coordinate (row) of the Pawn. |
x | The current x coordinate (column) of the Pawn. |
newY | The new y coordinate (row) to move the Pawn to. |
newX | The new x coordinate (column) to move the Pawn to. |
This method finds the Pawn at the specified current coordinates and moves it to the new coordinates.
`std::invalid_argument` | if there is no Pawn at the current coordinates or if the new coordinates are occupied by another Pawn or out of bounds. |
Coordinates sista::Field::movingByCoordinates | ( | Pawn * | pawn, |
short int | y, | ||
short int | x | ||
) | const |
Calculates the new coordinates of a Pawn after moving by a relative offset.
pawn | A pointer to the Pawn to calculate the new coordinates for. |
y | The relative y offset (rows) to move the Pawn by. |
x | The relative x offset (columns) to move the Pawn by. |
This method calculates the new coordinates of the specified Pawn after moving it by the given relative offset. It does not modify the Pawn's actual coordinates, it only computes and returns the new position.
`std::out_of_range` | if the resulting coordinates are out of bounds. |
Coordinates sista::Field::movingByCoordinates | ( | Pawn * | pawn, |
short int | y, | ||
short int | x, | ||
Effect | effect | ||
) | const |
Calculates the new coordinates of a Pawn after moving by a relative offset with specified effect on out-of-bounds coordinates.
pawn | A pointer to the Pawn to calculate the new coordinates for. |
y | The relative y offset (rows) to move the Pawn by. |
x | The relative x offset (columns) to move the Pawn by. |
effect | The Effect to apply when the resulting coordinates are out of bounds. |
This method calculates the new coordinates of the specified Pawn after moving it by the given relative offset. If the resulting coordinates go out of bounds, the specified effect is applied to adjust them accordingly.
`std::out_of_range` | if the resulting coordinates are out of bounds. |
void sista::Field::print | ( | ) | const |
Prints the entire field to the terminal.
This method iterates through the 2D grid of Pawns and prints each Pawn's symbol at its respective coordinates. The method uses the Cursor object to manage terminal cursor positioning.
void sista::Field::print | ( | Border & | border | ) | const |
Prints the entire field to the terminal.
border | A Border object to use for the border. |
This method iterates through the 2D grid of Pawns and prints each Pawn's symbol at its respective coordinates. It also prints a border around the field using the specified Border object. The method uses the Cursor object to manage terminal cursor positioning.
void sista::Field::print | ( | char | border | ) | const |
Prints the entire field to the terminal.
border | A character to use as the border symbol. |
This method iterates through the 2D grid of Pawns and prints each Pawn's symbol at its respective coordinates. It also prints a border around the field using the specified character. The method uses the Cursor object to manage terminal cursor positioning.
|
virtual |
Removes a Pawn from the field at specified coordinates.
coordinates | The Coordinates of the Pawn to remove. |
This method removes the Pawn located at the given coordinates from the field. If no Pawn is found at the specified coordinates, no action is taken.
|
virtual |
Removes a Pawn from the field.
pawn | A pointer to the Pawn to remove. |
This method removes the specified Pawn from the field. If the Pawn is not found at its coordinates, no action is taken.
Reimplemented in sista::SwappableField.
void sista::Field::rePrintPawn | ( | Pawn * | pawn | ) |
Reprints a Pawn at its current coordinates.
pawn | A pointer to the Pawn to reprint. |
This method reprints the specified Pawn at its current coordinates on the terminal. It is useful for updating the Pawn's appearance after changes to its settings or symbol.
void sista::Field::validateCoordinates | ( | const Coordinates & | coordinates | ) | const |
Validates that the given coordinates are within bounds and not occupied.
coordinates | The Coordinates to validate. |
This method checks if the specified coordinates are within the valid range of the field and not occupied by another Pawn. If either condition is not met, an exception is thrown.
`std::invalid_argument` | if the coordinates are occupied by another Pawn. |
`std::out_of_range` | if the coordinates are out of bounds. |
void sista::Field::validateCoordinates | ( | unsigned short | y, |
unsigned short | x | ||
) | const |
Validates that the given coordinates are within bounds and not occupied.
y | The y coordinate (row) to validate. |
x | The x coordinate (column) to validate. |
This method checks if the specified coordinates are within the valid range of the field and not occupied by another Pawn. If either condition is not met, an exception is thrown.
`std::invalid_argument` | if the coordinates are occupied by another Pawn. |
`std::out_of_range` | if the coordinates are out of bounds. |
|
protected |
|
protected |
Width of the matrix
|
protected |
2D grid of shared pointers to Pawn objects.
|
protected |
Cursor object for terminal operations.