Sista v3.0.0-beta.10
C++ lightweight OS-agnostic library for terminal animations and videogames
Loading...
Searching...
No Matches
field.hpp
Go to the documentation of this file.
1
26#pragma once
27
28#include <vector> // std::vector
29#include <memory> // std::shared_ptr, std::move
30#include <map> // std::map
31#include <set> // std::set
32#include "pawn.hpp"
33#include "border.hpp"
34#include "cursor.hpp"
35
36namespace sista {
50 enum class Effect { // Effect enum - effect when a coordinate is out of bounds
51 PACMAN = 0, // Pacman effect when a coordinate overflows
52 MATRIX = 1 // Classic C style matrix effect when a coordinate overflows
53 };
54
71 class Field { // Field class - represents the field [parent class]
72 protected:
74 std::vector<std::vector<std::shared_ptr<Pawn>>> pawns;
76 int width;
77 int height;
82 void cleanCoordinates(const Coordinates&) const;
87 void cleanCoordinates(unsigned short, unsigned short) const;
88
89 public:
93 void clear();
94
105 Field(int, int);
107 virtual ~Field() = default;
108
118 void print() const;
129 void print(char) const;
141 void print(Border&) const;
142
157 virtual void addPawn(std::shared_ptr<Pawn>);
170 virtual void removePawn(Pawn*);
183 virtual void removePawn(const Coordinates&);
195 virtual void erasePawn(Pawn*);
207 virtual void erasePawn(const Coordinates&);
208
220 void addPrintPawn(std::shared_ptr<Pawn>);
231 void rePrintPawn(Pawn*);
232
246 void movePawn(Pawn*, const Coordinates&);
263 void movePawn(Pawn*, unsigned short, unsigned short);
264
279 void movePawnBy(Pawn*, const Coordinates&);
295 void movePawnBy(Pawn*, unsigned short, unsigned short);
296
297 // 🎮 movePawnBy() with arcade game effects on coordinates overflow
316 void movePawnBy(Pawn*, const Coordinates&, Effect);
334 void movePawnBy(Pawn*, short int, short int, Effect);
335
352 void movePawnFromTo(const Coordinates&, const Coordinates&);
371 void movePawnFromTo(unsigned short, unsigned short, unsigned short, unsigned short);
372
389 Coordinates movingByCoordinates(Pawn*, short int, short int) const;
408 Coordinates movingByCoordinates(Pawn*, short int, short int, Effect) const; // movingByCoordinates - calculate the coordinates of a pawn after a movement
409
426 Pawn* getPawn(const Coordinates&) const;
441 Pawn* getPawn(unsigned short, unsigned short) const;
442
456 bool isOccupied(const Coordinates&) const;
471 bool isOccupied(unsigned short, unsigned short) const;
486 bool isOccupied(short int, short int) const;
487
498 bool isOutOfBounds(const Coordinates&) const;
510 bool isOutOfBounds(unsigned short, unsigned short) const;
523 bool isOutOfBounds(short int, short int) const;
524
538 bool isFree(const Coordinates&) const;
553 bool isFree(unsigned short, unsigned short) const;
568 bool isFree(short int, short int) const;
569
583 void validateCoordinates(const Coordinates&) const;
598 void validateCoordinates(unsigned short, unsigned short) const;
599 };
600
616 struct Path {
617 static long long int current_priority;
636
647 bool operator|(const Path&) const;
657 bool operator<(const Path&) const;
668 bool operator==(const Path&) const;
669 };
670}
671
672namespace std {
681 template<>
682 struct hash<sista::Path> {
683 std::size_t operator()(const sista::Path& k) const noexcept {
684 auto h1 = std::hash<sista::Coordinates>{}(k.begin);
685 auto h2 = std::hash<sista::Coordinates>{}(k.end);
686 return h1 ^ (h2 << 1);
687 }
688 };
689}
690
691namespace sista {
706 class SwappableField final : public Field {
707 private:
709 std::vector<std::vector<short int>> pawnsCount;
711 std::set<Path> pawnsToSwap;
712
713 Coordinates firstInvalidCell(std::map<Coordinates, short int>&) const; // firstInvalidCell - find the first cell with 2 or more pawns
714
722 void clearPawnsToSwap(); // clearPawnsToSwap - clear the pawnsToSwap
723
724 public:
735 SwappableField(int, int);
738
754 void addPawn(std::shared_ptr<Pawn>) override; // addPawn - add a pawn to the field
768 void removePawn(Pawn*) override; // removePawn - remove a pawn from the field
769
784 void movePawn(Pawn*, const Coordinates&); // movePawn - move a pawn to the coordinates
802 void movePawn(Pawn*, unsigned short, unsigned short); // movePawn - move a pawn to the coordinates
803
821 void addPawnToSwap(Pawn*, const Coordinates&); // addPawnToSwap - add a pawn to the pawnsToSwap
837 void addPawnToSwap(Path&); // addPawnToSwap - add a path to the pawnsToSwap
838
849 void simulateSwaps(); // simulateSwaps - simulate the swaps of the pawnsToSwap
858 void applySwaps(); // applySwaps - apply the swaps of the pawnsToSwap
859
871 void swapTwoPawns(const Coordinates&, const Coordinates&);
882 void swapTwoPawns(Pawn*, Pawn*);
883 };
884};
Border class header file.
Represents a brick of the border with a symbol and ANSI settings.
Definition border.hpp:34
Class representing a terminal Field for pawns and borders.
Definition field.hpp:71
void movePawn(Pawn *, const Coordinates &)
Moves a Pawn to new coordinates.
Definition field.cpp:181
Pawn * getPawn(const Coordinates &) const
Gets the Pawn at specified coordinates.
Definition field.cpp:270
Cursor cursor
Definition field.hpp:75
void addPrintPawn(std::shared_ptr< Pawn >)
Adds a Pawn to the field at its specified coordinates and prints it.
Definition field.cpp:171
int height
Definition field.hpp:77
void cleanCoordinates(const Coordinates &) const
Cleans the coordinates on screen by printing spaces.
Definition field.cpp:161
Coordinates movingByCoordinates(Pawn *, short int, short int) const
Calculates the new coordinates of a Pawn after moving by a relative offset.
Definition field.cpp:403
std::vector< std::vector< std::shared_ptr< Pawn > > > pawns
2D grid of shared pointers to Pawn objects.
Definition field.hpp:74
void validateCoordinates(const Coordinates &) const
Validates that the given coordinates are within bounds and not occupied.
Definition field.cpp:314
virtual ~Field()=default
Destructor to clean up resources.
virtual void erasePawn(Pawn *)
Removes a Pawn from the field at specified coordinates.
Definition field.cpp:151
void movePawnBy(Pawn *, const Coordinates &)
Moves a Pawn by a relative offset.
Definition field.cpp:207
void movePawnFromTo(const Coordinates &, const Coordinates &)
Moves a Pawn from one set of coordinates to another.
Definition field.cpp:257
bool isFree(const Coordinates &) const
Checks if specified coordinates are free (not occupied by a Pawn).
Definition field.cpp:303
virtual void addPawn(std::shared_ptr< Pawn >)
Adds a Pawn to the field at its specified coordinates.
Definition field.cpp:126
int width
Definition field.hpp:76
virtual void removePawn(Pawn *)
Removes a Pawn from the field.
Definition field.cpp:139
void print() const
Prints the entire field to the terminal.
Definition field.cpp:42
void clear()
Clears the field by removing all Pawns and resetting the grid.
Definition field.cpp:26
bool isOutOfBounds(const Coordinates &) const
Checks if specified coordinates are out of bounds.
Definition field.cpp:293
bool isOccupied(const Coordinates &) const
Checks if specified coordinates are occupied by a Pawn.
Definition field.cpp:283
void rePrintPawn(Pawn *)
Reprints a Pawn at its current coordinates.
Definition field.cpp:176
Class representing a Pawn owned by a Field or SwappableField.
Definition pawn.hpp:37
Class representing a terminal SwappableField for pawns and borders.
Definition field.hpp:706
void addPawn(std::shared_ptr< Pawn >) override
Adds a Pawn to the field and updates the pawnsCount grid.
Definition field.cpp:370
void simulateSwaps()
Simulates the swaps of the pawnsToSwap and removes unfeasible paths.
Definition field.cpp:485
void swapTwoPawns(const Coordinates &, const Coordinates &)
Swaps two Pawns at the specified coordinates.
Definition field.cpp:551
~SwappableField()
Destructor to clean up resources.
Definition field.cpp:363
void movePawn(Pawn *, const Coordinates &)
Moves a Pawn to new coordinates and updates the pawnsCount grid.
Definition field.cpp:381
void applySwaps()
Applies the swaps of the pawnsToSwap to the field.
Definition field.cpp:529
void removePawn(Pawn *) override
Removes a Pawn from the field and updates the pawnsCount grid.
Definition field.cpp:375
void addPawnToSwap(Pawn *, const Coordinates &)
Adds a Pawn to the set of pawnsToSwap.
Definition field.cpp:448
Cursor manipulation header file.
Sista library namespace.
Definition ansi.cpp:26
Effect
Enumeration for handling out-of-bounds coordinates.
Definition field.hpp:50
Definition coordinates.hpp:115
Pawn class header file.
Represents 2D coordinates with x and y values.
Definition coordinates.hpp:31
Class representing a terminal cursor for movement operations.
Definition cursor.hpp:132
Represents a movement path for a Pawn, including priority handling.
Definition field.hpp:616
bool operator==(const Path &) const
Overloaded operator to check if two paths are equal (same begin and end coordinates).
Definition field.cpp:340
int priority
Definition field.hpp:618
Coordinates end
Definition field.hpp:620
bool operator|(const Path &) const
Overloaded operator to check if two paths are opposite (reverse of each other).
Definition field.cpp:331
static long long int current_priority
Definition field.hpp:617
bool operator<(const Path &) const
Overloaded operator to compare paths based on priority.
Definition field.cpp:337
Pawn * pawn
Definition field.hpp:621
Coordinates begin
Definition field.hpp:619
std::size_t operator()(const sista::Path &k) const noexcept
Definition field.hpp:683