const everywhere + légèrement changer format fichiers

This commit is contained in:
2025-03-01 22:21:43 +01:00
parent 8088894073
commit f525c00662
15 changed files with 151 additions and 113 deletions

View File

@@ -33,6 +33,7 @@ void Generator::generate(int polyominoSize, int lastAddedPositionNumber, int nex
// we sort the rotations of the polyominos
std::vector<Polyomino> candidateRotations;
candidateRotations.reserve(4);
for (int i = 0; i < 4; i++) {
candidate.normalize();
candidateRotations.push_back(candidate);

View File

@@ -32,20 +32,10 @@ bool PiecesFiles::savePieces(int polyominoSize) const {
nMino.goToSpawnPosition();
}
std::sort(nMinos.begin(), nMinos.end());
Block pieceblock = firstPieceBlockType();
for (int i = 0; i < polyominoSize; i++) {
nextPieceBlockType(pieceblock);
}
for (const Polyomino& nMino : nMinos) {
// write polyomino length
char lengthByte = nMino.getLength();
piecesFile.write(&lengthByte, 1);
// write the type and block of the piece
char infoByte = (nMino.isConvex() << 7) + (nMino.hasHole() << 6) + pieceblock;
nextPieceBlockType(pieceblock);
// write the characteristics of the piece
char infoByte = (nMino.isConvex() << 7) + (nMino.hasHole() << 6) + nMino.getLength();
piecesFile.write(&infoByte, 1);
// write the positions of the piece
@@ -74,37 +64,41 @@ bool PiecesFiles::loadPieces(int polyominoSize, std::vector<Piece>& pieces, std:
holelessPieces.clear();
otherPieces.clear();
Block pieceBlock = firstPieceBlockType();
for (int i = 0; i < polyominoSize; i++) {
nextPieceBlockType(pieceBlock);
}
char convexMask = 0b1000'0000;
char holeMask = 0b0100'0000;
char blockMask = 0b0011'1111;
char lengthMask = 0b0011'1111;
char xMask = 0b1111'0000;
char yMask = 0b0000'1111;
char lengthByte;
char infoByte;
int i = 0;
// read piece length
while (piecesFile.get(lengthByte)) {
while (piecesFile.get(infoByte)) {
if (piecesFile.eof()) break;
// read piece infos
char infoByte;
piecesFile.get(infoByte);
bool isConvex = (infoByte & convexMask) >> 7;
bool hasHole = (infoByte & holeMask) >> 6;
Block block = Block(infoByte & blockMask);
int length = (infoByte & lengthMask);
// read positions
std::set<Position> piecepositions;
std::set<Position> piecePositions;
char positionByte;
for (int i = 0; i < polyominoSize; i++) {
piecesFile.get(positionByte);
int x = (positionByte & xMask) >> 4;
int y = positionByte & yMask;
piecepositions.insert(Position{x, y});
piecePositions.insert(Position{x, y});
}
// create piece
Piece readPiece(Polyomino(piecepositions, lengthByte), block);
Piece readPiece(Polyomino(piecePositions, length), pieceBlock);
nextPieceBlockType(pieceBlock);
pieces.push_back(readPiece);
if (isConvex) {
convexPieces.push_back(i);

View File

@@ -27,7 +27,7 @@ Polyomino::Polyomino(const std::set<Position>& positions) {
for (Position position : positions) {
newPositions.insert(Position{position.x - minX, position.y - minY});
}
this->positions = newPositions;
this->positions = std::move(newPositions);
// set polyomino length
this->length = std::max(maxX - minX + 1, maxY - minY + 1);
@@ -52,7 +52,7 @@ void Polyomino::normalize() {
for (Position position : this->positions) {
newPositions.insert(Position{position.x - minX, position.y - minY});
}
this->positions = newPositions;
this->positions = std::move(newPositions);
}
void Polyomino::rotateCW() {
@@ -60,7 +60,7 @@ void Polyomino::rotateCW() {
for (Position position : this->positions) {
newPositions.insert(Position{position.y, (length - 1) - (position.x)});
}
this->positions = newPositions;
this->positions = std::move(newPositions);
}
void Polyomino::rotate180() {
@@ -68,7 +68,7 @@ void Polyomino::rotate180() {
for (Position position : this->positions) {
newPositions.insert(Position{(length - 1) - (position.x), (length - 1) - (position.y)});
}
this->positions = newPositions;
this->positions = std::move(newPositions);
}
void Polyomino::rotateCCW() {
@@ -76,7 +76,7 @@ void Polyomino::rotateCCW() {
for (Position position : this->positions) {
newPositions.insert(Position{(length - 1) - (position.y), position.x});
}
this->positions = newPositions;
this->positions = std::move(newPositions);
}
void Polyomino::goToSpawnPosition() {
@@ -169,7 +169,7 @@ void Polyomino::goToSpawnPosition() {
for (Position position : positions) {
newPositions.insert(Position{(position.x - minX) + (verticalEmptyLines / 2), (position.y - minY) + ((horizontalEmptyLines + 1) / 2)});
}
this->positions = newPositions;
this->positions = std::move(newPositions);
}
void Polyomino::checkForFlattestSide(const std::vector<std::vector<int>>& linesCompleteness, bool currentFlattestSides[4], int& sideToBeOn, bool checkLeftSide) const {
@@ -285,9 +285,9 @@ bool Polyomino::operator<(const Polyomino& other) const {
// we check for all positions from left to right and top to bottom, until one has a square that the other doesn't
for (int y = this->length - 1; y >= 0; y--) {
for (int x = 0; x < this->length; x++) {
bool hasThisposition = this->positions.contains(Position{x, y});
bool hasOtherposition = other.positions.contains(Position{x, y});
if (hasThisposition != hasOtherposition) return hasThisposition;
bool hasThisPosition = this->positions.contains(Position{x, y});
bool hasOtherPosition = other.positions.contains(Position{x, y});
if (hasThisPosition != hasOtherPosition) return hasThisPosition;
}
}
return false;