fixed holding spwaning the piece 1 row lower

This commit is contained in:
2025-04-01 18:45:01 +02:00
parent 46ebb88ef2
commit a62b3c018d
5 changed files with 39 additions and 32 deletions

View File

@@ -11,6 +11,7 @@
#include <utility>
#include <cstdlib>
#include <iostream>
#include <optional>
GameBoard::GameBoard(int boardWidth, int boardHeight, const std::shared_ptr<PiecesList>& piecesList, int nextQueueLength) :
@@ -194,12 +195,12 @@ bool GameBoard::activePieceOverlaps(const std::set<Position>& safePositions, con
return false;
}
bool GameBoard::hold(Rotation initialRotation) {
bool GameBoard::hold(std::optional<Rotation> initialRotation) {
Position storedPosition = this->activePiecePosition;
std::swap(this->activePiece, this->heldPiece);
bool isFirstTimeHolding = (this->activePiece == nullptr);
if (isFirstTimeHolding) {
// try with the next piece in queue since there is no piece in the hold box yet
if (this->nextQueueLength == 0) {
this->activePiece = std::make_shared<Piece>(this->generator.lookNext());
}
@@ -208,21 +209,25 @@ bool GameBoard::hold(Rotation initialRotation) {
}
}
Piece stored = *this->activePiece;
Position storedPosition = this->activePiecePosition;
// try initial rotation
this->goToSpawnPosition();
this->rotate(initialRotation);
if (initialRotation.has_value()) {
std::shared_ptr<Piece> storedPiece(this->activePiece);
this->rotate(initialRotation.value());
// if the piece can't spawn, abort initial rotation
if (this->activePieceInWall()) {
this->activePiece = storedPiece;
}
}
// if the piece can't spawn, try 0° rotation
if (this->activePieceInWall()) {
this->activePiece = std::make_shared<Piece>(stored);
this->goToSpawnPosition();
std::shared_ptr<Piece> storedPiece(this->activePiece);
this->rotate(NONE);
// if the piece still can't spawn, abort holding
if (this->activePieceInWall()) {
if (isFirstTimeHolding) {
this->activePiece = nullptr;
}
this->activePiece = (isFirstTimeHolding) ? nullptr : storedPiece;
std::swap(this->activePiece, this->heldPiece);
this->activePiecePosition = storedPosition;
return false;
@@ -230,7 +235,6 @@ bool GameBoard::hold(Rotation initialRotation) {
}
if (isFirstTimeHolding) {
// confirm we keep the piece we tried with
this->nextQueue.push_back(this->generator.getNext());
this->nextQueue.erase(this->nextQueue.begin());
}