inline les fonctions opérateurs hors classes

This commit is contained in:
2025-02-25 15:10:50 +01:00
parent 59f457c752
commit 442ead0f38
7 changed files with 26 additions and 31 deletions

View File

@@ -36,7 +36,6 @@ Game::Game(Gamemode gamemode, const Player& controls, int boardWidth, int boardH
} }
void Game::start() { void Game::start() {
// starts the game
this->started = true; this->started = true;
this->lost = this->board.spawnNextPiece(); this->lost = this->board.spawnNextPiece();
} }

View File

@@ -20,23 +20,6 @@ void readStatsFromFilesForAllSizes(int amount);
int main(int argc, char** argv) { int main(int argc, char** argv) {
std::srand(std::time(NULL)); std::srand(std::time(NULL));
using std::chrono::high_resolution_clock;
using std::chrono::duration_cast;
using std::chrono::duration;
using std::chrono::milliseconds;
PiecesFiles pf;
std::vector<Piece> pieces;
std::vector<int> convexPieces;
std::vector<int> holelessPieces;
std::vector<int> otherPieces;
pf.loadPieces(13, pieces, convexPieces, holelessPieces, otherPieces);
auto t1 = high_resolution_clock::now();
Bag bg(pieces);
auto t2 = high_resolution_clock::now();
duration<double, std::milli> ms_double = t2 - t1;
std::cout << ms_double.count() << "ms" << std::endl;
return 0; return 0;
} }

View File

@@ -15,14 +15,14 @@ struct Cell {
/** /**
* Addition operator, returns the sums of the coordinates of both cells * Addition operator, returns the sums of the coordinates of both cells
*/ */
Cell operator+(const Cell& left, const Cell& right) { inline Cell operator+(const Cell& left, const Cell& right) {
return Cell{left.x + right.x, left.y + right.y}; return Cell{left.x + right.x, left.y + right.y};
} }
/** /**
* Additive assignation operator, adds the coordinates of the right cell to the left one * Additive assignation operator, adds the coordinates of the right cell to the left one
*/ */
Cell& operator+=(Cell& left, const Cell& right) { inline Cell& operator+=(Cell& left, const Cell& right) {
left = left + right; left = left + right;
return left; return left;
} }
@@ -30,7 +30,7 @@ Cell& operator+=(Cell& left, const Cell& right) {
/** /**
* Substraction operator, returns the difference of the coordinate between the left and right cell * Substraction operator, returns the difference of the coordinate between the left and right cell
*/ */
Cell operator-(const Cell& left, const Cell& right) { inline Cell operator-(const Cell& left, const Cell& right) {
return Cell{left.x - right.x, left.y - right.y}; return Cell{left.x - right.x, left.y - right.y};
} }
@@ -38,7 +38,7 @@ Cell operator-(const Cell& left, const Cell& right) {
/** /**
* Substractive assignation operator, substract the coordinates of the right cell from the left one * Substractive assignation operator, substract the coordinates of the right cell from the left one
*/ */
Cell& operator-=(Cell& left, const Cell& right) { inline Cell& operator-=(Cell& left, const Cell& right) {
left = left - right; left = left - right;
return left; return left;
} }
@@ -46,21 +46,21 @@ Cell& operator-=(Cell& left, const Cell& right) {
/** /**
* Strict inferiority operator, a cell is inferior to another if it is lower or at the same height and more to the left * Strict inferiority operator, a cell is inferior to another if it is lower or at the same height and more to the left
*/ */
bool operator<(const Cell& left, const Cell& right) { inline bool operator<(const Cell& left, const Cell& right) {
return (left.x == right.x) ? (left.y < right.y) : (left.x < right.x); return (left.x == right.x) ? (left.y < right.y) : (left.x < right.x);
} }
/** /**
* Equality operator, two cells are equal if they have the same coordinates * Equality operator, two cells are equal if they have the same coordinates
*/ */
bool operator==(const Cell& left, const Cell& right) { inline bool operator==(const Cell& left, const Cell& right) {
return (left.x == right.x) && (left.y == right.y); return (left.x == right.x) && (left.y == right.y);
} }
/** /**
* Stream output operator, adds the coordinates of the cell to the stream * Stream output operator, adds the coordinates of the cell to the stream
*/ */
std::ostream& operator<<(std::ostream& os, const Cell& cell) { inline std::ostream& operator<<(std::ostream& os, const Cell& cell) {
os << "x: " << cell.x << " y: " << cell.y; os << "x: " << cell.x << " y: " << cell.y;
return os; return os;
} }

View File

@@ -21,8 +21,19 @@ enum Color {
}; };
static const Color FIRST_PIECE_COLOR = PURPLE; // the first color a piece can be /**
static const Color LAST_PIECE_COLOR = GREEN; // the last color a piece can be * Returns the first color a piece can take
*/
inline Color firstPieceColor() {
return Color(PURPLE);
}
/**
* Sets the color to the next available piece color
*/
inline void nextPieceColor(Color& color) {
color = (color == GREEN) ? PURPLE : Color(color + 1);
}
static const std::string COLOR_RESET = "\033[38;2;255;255;255m"; // color code to reset the console color static const std::string COLOR_RESET = "\033[38;2;255;255;255m"; // color code to reset the console color
static const std::string COLOR_CODES[] = { // color codes to change the console color static const std::string COLOR_CODES[] = { // color codes to change the console color

View File

@@ -38,7 +38,8 @@ bool PiecesFiles::savePieces(int order) const {
std::sort(nMinos.begin(), nMinos.end()); std::sort(nMinos.begin(), nMinos.end());
// write pieces // write pieces
Color pieceColor = Color(FIRST_PIECE_COLOR + (order % (LAST_PIECE_COLOR - FIRST_PIECE_COLOR + 1))); Color pieceColor = firstPieceColor();
for (int i = 0; i < order; i++) nextPieceColor(pieceColor);
for (const Polyomino& nMino : nMinos) { for (const Polyomino& nMino : nMinos) {
// write polyomino length // write polyomino length
char lengthByte = nMino.getLength(); char lengthByte = nMino.getLength();
@@ -46,7 +47,7 @@ bool PiecesFiles::savePieces(int order) const {
// write the type and color of the piece // write the type and color of the piece
char infoByte = (nMino.isConvex() << 7) + (nMino.hasHole() << 6) + pieceColor; char infoByte = (nMino.isConvex() << 7) + (nMino.hasHole() << 6) + pieceColor;
pieceColor = (pieceColor == LAST_PIECE_COLOR) ? FIRST_PIECE_COLOR : Color(pieceColor + 1); nextPieceColor(pieceColor);
piecesFile.write(&infoByte, 1); piecesFile.write(&infoByte, 1);
// write the cells of the piece // write the cells of the piece

View File

@@ -15,14 +15,14 @@ enum Rotation {
/** /**
* Addition operator, returns a rotation corresponding to doing both rotations * Addition operator, returns a rotation corresponding to doing both rotations
*/ */
Rotation operator+(const Rotation& left, const Rotation& right) { inline Rotation operator+(const Rotation& left, const Rotation& right) {
return Rotation((left + right) % 4); return Rotation((left + right) % 4);
} }
/** /**
* Additive assignation operator, rotate the left rotation by the right rotation * Additive assignation operator, rotate the left rotation by the right rotation
*/ */
Rotation& operator+=(Rotation& left, const Rotation& right) { inline Rotation& operator+=(Rotation& left, const Rotation& right) {
left = left + right; left = left + right;
return left; return left;
} }

View File

@@ -5,6 +5,7 @@ target("main")
set_rundir(".") set_rundir(".")
add_files("./src/Pieces/*.cpp") add_files("./src/Pieces/*.cpp")
add_files("./src/Core/*.cpp") add_files("./src/Core/*.cpp")
set_optimize("fastest")
-- --