fix polyominos
All checks were successful
Linux arm64 / Build (push) Successful in 2m29s

This commit is contained in:
2025-07-20 22:32:33 +02:00
parent dd6da58642
commit ecc035c972
2 changed files with 12 additions and 6 deletions

View File

@@ -55,7 +55,12 @@ bool PiecesFiles::savePieces(int polyominoSize, std::vector<Polyomino>& polyomin
// write the positions of the piece // write the positions of the piece
for (int i = 0; i < bytesNeeded; i++) { for (int i = 0; i < bytesNeeded; i++) {
buffer << static_cast<std::uint8_t>(polyomino.getPositionsData()[i/4] >> ((i%4) * 8)); buffer << static_cast<std::uint8_t>(
(
polyomino.getPositionsData()[i / sizeof(std::uint64_t)] >>
(56 - ((i % sizeof(std::uint64_t)) * 8))
)
& 0xFF);
} }
} }
@@ -118,7 +123,8 @@ bool PiecesFiles::loadPieces(int polyominoSize, std::vector<Piece>& pieces, std:
for (int j = 0; j < bytesNeeded; j++) { for (int j = 0; j < bytesNeeded; j++) {
buffer >> positionByte; buffer >> positionByte;
polyominoData[j/4] |= static_cast<std::uint64_t>(positionByte) << ((j%4) * 8); polyominoData[j / sizeof(std::uint64_t)] |=
(static_cast<std::uint64_t>(positionByte) << (56 - ((j % sizeof(std::uint64_t)) * 8)));
} }
pieces.emplace_back(Polyomino(std::move(polyominoData), length), pieceBlock); pieces.emplace_back(Polyomino(std::move(polyominoData), length), pieceBlock);

View File

@@ -277,7 +277,7 @@ std::vector<Position> Polyomino::getPositions() const {
int posIndex = y * this->length + x; int posIndex = y * this->length + x;
int longIndex = posIndex / (sizeof(std::uint64_t) * 8); int longIndex = posIndex / (sizeof(std::uint64_t) * 8);
int bitIndex = posIndex % (sizeof(std::uint64_t) * 8); int bitIndex = posIndex % (sizeof(std::uint64_t) * 8);
if (this->positions[longIndex] & static_cast<std::uint64_t>(1) << (sizeof(std::uint64_t) * 8 - bitIndex)) { if (this->positions[longIndex] & static_cast<std::uint64_t>(1) << (sizeof(std::uint64_t) * 8 - bitIndex - 1)) {
result.push_back(Position(x, y)); result.push_back(Position(x, y));
} }
} }
@@ -328,19 +328,19 @@ bool Polyomino::contains(const Position& position) const {
int posIndex = position.y * this->length + position.x; int posIndex = position.y * this->length + position.x;
int longIndex = posIndex / (sizeof(std::uint64_t) * 8); int longIndex = posIndex / (sizeof(std::uint64_t) * 8);
int bitIndex = posIndex % (sizeof(std::uint64_t) * 8); int bitIndex = posIndex % (sizeof(std::uint64_t) * 8);
return this->positions[longIndex] & static_cast<std::uint64_t>(1) << (sizeof(std::uint64_t) * 8 - bitIndex); return this->positions[longIndex] & static_cast<std::uint64_t>(1) << (sizeof(std::uint64_t) * 8 - bitIndex - 1);
} }
void Polyomino::insert(const Position& position) { void Polyomino::insert(const Position& position) {
int posIndex = position.y * this->length + position.x; int posIndex = position.y * this->length + position.x;
int longIndex = posIndex / (sizeof(std::uint64_t) * 8); int longIndex = posIndex / (sizeof(std::uint64_t) * 8);
int bitIndex = posIndex % (sizeof(std::uint64_t) * 8); int bitIndex = posIndex % (sizeof(std::uint64_t) * 8);
this->positions[longIndex] |= static_cast<std::uint64_t>(1) << (sizeof(std::uint64_t) * 8 - bitIndex); this->positions[longIndex] |= static_cast<std::uint64_t>(1) << (sizeof(std::uint64_t) * 8 - bitIndex - 1);
} }
void Polyomino::erase(const Position& position) { void Polyomino::erase(const Position& position) {
int posIndex = position.y * this->length + position.x; int posIndex = position.y * this->length + position.x;
int longIndex = posIndex / (sizeof(std::uint64_t) * 8); int longIndex = posIndex / (sizeof(std::uint64_t) * 8);
int bitIndex = posIndex % (sizeof(std::uint64_t) * 8); int bitIndex = posIndex % (sizeof(std::uint64_t) * 8);
this->positions[longIndex] &= ~(static_cast<std::uint64_t>(1) << (sizeof(std::uint64_t) * 8 - bitIndex)); this->positions[longIndex] &= ~(static_cast<std::uint64_t>(1) << (sizeof(std::uint64_t) * 8 - bitIndex - 1));
} }